diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000000000000000000000000000000000000..fecc5f77e23a59af0967d862208a8cce7f2502da --- /dev/null +++ b/.dockerignore @@ -0,0 +1,71 @@ +# Git +.git +.gitignore + +# Python +__pycache__ +*.py[cod] +*$py.class +*.so +.Python +build/ +develop-eggs/ +dist/* +!dist/docker-input/ +!dist/docker-input/*.tar.gz +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# 虚拟环境 +venv/ +env/ +ENV/ +.venv + +# 环境配置(通过 docker-compose 挂载或环境变量传递) +.env +.env.local +.env.*.local +config.json + +# 开发工具 +.vscode/ +.idea/ +*.swp +*.swo +*~ + +# 测试 +tests/ +.pytest_cache/ +.coverage +htmlcov/ + +# Node.js / WebUI 开发依赖 +node_modules/ +webui/node_modules/ +npm-debug.log* +yarn-debug.log* +yarn-error.log* + +# 文档 +*.md +!README*.md + +# CI/CD +.github/ +.releaserc.json + +# 其他 +.DS_Store +Thumbs.db diff --git a/.env.example b/.env.example new file mode 100644 index 0000000000000000000000000000000000000000..840a81e1609221dc6627c1ce9d6c7e62b8c7c15d --- /dev/null +++ b/.env.example @@ -0,0 +1,20 @@ +# DS2API runtime +# Runtime listen port inside the app/container +PORT=5001 +# Docker Compose host port (compose only; container still listens on PORT) +DS2API_HOST_PORT=6011 +LOG_LEVEL=INFO + +# Admin authentication +DS2API_ADMIN_KEY=change-me + +# Config loading (choose one) +# 1) file-based config +DS2API_CONFIG_PATH=/app/config.json +# 2) inline JSON or Base64 JSON +# DS2API_CONFIG_JSON= +# 3) legacy compatibility alias +# CONFIG_JSON= + +# Optional: static admin assets path +# DS2API_STATIC_ADMIN_DIR=/app/static/admin diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000000000000000000000000000000000000..a6344aac8c09253b3b630fb776ae94478aa0275b --- /dev/null +++ b/.gitattributes @@ -0,0 +1,35 @@ +*.7z filter=lfs diff=lfs merge=lfs -text +*.arrow filter=lfs diff=lfs merge=lfs -text +*.bin filter=lfs diff=lfs merge=lfs -text +*.bz2 filter=lfs diff=lfs merge=lfs -text +*.ckpt filter=lfs diff=lfs merge=lfs -text +*.ftz filter=lfs diff=lfs merge=lfs -text +*.gz filter=lfs diff=lfs merge=lfs -text +*.h5 filter=lfs diff=lfs merge=lfs -text +*.joblib filter=lfs diff=lfs merge=lfs -text +*.lfs.* filter=lfs diff=lfs merge=lfs -text +*.mlmodel filter=lfs diff=lfs merge=lfs -text +*.model filter=lfs diff=lfs merge=lfs -text +*.msgpack filter=lfs diff=lfs merge=lfs -text +*.npy filter=lfs diff=lfs merge=lfs -text +*.npz filter=lfs diff=lfs merge=lfs -text +*.onnx filter=lfs diff=lfs merge=lfs -text +*.ot filter=lfs diff=lfs merge=lfs -text +*.parquet filter=lfs diff=lfs merge=lfs -text +*.pb filter=lfs diff=lfs merge=lfs -text +*.pickle filter=lfs diff=lfs merge=lfs -text +*.pkl filter=lfs diff=lfs merge=lfs -text +*.pt filter=lfs diff=lfs merge=lfs -text +*.pth filter=lfs diff=lfs merge=lfs -text +*.rar filter=lfs diff=lfs merge=lfs -text +*.safetensors filter=lfs diff=lfs merge=lfs -text +saved_model/**/* filter=lfs diff=lfs merge=lfs -text +*.tar.* filter=lfs diff=lfs merge=lfs -text +*.tar filter=lfs diff=lfs merge=lfs -text +*.tflite filter=lfs diff=lfs merge=lfs -text +*.tgz filter=lfs diff=lfs merge=lfs -text +*.wasm filter=lfs diff=lfs merge=lfs -text +*.xz filter=lfs diff=lfs merge=lfs -text +*.zip filter=lfs diff=lfs merge=lfs -text +*.zst filter=lfs diff=lfs merge=lfs -text +*tfevents* filter=lfs diff=lfs merge=lfs -text diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..b6a961f44de49469055d06f27cf0b3eb8aa6caec --- /dev/null +++ b/.gitignore @@ -0,0 +1,73 @@ +*.bak +config.json +.env + +# IDE +.vscode/ +.idea/ +*.swp +*.swo +*~ +.DS_Store +opencode.json + +# Logs +*.log +logs/ +artifacts/ + +# Vercel +.vercel + +# Node.js / Frontend +node_modules/ +webui/node_modules/ +webui/dist/ +.npm +.pnpm-store/ +yarn.lock +pnpm-lock.yaml + +# Build artifacts +dist/ +*.tsbuildinfo +.cache/ +.parcel-cache/ +static/admin/ +internal/webui/assets/admin/ + +# Go compiled binaries +/ds2api +//ds2api-tests + +# Environment +.env.local +.env.*.local + +# Testing +.coverage +htmlcov/ +.pytest_cache/ +.tox/ +*.coverprofile +coverage*.out +cover/ + +# Misc +.git/ +Thumbs.db + +# Claude Code +.claude/ +CLAUDE.local.md + +# Local tool bootstrap cache +.tmp/ + +# Chat history +data/ +.codex +.roomodes + +deepseek2api旧版/ +chat.deepseek.com_2026_05_30_09_34_45.har.txt diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000000000000000000000000000000000000..514a43c94adee9a5d58d4a097a6301d9efd60376 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,73 @@ +version: "2" + +run: + tests: true + +linters: + default: standard + enable: + - errcheck + - govet + - ineffassign + - staticcheck + - unused + settings: + dupl: + threshold: 100 + goconst: + min-len: 2 + min-occurrences: 2 + gocritic: + enabled-tags: + - diagnostic + - experimental + - opinionated + - performance + - style + disabled-checks: + - wrapperFunc + - rangeValCopy + - hugeParam + gocyclo: + min-complexity: 15 + lll: + line-length: 140 + misspell: + locale: US + nakedret: + max-func-lines: 30 + prealloc: + simple: true + range-loops: true + for-loops: false + exclusions: + generated: lax + rules: + - path: (.+)\.go$ + text: "ST1000: at least one file in a package should have a package comment" + paths: + - third_party$ + - builtin$ + - examples$ + - vendor$ + - webui/node_modules$ + +issues: + max-issues-per-linter: 0 + max-same-issues: 0 + +formatters: + enable: + - gofmt + settings: + goimports: + local-prefixes: + - ds2api + exclusions: + generated: lax + paths: + - third_party$ + - builtin$ + - examples$ + - vendor$ + - webui/node_modules$ diff --git a/.releaserc.json b/.releaserc.json new file mode 100644 index 0000000000000000000000000000000000000000..8931e50651523e821d39d0aa7b79c3746b67efc2 --- /dev/null +++ b/.releaserc.json @@ -0,0 +1,21 @@ +{ + "branches": [ + { + "name": "main" + }, + { + "name": "dev", + "prerelease": "beta", + "channel": "beta" + } + ], + "plugins": [ + ["@semantic-release/commit-analyzer", { + "preset": "angular" + }], + "@semantic-release/release-notes-generator", + ["@semantic-release/github", { + "successComment": ":tada: This release is now available as ${nextRelease.version}" + }] + ] +} \ No newline at end of file diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 0000000000000000000000000000000000000000..664f3f0f32f62eb5086bd7a678061db400d3905d --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,36 @@ +# AGENTS.md + +These rules apply to all agent-made changes in this repository. + +## PR Gate + +- Before opening or updating a PR, run the same local gates as `.github/workflows/quality-gates.yml`. +- Required commands: + - `./scripts/lint.sh` + - `./tests/scripts/check-refactor-line-gate.sh` + - `./tests/scripts/run-unit-all.sh` + - `npm run build --prefix webui` + +## Go Lint Rules + +- Run `gofmt -w` on every changed Go file before commit or push. +- Do not ignore error returns from I/O-style cleanup calls such as `Close`, `Flush`, `Sync`, or similar methods. +- If a cleanup error cannot be returned, log it explicitly. + +## Change Scope + +- Keep changes additive and tightly scoped to the requested feature or bugfix. +- Do not mix unrelated refactors into feature PRs unless they are required to make the change pass gates. + +## Protocol Adapter Boundary + +- Do not let OpenAI Chat, OpenAI Responses, Claude, Gemini, or other interface protocol formatting own shared business behavior. +- Normalize protocol-specific request shapes into the project standard request/turn model first, run shared business logic in one place, then render back to the target protocol at the boundary. +- Business logic that must stay globally consistent includes empty-output retry, thinking/reasoning handling, tool-call detection and policy, usage accounting, current-input-file injection, history persistence, file/reference handling, and completion payload assembly. +- If a behavior must differ by protocol, keep the difference as an explicit adapter/rendering concern and document why it cannot live in the shared normalized path. + +## Documentation Sync + +- When business logic or user-visible behavior changes, update the corresponding documentation in the same change. +- `docs/prompt-compatibility.md` is the source-of-truth document for the “API -> pure-text web-chat context” compatibility flow. +- If a change affects message normalization, tool prompt injection, prompt-visible tool history, file/reference handling, history split, or completion payload assembly, update `docs/prompt-compatibility.md` in the same change. diff --git a/API.en.md b/API.en.md new file mode 100644 index 0000000000000000000000000000000000000000..b5e53e0be28002e5929d4a254d3139935a2f52ed --- /dev/null +++ b/API.en.md @@ -0,0 +1,1449 @@ +# DS2API API Reference + +Language: [中文](API.md) | [English](API.en.md) + +This document describes the actual behavior of the current Go codebase. + +Docs: [Overview](README.en.md) / [Architecture](docs/ARCHITECTURE.en.md) / [Deployment](docs/DEPLOY.en.md) / [Testing](docs/TESTING.md) + +--- + +## Table of Contents + +- [Basics](#basics) +- [Configuration Best Practice](#configuration-best-practice) +- [Authentication](#authentication) +- [Route Index](#route-index) +- [Health Endpoints](#health-endpoints) +- [OpenAI-Compatible API](#openai-compatible-api) +- [Claude-Compatible API](#claude-compatible-api) +- [Gemini-Compatible API](#gemini-compatible-api) +- [Ollama API](#ollama-api) +- [Admin API](#admin-api) +- [Error Payloads](#error-payloads) +- [cURL Examples](#curl-examples) + +--- + +## Basics + +| Item | Details | +| --- | --- | +| Base URL | `http://localhost:5001` or your deployment domain | +| Default Content-Type | `application/json` | +| Health probes | `GET /healthz`, `GET /readyz` | +| CORS | Enabled (uniformly covers `/v1/*`, `/anthropic/*`, `/v1beta/models/*`, `/api/*`, and `/admin/*`; echoes the browser `Origin` when present, otherwise `*`; default allow-list includes `Content-Type`, `Authorization`, `X-API-Key`, `X-Ds2-Target-Account`, `X-Ds2-Source`, `X-Vercel-Protection-Bypass`, `X-Goog-Api-Key`, `Anthropic-Version`, `Anthropic-Beta`, and also accepts third-party preflight-requested headers such as `x-stainless-*`; `/v1/chat/completions` on Vercel Node Runtime matches the same behavior; internal-only `X-Ds2-Internal-Token` remains blocked) | + +- All JSON request bodies must be valid UTF-8; malformed byte sequences are rejected on ingress with `400 invalid json`. + +### 3.0 Adapter-Layer Notes + +- OpenAI / Claude / Gemini protocols are now mounted on one shared `chi` router tree assembled in `internal/server/router.go`. +- Adapter responsibilities are streamlined to: **request normalization → DeepSeek invocation → protocol-shaped rendering**, reducing legacy split-logic paths. +- Tool-calling semantics are aligned between Go and Node runtime: models should output the halfwidth-pipe DSML shell `<|DSML|tool_calls>` → `<|DSML|invoke name="...">` → `<|DSML|parameter name="...">`; DS2API also accepts DSML wrapper aliases such as `` and `<|tool_calls>`, common DSML separator drift such as `<|DSML tool_calls>`, collapsed DSML local names such as ``, control-separator drift such as `` / raw STX `\x02`, CJK angle bracket, fullwidth-bang / ideographic-comma separator drift, PascalCase local-name drift, and trailing attribute separator drift such as `...〈/DSM|parameter〉`, `<!DSML!invoke name=“Bash”>`, `<、DSML、tool_calls>`, ``, or ``, arbitrary protocol prefixes such as ``, and legacy canonical XML `` → `` → ``. The scanner normalizes fixed local names (`tool_calls` / `invoke` / `parameter`) with non-structural separators before or after them back to XML before parsing, and also tolerates CDATA opener drift such as `<![CDATA[` / `<、[CDATA[`; only wrapped tool blocks or the narrow missing-opening-wrapper repair path enter the tool path, while bare `` does not count as supported syntax. JSON literal parameter bodies are preserved as structured values, explicit empty or whitespace-only parameters are preserved as empty strings, malformed complete wrappers are released as plain text, and loose CDATA is narrowly repaired at final parse/flush when it can preserve a complete outer tool call. +- `Admin API` separates static config from runtime policy: `/admin/config*` for configuration state, `/admin/settings*` for runtime behavior. +- When upstream returns a thinking-only response with no visible text, the Go main path and the Vercel Node streaming path retry once in the same DeepSeek session: it appends the prompt suffix `"Please provide a non-empty final answer or tool call."` and sets `parent_message_id`. If that same-account retry would still end as `429 upstream_empty_output`, managed-account mode switches to the next available account, creates a fresh session, and retries the original payload once before returning 429. +- Citation/reference marker boundary: streaming output hides upstream `[citation:N]` / `[reference:N]` placeholders by default; non-stream output converts DeepSeek search reference markers into Markdown links. + +--- + +## Configuration Best Practice + +Use `config.json` as the single source of truth: + +```bash +cp config.example.json config.json +# Edit config.json (keys/accounts) +``` + +Use it per deployment mode: + +- Local run: read `config.json` directly +- Docker / Vercel: generate Base64 from `config.json`, then set `DS2API_CONFIG_JSON`, or paste raw JSON directly + +```bash +DS2API_CONFIG_JSON="$(base64 < config.json | tr -d '\n')" +``` + +For Vercel one-click bootstrap, you can set only `DS2API_ADMIN_KEY` first, then import config at `/admin` and sync env vars from the "Vercel Sync" page. + +--- + +## Authentication + +### Business Endpoints (`/v1/*`, `/anthropic/*`, `/v1beta/models/*`) + +Two header formats accepted: + +| Method | Example | +| --- | --- | +| Bearer Token | `Authorization: Bearer ` | +| API Key Header | `x-api-key: ` (no `Bearer` prefix) | +| Gemini-compatible | `x-goog-api-key: ` or `?key=` / `?api_key=` | + +**Auth behavior**: + +- Token is in `config.keys` → **Managed account mode**: DS2API auto-selects an account via rotation +- Token is not in `config.keys` → **Direct token mode**: treated as a DeepSeek token directly + +**Optional header**: `X-Ds2-Target-Account: ` — Pin a specific managed account; if the target account does not exist or the managed-account queue is exhausted, the request returns `429`, and current responses do not include `Retry-After`. If the account exists but login/refresh fails, the request returns the underlying `401` or upstream error. Without a pinned target, managed-account completion requests try one alternate-account fresh retry before returning an empty-output 429; pinned-target requests and requests with no other available account do not switch. +Gemini-compatible clients can also send `x-goog-api-key`, `?key=`, or `?api_key=` as the caller credential source. + +### Admin Endpoints (`/admin/*`) + +| Endpoint | Auth | +| --- | --- | +| `POST /admin/login` | Public | +| `GET /admin/verify` | `Authorization: Bearer ` (JWT only) | +| Other `/admin/*` | `Authorization: Bearer ` or `Authorization: Bearer ` | + +--- + +## Route Index + +| Method | Path | Auth | Description | +| --- | --- | --- | --- | +| GET | `/healthz` | None | Liveness probe | +| HEAD | `/healthz` | None | Liveness probe (no body) | +| GET | `/readyz` | None | Readiness probe | +| HEAD | `/readyz` | None | Readiness probe (no body) | +| GET | `/v1/models` | None | OpenAI model list | +| GET | `/v1/models/{id}` | None | OpenAI single-model query (alias accepted) | +| POST | `/v1/chat/completions` | Business | OpenAI chat completions | +| POST | `/v1/responses` | Business | OpenAI Responses API (stream/non-stream) | +| GET | `/v1/responses/{response_id}` | Business | Query stored response (in-memory TTL) | +| POST | `/v1/embeddings` | Business | OpenAI Embeddings API | +| POST | `/v1/files` | Business | OpenAI Files upload (multipart/form-data) | +| GET | `/v1/files/{file_id}` | Business | Retrieve uploaded file status | +| GET | `/anthropic/v1/models` | None | Claude model list | +| POST | `/anthropic/v1/messages` | Business | Claude messages | +| POST | `/anthropic/v1/messages/count_tokens` | Business | Claude token counting | +| POST | `/v1/messages` | Business | Claude shortcut path | +| POST | `/messages` | Business | Claude shortcut path | +| POST | `/v1/messages/count_tokens` | Business | Claude token counting shortcut | +| POST | `/messages/count_tokens` | Business | Claude token counting shortcut | +| POST | `/v1beta/models/{model}:generateContent` | Business | Gemini non-stream | +| POST | `/v1beta/models/{model}:streamGenerateContent` | Business | Gemini stream | +| POST | `/v1/models/{model}:generateContent` | Business | Gemini non-stream compat path | +| POST | `/v1/models/{model}:streamGenerateContent` | Business | Gemini stream compat path | +| GET | `/api/version` | None | Ollama version endpoint | +| GET | `/api/tags` | None | Ollama model list | +| POST | `/api/show` | None | Ollama model capability query (returns `id` + `capabilities`) | +| POST | `/admin/login` | None | Admin login | +| GET | `/admin/verify` | JWT | Verify admin JWT | +| GET | `/admin/vercel/config` | Admin | Read preconfigured Vercel creds | +| GET | `/admin/config` | Admin | Read sanitized config | +| POST | `/admin/config` | Admin | Update config | +| GET | `/admin/settings` | Admin | Read runtime settings | +| PUT | `/admin/settings` | Admin | Update runtime settings (hot reload) | +| POST | `/admin/settings/password` | Admin | Update admin password and invalidate old JWTs | +| POST | `/admin/config/import` | Admin | Import config (merge/replace) | +| GET | `/admin/config/export` | Admin | Export full config (`config`/`json`/`base64`) | +| POST | `/admin/keys` | Admin | Add API key (optional `name`/`remark`) | +| PUT | `/admin/keys/{key}` | Admin | Update API key metadata | +| DELETE | `/admin/keys/{key}` | Admin | Delete API key | +| GET | `/admin/proxies` | Admin | List proxies | +| POST | `/admin/proxies` | Admin | Add proxy | +| PUT | `/admin/proxies/{proxyID}` | Admin | Update proxy (empty password keeps old secret) | +| DELETE | `/admin/proxies/{proxyID}` | Admin | Delete proxy (auto-unbind referenced accounts) | +| POST | `/admin/proxies/test` | Admin | Test proxy connectivity | +| GET | `/admin/accounts` | Admin | Paginated account list | +| POST | `/admin/accounts` | Admin | Add account | +| PUT | `/admin/accounts/{identifier}` | Admin | Update account name/remark | +| DELETE | `/admin/accounts/{identifier}` | Admin | Delete account | +| PUT | `/admin/accounts/{identifier}/proxy` | Admin | Bind/unbind proxy for an account | +| GET | `/admin/queue/status` | Admin | Account queue status | +| POST | `/admin/accounts/test` | Admin | Test one account | +| POST | `/admin/accounts/test-all` | Admin | Test all accounts | +| POST | `/admin/accounts/sessions/delete-all` | Admin | Delete all sessions for one account | +| POST | `/admin/import` | Admin | Batch import keys/accounts | +| POST | `/admin/test` | Admin | Test API through service | +| POST | `/admin/dev/raw-samples/capture` | Admin | Fire one request and persist it as a raw sample | +| GET | `/admin/dev/raw-samples/query` | Admin | Search current in-memory capture chains by prompt keyword | +| POST | `/admin/dev/raw-samples/save` | Admin | Persist a selected in-memory capture chain as a raw sample | +| POST | `/admin/vercel/sync` | Admin | Sync config to Vercel | +| GET | `/admin/vercel/status` | Admin | Vercel sync status | +| POST | `/admin/vercel/status` | Admin | Vercel sync status / draft compare | +| GET | `/admin/export` | Admin | Export config JSON/Base64 | +| GET | `/admin/dev/captures` | Admin | Read local packet-capture entries | +| DELETE | `/admin/dev/captures` | Admin | Clear local packet-capture entries | +| GET | `/admin/chat-history` | Admin | Read server-side conversation history | +| DELETE | `/admin/chat-history` | Admin | Clear server-side conversation history | +| GET | `/admin/chat-history/{id}` | Admin | Read one server-side conversation entry | +| DELETE | `/admin/chat-history/{id}` | Admin | Delete one server-side conversation entry | +| PUT | `/admin/chat-history/settings` | Admin | Update conversation history retention limit | +| GET | `/admin/version` | Admin | Check current version and latest Release | + +OpenAI `/v1/*` paths are canonical. For clients configured with the bare DS2API service URL, the same OpenAI handlers are also exposed through root shortcuts: `/models`, `/models/{id}`, `/chat/completions`, `/responses`, `/responses/{response_id}`, `/embeddings`, `/files`, and `/files/{file_id}`. + +--- + +## Health Endpoints + +### `GET /healthz` + +```json +{"status": "ok"} +``` + +### `GET /readyz` + +```json +{"status": "ready"} +``` + +--- + +## OpenAI-Compatible API + +### `GET /v1/models` + +No auth required. Returns the currently supported DeepSeek native model list. + +**Response**: + +```json +{ + "object": "list", + "data": [ + {"id": "deepseek-v4-flash", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-flash-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-flash-search", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-flash-search-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro-search", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro-search-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-vision", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-vision-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []} + ] +} +``` + +> Note: `/v1/models` returns normalized DeepSeek native model IDs. Common aliases are accepted only as request input and are not expanded as separate items in this endpoint. + +### Model Alias Resolution + +For `chat` / `responses` / `embeddings`, DS2API follows a wide-input/strict-output policy: + +1. Match DeepSeek native model IDs first. +2. Then match exact keys in `model_aliases`. +3. If the request name ends with `-nothinking`, resolve the base alias and append the corresponding no-thinking variant. +4. If still unmatched, return `invalid_request_error`. Unknown model families are not guessed heuristically; add explicit compatibility names through `model_aliases`. + +Built-in aliases come from `internal/config/models.go`; `config.model_aliases` can override or add mappings at runtime. Excerpt: + +- OpenAI / Codex: `gpt-4o`, `gpt-4.1`, `gpt-5`, `gpt-5.5`, `gpt-5-codex`, `gpt-5.3-codex`, `codex-mini-latest` +- OpenAI reasoning: `o1`, `o3`, `o3-deep-research`, `o4-mini` +- Claude: `claude-opus-4-6`, `claude-sonnet-4-6`, `claude-haiku-4-5`, `claude-3-5-sonnet-latest` +- Gemini: `gemini-2.5-pro`, `gemini-2.5-flash`, `gemini-3.1-pro`, `gemini-3-pro`, `gemini-3-flash`, `gemini-3.1-flash-lite`, `gemini-pro-vision` +- Other exact built-in aliases: `llama-3.1-70b-instruct`, `qwen-max` + +Aliases with a `-nothinking` suffix also map to the corresponding forced no-thinking DeepSeek model. + +Current vision support resolves only to `deepseek-v4-vision` and does not expose a separate `vision-search` variant. + +Retired historical families such as `claude-1.*`, `claude-2.*`, `claude-instant-*`, and `gpt-3.5*` are explicitly rejected. + +### `POST /v1/chat/completions` + +> Path note: besides the canonical `/v1/chat/completions`, DS2API also accepts the root shortcut `/chat/completions`. On Vercel Runtime, `vercel.json` rewrites only the canonical `/v1/chat/completions` path to the Node streaming bridge; the root shortcut stays on the Go primary path. Use `/v1/chat/completions` on Vercel when real-time streaming is required. + +**Headers**: + +```http +Authorization: Bearer your-api-key +Content-Type: application/json +``` + +**Request body**: + +| Field | Type | Required | Notes | +| --- | --- | --- | --- | +| `model` | string | ✅ | DeepSeek native models + common aliases (`gpt-5.5`, `gpt-5.4-mini`, `gpt-5.3-codex`, `o3`, `claude-opus-4-6`, `gemini-2.5-pro`, `gemini-3.1-pro`, `gemini-3-flash`, etc.); `-nothinking` suffixes force thinking / reasoning off | +| `messages` | array | ✅ | OpenAI-style messages | +| `stream` | boolean | ❌ | Default `false` | +| `tools` | array | ❌ | Function calling schema | +| `temperature`, etc. | any | ❌ | Accepted but final behavior depends on upstream | + +#### Non-Stream Response + +```json +{ + "id": "", + "object": "chat.completion", + "created": 1738400000, + "model": "deepseek-v4-pro", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "final response", + "reasoning_content": "reasoning trace (when thinking is enabled)" + }, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 10, + "completion_tokens": 20, + "total_tokens": 30, + "completion_tokens_details": { + "reasoning_tokens": 5 + } + } +} +``` + +#### Streaming (`stream=true`) + +SSE format: each frame is `data: \n\n`, terminated by `data: [DONE]`. + +```text +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0}]} + +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"reasoning_content":"..."},"index":0}]} + +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"content":"..."},"index":0}]} + +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{},"index":0,"finish_reason":"stop"}],"usage":{...}} + +data: [DONE] +``` + +**Field notes**: + +- First delta includes `role: assistant` +- When thinking is enabled, the stream may emit `delta.reasoning_content` +- Text emits `delta.content` +- Last chunk includes `finish_reason` and `usage` +- Token counting prefers pass-through from upstream DeepSeek SSE (`accumulated_token_usage` / `token_usage`), and only falls back to local estimation when upstream usage is absent. Failed/interrupted endings (for example `response.failed`) may not include `usage` + +#### Tool Calls + +When `tools` is present, DS2API performs anti-leak handling: + +**Non-stream**: If detected, returns `message.tool_calls`, `finish_reason=tool_calls`, `message.content=null`. + +```json +{ + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [ + { + "id": "call_xxx", + "type": "function", + "function": { + "name": "get_weather", + "arguments": "{\"city\":\"beijing\"}" + } + } + ] + }, + "finish_reason": "tool_calls" + } + ] +} +``` + +**Stream**: Once high-confidence toolcall features are matched, DS2API emits `delta.tool_calls` immediately (without waiting for full argument closure), then keeps sending argument deltas; confirmed tool-call fragments are not forwarded as `delta.content`. + +Additional notes: + +- The parser treats the recommended halfwidth-pipe DSML shell tool blocks (`<|DSML|tool_calls>` / `<|DSML|invoke name="...">` / `<|DSML|parameter name="...">`), DSML wrapper aliases (``, `<|tool_calls>`), common DSML separator drift (`<|DSML tool_calls>` / `<|DSML invoke>` / `<|DSML parameter>`), collapsed DSML local names (`` / `` / ``), control-separator drift (`` / raw STX `\x02`), CJK angle bracket, fullwidth-bang / ideographic-comma separator drift, PascalCase local-name drift, and trailing attribute separator drift (`...〈/DSM|parameter〉` / `<!DSML!invoke name=“Bash”>` / `<、DSML、tool_calls>` / `` / ``), arbitrary protocol prefixes (``), and legacy canonical XML tool blocks (`` / `` / ``) as executable tool calls. These shells normalize non-structural separators back to XML first, while internal parsing remains XML-based; CDATA opener drift such as `<![CDATA[` / `<、[CDATA[` is also normalized for parameter bodies. Legacy ``, ``, ``, ``, ``, `tool_use`, antml variants, and standalone JSON `tool_calls` payloads are treated as plain text; complete but malformed wrappers are also released as plain text. +- The parser no longer drops tool calls solely because parameter values are empty; explicit empty strings or whitespace-only parameters become empty strings in structured `tool_calls`. Prompting still tells the model not to emit blank parameters, and missing/empty argument rejection belongs in the tool executor or client schema validation. +- If the final visible response text is empty but the reasoning stream contains an executable tool call, Chat / Responses emits a standard OpenAI `tool_calls` / `function_call` output during finalization. If thinking/reasoning was not enabled by the client, that reasoning text is used only for detection and is not exposed as visible text or `reasoning_content`. +- `tool_calls` shown inside fenced markdown code blocks (for example, ```json ... ```) are treated as examples, not executable calls. + +--- + +### `GET /v1/models/{id}` + +No auth required. Alias values are accepted as path params (for example `gpt-4o`), and the returned object is the mapped DeepSeek model. + +### `POST /v1/responses` + +OpenAI Responses-style endpoint, accepting either `input` or `messages`. + +| Field | Type | Required | Notes | +| --- | --- | --- | --- | +| `model` | string | ✅ | Supports native models + alias mapping | +| `input` | string/array/object | ❌ | One of `input` or `messages` is required | +| `messages` | array | ❌ | One of `input` or `messages` is required | +| `instructions` | string | ❌ | Prepended as a system message | +| `stream` | boolean | ❌ | Default `false` | +| `tools` | array | ❌ | Same tool detection/translation policy as chat | +| `tool_choice` | string/object | ❌ | Supports `auto`/`none`/`required` and forced function selection (`{"type":"function","name":"..."}`) | + +**Non-stream**: Returns a standard `response` object with an ID like `resp_xxx`, and stores it in in-memory TTL cache. +If `tool_choice=required` and no valid tool call is produced, DS2API returns HTTP `422` (`error.code=tool_choice_violation`). + +**Stream (SSE)**: minimal event sequence: + +```text +event: response.created +data: {"type":"response.created","id":"resp_xxx","status":"in_progress",...} + +event: response.output_item.added +data: {"type":"response.output_item.added","response_id":"resp_xxx","item":{"type":"message|function_call",...},...} + +event: response.content_part.added +data: {"type":"response.content_part.added","response_id":"resp_xxx","part":{"type":"output_text",...},...} + +event: response.output_text.delta +data: {"type":"response.output_text.delta","response_id":"resp_xxx","item_id":"msg_xxx","output_index":0,"content_index":0,"delta":"..."} + +event: response.function_call_arguments.delta +data: {"type":"response.function_call_arguments.delta","response_id":"resp_xxx","call_id":"call_xxx","delta":"..."} + +event: response.function_call_arguments.done +data: {"type":"response.function_call_arguments.done","response_id":"resp_xxx","call_id":"call_xxx","name":"tool","arguments":"{...}"} + +event: response.content_part.done +data: {"type":"response.content_part.done","response_id":"resp_xxx",...} + +event: response.output_item.done +data: {"type":"response.output_item.done","response_id":"resp_xxx","item":{"type":"message|function_call",...},...} + +event: response.completed +data: {"type":"response.completed","response":{...}} + +data: [DONE] +``` + +If `tool_choice=required` is violated in stream mode, DS2API emits `response.failed` then `[DONE]` (no `response.completed`). + +> Current behavior: the parser tries to extract structured tool calls and does not enforce a hard allow-list reject; your tool executor should still validate against a whitelist before executing. + +### `GET /v1/responses/{response_id}` + +Business auth required. Fetches cached responses created by `POST /v1/responses` (caller-scoped; only the same key/token can read). + +> Backed by in-memory TTL store. Default TTL is `900s` (configurable via `responses.store_ttl_seconds`). + +### `POST /v1/embeddings` + +Business auth required. Returns OpenAI-compatible embeddings shape. + +| Field | Type | Required | Notes | +| --- | --- | --- | --- | +| `model` | string | ✅ | Supports native models + alias mapping | +| `input` | string/array | ✅ | Supports string, string array, token array | + +> Requires `embeddings.provider`. Current supported values: `mock` / `deterministic` / `builtin` (all three use the same local deterministic implementation). If missing/unsupported, returns standard error shape with HTTP 501. + +### `POST /v1/files` + +Business auth required. OpenAI Files-compatible upload endpoint; currently only `multipart/form-data` is supported. + +| Field | Type | Required | Notes | +| --- | --- | --- | --- | +| `file` | file | ✅ | Binary payload | +| `purpose` | string | ❌ | Forwarded purpose field | + +Constraints and behavior: + +- `Content-Type` must be `multipart/form-data` (otherwise `400`). +- Total request size limit is **100 MiB** (over-limit returns `413`). +- Success returns an OpenAI `file` object (`id/object/bytes/filename/purpose/status`, etc.) and includes `account_id` for source-account tracing. + +### `GET /v1/files/{file_id}` + +Business auth required. Retrieves the current DeepSeek upload status for a file and returns an OpenAI `file` object. Returns `404` when no matching file is found. + +--- + +## Claude-Compatible API + +Besides `/anthropic/v1/*`, DS2API also supports shortcut paths: `/v1/messages`, `/messages`, `/v1/messages/count_tokens`, `/messages/count_tokens`. +Implementation-wise this path is unified on the OpenAI Chat Completions parse-and-translate pipeline to avoid maintaining divergent parsing chains. + +### `GET /anthropic/v1/models` + +No auth required. + +**Response**: + +```json +{ + "object": "list", + "data": [ + {"id": "claude-sonnet-4-6", "object": "model", "created": 1715635200, "owned_by": "anthropic"}, + {"id": "claude-haiku-4-5", "object": "model", "created": 1715635200, "owned_by": "anthropic"}, + {"id": "claude-opus-4-6", "object": "model", "created": 1715635200, "owned_by": "anthropic"} + ], + "first_id": "claude-opus-4-6", + "last_id": "claude-3-haiku-20240307", + "has_more": false +} +``` + +> Note: the example is partial; besides the current primary aliases, the real response also includes Claude 4.x snapshots plus historical 3.x IDs and common aliases. + +### `POST /anthropic/v1/messages` + +**Headers**: + +```http +x-api-key: your-api-key +Content-Type: application/json +anthropic-version: 2023-06-01 +``` + +> `anthropic-version` is optional; DS2API auto-fills `2023-06-01` when absent. + +**Request body**: + +| Field | Type | Required | Notes | +| --- | --- | --- | --- | +| `model` | string | ✅ | For example `claude-sonnet-4-6` / `claude-opus-4-6` / `claude-haiku-4-5` (compatible with `claude-3-5-haiku-latest`), plus historical Claude model IDs | +| `messages` | array | ✅ | Claude-style messages | +| `max_tokens` | number | ❌ | Auto-filled to `8192` when omitted; not strictly enforced by upstream bridge | +| `stream` | boolean | ❌ | Default `false` | +| `system` | string | ❌ | Optional system prompt | +| `tools` | array | ❌ | Claude tool schema | +| `thinking` | object | ❌ | Anthropic thinking config; translated into downstream reasoning control, and ignored by `-nothinking` models | +| `temperature` | number | ❌ | Passed through to the downstream bridge; if `temperature` and `top_p` are both present, `temperature` wins | +| `top_p` | number | ❌ | Passed through when `temperature` is absent | +| `stop_sequences` | array | ❌ | Passed through as downstream stop sequences | +| `tool_choice` | string/object | ❌ | Supports `auto` / `none` / `required` / `{"type":"function","name":"..."}` and is translated to downstream tool choice | + +> Note: `thinking`, `temperature`, `top_p`, `stop_sequences`, and `tool_choice` are translated through the compatibility bridge. Final behavior still depends on the selected model and upstream support. When both `temperature` and `top_p` are present, `temperature` takes precedence. + +#### Non-Stream Response + +```json +{ + "id": "msg_1738400000000000000", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-4-6", + "content": [ + {"type": "text", "text": "response"} + ], + "stop_reason": "end_turn", + "stop_sequence": null, + "usage": { + "input_tokens": 12, + "output_tokens": 34 + } +} +``` + +If tool use is detected, `stop_reason` becomes `tool_use` and `content` contains `tool_use` blocks. + +#### Streaming (`stream=true`) + +SSE uses paired `event:` + `data:` lines. Event type is also in JSON `type`. + +```text +event: message_start +data: {"type":"message_start","message":{...}} + +event: content_block_start +data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} + +event: content_block_delta +data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hello"}} + +event: ping +data: {"type":"ping"} + +event: content_block_stop +data: {"type":"content_block_stop","index":0} + +event: message_delta +data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":12}} + +event: message_stop +data: {"type":"message_stop"} +``` + +**Notes**: + +- Models that support thinking emit `thinking` blocks / `thinking_delta` by default; explicit thinking disablement or `-nothinking` models suppress them +- `signature_delta` is not emitted (DeepSeek does not provide verifiable thinking signatures) +- In `tools` mode, the stream avoids leaking raw tool JSON and does not force `input_json_delta` + +### `POST /anthropic/v1/messages/count_tokens` + +**Request**: + +```json +{ + "model": "claude-sonnet-4-6", + "messages": [ + {"role": "user", "content": "Hello"} + ] +} +``` + +**Response**: + +```json +{ + "input_tokens": 5 +} +``` + +--- + +## Gemini-Compatible API + +Supported paths: + +- `/v1beta/models/{model}:generateContent` +- `/v1beta/models/{model}:streamGenerateContent` +- `/v1/models/{model}:generateContent` (compat path) +- `/v1/models/{model}:streamGenerateContent` (compat path) + +Authentication is the same as other business routes (`Authorization: Bearer ` or `x-api-key`). +Implementation-wise this path is unified on the OpenAI Chat Completions parse-and-translate pipeline to avoid maintaining divergent parsing chains. + +### `POST /v1beta/models/{model}:generateContent` + +Request body accepts Gemini-style `contents` / `tools`. Model names can use aliases and are mapped to DeepSeek models. + +Response uses Gemini-compatible fields, including: + +- `candidates[].content.parts[].text` +- `candidates[].content.parts[].thought=true` for thinking output +- `candidates[].content.parts[].functionCall` (when tool call is produced) +- `usageMetadata` (`promptTokenCount` / `candidatesTokenCount` / `totalTokenCount`) + +### `POST /v1beta/models/{model}:streamGenerateContent` + +Returns SSE (`text/event-stream`), each chunk as `data: `: + +- regular text: incremental text chunks +- thinking: incremental chunks with `parts[].thought=true` +- `tools` mode: buffered and emitted as `functionCall` at finalize phase +- final chunk: includes `finishReason: "STOP"` and `usageMetadata` +- Token counting prefers pass-through from upstream DeepSeek SSE (`accumulated_token_usage` / `token_usage`), and only falls back to local estimation when upstream usage is absent + +--- + +## Ollama API + +- `POST /api/show` request body: `{"model":""}`. +- Response uses lowercase `id` (not `ID`) and includes `capabilities` for Ollama-style clients and strict schemas. + +Example response: + +```json +{ + "id": "deepseek-v4-flash", + "capabilities": ["tools", "thinking"] +} +``` + +## Admin API + +### `POST /admin/login` + +Public endpoint. + +**Request**: + +```json +{ + "admin_key": "admin", + "expire_hours": 24 +} +``` + +`expire_hours` is optional, default `24`. + +**Response**: + +```json +{ + "success": true, + "token": "", + "expires_in": 86400 +} +``` + +### `GET /admin/verify` + +Requires JWT: `Authorization: Bearer ` + +**Response**: + +```json +{ + "valid": true, + "expires_at": 1738400000, + "remaining_seconds": 72000 +} +``` + +### `GET /admin/vercel/config` + +Returns Vercel preconfiguration status. Environment variables are preferred, then the saved `vercel` config block is used as a fallback. + +```json +{ + "has_token": true, + "token_preview": "vc****en", + "token_source": "config", + "project_id": "prj_xxx", + "team_id": null +} +``` + +### `GET /admin/config` + +Returns sanitized config, including both `keys` and `api_keys`. + +```json +{ + "keys": ["k1", "k2"], + "api_keys": [ + {"key": "k1", "name": "Primary", "remark": "Production"}, + {"key": "k2", "name": "Backup", "remark": "Load test"} + ], + "env_backed": false, + "env_source_present": true, + "env_writeback_enabled": true, + "config_path": "/data/config.json", + "vercel": { + "has_token": true, + "token_preview": "vc****en", + "project_id": "prj_xxx", + "team_id": "" + }, + "accounts": [ + { + "identifier": "user@example.com", + "email": "user@example.com", + "mobile": "", + "has_password": true, + "has_token": true, + "token_preview": "abcde..." + } + ], + "model_aliases": { + "claude-sonnet-4-6": "deepseek-v4-flash", + "claude-opus-4-6": "deepseek-v4-pro" + } +} +``` + +### `POST /admin/config` + +Only updates `keys`, `api_keys`, `accounts`, and `model_aliases`. +If both `api_keys` and `keys` are sent, the structured `api_keys` entries win so `name` / `remark` metadata is preserved; `keys` remains a legacy fallback. + +**Request**: + +```json +{ + "keys": ["k1", "k2"], + "api_keys": [ + {"key": "k1", "name": "Primary", "remark": "Production"}, + {"key": "k2", "name": "Backup", "remark": "Load test"} + ], + "accounts": [ + {"email": "user@example.com", "password": "pwd", "token": ""} + ], + "model_aliases": { + "claude-sonnet-4-6": "deepseek-v4-flash", + "claude-opus-4-6": "deepseek-v4-pro" + } +} +``` + +### `GET /admin/settings` + +Reads runtime settings and status, including: + +- `success` +- `admin` (`has_password_hash`, `jwt_expire_hours`, `jwt_valid_after_unix`, `default_password_warning`) +- `runtime` (`account_max_inflight`, `account_max_queue`, `global_max_inflight`, `token_refresh_interval_hours`) +- `config_snapshot`: raw config values (`admin` / `runtime` / `responses` / `embeddings` / `auto_delete` / `current_input_file` / `thinking_injection` / `model_aliases`) before env overrides +- `responses` / `embeddings` +- `auto_delete` (`mode`: `none` / `single` / `all`; legacy `sessions=true` is still treated as `all`) +- `current_input_file` (`enabled` defaults to `true`, plus `min_chars`) +- `thinking_injection` (`enabled` defaults to `true`, `prompt`, and `default_prompt`) +- `model_aliases` +- `env_backed`, `needs_vercel_sync` +- `toolcall` policy is fixed to `feature_match + high` and is no longer returned or editable via settings + +### `PUT /admin/settings` + +Hot-updates runtime settings. Supported fields: + +- `admin.jwt_expire_hours` +- `runtime.account_max_inflight` / `runtime.account_max_queue` / `runtime.global_max_inflight` / `runtime.token_refresh_interval_hours` +- `responses.store_ttl_seconds` +- `embeddings.provider` +- `auto_delete.mode` +- `current_input_file.enabled` / `current_input_file.min_chars` +- `thinking_injection.enabled` / `thinking_injection.prompt` +- `model_aliases` +- `toolcall` policy is fixed and is no longer writable through settings + +### `POST /admin/settings/password` + +Updates admin password and invalidates existing JWTs. + +Request example: + +```json +{"new_password":"your-new-password"} +``` + +It also accepts `{"password":"your-new-password"}`. + +### `POST /admin/config/import` + +Imports full config with: + +- `mode=merge` (default) +- `mode=replace` + +The request can send config directly, or wrapped as `{"config": {...}, "mode":"merge"}`. +Query params `?mode=merge` / `?mode=replace` are also supported. +`replace` mode replaces the full config shape while preserving Vercel sync metadata. `merge` mode merges `keys`, `api_keys`, `accounts`, and `model_aliases`, and overwrites non-empty fields under `admin`, `runtime`, `responses`, and `embeddings`. Manage `auto_delete` and `current_input_file` via `/admin/settings` or the config file; legacy `compat` and `toolcall` fields are ignored. + +> Note: `merge` mode does not update `auto_delete` or `current_input_file`. + +### `GET /admin/config/export` + +Exports full config in three forms: `config`, `json`, and `base64`. + +### `POST /admin/keys` + +```json +{"key": "new-api-key", "name": "Primary", "remark": "Production"} +``` + +**Response**: `{"success": true, "total_keys": 3}` + +### `PUT /admin/keys/{key}` + +Updates the `name` / `remark` of the specified API key. The path `key` is read-only and cannot be changed. + +```json +{"name": "Backup", "remark": "Load test"} +``` + +**Response**: `{"success": true, "total_keys": 3}` + +### `DELETE /admin/keys/{key}` + +**Response**: `{"success": true, "total_keys": 2}` + +### `GET /admin/proxies` + +Lists proxy configs (password is never returned; use `has_password` as a marker). + +### `POST /admin/proxies` + +Adds a proxy. Request accepts `id` (optional; auto-generated when omitted), `name`, `type` (`http` / `socks5`), `host`, `port`, `username`, `password`. + +### `PUT /admin/proxies/{proxyID}` + +Updates a proxy. If `password` is an empty string, the existing secret is preserved. + +### `DELETE /admin/proxies/{proxyID}` + +Deletes a proxy and automatically clears `proxy_id` on all accounts that reference it. + +### `POST /admin/proxies/test` + +Tests proxy connectivity: provide `proxy_id` to test a saved proxy; omit it to run a one-off test using proxy fields in the request body. + +### `GET /admin/accounts` + +**Query params**: + +| Param | Default | Range | +| --- | --- | --- | +| `page` | `1` | ≥ 1 | +| `page_size` | `10` | 1–5000 | +| `q` | empty | Filter by identifier / email / mobile | + +**Response**: + +```json +{ + "items": [ + { + "identifier": "user@example.com", + "email": "user@example.com", + "mobile": "", + "has_password": true, + "has_token": true, + "token_preview": "abc...", + "test_status": "ok" + } + ], + "total": 25, + "page": 1, + "page_size": 10, + "total_pages": 3 +} +``` + +Returned items also include `test_status`, usually `ok` or `failed`. + +### `POST /admin/accounts` + +```json +{"email": "user@example.com", "password": "pwd"} +``` + +**Response**: `{"success": true, "total_accounts": 6}` + +### `PUT /admin/accounts/{identifier}` + +Updates the `name` / `remark` of the specified account. The path `identifier` can be email or mobile and cannot be changed. + +```json +{"name": "Primary account", "remark": "Shared with the team"} +``` + +**Response**: `{"success": true, "total_accounts": 6}` + +### `DELETE /admin/accounts/{identifier}` + +`identifier` can be email, mobile, or the synthetic id for token-only accounts (`token:`). + +**Response**: `{"success": true, "total_accounts": 5}` + +### `PUT /admin/accounts/{identifier}/proxy` + +Updates proxy binding for a specific account. + +- Request body: `{"proxy_id":"..."}`. +- Use empty `proxy_id` to unbind proxy. +- `identifier` supports email / mobile / token-only synthetic id. + +### `GET /admin/queue/status` + +```json +{ + "available": 3, + "in_use": 1, + "total": 4, + "available_accounts": ["a@example.com"], + "in_use_accounts": ["b@example.com"], + "max_inflight_per_account": 2, + "global_max_inflight": 8, + "recommended_concurrency": 8, + "waiting": 0, + "max_queue_size": 8 +} +``` + +| Field | Description | +| --- | --- | +| `available` | Accounts that still have spare inflight capacity | +| `in_use` | Number of occupied in-flight slots | +| `total` | Total accounts | +| `available_accounts` | List of account IDs with remaining inflight capacity | +| `in_use_accounts` | List of account IDs currently in use | +| `max_inflight_per_account` | Per-account inflight limit | +| `global_max_inflight` | Global inflight limit | +| `recommended_concurrency` | Suggested concurrency (`total × max_inflight_per_account`) | +| `waiting` | Number of queued requests currently waiting | +| `max_queue_size` | Waiting queue limit | + +### `POST /admin/accounts/test` + +| Field | Required | Notes | +| --- | --- | --- | +| `identifier` | ✅ | email / mobile / token-only synthetic id | +| `model` | ❌ | default `deepseek-v4-flash` | +| `message` | ❌ | if empty, only session creation is tested | + +**Response**: + +```json +{ + "account": "user@example.com", + "success": true, + "response_time": 1240, + "message": "API test successful (session creation only)", + "model": "deepseek-v4-flash", + "session_count": 0, + "config_writable": true, + "config_warning": "" +} +``` + +If a `message` is provided, `thinking` may also be included when the upstream response carries reasoning text. + +When the configured file path is not writable (for example, read-only `/app/config.json` inside some containers), login/session testing still proceeds; `config_warning` is returned to indicate token persistence failed and the token is memory-only until restart. + +### `POST /admin/accounts/test-all` + +Optional request field: `model`. + +```json +{ + "total": 5, + "success": 4, + "failed": 1, + "results": [...] +} +``` + +The internal concurrency limit is currently fixed at 5. + +### `POST /admin/accounts/sessions/delete-all` + +Deletes all DeepSeek sessions for a specific account. Request example: + +```json +{"identifier":"user@example.com"} +``` + +Response: + +```json +{"success": true, "message": "删除成功"} +``` + +If the account is missing or deletion fails, `success` becomes `false` and `message` contains the error. +The current handler returns the Chinese literal `删除成功` on success. + +### `POST /admin/import` + +Batch import keys and accounts. + +**Request**: + +```json +{ + "keys": ["k1", "k2"], + "accounts": [ + {"email": "user@example.com", "password": "pwd", "token": ""} + ] +} +``` + +**Response**: + +```json +{ + "success": true, + "imported_keys": 2, + "imported_accounts": 1 +} +``` + +### `POST /admin/test` + +Test API availability through the service itself. + +| Field | Required | Default | +| --- | --- | --- | +| `model` | ❌ | `deepseek-v4-flash` | +| `message` | ❌ | `你好` | +| `api_key` | ❌ | First key in config | + +**Response**: + +```json +{ + "success": true, + "status_code": 200, + "response": {"id": "..."} +} +``` + +### `POST /admin/dev/raw-samples/capture` + +Internally issues one `/v1/chat/completions` request through the service, then persists the request metadata and raw upstream SSE into `tests/raw_stream_samples//`. + +Common request fields: + +| Field | Required | Default | Notes | +| --- | --- | --- | --- | +| `message` | No | `你好` | Convenience single-turn user message | +| `messages` | No | Auto-derived from `message` | OpenAI-style message array | +| `model` | No | `deepseek-v4-flash` | Target model | +| `stream` | No | `true` | Recommended to keep streaming enabled so raw SSE is recorded | +| `api_key` | No | First configured key | Business API key to use | +| `sample_id` | No | Auto-generated | Sample directory name | + +On success, the response headers include: + +- `X-Ds2-Sample-Id` +- `X-Ds2-Sample-Dir` +- `X-Ds2-Sample-Meta` +- `X-Ds2-Sample-Upstream` + +If the request itself succeeds but the process did not record a new upstream capture, the endpoint returns: + +```json +{"detail":"no upstream capture was recorded"} +``` + +### `GET /admin/dev/raw-samples/query` + +Searches the current process's in-memory capture entries and groups `completion + continue` rounds by `chat_session_id`. + +**Query parameters**: + +| Param | Default | Notes | +| --- | --- | --- | +| `q` | empty | Fuzzy match against request/response text | +| `limit` | `20` | Max number of chains returned | + +**Response fields** include: + +- `items[].chain_key` +- `items[].capture_ids` +- `items[].round_count` +- `items[].initial_label` +- `items[].request_preview` +- `items[].response_preview` + +### `POST /admin/dev/raw-samples/save` + +Persists one selected in-memory capture chain into `tests/raw_stream_samples//`. + +Any one of these selectors is accepted: + +```json +{"chain_key":"session:xxxx","sample_id":"tmp-from-memory"} +``` + +```json +{"capture_id":"cap_xxx","sample_id":"tmp-from-memory"} +``` + +```json +{"query":"Guangzhou weather","sample_id":"tmp-from-memory"} +``` + +The success payload includes `sample_id`, `dir`, `meta_path`, and `upstream_path`. + +### `POST /admin/vercel/sync` + +| Field | Required | Notes | +| --- | --- | --- | +| `vercel_token` | ❌ | If empty or `__USE_PRECONFIG__`, read env, then saved config | +| `project_id` | ❌ | Fallback: `VERCEL_PROJECT_ID`, then saved config | +| `team_id` | ❌ | Fallback: `VERCEL_TEAM_ID`, then saved config | +| `auto_validate` | ❌ | Default `true` | +| `save_credentials` | ❌ | Default `true`; saves explicitly supplied Vercel credentials for the next sync | +| `config_override` | ❌ | Provide a full config object to sync instead of the runtime config | + +**Success response**: + +```json +{ + "success": true, + "validated_accounts": 3, + "message": "Config synced, redeploying...", + "deployment_url": "https://..." +} +``` + +Or manual deploy required: + +```json +{ + "success": true, + "validated_accounts": 3, + "message": "Config synced to Vercel, please trigger redeploy manually", + "manual_deploy_required": true +} +``` + +Failed account checks are returned in `failed_accounts`, and any saved Vercel credentials are returned in `saved_credentials`. + +### `GET /admin/vercel/status` + +```json +{ + "synced": true, + "last_sync_time": 1738400000, + "has_synced_before": true, + "env_backed": false, + "config_hash": "....", + "last_synced_hash": "....", + "draft_hash": "....", + "draft_differs": false +} +``` + +`POST /admin/vercel/status` can also accept `config_override` to compare a draft config against the current synced config. + +### `GET /admin/export` + +```json +{ + "json": "{...}", + "base64": "ey4uLn0=" +} +``` + +This is the same payload as `GET /admin/config/export`, just with a shorter path. + +### `GET /admin/version` + +Checks the current build version and the latest GitHub Release: + +```json +{ + "success": true, + "current_version": "3.0.0", + "current_tag": "v3.0.0", + "source": "file:VERSION", + "checked_at": "2026-03-29T00:00:00Z", + "latest_tag": "v3.0.0", + "latest_version": "3.0.0", + "release_url": "https://github.com/CJackHwang/ds2api/releases/tag/v3.0.0", + "published_at": "2026-03-28T12:00:00Z", + "has_update": false +} +``` + +If GitHub API access fails, the response includes `check_error` while still returning HTTP 200. + +### `GET /admin/dev/captures` + +Reads local packet-capture status and recent entries (Admin auth required): + +- `enabled` +- `limit` +- `max_body_bytes` +- `items` + +### `DELETE /admin/dev/captures` + +Clears packet-capture entries: + +```json +{"success":true,"detail":"capture logs cleared"} +``` + +--- + +## Error Payloads + +Compatible routes (`/v1/*`, `/anthropic/*`) use the same error envelope: + +```json +{ + "error": { + "message": "...", + "type": "invalid_request_error", + "code": "invalid_request", + "param": null + } +} +``` + +Admin routes keep `{"detail":"..."}`. + +Gemini routes use Google-style errors: + +```json +{ + "error": { + "code": 400, + "message": "invalid json", + "status": "INVALID_ARGUMENT" + } +} +``` + +Clients should handle HTTP status code plus `error` / `detail` fields. + +**Common status codes**: + +| Code | Meaning | +| --- | --- | +| `401` | Authentication failed (invalid key/token, or expired admin JWT) | +| `429` | Too many requests (exceeded inflight + queue capacity, or upstream thinking-only output with no visible answer; managed-account mode first tries one alternate-account fresh retry; current responses do not include `Retry-After`) | +| `503` | Model unavailable or upstream error | + +--- + +## cURL Examples + +### OpenAI Non-Stream + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash", + "messages": [{"role": "user", "content": "Hello"}], + "stream": false + }' +``` + +### OpenAI Stream + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-pro", + "messages": [{"role": "user", "content": "Explain quantum entanglement"}], + "stream": true + }' +``` + +### OpenAI Responses (Stream) + +```bash +curl http://localhost:5001/v1/responses \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "gpt-5-codex", + "input": "Write a hello world in golang", + "stream": true + }' +``` + +### OpenAI Embeddings + +```bash +curl http://localhost:5001/v1/embeddings \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "gpt-4o", + "input": ["first text", "second text"] + }' +``` + +### OpenAI with Search + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash-search", + "messages": [{"role": "user", "content": "Latest news today"}], + "stream": true + }' +``` + +### OpenAI Tool Calling + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash", + "messages": [{"role": "user", "content": "What is the weather in Beijing?"}], + "tools": [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "Get weather for a city", + "parameters": { + "type": "object", + "properties": { + "city": {"type": "string", "description": "City name"} + }, + "required": ["city"] + } + } + } + ] + }' +``` + +### Gemini Non-Stream + +```bash +curl "http://localhost:5001/v1beta/models/gemini-2.5-pro:generateContent" \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "contents": [ + { + "role": "user", + "parts": [{"text": "Introduce Go in three sentences"}] + } + ] + }' +``` + +### Gemini Stream + +```bash +curl "http://localhost:5001/v1beta/models/gemini-2.5-flash:streamGenerateContent" \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "contents": [ + { + "role": "user", + "parts": [{"text": "Write a short summary"}] + } + ] + }' +``` + +### Claude Non-Stream + +```bash +curl http://localhost:5001/anthropic/v1/messages \ + -H "x-api-key: your-api-key" \ + -H "Content-Type: application/json" \ + -H "anthropic-version: 2023-06-01" \ + -d '{ + "model": "claude-sonnet-4-6", + "max_tokens": 1024, + "messages": [{"role": "user", "content": "Hello"}] + }' +``` + +### Claude Stream + +```bash +curl http://localhost:5001/anthropic/v1/messages \ + -H "x-api-key: your-api-key" \ + -H "Content-Type: application/json" \ + -H "anthropic-version: 2023-06-01" \ + -d '{ + "model": "claude-opus-4-6", + "max_tokens": 1024, + "messages": [{"role": "user", "content": "Explain relativity"}], + "stream": true + }' +``` + +### Admin Login + +```bash +curl http://localhost:5001/admin/login \ + -H "Content-Type: application/json" \ + -d '{"admin_key": "admin"}' +``` + +### Pin Specific Account + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "X-Ds2-Target-Account: user@example.com" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash", + "messages": [{"role": "user", "content": "Hello"}] + }' +``` diff --git a/API.md b/API.md new file mode 100644 index 0000000000000000000000000000000000000000..dcca6019f2ab47244bc8ce9aca4ed6713ecf6c41 --- /dev/null +++ b/API.md @@ -0,0 +1,1459 @@ +# DS2API 接口文档1 + +语言 / Language: [中文](API.md) | [English](API.en.md) + +本文档描述当前 Go 代码库的实际 API 行为。 + +文档导航:[总览](README.MD) / [架构说明](docs/ARCHITECTURE.md) / [部署指南](docs/DEPLOY.md) / [测试指南](docs/TESTING.md) + +--- + +## 目录 + +- [基础信息](#基础信息) +- [配置最佳实践](#配置最佳实践) +- [鉴权规则](#鉴权规则) +- [路由总览](#路由总览) +- [健康检查](#健康检查) +- [OpenAI 兼容接口](#openai-兼容接口) +- [Claude 兼容接口](#claude-兼容接口) +- [Gemini 兼容接口](#gemini-兼容接口) +- [Ollama 兼容接口](#ollama-兼容接口) +- [Admin 接口](#admin-接口) +- [错误响应格式](#错误响应格式) +- [cURL 示例](#curl-示例) + +--- + +## 基础信息 + +| 项目 | 说明 | +| --- | --- | +| Base URL | `http://localhost:5001` 或你的部署域名 | +| 默认 Content-Type | `application/json` | +| 健康检查 | `GET /healthz`、`GET /readyz` | +| CORS | 已启用(统一覆盖 `/v1/*`、`/anthropic/*`、`/v1beta/models/*`、`/api/*`、`/admin/*`;浏览器有 `Origin` 时回显该 Origin,否则为 `*`;默认允许 `Content-Type`, `Authorization`, `X-API-Key`, `X-Ds2-Target-Account`, `X-Ds2-Source`, `X-Vercel-Protection-Bypass`, `X-Goog-Api-Key`, `Anthropic-Version`, `Anthropic-Beta`,并会放行预检里声明的第三方请求头,如 `x-stainless-*`;Vercel 上 `/v1/chat/completions` 的 Node Runtime 也对齐相同行为;内部专用头 `X-Ds2-Internal-Token` 仍被拦截) | + +- 所有 JSON 请求体都必须是合法 UTF-8;非法字节序列会在入站阶段被拒绝为 `400 invalid json`。 + +### 3.0 接口适配层说明 + +- OpenAI / Claude / Gemini 三套协议已统一挂在同一 `chi` 路由树上,由 `internal/server/router.go` 负责装配。 +- 适配器层职责收敛为:**请求归一化 → DeepSeek 调用 → 协议形态渲染**,减少历史版本中“同能力多处实现”的分叉。 +- Tool Calling 的解析策略在 Go 与 Node Runtime 间保持一致:推荐模型输出半角管道符 DSML 外壳 `<|DSML|tool_calls>` → `<|DSML|invoke name="...">` → `<|DSML|parameter name="...">`;兼容层也接受 DSML wrapper 别名 ``、`<|tool_calls>`、常见 DSML 分隔符漏写形态(如 `<|DSML tool_calls>`)、`DSML` 与工具标签名黏连的常见 typo(如 ``)、控制分隔符漂移(如 `` / 原始 STX `\x02`)、CJK 尖括号、全角感叹号、顿号、PascalCase 本地名、弯引号属性值与属性尾部分隔符漂移(如 `...〈/DSM|parameter〉` / `<!DSML!invoke name=“Bash”>` / `<、DSML、tool_calls>` / `` / ``)、任意协议前缀壳(如 ``),以及旧式 canonical XML `` → `` → ``。实现上采用结构扫描:只要固定本地标签名是 `tool_calls` / `invoke` / `parameter`,标签名前或标签名后的非结构性分隔符会在解析入口归一化;CDATA 开头也会容错 `<![CDATA[` / `<、[CDATA[` 这类分隔符漂移;只有 `tool_calls` wrapper 或可修复的缺失 opening wrapper 会进入工具路径,裸 `` 不计为已支持语法;流式场景继续执行防泄漏筛分。若参数体本身是合法 JSON 字面量(如 `123`、`true`、`null`、数组或对象),会按结构化值输出,不再一律当作字符串;显式空字符串和纯空白参数会结构化保留为空字符串,是否拒绝缺参由工具执行侧决定;完整但 malformed 的 wrapper 会作为普通文本释放,不会吞掉或伪造成工具调用;若 CDATA 偶发漏闭合,则会在最终 parse / flush 恢复阶段做窄修复,尽量保住已完整包裹的外层工具调用。 +- `Admin API` 将配置与运行时策略分开:`/admin/config*` 管静态配置,`/admin/settings*` 管运行时行为。 +- 当上游返回 thinking-only 响应(模型输出了推理链但无可见文本)时,Go 主路径与 Vercel Node 流式路径都会先自动重试一次:以多轮对话 follow-up 方式追加 prompt 后缀 `"Please provide a non-empty final answer or tool call."` 并设置 `parent_message_id` 在同一 DeepSeek session 内让模型重新输出;同账号重试最大 1 次。若同账号重试后仍即将返回 `429 upstream_empty_output`,托管账号模式会在返回 429 前自动切换到下一个可用账号,新建 session,用原始 payload 再 fresh retry 一次。 +- 引用标记处理边界:流式输出默认隐藏 `[citation:N]` / `[reference:N]` 这类上游内部占位符;非流式输出默认把 DeepSeek 搜索引用标记转换为 Markdown 引用链接。 + +--- + +## 配置最佳实践 + +推荐把 `config.json` 作为唯一配置源: + +```bash +cp config.example.json config.json +# 编辑 config.json(keys/accounts) +``` + +按部署方式使用: + +- 本地运行:直接读取 `config.json` +- Docker / Vercel:从 `config.json` 生成 Base64,填入 `DS2API_CONFIG_JSON`,也可以直接填原始 JSON + +```bash +DS2API_CONFIG_JSON="$(base64 < config.json | tr -d '\n')" +``` + +Vercel 一键部署可先只填 `DS2API_ADMIN_KEY`,部署后在 `/admin` 导入配置,再通过 “Vercel 同步” 写回环境变量。 + +--- + +## 鉴权规则 + +### 业务接口(`/v1/*`、`/anthropic/*`、`/v1beta/models/*`) + +支持两种传参方式: + +| 方式 | 示例 | +| --- | --- | +| Bearer Token | `Authorization: Bearer ` | +| API Key Header | `x-api-key: `(无 `Bearer` 前缀) | +| Gemini 兼容 | `x-goog-api-key: ` 或 `?key=` / `?api_key=` | + +**鉴权行为**: + +- token 在 `config.keys` 中 → **托管账号模式**,自动轮询选择账号 +- token 不在 `config.keys` 中 → **直通 token 模式**,直接作为 DeepSeek token 使用 + +**可选请求头**:`X-Ds2-Target-Account: ` — 指定使用某个托管账号;如果目标账号不存在,或管理账号队列已耗尽,相关业务请求会返回 `429`,当前不会附带 `Retry-After` 头。若账号存在但登录/刷新失败,则返回对应的 `401` 或上游错误。未指定目标账号时,托管账号模式的 completion 空输出 429 会先尝试切到另一个可用账号 fresh retry 一次;指定目标账号或无其他可用账号时不会切号。 +Gemini 兼容客户端还可以使用 `x-goog-api-key`、`?key=` 或 `?api_key=` 作为凭据来源。 + +### Admin 接口(`/admin/*`) + +| 端点 | 鉴权 | +| --- | --- | +| `POST /admin/login` | 无需鉴权 | +| `GET /admin/verify` | `Authorization: Bearer `(仅 JWT) | +| 其他 `/admin/*` | `Authorization: Bearer ` 或 `Authorization: Bearer `(直传管理密钥) | + +--- + +## 路由总览 + +| 方法 | 路径 | 鉴权 | 说明 | +| --- | --- | --- | --- | +| GET | `/healthz` | 无 | 存活探针 | +| HEAD | `/healthz` | 无 | 存活探针(无响应体) | +| GET | `/readyz` | 无 | 就绪探针 | +| HEAD | `/readyz` | 无 | 就绪探针(无响应体) | +| GET | `/v1/models` | 无 | OpenAI 模型列表 | +| GET | `/v1/models/{id}` | 无 | OpenAI 单模型查询(支持 alias 入参) | +| POST | `/v1/chat/completions` | 业务 | OpenAI 对话补全 | +| POST | `/v1/responses` | 业务 | OpenAI Responses 接口(流式/非流式) | +| GET | `/v1/responses/{response_id}` | 业务 | 查询已生成 response(内存 TTL) | +| POST | `/v1/embeddings` | 业务 | OpenAI Embeddings 接口 | +| POST | `/v1/files` | 业务 | OpenAI Files 上传(multipart/form-data) | +| GET | `/v1/files/{file_id}` | 业务 | 查询已上传文件状态 | +| GET | `/anthropic/v1/models` | 无 | Claude 模型列表 | +| POST | `/anthropic/v1/messages` | 业务 | Claude 消息接口 | +| POST | `/anthropic/v1/messages/count_tokens` | 业务 | Claude token 计数 | +| POST | `/v1/messages` | 业务 | Claude 消息快捷路径 | +| POST | `/messages` | 业务 | Claude 消息快捷路径 | +| POST | `/v1/messages/count_tokens` | 业务 | Claude token 计数快捷路径 | +| POST | `/messages/count_tokens` | 业务 | Claude token 计数快捷路径 | +| POST | `/v1beta/models/{model}:generateContent` | 业务 | Gemini 非流式 | +| POST | `/v1beta/models/{model}:streamGenerateContent` | 业务 | Gemini 流式 | +| POST | `/v1/models/{model}:generateContent` | 业务 | Gemini 非流式兼容路径 | +| POST | `/v1/models/{model}:streamGenerateContent` | 业务 | Gemini 流式兼容路径 | +| GET | `/api/version` | 无 | Ollama 版本接口 | +| GET | `/api/tags` | 无 | Ollama 模型列表 | +| POST | `/api/show` | 无 | Ollama 单模型能力查询(返回 `id` 与 `capabilities`) | +| POST | `/admin/login` | 无 | 管理登录 | +| GET | `/admin/verify` | JWT | 校验管理 JWT | +| GET | `/admin/vercel/config` | Admin | 读取 Vercel 预配置 | +| GET | `/admin/config` | Admin | 读取配置(脱敏) | +| POST | `/admin/config` | Admin | 更新配置 | +| GET | `/admin/settings` | Admin | 读取运行时设置 | +| PUT | `/admin/settings` | Admin | 更新运行时设置(热更新) | +| POST | `/admin/settings/password` | Admin | 更新 Admin 密码并使旧 JWT 失效 | +| POST | `/admin/config/import` | Admin | 导入配置(merge/replace) | +| GET | `/admin/config/export` | Admin | 导出完整配置(含 `config`/`json`/`base64`) | +| POST | `/admin/keys` | Admin | 添加 API key(可附 name/remark) | +| PUT | `/admin/keys/{key}` | Admin | 更新 API key 备注信息 | +| DELETE | `/admin/keys/{key}` | Admin | 删除 API key | +| GET | `/admin/proxies` | Admin | 代理列表 | +| POST | `/admin/proxies` | Admin | 添加代理 | +| PUT | `/admin/proxies/{proxyID}` | Admin | 更新代理(留空 password 表示保留原密码) | +| DELETE | `/admin/proxies/{proxyID}` | Admin | 删除代理(自动解绑引用该代理的账号) | +| POST | `/admin/proxies/test` | Admin | 测试代理连通性 | +| GET | `/admin/accounts` | Admin | 分页账号列表 | +| POST | `/admin/accounts` | Admin | 添加账号 | +| PUT | `/admin/accounts/{identifier}` | Admin | 更新账号 name/remark | +| DELETE | `/admin/accounts/{identifier}` | Admin | 删除账号 | +| PUT | `/admin/accounts/{identifier}/proxy` | Admin | 为账号绑定/解绑代理 | +| GET | `/admin/queue/status` | Admin | 账号队列状态 | +| POST | `/admin/accounts/test` | Admin | 测试单个账号 | +| POST | `/admin/accounts/test-all` | Admin | 测试全部账号 | +| POST | `/admin/accounts/sessions/delete-all` | Admin | 删除某账号的全部会话 | +| POST | `/admin/import` | Admin | 批量导入 keys/accounts | +| POST | `/admin/test` | Admin | 测试当前 API 可用性 | +| POST | `/admin/dev/raw-samples/capture` | Admin | 直接发起一次请求并保存为 raw sample | +| GET | `/admin/dev/raw-samples/query` | Admin | 按问题关键词查询当前内存抓包链 | +| POST | `/admin/dev/raw-samples/save` | Admin | 把命中的内存抓包链保存为 raw sample | +| POST | `/admin/vercel/sync` | Admin | 同步配置到 Vercel | +| GET | `/admin/vercel/status` | Admin | Vercel 同步状态 | +| POST | `/admin/vercel/status` | Admin | Vercel 同步状态 / 草稿对比 | +| GET | `/admin/export` | Admin | 导出配置 JSON/Base64 | +| GET | `/admin/dev/captures` | Admin | 查看本地抓包记录 | +| DELETE | `/admin/dev/captures` | Admin | 清空本地抓包记录 | +| GET | `/admin/chat-history` | Admin | 查看服务器端对话记录 | +| DELETE | `/admin/chat-history` | Admin | 清空服务器端对话记录 | +| GET | `/admin/chat-history/{id}` | Admin | 查看单条服务器端对话记录 | +| DELETE | `/admin/chat-history/{id}` | Admin | 删除单条服务器端对话记录 | +| PUT | `/admin/chat-history/settings` | Admin | 更新对话记录保留条数 | +| GET | `/admin/version` | Admin | 查询当前版本与最新 Release | + +OpenAI `/v1/*` 仍是规范路径。对于只配置 DS2API 根地址的客户端,同一套 OpenAI handler 也通过根路径快捷路由暴露:`/models`、`/models/{id}`、`/chat/completions`、`/responses`、`/responses/{response_id}`、`/embeddings`、`/files`、`/files/{file_id}`。 + +服务器端记录本质上是 DeepSeek 上游响应归档:OpenAI Chat、OpenAI Responses、Claude Messages、Gemini GenerateContent 等直连 DeepSeek 的生成接口,在收到上游响应后会于各协议回译/裁剪前写入记录;列表按请求创建时间倒序展示,流式请求会在生成过程中持续刷新状态与详情。WebUI「API 测试」发出的请求也会进入该记录。 + +--- + +## 健康检查 + +### `GET /healthz` + +```json +{"status": "ok"} +``` + +### `GET /readyz` + +```json +{"status": "ready"} +``` + +--- + +## OpenAI 兼容接口 + +### `GET /v1/models` + +无需鉴权。返回当前支持的 DeepSeek 原生模型列表。 + +**响应示例**: + +```json +{ + "object": "list", + "data": [ + {"id": "deepseek-v4-flash", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-flash-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-flash-search", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-flash-search-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro-search", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-pro-search-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-vision", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []}, + {"id": "deepseek-v4-vision-nothinking", "object": "model", "created": 1677610602, "owned_by": "deepseek", "permission": []} + ] +} +``` + +> 说明:`/v1/models` 返回的是规范化后的 DeepSeek 原生模型 ID;常见 alias 仅用于请求入参解析,不会在该接口中单独展开返回。带 `-nothinking` 后缀的模型表示无论请求里是否显式开启 thinking / reasoning,都会强制关闭思考输出。 + +### 模型 alias 解析策略 + +对 `chat` / `responses` / `embeddings` 的 `model` 字段采用“宽进严出”: + +1. 先匹配 DeepSeek 原生模型。 +2. 再匹配 `model_aliases` 精确映射。 +3. 如果请求名以 `-nothinking` 结尾,则在最终解析出的规范模型上追加对应的无思考变体。 +4. 仍未命中则返回 `invalid_request_error`。当前不会按未知模型家族做启发式兜底;需要新增兼容名时请通过 `model_aliases` 明确配置。 + +当前内置默认 alias 来自 `internal/config/models.go`,`config.model_aliases` 会在运行时覆盖或补充同名映射。节选: + +- OpenAI / Codex:`gpt-4o`、`gpt-4.1`、`gpt-5`、`gpt-5.5`、`gpt-5-codex`、`gpt-5.3-codex`、`codex-mini-latest` +- OpenAI reasoning:`o1`、`o3`、`o3-deep-research`、`o4-mini` +- Claude:`claude-opus-4-6`、`claude-sonnet-4-6`、`claude-haiku-4-5`、`claude-3-5-sonnet-latest` +- Gemini:`gemini-2.5-pro`、`gemini-2.5-flash`、`gemini-3.1-pro`、`gemini-3-pro`、`gemini-3-flash`、`gemini-3.1-flash-lite`、`gemini-pro-vision` +- 其他内置精确 alias:`llama-3.1-70b-instruct`、`qwen-max` + +上述 alias 若在请求名后追加 `-nothinking` 后缀,也会映射到对应的强制关闭 thinking 版本。 +当前视觉能力仅对应 `deepseek-v4-vision` / `deepseek-v4-vision-nothinking`,不会解析出独立的 `vision-search` 变体。 + +退役历史模型(如 `claude-1.*`、`claude-2.*`、`claude-instant-*`、`gpt-3.5*`)会被显式拒绝。 + +### `POST /v1/chat/completions` + +> 路径说明:除规范路径 `/v1/chat/completions` 外,也支持根路径快捷别名 `/chat/completions`。在 Vercel Runtime 上,`vercel.json` 仅把规范路径 `/v1/chat/completions` 重写到 Node 流式桥接;根路径快捷别名仍走 Go 主链路。因此 Vercel 上需要实时流式时请使用 `/v1/chat/completions`。 + +**请求头**: + +```http +Authorization: Bearer your-api-key +Content-Type: application/json +``` + +**请求体**: + +| 字段 | 类型 | 必填 | 说明 | +| --- | --- | --- | --- | +| `model` | string | ✅ | 支持 DeepSeek 原生模型 + 常见 alias(如 `gpt-5.5`、`gpt-5.4-mini`、`gpt-5.3-codex`、`o3`、`claude-opus-4-6`、`claude-sonnet-4-6`、`gemini-2.5-pro`、`gemini-3.1-pro`、`gemini-3-flash` 等);若模型名带 `-nothinking` 后缀,则强制关闭 thinking / reasoning | +| `messages` | array | ✅ | OpenAI 风格消息数组 | +| `stream` | boolean | ❌ | 默认 `false` | +| `tools` | array | ❌ | Function Calling 定义 | +| `temperature` 等 | any | ❌ | 兼容透传字段(最终效果由上游决定) | + +#### 非流式响应 + +```json +{ + "id": "", + "object": "chat.completion", + "created": 1738400000, + "model": "deepseek-v4-pro", + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": "最终回复", + "reasoning_content": "思考内容(开启 thinking 时)" + }, + "finish_reason": "stop" + } + ], + "usage": { + "prompt_tokens": 10, + "completion_tokens": 20, + "total_tokens": 30, + "completion_tokens_details": { + "reasoning_tokens": 5 + } + } +} +``` + +#### 流式响应(`stream=true`) + +SSE 格式:每段为 `data: \n\n`,结束为 `data: [DONE]`。 + +```text +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"role":"assistant"},"index":0}]} + +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"reasoning_content":"..."},"index":0}]} + +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{"content":"..."},"index":0}]} + +data: {"id":"...","object":"chat.completion.chunk","choices":[{"delta":{},"index":0,"finish_reason":"stop"}],"usage":{...}} + +data: [DONE] +``` + +**字段说明**: + +- 首个 delta 包含 `role: assistant` +- 开启 thinking 时会输出 `delta.reasoning_content` +- 普通文本输出 `delta.content` +- 最后一段包含 `finish_reason` 和 `usage` +- token 计数优先透传上游 DeepSeek SSE(如 `accumulated_token_usage` / `token_usage`);仅在上游缺失时回退本地估算。失败/中断型结束(例如 `response.failed`)可能不会携带 `usage` + +#### Tool Calls + +当请求中含 `tools` 时,DS2API 做防泄漏处理: + +**非流式**:识别到工具调用时,返回 `message.tool_calls`,设置 `finish_reason=tool_calls`,`message.content=null`。 + +```json +{ + "choices": [ + { + "index": 0, + "message": { + "role": "assistant", + "content": null, + "tool_calls": [ + { + "id": "call_xxx", + "type": "function", + "function": { + "name": "get_weather", + "arguments": "{\"city\":\"beijing\"}" + } + } + ] + }, + "finish_reason": "tool_calls" + } + ] +} +``` + +**流式**:命中高置信特征后立即输出 `delta.tool_calls`(不等待完整工具参数闭合),并持续发送 arguments 增量;已确认的工具调用片段不会回流到 `delta.content`。 + +补充说明: + +- **非代码块上下文**下,工具负载即使与普通文本混合,也会按特征识别并产出可执行 tool call(前后普通文本仍可透传)。 +- 解析器当前把推荐半角管道符 DSML 外壳(`<|DSML|tool_calls>` / `<|DSML|invoke name="...">` / `<|DSML|parameter name="...">`)、DSML wrapper 别名(``、`<|tool_calls>`)、常见 DSML 分隔符漏写形态(如 `<|DSML tool_calls>` / `<|DSML invoke>` / `<|DSML parameter>`)、`DSML` 与工具标签名黏连的常见 typo(如 `` / `` / ``)、控制分隔符漂移(如 `` / 原始 STX `\x02`)、CJK 尖括号、全角感叹号、顿号、PascalCase 本地名、弯引号属性值与属性尾部分隔符漂移(如 `...〈/DSM|parameter〉` / `<!DSML!invoke name=“Bash”>` / `<、DSML、tool_calls>` / `` / ``)、任意协议前缀壳(如 ``)和旧式 canonical XML 工具块(`` / `` / ``)作为可执行调用解析;这些非结构性分隔符壳会先归一化回 XML,内部仍以 XML 解析语义为准,CDATA 开头也会容错 `<![CDATA[` / `<、[CDATA[`。旧式 ``、``、``、``、``、`tool_use`、antml 风格与纯 JSON `tool_calls` 片段默认都会按普通文本处理;完整但 malformed 的 wrapper 同样会作为普通文本释放。 +- 解析层不会因为参数值为空而丢弃工具调用;显式空字符串或纯空白参数会按空字符串进入结构化 `tool_calls`。Prompt 会要求模型不要主动输出空参数,缺参/空命令的拒绝应由工具执行侧或客户端 schema 校验负责。 +- 当最终可见正文为空但思维链里包含可执行工具调用时,Chat / Responses 会在收尾阶段补发标准 OpenAI `tool_calls` / `function_call` 输出;如果客户端未开启 thinking / reasoning,该思维链只用于检测,不会作为可见正文或 `reasoning_content` 暴露。 +- Markdown fenced code block(例如 ```json ... ```)和行内 code span(例如 `` `...` ``)中的 `tool_calls` 仅视为示例文本,不会被执行。 + +--- + +### `GET /v1/models/{id}` + +无需鉴权。入参支持 alias(例如 `gpt-4o`),返回的是映射后的 DeepSeek 模型对象。 + +### `POST /v1/responses` + +OpenAI Responses 风格接口,兼容 `input` 或 `messages`。 + +| 字段 | 类型 | 必填 | 说明 | +| --- | --- | --- | --- | +| `model` | string | ✅ | 支持原生模型 + alias 自动映射 | +| `input` | string/array/object | ❌ | 与 `messages` 二选一 | +| `messages` | array | ❌ | 与 `input` 二选一 | +| `instructions` | string | ❌ | 自动前置为 system 消息 | +| `stream` | boolean | ❌ | 默认 `false` | +| `tools` | array | ❌ | 与 chat 同样的工具识别与转译策略(含代码块示例豁免) | +| `tool_choice` | string/object | ❌ | 支持 `auto`/`none`/`required` 与强制函数(`{"type":"function","name":"..."}`) | + +**非流式响应**:返回标准 `response` 对象,`id` 形如 `resp_xxx`,并写入内存 TTL 存储。 +当 `tool_choice=required` 且未产出有效工具调用时,返回 HTTP `422`(`error.code=tool_choice_violation`)。 + +**流式响应(SSE)**:最小事件序列如下。 + +```text +event: response.created +data: {"type":"response.created","id":"resp_xxx","status":"in_progress",...} + +event: response.output_item.added +data: {"type":"response.output_item.added","response_id":"resp_xxx","item":{"type":"message|function_call",...},...} + +event: response.content_part.added +data: {"type":"response.content_part.added","response_id":"resp_xxx","part":{"type":"output_text",...},...} + +event: response.output_text.delta +data: {"type":"response.output_text.delta","response_id":"resp_xxx","item_id":"msg_xxx","output_index":0,"content_index":0,"delta":"..."} + +event: response.function_call_arguments.delta +data: {"type":"response.function_call_arguments.delta","response_id":"resp_xxx","call_id":"call_xxx","delta":"..."} + +event: response.function_call_arguments.done +data: {"type":"response.function_call_arguments.done","response_id":"resp_xxx","call_id":"call_xxx","name":"tool","arguments":"{...}"} + +event: response.content_part.done +data: {"type":"response.content_part.done","response_id":"resp_xxx",...} + +event: response.output_item.done +data: {"type":"response.output_item.done","response_id":"resp_xxx","item":{"type":"message|function_call",...},...} + +event: response.completed +data: {"type":"response.completed","response":{...}} + +data: [DONE] +``` + +流式场景下若 `tool_choice=required` 违规,会返回 `response.failed` 后结束(不再发送 `response.completed`)。 + +> 当前版本说明:解析层默认“尽量提取结构化 tool call”,未启用基于 `tools` allow-list 的硬拒绝;是否执行仍应由你的工具执行器做白名单校验。 + +### `GET /v1/responses/{response_id}` + +需要业务鉴权。查询 `POST /v1/responses` 生成并缓存的 response 对象(按调用方鉴权隔离,仅同一 key/token 可读取)。 + +> 当前为内存 TTL 存储,默认过期时间 `900s`(可用 `responses.store_ttl_seconds` 调整)。 + +### `POST /v1/embeddings` + +需要业务鉴权。返回 OpenAI Embeddings 兼容结构。 + +| 字段 | 类型 | 必填 | 说明 | +| --- | --- | --- | --- | +| `model` | string | ✅ | 支持原生模型 + alias 自动映射 | +| `input` | string/array | ✅ | 支持字符串、字符串数组、token 数组 | + +> 需配置 `embeddings.provider`。当前支持:`mock` / `deterministic` / `builtin`(三者都走同一套本地确定性实现)。未配置或不支持时返回标准错误结构(HTTP 501)。 + +### `POST /v1/files` + +需要业务鉴权。兼容 OpenAI Files 上传接口,当前仅支持 `multipart/form-data`。 + +| 字段 | 类型 | 必填 | 说明 | +| --- | --- | --- | --- | +| `file` | file | ✅ | 上传文件二进制 | +| `purpose` | string | ❌ | 透传到上游用途字段 | + +约束与行为: + +- 请求必须为 `multipart/form-data`,否则返回 `400`。 +- 请求体总大小上限 **100 MiB**(超限返回 `413`)。 +- 成功返回 OpenAI `file` 对象(`id/object/bytes/filename/purpose/status` 等字段),并附带 `account_id` 便于定位来源账号。 + +### `GET /v1/files/{file_id}` + +需要业务鉴权。查询 DeepSeek 上传文件的当前状态,并返回 OpenAI `file` 对象;未找到匹配文件时返回 `404`。 + +--- + +## Claude 兼容接口 + +除标准路径 `/anthropic/v1/*` 外,还支持快捷路径 `/v1/messages`、`/messages`、`/v1/messages/count_tokens`、`/messages/count_tokens`。 +实现上统一走 OpenAI Chat Completions 解析与回译链路,避免多套解析逻辑分叉维护。 + +### `GET /anthropic/v1/models` + +无需鉴权。 + +**响应示例**: + +```json +{ + "object": "list", + "data": [ + {"id": "claude-sonnet-4-6", "object": "model", "created": 1715635200, "owned_by": "anthropic"}, + {"id": "claude-sonnet-4-6-nothinking", "object": "model", "created": 1715635200, "owned_by": "anthropic"}, + {"id": "claude-haiku-4-5", "object": "model", "created": 1715635200, "owned_by": "anthropic"}, + {"id": "claude-haiku-4-5-nothinking", "object": "model", "created": 1715635200, "owned_by": "anthropic"}, + {"id": "claude-opus-4-6", "object": "model", "created": 1715635200, "owned_by": "anthropic"}, + {"id": "claude-opus-4-6-nothinking", "object": "model", "created": 1715635200, "owned_by": "anthropic"} + ], + "first_id": "claude-opus-4-6", + "last_id": "claude-3-haiku-20240307-nothinking", + "has_more": false +} +``` + +> 说明:示例仅展示部分模型;实际返回除当前主别名外,还包含 Claude 4.x snapshots、3.x 历史模型 ID 与常见别名,并为这些可映射模型额外提供 `-nothinking` 变体。 + +### `POST /anthropic/v1/messages` + +**请求头**: + +```http +x-api-key: your-api-key +Content-Type: application/json +anthropic-version: 2023-06-01 +``` + +> `anthropic-version` 可省略,服务端会自动补为 `2023-06-01`。 + +**请求体**: + +| 字段 | 类型 | 必填 | 说明 | +| --- | --- | --- | --- | +| `model` | string | ✅ | 例如 `claude-sonnet-4-6` / `claude-opus-4-6` / `claude-haiku-4-5`(兼容 `claude-sonnet-4-5`、`claude-3-5-haiku-latest`),并支持历史 Claude 模型 ID;若模型名带 `-nothinking` 后缀,则强制关闭 thinking / reasoning | +| `messages` | array | ✅ | Claude 风格消息数组 | +| `max_tokens` | number | ❌ | 缺省自动补 `8192`;当前实现不会硬性截断上游输出 | +| `stream` | boolean | ❌ | 默认 `false` | +| `system` | string | ❌ | 可选系统提示 | +| `tools` | array | ❌ | Claude tool 定义 | +| `thinking` | object | ❌ | Anthropic thinking 配置;会转译为下游 reasoning 控制,`-nothinking` 模型会忽略 | +| `temperature` | number | ❌ | 透传到下游;若同时提供 `top_p`,以 `temperature` 为准 | +| `top_p` | number | ❌ | 当未提供 `temperature` 时透传到下游 | +| `stop_sequences` | array | ❌ | 透传到下游停用序列 | +| `tool_choice` | string/object | ❌ | 支持 `auto` / `none` / `required` / `{"type":"function","name":"..."}`,并会转译为下游工具选择 | + +> 说明:上述 `thinking`、`temperature`、`top_p`、`stop_sequences`、`tool_choice` 都会走兼容层转译;最终是否生效仍取决于当前模型和上游能力。`temperature` 与 `top_p` 同时存在时,`temperature` 优先。 + +#### 非流式响应 + +```json +{ + "id": "msg_1738400000000000000", + "type": "message", + "role": "assistant", + "model": "claude-sonnet-4-6", + "content": [ + {"type": "text", "text": "回复内容"} + ], + "stop_reason": "end_turn", + "stop_sequence": null, + "usage": { + "input_tokens": 12, + "output_tokens": 34 + } +} +``` + +若识别到工具调用,`stop_reason=tool_use`,`content` 中返回 `tool_use` block。 + +#### 流式响应(`stream=true`) + +SSE 使用 `event:` + `data:` 双行格式,JSON 中保留 `type` 字段。 + +```text +event: message_start +data: {"type":"message_start","message":{...}} + +event: content_block_start +data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} + +event: content_block_delta +data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"hello"}} + +event: ping +data: {"type":"ping"} + +event: content_block_stop +data: {"type":"content_block_stop","index":0} + +event: message_delta +data: {"type":"message_delta","delta":{"stop_reason":"end_turn","stop_sequence":null},"usage":{"output_tokens":12}} + +event: message_stop +data: {"type":"message_stop"} +``` + +**说明**: + +- 默认支持 thinking 的模型会输出 `thinking` block / `thinking_delta`;请求显式关闭 thinking 或使用 `-nothinking` 模型时不会输出 +- 带 `-nothinking` 后缀的模型会强制关闭 thinking,即使请求显式传了 `thinking` / `reasoning` / `reasoning_effort` 也不会输出 `thinking_delta` +- 不会输出 `signature_delta`(上游 DeepSeek 未提供可验证签名) +- `tools` 场景优先避免泄露原始工具 JSON,不强制发送 `input_json_delta` + +### `POST /anthropic/v1/messages/count_tokens` + +**请求**: + +```json +{ + "model": "claude-sonnet-4-6", + "messages": [ + {"role": "user", "content": "你好"} + ] +} +``` + +**响应**: + +```json +{ + "input_tokens": 5 +} +``` + +--- + +## Gemini 兼容接口 + +支持路径: + +- `/v1beta/models/{model}:generateContent` +- `/v1beta/models/{model}:streamGenerateContent` +- `/v1/models/{model}:generateContent`(兼容路径) +- `/v1/models/{model}:streamGenerateContent`(兼容路径) + +鉴权方式同业务接口(`Authorization: Bearer ` 或 `x-api-key`)。 +实现上统一走 OpenAI Chat Completions 解析与回译链路,避免多套解析逻辑分叉维护。 + +### `POST /v1beta/models/{model}:generateContent` + +请求体兼容 Gemini `contents` / `tools` 字段,模型名可用 alias 自动映射到 DeepSeek 模型;若路径中的模型名带 `-nothinking` 后缀,则最终会映射到对应的无思考模型。 + +响应为 Gemini 兼容结构,核心字段包括: + +- `candidates[].content.parts[].text` +- `candidates[].content.parts[].thought=true`(thinking 输出) +- `candidates[].content.parts[].functionCall`(工具调用时) +- `usageMetadata`(`promptTokenCount` / `candidatesTokenCount` / `totalTokenCount`) + +### `POST /v1beta/models/{model}:streamGenerateContent` + +返回 SSE(`text/event-stream`),每个 chunk 为一条 `data: `: + +- 常规文本:持续返回增量文本 chunk +- thinking:持续返回 `parts[].thought=true` 的增量 chunk +- `tools` 场景:会缓冲并在结束时输出 `functionCall` 结构 +- 结束 chunk:包含 `finishReason: "STOP"` 与 `usageMetadata` +- token 计数优先透传上游 DeepSeek SSE(如 `accumulated_token_usage` / `token_usage`);仅在上游缺失时回退本地估算 + +--- + +## Ollama 兼容接口 + +- `POST /api/show` 请求体:`{"model":""}`。 +- 响应字段使用小写 `id`(不是 `ID`),并返回 `capabilities` 数组,便于与 Ollama 风格客户端/严格 schema 对齐。 + +示例响应: + +```json +{ + "id": "deepseek-v4-flash", + "capabilities": ["tools", "thinking"] +} +``` + +## Admin 接口 + +### `POST /admin/login` + +无需鉴权。 + +**请求**: + +```json +{ + "admin_key": "admin", + "expire_hours": 24 +} +``` + +`expire_hours` 可省略,默认 `24`。 + +**响应**: + +```json +{ + "success": true, + "token": "", + "expires_in": 86400 +} +``` + +### `GET /admin/verify` + +需要 JWT:`Authorization: Bearer ` + +**响应**: + +```json +{ + "valid": true, + "expires_at": 1738400000, + "remaining_seconds": 72000 +} +``` + +### `GET /admin/vercel/config` + +返回 Vercel 预配置状态。优先读取环境变量,其次回退到已保存的 `vercel` 配置块。 + +```json +{ + "has_token": true, + "token_preview": "vc****en", + "token_source": "config", + "project_id": "prj_xxx", + "team_id": null +} +``` + +### `GET /admin/config` + +返回脱敏后的配置,包含 `keys` 与 `api_keys`。 + +```json +{ + "keys": ["k1", "k2"], + "api_keys": [ + {"key": "k1", "name": "主 Key", "remark": "生产流量"}, + {"key": "k2", "name": "备用 Key", "remark": "压测"} + ], + "env_backed": false, + "env_source_present": true, + "env_writeback_enabled": true, + "config_path": "/data/config.json", + "vercel": { + "has_token": true, + "token_preview": "vc****en", + "project_id": "prj_xxx", + "team_id": "" + }, + "accounts": [ + { + "identifier": "user@example.com", + "email": "user@example.com", + "mobile": "", + "has_password": true, + "has_token": true, + "token_preview": "abcde..." + } + ], + "model_aliases": { + "claude-sonnet-4-6": "deepseek-v4-flash", + "claude-opus-4-6": "deepseek-v4-pro" + } +} +``` + +### `POST /admin/config` + +只更新 `keys`、`api_keys`、`accounts`、`model_aliases`。 +如果同时发送 `api_keys` 与 `keys`,优先保留 `api_keys` 中的结构化 `name` / `remark`;`keys` 仅作为旧格式兼容回退。 + +**请求**: + +```json +{ + "keys": ["k1", "k2"], + "api_keys": [ + {"key": "k1", "name": "主 Key", "remark": "生产流量"}, + {"key": "k2", "name": "备用 Key", "remark": "压测"} + ], + "accounts": [ + {"email": "user@example.com", "password": "pwd", "token": ""} + ], + "model_aliases": { + "claude-sonnet-4-6": "deepseek-v4-flash", + "claude-opus-4-6": "deepseek-v4-pro" + } +} +``` + +### `GET /admin/settings` + +读取运行时设置与状态,返回: + +- `success` +- `admin`(`has_password_hash`、`jwt_expire_hours`、`jwt_valid_after_unix`、`default_password_warning`) +- `runtime`(`account_max_inflight`、`account_max_queue`、`global_max_inflight`、`token_refresh_interval_hours`) +- `config_snapshot`:原始配置快照(`admin` / `runtime` / `responses` / `embeddings` / `auto_delete` / `current_input_file` / `thinking_injection` / `model_aliases`),用于区分环境变量覆盖前的配置 +- `responses` / `embeddings` +- `auto_delete`(`mode`:`none` / `single` / `all`;旧配置 `sessions=true` 仍按 `all` 处理) +- `current_input_file`(`enabled` 默认返回 `true`、`min_chars`) +- `thinking_injection`(`enabled` 默认返回 `true`、`prompt`、`default_prompt`) +- `model_aliases` +- `env_backed`、`needs_vercel_sync` +- `toolcall` 策略已固定为 `feature_match + high`,不再通过 settings 返回或修改 + +### `PUT /admin/settings` + +热更新运行时设置。支持更新: + +- `admin.jwt_expire_hours` +- `runtime.account_max_inflight` / `runtime.account_max_queue` / `runtime.global_max_inflight` / `runtime.token_refresh_interval_hours` +- `responses.store_ttl_seconds` +- `embeddings.provider` +- `auto_delete.mode` +- `current_input_file.enabled` / `current_input_file.min_chars` +- `thinking_injection.enabled` / `thinking_injection.prompt` +- `model_aliases` +- `toolcall` 策略已固定,不再作为可写入字段 + +### `POST /admin/settings/password` + +更新管理密码并使旧 JWT 失效。 + +请求示例: + +```json +{"new_password":"your-new-password"} +``` + +也兼容 `{"password":"your-new-password"}`。 + +### `POST /admin/config/import` + +导入完整配置,支持: + +- `mode=merge`(默认) +- `mode=replace` + +请求可直接传配置对象,或使用 `{"config": {...}, "mode":"merge"}` 包裹格式。 +也支持在查询参数里传 `?mode=merge` / `?mode=replace`。 +`replace` 模式会按完整配置结构替换(保留 Vercel 同步元信息);`merge` 模式会合并 `keys`、`api_keys`、`accounts`、`model_aliases`,并覆盖 `admin`、`runtime`、`responses`、`embeddings` 中的非空字段。`auto_delete`、`current_input_file` 建议通过 `/admin/settings` 或配置文件管理;`compat` 与 `toolcall` 相关字段会被忽略。 + +> 注意:`merge` 模式不会更新 `auto_delete`、`current_input_file`。 + +### `GET /admin/config/export` + +导出完整配置,返回 `config`、`json`、`base64` 三种格式。 + +响应示例: + + +> 注:`_vercel_sync_hash` 和 `_vercel_sync_time` 为内部同步元数据字段,用于 Vercel 配置漂移检测。 + +### `POST /admin/keys` + +```json +{"key": "new-api-key", "name": "主 Key", "remark": "生产流量"} +``` + +**响应**:`{"success": true, "total_keys": 3}` + +### `PUT /admin/keys/{key}` + +更新指定 API key 的 `name` / `remark`,路径参数中的 `key` 为只读标识,不可修改。 + +```json +{"name": "备用 Key", "remark": "压测"} +``` + +**响应**:`{"success": true, "total_keys": 3}` + +### `DELETE /admin/keys/{key}` + +**响应**:`{"success": true, "total_keys": 2}` + +### `GET /admin/proxies` + +列出代理配置(密码不回传,仅返回 `has_password` 标记)。 + +### `POST /admin/proxies` + +新增代理。请求体支持 `id`(可选,未传则自动生成)、`name`、`type`(`http` / `socks5`)、`host`、`port`、`username`、`password`。 + +### `PUT /admin/proxies/{proxyID}` + +更新指定代理。若请求中 `password` 为空字符串,则保留原密码。 + +### `DELETE /admin/proxies/{proxyID}` + +删除代理,并自动清空所有引用该代理账号的 `proxy_id`。 + +### `POST /admin/proxies/test` + +测试代理连通性:传 `proxy_id` 时测试已保存代理;不传时按请求体代理字段做临时连通性测试。 + +### `GET /admin/accounts` + +**查询参数**: + +| 参数 | 默认 | 范围 | +| --- | --- | --- | +| `page` | `1` | ≥ 1 | +| `page_size` | `10` | 1–5000 | +| `q` | 空 | 按 identifier / email / mobile 过滤 | + +**响应**: + +```json +{ + "items": [ + { + "identifier": "user@example.com", + "email": "user@example.com", + "mobile": "", + "has_password": true, + "has_token": true, + "token_preview": "abc...", + "test_status": "ok" + } + ], + "total": 25, + "page": 1, + "page_size": 10, + "total_pages": 3 +} +``` + +### `POST /admin/accounts` + +```json +{"email": "user@example.com", "password": "pwd"} +``` + +**响应**:`{"success": true, "total_accounts": 6}` + +### `PUT /admin/accounts/{identifier}` + +更新指定账号的 `name` / `remark`。路径参数中的 `identifier` 可以是 email 或 mobile,且不可修改。 + +```json +{"name": "主账号", "remark": "团队共享"} +``` + +**响应**:`{"success": true, "total_accounts": 6}` + +### `DELETE /admin/accounts/{identifier}` + +`identifier` 可为 email、mobile,或 token-only 账号的合成标识(`token:`)。 + +**响应**:`{"success": true, "total_accounts": 5}` + +### `PUT /admin/accounts/{identifier}/proxy` + +更新指定账号绑定代理。 + +- 请求体:`{"proxy_id":"..."}`; +- `proxy_id` 传空字符串时表示解绑代理; +- `identifier` 支持 email / mobile / token-only 合成标识。 + +### `GET /admin/queue/status` + +```json +{ + "available": 3, + "in_use": 1, + "total": 4, + "available_accounts": ["a@example.com"], + "in_use_accounts": ["b@example.com"], + "max_inflight_per_account": 2, + "global_max_inflight": 8, + "recommended_concurrency": 8, + "waiting": 0, + "max_queue_size": 8 +} +``` + +| 字段 | 说明 | +| --- | --- | +| `available` | 仍有剩余并发槽位的账号数 | +| `in_use` | 当前已占用的 in-flight 槽位数 | +| `total` | 总账号数 | +| `available_accounts` | 仍有剩余并发槽位的账号 ID 列表 | +| `in_use_accounts` | 当前处于使用中的账号 ID 列表 | +| `max_inflight_per_account` | 每账号并发上限 | +| `global_max_inflight` | 全局并发上限 | +| `recommended_concurrency` | 建议并发值(`total × max_inflight_per_account`) | +| `waiting` | 当前等待中的请求数 | +| `max_queue_size` | 等待队列上限 | + +### `POST /admin/accounts/test` + +| 字段 | 必填 | 说明 | +| --- | --- | --- | +| `identifier` | ✅ | email / mobile / token-only 合成标识 | +| `model` | ❌ | 默认 `deepseek-v4-flash` | +| `message` | ❌ | 空字符串时仅测试会话创建 | + +**响应**: + +```json +{ + "account": "user@example.com", + "success": true, + "response_time": 1240, + "message": "API 测试成功(仅会话创建)", + "model": "deepseek-v4-flash", + "session_count": 0, + "config_writable": true, + "config_warning": "" +} +``` + +如果传入 `message`,还会附带 `thinking`(当上游返回思考内容时)。 + +当部署环境配置文件路径不可写(例如容器内默认 `/app/config.json` 只读)时,登录与会话测试仍可继续;此时会返回 `config_warning` 提示 token 仅保存在内存、重启后丢失。 + +### `POST /admin/accounts/test-all` + +可选请求字段:`model` + +```json +{ + "total": 5, + "success": 4, + "failed": 1, + "results": [...] +} +``` + +内部并发上限当前固定为 5。 + +### `POST /admin/accounts/sessions/delete-all` + +清空指定账号的所有 DeepSeek 会话。请求体示例: + +```json +{"identifier":"user@example.com"} +``` + +响应: + +```json +{"success": true, "message": "删除成功"} +``` + +如果账号不存在或删除失败,`success` 会是 `false`,`message` 会返回错误原因。 + +### `POST /admin/import` + +批量导入 keys 与 accounts。 + +**请求**: + +```json +{ + "keys": ["k1", "k2"], + "accounts": [ + {"email": "user@example.com", "password": "pwd", "token": ""} + ] +} +``` + +**响应**: + +```json +{ + "success": true, + "imported_keys": 2, + "imported_accounts": 1 +} +``` + +### `POST /admin/test` + +测试当前 API 可用性(通过自身接口调用)。 + +| 字段 | 必填 | 默认值 | +| --- | --- | --- | +| `model` | ❌ | `deepseek-v4-flash` | +| `message` | ❌ | `你好` | +| `api_key` | ❌ | 配置中第一个 key | + +**响应**: + +```json +{ + "success": true, + "status_code": 200, + "response": {"id": "..."} +} +``` + +### `POST /admin/dev/raw-samples/capture` + +直接通过服务自身发起一次 `/v1/chat/completions` 请求,并把请求元信息和上游原始 SSE 保存到 `tests/raw_stream_samples//`。 + +常用请求字段: + +| 字段 | 必填 | 默认值 | 说明 | +| --- | --- | --- | --- | +| `message` | 否 | `你好` | 便捷单轮用户消息 | +| `messages` | 否 | 自动由 `message` 生成 | OpenAI 风格消息数组 | +| `model` | 否 | `deepseek-v4-flash` | 目标模型 | +| `stream` | 否 | `true` | 建议保留流式,以记录原始 SSE | +| `api_key` | 否 | 配置中第一个 key | 调用业务接口使用的 key | +| `sample_id` | 否 | 自动生成 | 样本目录名 | + +成功时会在响应头里附带: + +- `X-Ds2-Sample-Id` +- `X-Ds2-Sample-Dir` +- `X-Ds2-Sample-Meta` +- `X-Ds2-Sample-Upstream` + +如果请求本身成功,但当前进程没有记录到新的上游抓包,会返回: + +```json +{"detail":"no upstream capture was recorded"} +``` + +### `GET /admin/dev/raw-samples/query` + +按关键词查询当前进程内存里的抓包记录,并按 `chat_session_id` 归并 `completion + continue` 链。 + +**查询参数**: + +| 参数 | 默认值 | 说明 | +| --- | --- | --- | +| `q` | 空 | 按请求体/响应体关键词模糊匹配 | +| `limit` | `20` | 返回链条数上限 | + +**响应字段**包含: + +- `items[].chain_key` +- `items[].capture_ids` +- `items[].round_count` +- `items[].initial_label` +- `items[].request_preview` +- `items[].response_preview` + +### `POST /admin/dev/raw-samples/save` + +把当前内存中的某条抓包链落盘为 `tests/raw_stream_samples//`。 + +支持以下任一种选中方式: + +```json +{"chain_key":"session:xxxx","sample_id":"tmp-from-memory"} +``` + +```json +{"capture_id":"cap_xxx","sample_id":"tmp-from-memory"} +``` + +```json +{"query":"广州天气","sample_id":"tmp-from-memory"} +``` + +成功响应会返回 `sample_id`、`dir`、`meta_path`、`upstream_path`。 + +### `POST /admin/vercel/sync` + +| 字段 | 必填 | 说明 | +| --- | --- | --- | +| `vercel_token` | ❌ | 空或 `__USE_PRECONFIG__` 则读环境变量,再回退到已保存配置 | +| `project_id` | ❌ | 空则读 `VERCEL_PROJECT_ID`,再回退到已保存配置 | +| `team_id` | ❌ | 空则读 `VERCEL_TEAM_ID`,再回退到已保存配置 | +| `auto_validate` | ❌ | 默认 `true` | +| `save_credentials` | ❌ | 默认 `true`;保存本次显式填写的 Vercel 凭据,供下次同步复用 | +| `config_override` | ❌ | 传入完整配置对象,覆盖当前内存配置用于同步 | + +**成功响应**: + +```json +{ + "success": true, + "validated_accounts": 3, + "message": "配置已同步,正在重新部署...", + "deployment_url": "https://..." +} +``` + +或需要手动部署: + +```json +{ + "success": true, + "validated_accounts": 3, + "message": "配置已同步到 Vercel,请手动触发重新部署", + "manual_deploy_required": true +} +``` + +失败校验的账号会通过 `failed_accounts` 返回;成功保存到 Vercel 的凭据会通过 `saved_credentials` 返回。 + +### `GET /admin/vercel/status` + +也支持 `POST /admin/vercel/status`,可提交 `config_override` 用于比对草稿是否与已同步配置一致。 + +```json +{ + "synced": true, + "last_sync_time": 1738400000, + "has_synced_before": true, + "env_backed": false, + "config_hash": "....", + "last_synced_hash": "....", + "draft_hash": "....", + "draft_differs": false +} +``` + +`POST /admin/vercel/status` 还可以携带 `config_override`,用于对比“草稿配置”和当前已同步配置。 + +### `GET /admin/export` + +```json +{ + "json": "{...}", + "base64": "ey4uLn0=" +} +``` + +该接口与 `GET /admin/config/export` 返回相同内容,只是路径更短。 + +### `GET /admin/version` + +查询当前构建版本与 GitHub 最新 Release: + +```json +{ + "success": true, + "current_version": "3.0.0", + "current_tag": "v3.0.0", + "source": "file:VERSION", + "checked_at": "2026-03-29T00:00:00Z", + "latest_tag": "v3.0.0", + "latest_version": "3.0.0", + "release_url": "https://github.com/CJackHwang/ds2api/releases/tag/v3.0.0", + "published_at": "2026-03-28T12:00:00Z", + "has_update": false +} +``` + +如果 GitHub API 不可用,响应里会额外包含 `check_error`,但 HTTP 状态仍为 200。 + +### `GET /admin/dev/captures` + +查看本地抓包状态与最近记录(需 Admin 鉴权): + +- `enabled` +- `limit` +- `max_body_bytes` +- `items` + +### `DELETE /admin/dev/captures` + +清空抓包记录,返回: + +```json +{"success":true,"detail":"capture logs cleared"} +``` + +--- + +## 错误响应格式 + +兼容路由(`/v1/*`、`/anthropic/*`)统一使用以下结构: + +```json +{ + "error": { + "message": "...", + "type": "invalid_request_error", + "code": "invalid_request", + "param": null + } +} +``` + +Admin 接口保持 `{"detail":"..."}`。 + +Gemini 路由使用 Google 风格错误结构: + +```json +{ + "error": { + "code": 400, + "message": "invalid json", + "status": "INVALID_ARGUMENT" + } +} +``` + +建议客户端处理逻辑:检查 HTTP 状态码 + 解析 `error` 或 `detail` 字段。 + +**常见状态码**: + +| 状态码 | 说明 | +| --- | --- | +| `401` | 鉴权失败(key/token 无效,或 Admin JWT 过期) | +| `429` | 请求过多(超出并发上限 + 等待队列,或上游账号 thinking-only 后仍无可见输出;托管账号模式会先尝试一次切号 fresh retry;当前不附带 `Retry-After` 头) | +| `503` | 模型不可用或上游服务异常 | + +--- + +## cURL 示例 + +### OpenAI 非流式 + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash", + "messages": [{"role": "user", "content": "你好"}], + "stream": false + }' +``` + +### OpenAI 流式 + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-pro", + "messages": [{"role": "user", "content": "解释一下量子纠缠"}], + "stream": true + }' +``` + +### OpenAI Responses(流式) + +```bash +curl http://localhost:5001/v1/responses \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "gpt-5.3-codex", + "input": "写一个 golang 的 hello world", + "stream": true + }' +``` + +### OpenAI Embeddings + +```bash +curl http://localhost:5001/v1/embeddings \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "gpt-4o", + "input": ["第一段文本", "第二段文本"] + }' +``` + +### OpenAI 带搜索 + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash-search", + "messages": [{"role": "user", "content": "今天的新闻"}], + "stream": true + }' +``` + +### OpenAI Tool Calling + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash", + "messages": [{"role": "user", "content": "北京今天天气怎么样?"}], + "tools": [ + { + "type": "function", + "function": { + "name": "get_weather", + "description": "获取指定城市的天气", + "parameters": { + "type": "object", + "properties": { + "city": {"type": "string", "description": "城市名"} + }, + "required": ["city"] + } + } + } + ] + }' +``` + +### Gemini 非流式 + +```bash +curl "http://localhost:5001/v1beta/models/gemini-2.5-pro:generateContent" \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "contents": [ + { + "role": "user", + "parts": [{"text": "用三句话介绍 Go 语言"}] + } + ] + }' +``` + +### Gemini 流式 + +```bash +curl "http://localhost:5001/v1beta/models/gemini-2.5-flash:streamGenerateContent" \ + -H "Authorization: Bearer your-api-key" \ + -H "Content-Type: application/json" \ + -d '{ + "contents": [ + { + "role": "user", + "parts": [{"text": "写一个简短摘要"}] + } + ] + }' +``` + +### Claude 非流式 + +```bash +curl http://localhost:5001/anthropic/v1/messages \ + -H "x-api-key: your-api-key" \ + -H "Content-Type: application/json" \ + -H "anthropic-version: 2023-06-01" \ + -d '{ + "model": "claude-sonnet-4-6", + "max_tokens": 1024, + "messages": [{"role": "user", "content": "你好"}] + }' +``` + +### Claude 流式 + +```bash +curl http://localhost:5001/anthropic/v1/messages \ + -H "x-api-key: your-api-key" \ + -H "Content-Type: application/json" \ + -H "anthropic-version: 2023-06-01" \ + -d '{ + "model": "claude-opus-4-6", + "max_tokens": 1024, + "messages": [{"role": "user", "content": "解释相对论"}], + "stream": true + }' +``` + +### Admin 登录 + +```bash +curl http://localhost:5001/admin/login \ + -H "Content-Type: application/json" \ + -d '{"admin_key": "admin"}' +``` + +### 指定账号请求 + +```bash +curl http://localhost:5001/v1/chat/completions \ + -H "Authorization: Bearer your-api-key" \ + -H "X-Ds2-Target-Account: user@example.com" \ + -H "Content-Type: application/json" \ + -d '{ + "model": "deepseek-v4-flash", + "messages": [{"role": "user", "content": "你好"}] + }' +``` diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000000000000000000000000000000000000..eb481f74aa21d90939c9a970a5b81185d74fdc72 --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,128 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +We as members, contributors, and leaders pledge to make participation in our +community a harassment-free experience for everyone, regardless of age, body +size, visible or invisible disability, ethnicity, sex characteristics, gender +identity and expression, level of experience, education, socio-economic status, +nationality, personal appearance, race, religion, or sexual identity +and orientation. + +We pledge to act and interact in ways that contribute to an open, welcoming, +diverse, inclusive, and healthy community. + +## Our Standards + +Examples of behavior that contributes to a positive environment for our +community include: + +* Demonstrating empathy and kindness toward other people +* Being respectful of differing opinions, viewpoints, and experiences +* Giving and gracefully accepting constructive feedback +* Accepting responsibility and apologizing to those affected by our mistakes, + and learning from the experience +* Focusing on what is best not just for us as individuals, but for the + overall community + +Examples of unacceptable behavior include: + +* The use of sexualized language or imagery, and sexual attention or + advances of any kind +* Trolling, insulting or derogatory comments, and personal or political attacks +* Public or private harassment +* Publishing others' private information, such as a physical or email + address, without their explicit permission +* Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Enforcement Responsibilities + +Community leaders are responsible for clarifying and enforcing our standards of +acceptable behavior and will take appropriate and fair corrective action in +response to any behavior that they deem inappropriate, threatening, offensive, +or harmful. + +Community leaders have the right and responsibility to remove, edit, or reject +comments, commits, code, wiki edits, issues, and other contributions that are +not aligned to this Code of Conduct, and will communicate reasons for moderation +decisions when appropriate. + +## Scope + +This Code of Conduct applies within all community spaces, and also applies when +an individual is officially representing the community in public spaces. +Examples of representing our community include using an official e-mail address, +posting via an official social media account, or acting as an appointed +representative at an online or offline event. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported to the community leaders responsible for enforcement at +cjackhwang@qq.com. +All complaints will be reviewed and investigated promptly and fairly. + +All community leaders are obligated to respect the privacy and security of the +reporter of any incident. + +## Enforcement Guidelines + +Community leaders will follow these Community Impact Guidelines in determining +the consequences for any action they deem in violation of this Code of Conduct: + +### 1. Correction + +**Community Impact**: Use of inappropriate language or other behavior deemed +unprofessional or unwelcome in the community. + +**Consequence**: A private, written warning from community leaders, providing +clarity around the nature of the violation and an explanation of why the +behavior was inappropriate. A public apology may be requested. + +### 2. Warning + +**Community Impact**: A violation through a single incident or series +of actions. + +**Consequence**: A warning with consequences for continued behavior. No +interaction with the people involved, including unsolicited interaction with +those enforcing the Code of Conduct, for a specified period of time. This +includes avoiding interactions in community spaces as well as external channels +like social media. Violating these terms may lead to a temporary or +permanent ban. + +### 3. Temporary Ban + +**Community Impact**: A serious violation of community standards, including +sustained inappropriate behavior. + +**Consequence**: A temporary ban from any sort of interaction or public +communication with the community for a specified period of time. No public or +private interaction with the people involved, including unsolicited interaction +with those enforcing the Code of Conduct, is allowed during this period. +Violating these terms may lead to a permanent ban. + +### 4. Permanent Ban + +**Community Impact**: Demonstrating a pattern of violation of community +standards, including sustained inappropriate behavior, harassment of an +individual, or aggression toward or disparagement of classes of individuals. + +**Consequence**: A permanent ban from any sort of public interaction within +the community. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], +version 2.0, available at +https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. + +Community Impact Guidelines were inspired by [Mozilla's code of conduct +enforcement ladder](https://github.com/mozilla/diversity). + +[homepage]: https://www.contributor-covenant.org + +For answers to common questions about this code of conduct, see the FAQ at +https://www.contributor-covenant.org/faq. Translations are available at +https://www.contributor-covenant.org/translations. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000000000000000000000000000000000000..7ace07abb51101c569e50d655a7912797801e013 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,73 @@ +FROM node:24 AS webui-builder + +WORKDIR /app/webui +COPY webui/package.json webui/package-lock.json ./ +RUN npm ci +COPY config.example.json /app/config.example.json +COPY webui ./ +RUN npm run build + +FROM golang:1.26 AS go-builder +WORKDIR /app +ARG TARGETOS +ARG TARGETARCH +ARG BUILD_VERSION +COPY go.mod go.sum* ./ +RUN go mod download +COPY . . +RUN set -eux; \ + GOOS="${TARGETOS:-$(go env GOOS)}"; \ + GOARCH="${TARGETARCH:-$(go env GOARCH)}"; \ + BUILD_VERSION_RESOLVED="${BUILD_VERSION:-}"; \ + if [ -z "${BUILD_VERSION_RESOLVED}" ] && [ -f VERSION ]; then BUILD_VERSION_RESOLVED="$(cat VERSION | tr -d "[:space:]")"; fi; \ + CGO_ENABLED=0 GOOS="${GOOS}" GOARCH="${GOARCH}" go build -buildvcs=false -ldflags="-s -w -X ds2api/internal/version.BuildVersion=${BUILD_VERSION_RESOLVED}" -o /out/ds2api ./cmd/ds2api + +FROM busybox:1.36.1-musl AS busybox-tools + +FROM debian:bookworm-slim AS runtime-base +WORKDIR /app +RUN apt-get update \ + && apt-get install -y --no-install-recommends ca-certificates gosu \ + && groupadd -r ds2api && useradd -r -g ds2api -d /app -s /sbin/nologin ds2api \ + && mkdir -p /app/data /data && chown -R ds2api:ds2api /app /data \ + && rm -rf /var/lib/apt/lists/* +COPY --from=busybox-tools /bin/busybox /usr/local/bin/busybox +EXPOSE 7860 +ENV PORT=7860 +ENV DS2API_CONFIG_PATH=/data/config.json +ENV DS2API_CHAT_HISTORY_PATH=/data/chat_history.json +COPY entrypoint.sh /usr/local/bin/entrypoint.sh +RUN chmod +x /usr/local/bin/entrypoint.sh +CMD ["/usr/local/bin/entrypoint.sh"] + +FROM runtime-base AS runtime-from-source +COPY --from=go-builder /out/ds2api /usr/local/bin/ds2api + +COPY --from=go-builder --chown=ds2api:ds2api /app/config.example.json /app/config.example.json +COPY --from=webui-builder --chown=ds2api:ds2api /app/static/admin /app/static/admin + +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}/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 --chown=ds2api:ds2api /out/config.example.json /app/config.example.json +COPY --from=dist-extract --chown=ds2api:ds2api /out/static/admin /app/static/admin + +FROM runtime-from-source AS final diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..0ad25db4bd1d86c452db3f9602ccdbe172438f52 --- /dev/null +++ b/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000000000000000000000000000000000000..a6b3950b706e9b28251d5bfb37b3b7ee31a1ac15 --- /dev/null +++ b/README.en.md @@ -0,0 +1,441 @@ +

+ DS2API icon +

+ +# DS2API + +CJackHwang%2Fds2api | Trendshift + +[![License](https://img.shields.io/github/license/CJackHwang/ds2api.svg)](LICENSE) +![Stars](https://img.shields.io/github/stars/CJackHwang/ds2api.svg) +![Forks](https://img.shields.io/github/forks/CJackHwang/ds2api.svg) +[![Release](https://img.shields.io/github/v/release/CJackHwang/ds2api?display_name=tag)](https://github.com/CJackHwang/ds2api/releases) +[![Docker](https://img.shields.io/badge/docker-ready-blue.svg)](docs/DEPLOY.en.md) +[![Deploy on Zeabur](https://zeabur.com/button.svg)](https://zeabur.com/templates/L4CFHP) +[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https://github.com/CJackHwang/ds2api) + +Language: [中文](README.MD) | [English](README.en.md) + +DS2API converts DeepSeek Web chat capability into OpenAI-compatible, Claude-compatible, and Gemini-compatible APIs. The core backend is Go-based, with a small Node Runtime bridge used for Vercel streaming, and the React WebUI admin panel lives in `webui/` (build output auto-generated to `static/admin` during deployment). + +Documentation entry: [Docs Index](docs/README.md) / [Architecture](docs/ARCHITECTURE.en.md) / [API Reference](API.en.md) + +## Star History + + + + + + Star History Chart + + + +> **Important Disclaimer** +> +> This repository is provided for learning, research, personal experimentation, and internal validation only. It does not grant any commercial authorization and comes with no warranty of fitness, stability, or results. +> +> The author and repository maintainers are not responsible for any direct or indirect loss, account suspension, data loss, legal risk, or third-party claims arising from use, modification, distribution, deployment, or reliance on this project. +> +> Do not use this project in ways that violate service terms, agreements, laws, or platform rules. Before any commercial use, review the `LICENSE`, the relevant terms, and confirm that you have the author's written permission. + +## Table of Contents + +- [Architecture Overview (Summary)](#architecture-overview-summary) +- [Key Capabilities](#key-capabilities) +- [Platform Compatibility Matrix](#platform-compatibility-matrix) +- [Model Support](#model-support) + - [OpenAI Endpoint](#openai-endpoint-get-v1models) + - [Claude Endpoint](#claude-endpoint-get-anthropicv1models) + - [Gemini Endpoint](#gemini-endpoint) +- [Quick Start](#quick-start) + - [Option 1: Download Release Binaries](#option-1-download-release-binaries) + - [Option 2: Docker / GHCR](#option-2-docker--ghcr) + - [Option 3: Vercel](#option-3-vercel) + - [Option 4: Local Run](#option-4-local-run) +- [Configuration](#configuration) +- [Authentication Modes](#authentication-modes) +- [Concurrency Model](#concurrency-model) +- [Tool Call Adaptation](#tool-call-adaptation) +- [Local Dev Packet Capture](#local-dev-packet-capture) +- [Documentation Index](#documentation-index) +- [Testing](#testing) +- [Release Artifact Automation (GitHub Actions)](#release-artifact-automation-github-actions) +- [Disclaimer](#disclaimer) + +## Architecture Overview (Summary) + +```mermaid +flowchart LR + Client["🖥️ Clients / SDKs\n(OpenAI / Claude / Gemini)"] + Upstream["☁️ DeepSeek API"] + + subgraph DS2API["DS2API 4.x (Modular HTTP Surface + PromptCompat Core)"] + Router["chi Router + Middleware\n(RequestID / RealIP / Logger / Recoverer / CORS)"] + + subgraph HTTP["HTTP API Surface"] + OA["OpenAI\nchat / responses / files / embeddings"] + CA["Claude\n/anthropic/* + /v1/messages"] + GA["Gemini\n/v1beta/models/* + /v1/models/*"] + Admin["Admin API\nresource packages"] + WebUI["WebUI\n/admin (static hosting)"] + Vercel["Vercel Node Stream\n/v1/chat/completions"] + end + + subgraph Runtime["Runtime + Core Capabilities"] + Compat["PromptCompat\n(API -> web-chat plain text context)"] + Completion["Completion Runtime\n(session / PoW / completion)"] + Turn["AssistantTurn\n(output semantic normalization)"] + Auth["Auth Resolver\n(API key / bearer / x-goog-api-key)"] + Pool["Account Pool + Queue\n(in-flight slots + wait queue)"] + DSClient["DeepSeek Client\n(session / auth / completion / files)"] + Pow["PoW Solver\n(Pure Go)"] + Tool["Tool Sieve\n(Go/Node semantic parity)"] + History["Current Input File\n(random context file)"] + end + end + + Client --> Router + Router --> OA & CA & GA + Router --> Admin + Router --> WebUI + Router --> Vercel + + OA --> Compat + CA & GA --> Compat + Compat --> Completion + Completion -.full context.-> History + Completion --> Turn + Vercel -.Go prepare.-> Completion + Vercel -.Node SSE.-> Tool + Completion --> Auth + Completion -.account rotation.-> Pool + Completion -.tool-call parsing.-> Tool + Completion -.PoW solving.-> Pow + Auth --> DSClient + DSClient --> Upstream + Upstream --> DSClient + Turn --> Client + Vercel --> Client +``` + +For the full module-by-module architecture and directory responsibilities, see [docs/ARCHITECTURE.en.md](docs/ARCHITECTURE.en.md). + +- **Backend**: Go (`cmd/ds2api/`, `api/`, `internal/`), no Python runtime +- **Frontend**: React admin panel (`webui/`), served as static build at runtime +- **Deployment**: local run, Docker, Vercel serverless, Linux systemd + +## Key Capabilities + +| Capability | Details | +| --- | --- | +| OpenAI compatible | `GET /v1/models`, `GET /v1/models/{id}`, `POST /v1/chat/completions`, `POST /v1/responses`, `GET /v1/responses/{response_id}`, `POST /v1/embeddings`, `POST /v1/files`, `GET /v1/files/{file_id}` | +| Claude compatible | `GET /anthropic/v1/models`, `POST /anthropic/v1/messages`, `POST /anthropic/v1/messages/count_tokens` (plus shortcut paths `/v1/messages`, `/messages`) | +| Gemini compatible | `POST /v1beta/models/{model}:generateContent`, `POST /v1beta/models/{model}:streamGenerateContent` (plus `/v1/models/{model}:*` paths) | +| Ollama compatible | `GET /api/version`, `GET /api/tags`, `POST /api/show` | +| Unified CORS compatibility | `/v1/*`, `/anthropic/*`, `/v1beta/models/*`, `/api/*`, and `/admin/*` share one CORS policy; on Vercel, the Node Runtime for `/v1/chat/completions` mirrors the same relaxed preflight behavior for third-party clients | +| Multi-account rotation | Auto token refresh, email/mobile dual login | +| Concurrency control | Per-account in-flight limit + waiting queue, dynamic recommended concurrency | +| DeepSeek PoW | Pure Go high-performance solver (DeepSeekHashV1), ms-level response | +| Tool Calling | Anti-leak handling: non-code-block feature match, early `delta.tool_calls`, structured incremental output | +| Admin API | Config management, runtime settings hot-reload, proxy management, account testing/batch test, session cleanup, import/export, Vercel sync, version check | +| WebUI Admin Panel | SPA at `/admin` (bilingual Chinese/English, dark mode, with server-side conversation history) | +| Health Probes | `GET /healthz` (liveness), `GET /readyz` (readiness) | + +OpenAI `/v1/*` routes remain canonical, and DS2API also accepts root shortcuts such as `/models`, `/chat/completions`, `/responses`, `/embeddings`, `/files`, and `/files/{file_id}` for clients configured with the bare service URL. + +## Platform Compatibility Matrix + +| Tier | Platform | Status | +| --- | --- | --- | +| P0 | Codex CLI/SDK (`wire_api=chat` / `wire_api=responses`) | ✅ | +| P0 | OpenAI SDK (JS/Python, chat + responses) | ✅ | +| P0 | Vercel AI SDK (openai-compatible) | ✅ | +| P0 | Anthropic SDK (messages) | ✅ | +| P0 | Google Gemini SDK (generateContent) | ✅ | +| P1 | LangChain / LlamaIndex / OpenWebUI (OpenAI-compatible integration) | ✅ | + +## Model Support + +### OpenAI Endpoint (`GET /v1/models`) + +| Family | Model ID | thinking | search | +| --- | --- | --- | --- | +| default | `deepseek-v4-flash` | enabled by default, request-controlled | ❌ | +| expert | `deepseek-v4-pro` | enabled by default, request-controlled | ❌ | +| default | `deepseek-v4-flash-search` | enabled by default, request-controlled | ✅ | +| expert | `deepseek-v4-pro-search` | enabled by default, request-controlled | ✅ | +| vision | `deepseek-v4-vision` | enabled by default, request-controlled | ❌ | + +Besides native IDs, DS2API also accepts common aliases as input (for example `gpt-4.1`, `gpt-5`, `gpt-5-codex`, `o3`, `claude-*`, `gemini-*`), but `/v1/models` returns normalized DeepSeek native model IDs. The complete alias behavior is documented in [API.en.md](API.en.md#model-alias-resolution) and `config.example.json`. +Current upstream vision support exposes only the `vision` lane and does not provide a separate search-enabled vision variant. + +### Claude Endpoint (`GET /anthropic/v1/models`) + +| Current common model | Default Mapping | +| --- | --- | +| `claude-sonnet-4-6` | `deepseek-v4-flash` | +| `claude-haiku-4-5` (compatible with `claude-3-5-haiku-latest`) | `deepseek-v4-flash` | +| `claude-opus-4-6` | `deepseek-v4-pro` | + +Override mapping via the global `model_aliases` config. +Besides the primary aliases above, `/anthropic/v1/models` also returns Claude 4.x snapshots plus historical 3.x IDs and common aliases for legacy client compatibility. + +#### Claude Code integration pitfalls (validated) + +- Set `ANTHROPIC_BASE_URL` to the DS2API root URL (for example `http://127.0.0.1:5001`). Claude Code sends requests to `/v1/messages?beta=true`. +- `ANTHROPIC_API_KEY` must match an entry in `keys` from `config.json`. Keeping both a regular key and an `sk-ant-*` style key improves client compatibility. +- If your environment has proxy variables, set `NO_PROXY=127.0.0.1,localhost,` for DS2API to avoid proxy interception of local traffic. +- If tool calls are rendered as plain text and not executed, first verify the model output uses the recommended halfwidth-pipe DSML block: `<|DSML|tool_calls><|DSML|invoke name="..."><|DSML|parameter name="...">...`. DS2API also accepts legacy canonical XML: `...`; legacy `` / `` / `` / ``, ``, `tool_use`, or standalone JSON `tool_calls` are not executed and stay plain text. + +### Gemini Endpoint + +The Gemini adapter maps model names to DeepSeek native models via `model_aliases` or exact built-in aliases (covering common `gemini-2.5-*`, `gemini-3*`, and `gemini-pro-vision` names), supporting both `generateContent` and `streamGenerateContent` call patterns with full Tool Calling support (`functionDeclarations` → `functionCall` output). If the Gemini model name has a `-nothinking` suffix, such as `gemini-2.5-pro-nothinking`, it maps to the corresponding forced no-thinking model. + +## Quick Start + +### Recommended deployment priority + +Recommended order when choosing a deployment method: + +1. **Download and run release binaries**: the easiest path for most users because the artifacts are already built. +2. **Docker / GHCR image deployment**: suitable for containerized, orchestrated, or cloud environments. +3. **Vercel deployment**: suitable if you already use Vercel and accept its platform constraints. +4. **Run from source / build locally**: suitable for development, debugging, or when you need to modify the code yourself. + +### Universal First Step (all deployment modes) + +Use `config.json` as the single source of truth (recommended): + +```bash +cp config.example.json config.json +# Edit config.json +``` + +Recommended per deployment mode: +- Local run: read `config.json` directly +- Docker / Vercel: generate Base64 from `config.json` and inject as `DS2API_CONFIG_JSON`, or paste raw JSON directly + +The WebUI admin panel’s “Full configuration template” is loaded from the same `config.example.json`, so updating that file keeps the frontend template in sync. + +### Option 1: Download Release Binaries + +GitHub Actions automatically builds multi-platform archives on each Release: + +```bash +# After downloading the archive for your platform +tar -xzf ds2api__linux_amd64.tar.gz +cd ds2api__linux_amd64 +cp config.example.json config.json +# Edit config.json +./ds2api +``` + +### Option 2: Docker / GHCR + +```bash +# Pull prebuilt image +docker pull ghcr.io/cjackhwang/ds2api:latest + +# Or run a pinned version +# docker pull ghcr.io/cjackhwang/ds2api:v3.0.0 + +# Prepare env file and config file +cp .env.example .env +cp config.example.json config.json + +# Start with compose +docker-compose up -d +``` + +The default `docker-compose.yml` uses `ghcr.io/cjackhwang/ds2api:latest` and maps host port `6011` to container port `5001`. If you want `5001` exposed directly, set `DS2API_HOST_PORT=5001` (or adjust the `ports` mapping). +It also mounts `./config.json` to `/data/config.json` and sets `DS2API_CONFIG_PATH=/data/config.json` by default, which avoids runtime token persistence failures caused by read-only `/app`. + +Rebuild after updates: `docker-compose up -d --build` + +#### Zeabur One-Click (Dockerfile) + +1. Click the “Deploy on Zeabur” button above to deploy. +2. After deployment, open `/admin` and login with `DS2API_ADMIN_KEY` shown in Zeabur env/template instructions. +3. Import / edit config in Admin UI (it will be written and persisted to `/data/config.json`). + +Fresh Zeabur volumes can start without `/data/config.json`; DS2API will boot with an empty file-backed config and create the file on the first Admin UI save. + +For manual deployment without the template, create a Zeabur GitHub service, keep Root Directory as `/`, build with the repo-root `Dockerfile`, mount a persistent volume at `/data`, set `PORT=5001`, `DS2API_ADMIN_KEY=your-strong-secret`, and `DS2API_CONFIG_PATH=/data/config.json`, then expose HTTP port `5001`. See [docs/DEPLOY.en.md](docs/DEPLOY.en.md#manual-deployment-without-the-template) for the full guide. + +Note: when Zeabur builds directly from the repo `Dockerfile`, you do not need to pass `BUILD_VERSION`. The image prefers that build arg when provided, and automatically falls back to the repo-root `VERSION` file when it is absent. + +### Option 3: Vercel + +1. Fork this repo to your GitHub account +2. Import the project on Vercel +3. Set environment variables (minimum: `DS2API_ADMIN_KEY`; recommended to also set `DS2API_CONFIG_JSON`) +4. Deploy + +Recommended first step in repo root: + +```bash +cp config.example.json config.json +# Edit config.json +``` + +Recommended: convert `config.json` to Base64 locally, then paste into `DS2API_CONFIG_JSON` to avoid JSON formatting mistakes: + +```bash +base64 < config.json | tr -d '\n' +``` + +> **Streaming note**: OpenAI Chat streaming on Vercel is routed to `api/chat-stream.js` (Node Runtime), but `vercel.json` rewrites only the canonical `/v1/chat/completions` path to Node; the root shortcut `/chat/completions` stays on the Go main path. Auth, account selection, and session/PoW preparation are still handled by the Go internal prepare endpoint; streaming output (including `tools`) is assembled on Node with Go-aligned anti-leak handling. Use `/v1/chat/completions` on Vercel when real-time streaming is required. + +For detailed deployment instructions, see the [Deployment Guide](docs/DEPLOY.en.md). + +### Option 4: Local Run + +**Prerequisites**: Go 1.26+, Node.js `20.19+` or `22.12+` (only if building WebUI locally; CI / Docker builds use Node 24), and npm available; npm 10+ is recommended + +```bash +# 1. Clone +git clone https://github.com/CJackHwang/ds2api.git +cd ds2api + +# 2. Configure +cp config.example.json config.json +# Edit config.json with your DeepSeek account info and API keys + +# 3. Start +go run ./cmd/ds2api +``` + +Default local URL: `http://127.0.0.1:5001` + +The server actually binds to `0.0.0.0:5001`, so devices on the same LAN can usually reach it through your private IP as well. + +> **WebUI auto-build**: On first local startup, if the WebUI static directory is missing, DS2API auto-runs `npm ci --prefix webui` (only when dependencies are missing) and `npm run build --prefix webui -- --outDir static/admin --emptyOutDir` (requires Node.js; `DS2API_STATIC_ADMIN_DIR` can override the static directory). You can also build manually: `./scripts/build-webui.sh` + +## Configuration + +`README` keeps only the onboarding path. Use [config.example.json](config.example.json) as the field template, and see the [deployment guide](docs/DEPLOY.en.md#0-prerequisites) plus [API configuration notes](API.en.md#configuration-best-practice) for full details. + +Common fields: + +- `keys` / `api_keys`: client API keys; `api_keys` adds `name` and `remark` metadata while `keys` remains compatible. +- `accounts`: managed DeepSeek accounts, supporting `email` or `mobile` login plus proxy/name/remark metadata. +- `model_aliases`: one shared alias map for OpenAI / Claude / Gemini model names. +- `runtime`: account concurrency, queueing, and token refresh behavior, hot-reloadable via Admin Settings. +- `auto_delete.mode`: remote session cleanup after each request, supporting `none` / `single` / `all`. +- `current_input_file`: the global context split/upload mode; it is enabled by default and uploads the full context as a randomly named context file (filename never includes "history") once the character threshold is reached. +- If you turn off `current_input_file`, requests pass through directly without uploading any split context file. + +For the full environment variable list, see [docs/DEPLOY.en.md](docs/DEPLOY.en.md). For auth behavior, see [API.en.md](API.en.md#authentication). + +## Authentication Modes + +For business endpoints (`/v1/*`, `/anthropic/*`, Gemini routes), DS2API supports two modes: + +| Mode | Description | +| --- | --- | +| **Managed account** | Use a key from `config.keys` via `Authorization: Bearer ...` or `x-api-key`; DS2API auto-selects an account | +| **Direct token** | If the token is not in `config.keys`, DS2API treats it as a DeepSeek token directly | + +Optional header `X-Ds2-Target-Account`: Pin a specific managed account (value is email or mobile). +When no target account is pinned, if a completion would end as `429 upstream_empty_output` after the same-account empty-output retry, managed-account mode switches to the next available account, creates a fresh session, and retries the original payload once. +Gemini routes also accept `x-goog-api-key`, or `?key=` / `?api_key=` when no auth header is present. + +## Concurrency Model + +``` +Per-account inflight = DS2API_ACCOUNT_MAX_INFLIGHT (default 2) +Recommended concurrency = account_count × per_account_inflight +Queue limit = DS2API_ACCOUNT_MAX_QUEUE (default = recommended concurrency) +429 threshold = inflight + queue ≈ account_count × 4 +``` + +- When inflight slots are full, requests enter a waiting queue — **no immediate 429** +- 429 is returned only when total load exceeds inflight + queue capacity; current responses do not include `Retry-After` +- Completion empty-output 429s first get the same-account compensation retry; managed-account mode also tries one alternate-account fresh retry before returning the final 429 +- `GET /admin/queue/status` returns real-time concurrency state + +## Tool Call Adaptation + +When `tools` is present in the request, DS2API performs anti-leak handling: + +1. Toolcall feature matching is enabled only in **non-code-block context** (fenced examples are ignored) +2. The parser treats the halfwidth-pipe DSML shell as the recommended executable tool-calling syntax: `<|DSML|tool_calls>` → `<|DSML|invoke name="...">` → `<|DSML|parameter name="...">`; it also accepts legacy canonical XML `` → `` → ``, plus common DSML prefix/separator drift. DSML is a shell alias and internal parsing remains XML-based; legacy `` / `` / `` / ``, ``, `tool_use`, antml variants, and standalone JSON `tool_calls` payloads are treated as plain text, and complete but malformed wrappers are released as plain text too +3. `responses` streaming strictly uses official item lifecycle events (`response.output_item.*`, `response.content_part.*`, `response.function_call_arguments.*`) +4. `responses` supports and enforces `tool_choice` (`auto`/`none`/`required`/forced function); `required` violations return `422` for non-stream and `response.failed` for stream +5. The output protocol follows the client request (OpenAI / Claude / Gemini native shapes); model-side prompting can prefer XML, and the compatibility layer handles the protocol-specific translation + +> Note: the current parser still prioritizes “parse successfully whenever possible”; hard allow-list rejection for undeclared tool names is not enabled yet. +> Explicit empty strings or whitespace-only parameters are preserved by the parser; prompting tells the model not to emit blank parameters, and missing/empty argument rejection belongs in the tool executor or client schema validation. + +## Local Dev Packet Capture + +This is for debugging issues such as Responses reasoning streaming and tool-call handoff. When enabled, DS2API stores the latest N DeepSeek conversation payload pairs (request body + upstream response body), defaulting to 20 entries with auto-eviction; each response body is capped at 5 MB by default. + +Enable example: + +```bash +DS2API_DEV_PACKET_CAPTURE=true \ +DS2API_DEV_PACKET_CAPTURE_LIMIT=20 \ +go run ./cmd/ds2api +``` + +Inspect/clear (Admin JWT required): + +- `GET /admin/dev/captures`: list captured items (newest first) +- `DELETE /admin/dev/captures`: clear captured items +- `GET /admin/dev/raw-samples/query?q=keyword&limit=20`: search current in-memory captures by prompt keyword and group `completion + continue` by `chat_session_id` +- `POST /admin/dev/raw-samples/save`: persist a selected capture chain as `tests/raw_stream_samples//` + +Response fields include: + +- `request_body`: full payload sent to DeepSeek +- `response_body`: concatenated raw upstream stream body text +- `response_truncated`: whether body-size truncation happened + +The save endpoint can target a chain by `query`, `chain_key`, or `capture_id`. Example: + +```json +{"query":"Guangzhou weather","sample_id":"gz-weather-from-memory"} +``` + +## Documentation Index + +| Document | Description | +| --- | --- | +| [API.md](API.md) / [API.en.md](API.en.md) | API reference with request/response examples | +| [DEPLOY.md](docs/DEPLOY.md) / [DEPLOY.en.md](docs/DEPLOY.en.md) | Deployment guide (local/Docker/Vercel/systemd) | +| [CONTRIBUTING.md](docs/CONTRIBUTING.md) / [CONTRIBUTING.en.md](docs/CONTRIBUTING.en.md) | Contributing guide | +| [TESTING.md](docs/TESTING.md) | Testsuite guide | + +## Testing + +For the full testing guide, see [docs/TESTING.md](docs/TESTING.md). + +Quick commands: + +```bash +# Local PR gates +./scripts/lint.sh +./tests/scripts/check-refactor-line-gate.sh +./tests/scripts/run-unit-all.sh +npm run build --prefix webui + +# Live end-to-end tests (real accounts, full request/response logs) +./tests/scripts/run-live.sh +``` + +## Release Artifact Automation (GitHub Actions) + +Workflow: `.github/workflows/release-artifacts.yml` + +- **Trigger**: by default only on GitHub Release `published`; you can also run it manually via `workflow_dispatch` and pass `release_tag` to rerun / backfill +- **Outputs**: multi-platform binary archives (`linux/amd64`, `linux/arm64`, `linux/armv7`, `darwin/amd64`, `darwin/arm64`, `windows/amd64`, `windows/arm64`), Linux Docker image export tarballs, and `sha256sums.txt` +- **Container publishing**: GHCR only (`ghcr.io/cjackhwang/ds2api`) +- **Each binary archive includes**: the `ds2api` executable, `static/admin`, `config.example.json`, `.env.example`, `README.MD`, `README.en.md`, and `LICENSE` + +## Disclaimer + +This project is built through reverse engineering and is provided for learning, research, personal experimentation, and internal validation only. No commercial authorization is granted, and no warranty of stability, fitness, or results is provided. +The author and repository maintainers are not responsible for any direct or indirect loss, account suspension, data loss, legal risk, or third-party claims arising from use, modification, distribution, deployment, or reliance on this project. + +Do not use this project in ways that violate service terms, agreements, laws, or platform rules. Before any commercial use, review the `LICENSE`, the relevant terms, and confirm that you have the author's written permission. diff --git a/README.md b/README.md new file mode 100644 index 0000000000000000000000000000000000000000..2f5e7b7f6f8448937b0e674e82733f33bd3ee9a1 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +--- +title: DS2API +emoji: 🚀 +colorFrom: blue +colorTo: purple +sdk: docker +pinned: false +--- + +# DS2API - DeepSeek to OpenAI API Adapter + +DeepSeek web chat to OpenAI-compatible API adapter with multi-protocol support (OpenAI, Claude, Gemini). + +## Quick Start + +1. Visit /admin to configure your DeepSeek accounts and API keys +2. Use the OpenAI-compatible API at /v1/chat/completions diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000000000000000000000000000000000000..b20a4fd4f9808592eeeb35cfbff0f4fee2ab8065 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,65 @@ +# Security Policy + +## Supported Versions + +**Only the latest version** receives security updates. +If you are using an older version, please upgrade to the latest release. + +| Version | Supported | +| -------------- | ------------------ | +| latest | :white_check_mark: | +| < latest | :x: | + +> **Why?** This project is maintained by a single developer. Keeping only one active version ensures fast response times and avoids legacy maintenance overhead. + +## What is a Security Vulnerability? + +A **security vulnerability** is a bug that can be exploited to compromise: +- Data confidentiality (e.g., leaking secrets, user data) +- Data integrity (e.g., unauthorized modification) +- System availability (e.g., remote crash, denial of service) +- Privilege escalation (e.g., normal user gains admin rights) + +**Examples**: SQL injection, command injection, path traversal, authentication bypass, insecure deserialization, sensitive data exposure. + +**What is NOT a security vulnerability?** +Regular bugs like crashes (without exploit potential), incorrect return values, performance issues, missing features, or documentation typos. Please report those via **GitHub Issues** publicly. + +## Reporting a Vulnerability + +If you believe you have found a security vulnerability, **please do NOT open a public issue**. + +Instead, send an email to: **cjackhwang@qq.com** + +Please include as much as possible: +- A clear description of the issue +- Steps to reproduce (code / input / environment) +- Potential impact (what could an attacker do?) +- Suggested fix (if any) + +You can expect: +- **Initial response** within 3 business days (acknowledgment) +- **Confirmation or clarification** within 7 days +- **Fix or decision** within 14 days (depending on complexity) + +## What to Expect After Reporting + +| Outcome | What happens | +| ------------------ | ------------- | +| **Accepted** | I will develop a fix, release a patch version, and may credit you in the release notes (unless you prefer anonymity). | +| **Declined** | I will explain why (e.g., not a security issue, already fixed, out of scope, or requires a larger redesign). | +| **Need more info** | I will ask follow-up questions. If no response within 14 days, the report may be considered stale. | + +## Disclosure Policy + +- Vulnerabilities will be **fixed privately** and then released as a new version. +- After the fix is released, I will typically publish a short security advisory (via GitHub Security Advisories) without revealing exploit details. +- Public disclosure can be coordinated if you request it. + +## Recognition + +I appreciate security researchers who follow responsible disclosure. Contributors who report valid, previously unknown vulnerabilities may be acknowledged in the project's README or release notes (unless they prefer to stay anonymous). + +--- + +*Thank you for helping keep this project safe!* diff --git a/VERSION b/VERSION new file mode 100644 index 0000000000000000000000000000000000000000..8ac28bf9f0f0ea8466032de41a817507691422db --- /dev/null +++ b/VERSION @@ -0,0 +1 @@ +4.6.1 diff --git a/_fix.py b/_fix.py new file mode 100644 index 0000000000000000000000000000000000000000..058a6bbfc3ff6882b094667e39381d7ddb86337f --- /dev/null +++ b/_fix.py @@ -0,0 +1,8 @@ +import pathlib +p = pathlib.Path("Dockerfile") +t = p.read_text() +old = 'CMD["/usr/local/bin/ds2api"]' +new = 'COPY entrypoint.sh /usr/local/bin/entrypoint.sh\nRUN chmod +x /usr/local/bin/entrypoint.sh\nCMD["/usr/local/bin/entrypoint.sh"]' +t = t.replace(old, new) +p.write_text(t) +print("Done") diff --git a/_fix2.py b/_fix2.py new file mode 100644 index 0000000000000000000000000000000000000000..1e7393b2f9861f1e7b1296d0c0f8a32078ecfe65 --- /dev/null +++ b/_fix2.py @@ -0,0 +1,7 @@ +import pathlib +p=pathlib.Path(chr(68)+chr(111)+chr(99)+chr(107)+chr(101)+chr(114)+chr(102)+chr(105)+chr(108)+chr(101)) +t=p.read_text() +old=CMD [/usr/local/bin/ds2api] +new=CMD [/usr/local/bin/entrypoint.sh] +print(repr(old)) +print(repr(new)) diff --git a/api/chat-stream.js b/api/chat-stream.js new file mode 100644 index 0000000000000000000000000000000000000000..9241b049a2163b17f674f6ebabb126fd7d2327b1 --- /dev/null +++ b/api/chat-stream.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('../internal/js/chat-stream/index.js'); diff --git a/api/index.go b/api/index.go new file mode 100644 index 0000000000000000000000000000000000000000..147ce32e68fa1a7a189eb6f81b661c0124a212a4 --- /dev/null +++ b/api/index.go @@ -0,0 +1,20 @@ +package handler + +import ( + "net/http" + "sync" + + "ds2api/app" +) + +var ( + once sync.Once + h http.Handler +) + +func Handler(w http.ResponseWriter, r *http.Request) { + once.Do(func() { + h = app.NewHandler() + }) + h.ServeHTTP(w, r) +} diff --git a/app/handler.go b/app/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..bc26a67b9e727435b74f06edcb90349d04da623f --- /dev/null +++ b/app/handler.go @@ -0,0 +1,19 @@ +package app + +import ( + "net/http" + + "ds2api/internal/config" + "ds2api/internal/server" +) + +func NewHandler() http.Handler { + app, err := server.NewApp() + if err != nil { + config.Logger.Error("[app] init failed", "error", err) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + server.WriteUnhandledError(w, err) + }) + } + return app.Router +} diff --git a/cf-worker/package-lock.json b/cf-worker/package-lock.json new file mode 100644 index 0000000000000000000000000000000000000000..dae4c2ddc727cf545f2ea2d5e5a4e261fd71bace --- /dev/null +++ b/cf-worker/package-lock.json @@ -0,0 +1,1504 @@ +{ + "name": "ds2api-health-check", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "ds2api-health-check", + "version": "1.0.0", + "devDependencies": { + "wrangler": "^4" + } + }, + "node_modules/@cloudflare/kv-asset-handler": { + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/@cloudflare/kv-asset-handler/-/kv-asset-handler-0.5.0.tgz", + "integrity": "sha512-jxQYkj8dSIzc0cD6cMMNdOc1UVjqSqu8BZdor5s8cGjW2I8BjODt/kWPVdY+u9zj3ms75Q5qaZgnxUad83+eAg==", + "dev": true, + "license": "MIT OR Apache-2.0", + "engines": { + "node": ">=22.0.0" + } + }, + "node_modules/@cloudflare/unenv-preset": { + "version": "2.16.1", + "resolved": "https://registry.npmjs.org/@cloudflare/unenv-preset/-/unenv-preset-2.16.1.tgz", + "integrity": "sha512-ECxObrMfyTl5bhQf/lZCXwo5G6xX9IAUo+nDMKK4SZ8m4Jvvxp52vilxyySSWh2YTZz8+HQ07qGH/2rEom1vDw==", + "dev": true, + "license": "MIT OR Apache-2.0", + "peerDependencies": { + "unenv": "2.0.0-rc.24", + "workerd": ">1.20260305.0 <2.0.0-0" + }, + "peerDependenciesMeta": { + "workerd": { + "optional": true + } + } + }, + "node_modules/@cloudflare/workerd-darwin-64": { + "version": "1.20260603.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-64/-/workerd-darwin-64-1.20260603.1.tgz", + "integrity": "sha512-cEXDWu6V3ZrpmwWkM4OJE9AeXjdAgOY5rh8EHhcBVCuP5rxnzUbPzLtrVOHx0UUUAcCrFq0Xsa6mZKL1VUZsKQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-darwin-arm64": { + "version": "1.20260603.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-darwin-arm64/-/workerd-darwin-arm64-1.20260603.1.tgz", + "integrity": "sha512-uBPK4LaWJNbbCYwPnUAehlHbbVulhVZPZsdcAhBPfZhHb3QAuAEPAQepO/P67R3V6Cni4YGx1fLbL8A5wwoaNA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-linux-64": { + "version": "1.20260603.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-64/-/workerd-linux-64-1.20260603.1.tgz", + "integrity": "sha512-ht9l6/8Tk7Rp6kA4S9oFZ4X8u0VjnnFdmU/6B3fnABYKREYTKh2RdOqXqXxcp5eNJseireKnWik/hQOPK1CutQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-linux-arm64": { + "version": "1.20260603.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-linux-arm64/-/workerd-linux-arm64-1.20260603.1.tgz", + "integrity": "sha512-LJZ6x00rAjSrobV4m0ZW0TpH5ilBbKcWBzlH+y+KOUsIE/CpTuhAzKV43TbSnFLRX5+jrWKiz2v0hO91lPXy6A==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cloudflare/workerd-windows-64": { + "version": "1.20260603.1", + "resolved": "https://registry.npmjs.org/@cloudflare/workerd-windows-64/-/workerd-windows-64-1.20260603.1.tgz", + "integrity": "sha512-DvwqkXMAJRPoDN4PxapAwhlz/6ouD+6R1ttbAEK3cWD/QBvFF5STx7Ds/9Irf+rBly3np3uHWkeX+wZnNFEuzA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=16" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@emnapi/runtime": { + "version": "1.10.0", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.10.0.tgz", + "integrity": "sha512-ewvYlk86xUoGI0zQRNq/mC+16R1QeDlKQy21Ki3oSYXNgLb45GV1P6A0M+/s6nyCuNDqe5VpaY84BzXGwVbwFA==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@esbuild/aix-ppc64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.27.3.tgz", + "integrity": "sha512-9fJMTNFTWZMh5qwrBItuziu834eOCUcEqymSH7pY+zoMVEZg3gcPuBNxH1EvfVYe9h0x/Ptw8KBzv7qxb7l8dg==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "aix" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.27.3.tgz", + "integrity": "sha512-i5D1hPY7GIQmXlXhs2w8AWHhenb00+GxjxRncS2ZM7YNVGNfaMxgzSGuO8o8SJzRc/oZwU2bcScvVERk03QhzA==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.27.3.tgz", + "integrity": "sha512-YdghPYUmj/FX2SYKJ0OZxf+iaKgMsKHVPF1MAq/P8WirnSpCStzKJFjOjzsW0QQ7oIAiccHdcqjbHmJxRb/dmg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.27.3.tgz", + "integrity": "sha512-IN/0BNTkHtk8lkOM8JWAYFg4ORxBkZQf9zXiEOfERX/CzxW3Vg1ewAhU7QSWQpVIzTW+b8Xy+lGzdYXV6UZObQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.27.3.tgz", + "integrity": "sha512-Re491k7ByTVRy0t3EKWajdLIr0gz2kKKfzafkth4Q8A5n1xTHrkqZgLLjFEHVD+AXdUGgQMq+Godfq45mGpCKg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.27.3.tgz", + "integrity": "sha512-vHk/hA7/1AckjGzRqi6wbo+jaShzRowYip6rt6q7VYEDX4LEy1pZfDpdxCBnGtl+A5zq8iXDcyuxwtv3hNtHFg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.27.3.tgz", + "integrity": "sha512-ipTYM2fjt3kQAYOvo6vcxJx3nBYAzPjgTCk7QEgZG8AUO3ydUhvelmhrbOheMnGOlaSFUoHXB6un+A7q4ygY9w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.27.3.tgz", + "integrity": "sha512-dDk0X87T7mI6U3K9VjWtHOXqwAMJBNN2r7bejDsc+j03SEjtD9HrOl8gVFByeM0aJksoUuUVU9TBaZa2rgj0oA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.27.3.tgz", + "integrity": "sha512-s6nPv2QkSupJwLYyfS+gwdirm0ukyTFNl3KTgZEAiJDd+iHZcbTPPcWCcRYH+WlNbwChgH2QkE9NSlNrMT8Gfw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.27.3.tgz", + "integrity": "sha512-sZOuFz/xWnZ4KH3YfFrKCf1WyPZHakVzTiqji3WDc0BCl2kBwiJLCXpzLzUBLgmp4veFZdvN5ChW4Eq/8Fc2Fg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.27.3.tgz", + "integrity": "sha512-yGlQYjdxtLdh0a3jHjuwOrxQjOZYD/C9PfdbgJJF3TIZWnm/tMd/RcNiLngiu4iwcBAOezdnSLAwQDPqTmtTYg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.27.3.tgz", + "integrity": "sha512-WO60Sn8ly3gtzhyjATDgieJNet/KqsDlX5nRC5Y3oTFcS1l0KWba+SEa9Ja1GfDqSF1z6hif/SkpQJbL63cgOA==", + "cpu": [ + "loong64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.27.3.tgz", + "integrity": "sha512-APsymYA6sGcZ4pD6k+UxbDjOFSvPWyZhjaiPyl/f79xKxwTnrn5QUnXR5prvetuaSMsb4jgeHewIDCIWljrSxw==", + "cpu": [ + "mips64el" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.27.3.tgz", + "integrity": "sha512-eizBnTeBefojtDb9nSh4vvVQ3V9Qf9Df01PfawPcRzJH4gFSgrObw+LveUyDoKU3kxi5+9RJTCWlj4FjYXVPEA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.27.3.tgz", + "integrity": "sha512-3Emwh0r5wmfm3ssTWRQSyVhbOHvqegUDRd0WhmXKX2mkHJe1SFCMJhagUleMq+Uci34wLSipf8Lagt4LlpRFWQ==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.27.3.tgz", + "integrity": "sha512-pBHUx9LzXWBc7MFIEEL0yD/ZVtNgLytvx60gES28GcWMqil8ElCYR4kvbV2BDqsHOvVDRrOxGySBM9Fcv744hw==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.27.3.tgz", + "integrity": "sha512-Czi8yzXUWIQYAtL/2y6vogER8pvcsOsk5cpwL4Gk5nJqH5UZiVByIY8Eorm5R13gq+DQKYg0+JyQoytLQas4dA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-arm64/-/netbsd-arm64-0.27.3.tgz", + "integrity": "sha512-sDpk0RgmTCR/5HguIZa9n9u+HVKf40fbEUt+iTzSnCaGvY9kFP0YKBWZtJaraonFnqef5SlJ8/TiPAxzyS+UoA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.27.3.tgz", + "integrity": "sha512-P14lFKJl/DdaE00LItAukUdZO5iqNH7+PjoBm+fLQjtxfcfFE20Xf5CrLsmZdq5LFFZzb5JMZ9grUwvtVYzjiA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-arm64/-/openbsd-arm64-0.27.3.tgz", + "integrity": "sha512-AIcMP77AvirGbRl/UZFTq5hjXK+2wC7qFRGoHSDrZ5v5b8DK/GYpXW3CPRL53NkvDqb9D+alBiC/dV0Fb7eJcw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.27.3.tgz", + "integrity": "sha512-DnW2sRrBzA+YnE70LKqnM3P+z8vehfJWHXECbwBmH/CU51z6FiqTQTHFenPlHmo3a8UgpLyH3PT+87OViOh1AQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/openharmony-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/openharmony-arm64/-/openharmony-arm64-0.27.3.tgz", + "integrity": "sha512-NinAEgr/etERPTsZJ7aEZQvvg/A6IsZG/LgZy+81wON2huV7SrK3e63dU0XhyZP4RKGyTm7aOgmQk0bGp0fy2g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.27.3.tgz", + "integrity": "sha512-PanZ+nEz+eWoBJ8/f8HKxTTD172SKwdXebZ0ndd953gt1HRBbhMsaNqjTyYLGLPdoWHy4zLU7bDVJztF5f3BHA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.27.3.tgz", + "integrity": "sha512-B2t59lWWYrbRDw/tjiWOuzSsFh1Y/E95ofKz7rIVYSQkUYBjfSgf6oeYPNWHToFRr2zx52JKApIcAS/D5TUBnA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.27.3.tgz", + "integrity": "sha512-QLKSFeXNS8+tHW7tZpMtjlNb7HKau0QDpwm49u0vUp9y1WOF+PEzkU84y9GqYaAVW8aH8f3GcBck26jh54cX4Q==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.27.3.tgz", + "integrity": "sha512-4uJGhsxuptu3OcpVAzli+/gWusVGwZZHTlS63hh++ehExkVT8SgiEf7/uC/PclrPPkLhZqGgCTjd0VWLo6xMqA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/colour": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@img/colour/-/colour-1.1.0.tgz", + "integrity": "sha512-Td76q7j57o/tLVdgS746cYARfSyxk8iEfRxewL9h4OMzYhbW4TAcppl0mT4eyqXddh6L/jwoM75mo7ixa/pCeQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + } + }, + "node_modules/@img/sharp-darwin-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-arm64/-/sharp-darwin-arm64-0.34.5.tgz", + "integrity": "sha512-imtQ3WMJXbMY4fxb/Ndp6HBTNVtWCUI0WdobyheGf5+ad6xX8VIDO8u2xE4qc/fr08CKG/7dDseFtn6M6g/r3w==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-darwin-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-darwin-x64/-/sharp-darwin-x64-0.34.5.tgz", + "integrity": "sha512-YNEFAF/4KQ/PeW0N+r+aVVsoIY0/qxxikF2SWdp+NRkmMB7y9LBZAVqQ4yhGCm/H3H270OSykqmQMKLBhBJDEw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-darwin-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-libvips-darwin-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-arm64/-/sharp-libvips-darwin-arm64-1.2.4.tgz", + "integrity": "sha512-zqjjo7RatFfFoP0MkQ51jfuFZBnVE2pRiaydKJ1G/rHZvnsrHAOcQALIi9sA5co5xenQdTugCvtb1cuf78Vf4g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-darwin-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-darwin-x64/-/sharp-libvips-darwin-x64-1.2.4.tgz", + "integrity": "sha512-1IOd5xfVhlGwX+zXv2N93k0yMONvUlANylbJw1eTah8K/Jtpi15KC+WSiaX/nBmbm2HxRM1gZ0nSdjSsrZbGKg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "darwin" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm/-/sharp-libvips-linux-arm-1.2.4.tgz", + "integrity": "sha512-bFI7xcKFELdiNCVov8e44Ia4u2byA+l3XtsAj+Q8tfCwO6BQ8iDojYdvoPMqsKDkuoOo+X6HZA0s0q11ANMQ8A==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-arm64/-/sharp-libvips-linux-arm64-1.2.4.tgz", + "integrity": "sha512-excjX8DfsIcJ10x1Kzr4RcWe1edC9PquDRRPx3YVCvQv+U5p7Yin2s32ftzikXojb1PIFc/9Mt28/y+iRklkrw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-ppc64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-ppc64/-/sharp-libvips-linux-ppc64-1.2.4.tgz", + "integrity": "sha512-FMuvGijLDYG6lW+b/UvyilUWu5Ayu+3r2d1S8notiGCIyYU/76eig1UfMmkZ7vwgOrzKzlQbFSuQfgm7GYUPpA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-riscv64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-riscv64/-/sharp-libvips-linux-riscv64-1.2.4.tgz", + "integrity": "sha512-oVDbcR4zUC0ce82teubSm+x6ETixtKZBh/qbREIOcI3cULzDyb18Sr/Wcyx7NRQeQzOiHTNbZFF1UwPS2scyGA==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-s390x": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-s390x/-/sharp-libvips-linux-s390x-1.2.4.tgz", + "integrity": "sha512-qmp9VrzgPgMoGZyPvrQHqk02uyjA0/QrTO26Tqk6l4ZV0MPWIW6LTkqOIov+J1yEu7MbFQaDpwdwJKhbJvuRxQ==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linux-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linux-x64/-/sharp-libvips-linux-x64-1.2.4.tgz", + "integrity": "sha512-tJxiiLsmHc9Ax1bz3oaOYBURTXGIRDODBqhveVHonrHJ9/+k89qbLl0bcJns+e4t4rvaNBxaEZsFtSfAdquPrw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-arm64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-arm64/-/sharp-libvips-linuxmusl-arm64-1.2.4.tgz", + "integrity": "sha512-FVQHuwx1IIuNow9QAbYUzJ+En8KcVm9Lk5+uGUQJHaZmMECZmOlix9HnH7n1TRkXMS0pGxIJokIVB9SuqZGGXw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-libvips-linuxmusl-x64": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/@img/sharp-libvips-linuxmusl-x64/-/sharp-libvips-linuxmusl-x64-1.2.4.tgz", + "integrity": "sha512-+LpyBk7L44ZIXwz/VYfglaX/okxezESc6UxDSoyo2Ks6Jxc4Y7sGjpgU9s4PMgqgjj1gZCylTieNamqA1MF7Dg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "LGPL-3.0-or-later", + "optional": true, + "os": [ + "linux" + ], + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-linux-arm": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm/-/sharp-linux-arm-0.34.5.tgz", + "integrity": "sha512-9dLqsvwtg1uuXBGZKsxem9595+ujv0sJ6Vi8wcTANSFpwV/GONat5eCkzQo/1O6zRIkh0m/8+5BjrRr7jDUSZw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-arm64/-/sharp-linux-arm64-0.34.5.tgz", + "integrity": "sha512-bKQzaJRY/bkPOXyKx5EVup7qkaojECG6NLYswgktOZjaXecSAeCWiZwwiFf3/Y+O1HrauiE3FVsGxFg8c24rZg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-ppc64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-ppc64/-/sharp-linux-ppc64-0.34.5.tgz", + "integrity": "sha512-7zznwNaqW6YtsfrGGDA6BRkISKAAE1Jo0QdpNYXNMHu2+0dTrPflTLNkpc8l7MUP5M16ZJcUvysVWWrMefZquA==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-ppc64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-riscv64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-riscv64/-/sharp-linux-riscv64-0.34.5.tgz", + "integrity": "sha512-51gJuLPTKa7piYPaVs8GmByo7/U7/7TZOq+cnXJIHZKavIRHAP77e3N2HEl3dgiqdD/w0yUfiJnII77PuDDFdw==", + "cpu": [ + "riscv64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-riscv64": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-s390x": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-s390x/-/sharp-linux-s390x-0.34.5.tgz", + "integrity": "sha512-nQtCk0PdKfho3eC5MrbQoigJ2gd1CgddUMkabUj+rBevs8tZ2cULOx46E7oyX+04WGfABgIwmMC0VqieTiR4jg==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-s390x": "1.2.4" + } + }, + "node_modules/@img/sharp-linux-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linux-x64/-/sharp-linux-x64-0.34.5.tgz", + "integrity": "sha512-MEzd8HPKxVxVenwAa+JRPwEC7QFjoPWuS5NZnBt6B3pu7EG2Ge0id1oLHZpPJdn3OQK+BQDiw9zStiHBTJQQQQ==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linux-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-linuxmusl-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-arm64/-/sharp-linuxmusl-arm64-0.34.5.tgz", + "integrity": "sha512-fprJR6GtRsMt6Kyfq44IsChVZeGN97gTD331weR1ex1c1rypDEABN6Tm2xa1wE6lYb5DdEnk03NZPqA7Id21yg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4" + } + }, + "node_modules/@img/sharp-linuxmusl-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-linuxmusl-x64/-/sharp-linuxmusl-x64-0.34.5.tgz", + "integrity": "sha512-Jg8wNT1MUzIvhBFxViqrEhWDGzqymo3sV7z7ZsaWbZNDLXRJZoRGrjulp60YYtV4wfY8VIKcWidjojlLcWrd8Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-libvips-linuxmusl-x64": "1.2.4" + } + }, + "node_modules/@img/sharp-wasm32": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-wasm32/-/sharp-wasm32-0.34.5.tgz", + "integrity": "sha512-OdWTEiVkY2PHwqkbBI8frFxQQFekHaSSkUIJkwzclWZe64O1X4UlUjqqqLaPbUpMOQk6FBu/HtlGXNblIs0huw==", + "cpu": [ + "wasm32" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later AND MIT", + "optional": true, + "dependencies": { + "@emnapi/runtime": "^1.7.0" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-arm64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-arm64/-/sharp-win32-arm64-0.34.5.tgz", + "integrity": "sha512-WQ3AgWCWYSb2yt+IG8mnC6Jdk9Whs7O0gxphblsLvdhSpSTtmu69ZG1Gkb6NuvxsNACwiPV6cNSZNzt0KPsw7g==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-ia32": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-ia32/-/sharp-win32-ia32-0.34.5.tgz", + "integrity": "sha512-FV9m/7NmeCmSHDD5j4+4pNI8Cp3aW+JvLoXcTUo0IqyjSfAZJ8dIUmijx1qaJsIiU+Hosw6xM5KijAWRJCSgNg==", + "cpu": [ + "ia32" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@img/sharp-win32-x64": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/@img/sharp-win32-x64/-/sharp-win32-x64-0.34.5.tgz", + "integrity": "sha512-+29YMsqY2/9eFEiW93eqWnuLcWcufowXewwSNIT6UwZdUUCrM3oFjMWH/Z6/TMmb4hlFenmfAVbpWeup2jryCw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "Apache-2.0 AND LGPL-3.0-or-later", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz", + "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@poppinss/colors": { + "version": "4.1.6", + "resolved": "https://registry.npmjs.org/@poppinss/colors/-/colors-4.1.6.tgz", + "integrity": "sha512-H9xkIdFswbS8n1d6vmRd8+c10t2Qe+rZITbbDHHkQixH5+2x1FDGmi/0K+WgWiqQFKPSlIYB7jlH6Kpfn6Fleg==", + "dev": true, + "license": "MIT", + "dependencies": { + "kleur": "^4.1.5" + } + }, + "node_modules/@poppinss/dumper": { + "version": "0.6.5", + "resolved": "https://registry.npmjs.org/@poppinss/dumper/-/dumper-0.6.5.tgz", + "integrity": "sha512-NBdYIb90J7LfOI32dOewKI1r7wnkiH6m920puQ3qHUeZkxNkQiFnXVWoE6YtFSv6QOiPPf7ys6i+HWWecDz7sw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/colors": "^4.1.5", + "@sindresorhus/is": "^7.0.2", + "supports-color": "^10.0.0" + } + }, + "node_modules/@poppinss/exception": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/@poppinss/exception/-/exception-1.2.3.tgz", + "integrity": "sha512-dCED+QRChTVatE9ibtoaxc+WkdzOSjYTKi/+uacHWIsfodVfpsueo3+DKpgU5Px8qXjgmXkSvhXvSCz3fnP9lw==", + "dev": true, + "license": "MIT" + }, + "node_modules/@sindresorhus/is": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/@sindresorhus/is/-/is-7.2.0.tgz", + "integrity": "sha512-P1Cz1dWaFfR4IR+U13mqqiGsLFf1KbayybWwdd2vfctdV6hDpUkgCY0nKOLLTMSoRd/jJNjtbqzf13K8DCCXQw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/sindresorhus/is?sponsor=1" + } + }, + "node_modules/@speed-highlight/core": { + "version": "1.2.15", + "resolved": "https://registry.npmjs.org/@speed-highlight/core/-/core-1.2.15.tgz", + "integrity": "sha512-BMq1K3DsElxDWawkX6eLg9+CKJrTVGCBAWVuHXVUV2u0s2711qiChLSId6ikYPfxhdYocLNt3wWwSvDiTvFabw==", + "dev": true, + "license": "CC0-1.0" + }, + "node_modules/blake3-wasm": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/blake3-wasm/-/blake3-wasm-2.1.5.tgz", + "integrity": "sha512-F1+K8EbfOZE49dtoPtmxUQrpXaBIl3ICvasLh+nJta0xkz+9kF/7uet9fLnwKqhDrmj6g+6K3Tw9yQPUg2ka5g==", + "dev": true, + "license": "MIT" + }, + "node_modules/cookie": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-1.1.1.tgz", + "integrity": "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/error-stack-parser-es": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/error-stack-parser-es/-/error-stack-parser-es-1.0.5.tgz", + "integrity": "sha512-5qucVt2XcuGMcEGgWI7i+yZpmpByQ8J1lHhcL7PwqCwu9FPP3VUXzT4ltHe5i2z9dePwEHcDVOAfSnHsOlCXRA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/antfu" + } + }, + "node_modules/esbuild": { + "version": "0.27.3", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.27.3.tgz", + "integrity": "sha512-8VwMnyGCONIs6cWue2IdpHxHnAjzxnw2Zr7MkVxB2vjmQ2ivqGFb4LEG3SMnv0Gb2F/G/2yA8zUaiL1gywDCCg==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=18" + }, + "optionalDependencies": { + "@esbuild/aix-ppc64": "0.27.3", + "@esbuild/android-arm": "0.27.3", + "@esbuild/android-arm64": "0.27.3", + "@esbuild/android-x64": "0.27.3", + "@esbuild/darwin-arm64": "0.27.3", + "@esbuild/darwin-x64": "0.27.3", + "@esbuild/freebsd-arm64": "0.27.3", + "@esbuild/freebsd-x64": "0.27.3", + "@esbuild/linux-arm": "0.27.3", + "@esbuild/linux-arm64": "0.27.3", + "@esbuild/linux-ia32": "0.27.3", + "@esbuild/linux-loong64": "0.27.3", + "@esbuild/linux-mips64el": "0.27.3", + "@esbuild/linux-ppc64": "0.27.3", + "@esbuild/linux-riscv64": "0.27.3", + "@esbuild/linux-s390x": "0.27.3", + "@esbuild/linux-x64": "0.27.3", + "@esbuild/netbsd-arm64": "0.27.3", + "@esbuild/netbsd-x64": "0.27.3", + "@esbuild/openbsd-arm64": "0.27.3", + "@esbuild/openbsd-x64": "0.27.3", + "@esbuild/openharmony-arm64": "0.27.3", + "@esbuild/sunos-x64": "0.27.3", + "@esbuild/win32-arm64": "0.27.3", + "@esbuild/win32-ia32": "0.27.3", + "@esbuild/win32-x64": "0.27.3" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/kleur": { + "version": "4.1.5", + "resolved": "https://registry.npmjs.org/kleur/-/kleur-4.1.5.tgz", + "integrity": "sha512-o+NO+8WrRiQEE4/7nwRJhN1HWpVmJm511pBHUxPLtp0BUISzlBplORYSmTclCnJvQq2tKu/sgl3xVpkc7ZWuQQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/miniflare": { + "version": "4.20260603.0", + "resolved": "https://registry.npmjs.org/miniflare/-/miniflare-4.20260603.0.tgz", + "integrity": "sha512-+kMQYB82gC8MPOuojHur3icQsUeZUEJ+Sphuo5rVC3Ri9txBLAW/mH33b9OVrpmkogQeaaqPS4tPtugJZhk5Kw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@cspotcode/source-map-support": "0.8.1", + "sharp": "0.34.5", + "undici": "7.24.8", + "workerd": "1.20260603.1", + "ws": "8.20.1", + "youch": "4.1.0-beta.10" + }, + "bin": { + "miniflare": "bootstrap.js" + }, + "engines": { + "node": ">=22.0.0" + } + }, + "node_modules/path-to-regexp": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/path-to-regexp/-/path-to-regexp-6.3.0.tgz", + "integrity": "sha512-Yhpw4T9C6hPpgPeA28us07OJeqZ5EzQTkbfwuhsUg0c237RomFoETJgmp2sa3F/41gfLE6G5cqcYwznmeEeOlQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/pathe": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/pathe/-/pathe-2.0.3.tgz", + "integrity": "sha512-WUjGcAqP1gQacoQe+OBJsFA7Ld4DyXuUIjZ5cc75cLHvJ7dtNsTugphxIADwspS+AraAUePCKrSVtPLFj/F88w==", + "dev": true, + "license": "MIT" + }, + "node_modules/semver": { + "version": "7.8.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-7.8.2.tgz", + "integrity": "sha512-c8jsqUZm3omBOI66G90z1Dyw5z622G8oLG+omfsHBJf3CWQTlOcwOjvOG6wtiNfW6anKm/eA39LMwMtMez2TiQ==", + "dev": true, + "license": "ISC", + "bin": { + "semver": "bin/semver.js" + }, + "engines": { + "node": ">=10" + } + }, + "node_modules/sharp": { + "version": "0.34.5", + "resolved": "https://registry.npmjs.org/sharp/-/sharp-0.34.5.tgz", + "integrity": "sha512-Ou9I5Ft9WNcCbXrU9cMgPBcCK8LiwLqcbywW3t4oDV37n1pzpuNLsYiAV8eODnjbtQlSDwZ2cUEeQz4E54Hltg==", + "dev": true, + "hasInstallScript": true, + "license": "Apache-2.0", + "dependencies": { + "@img/colour": "^1.0.0", + "detect-libc": "^2.1.2", + "semver": "^7.7.3" + }, + "engines": { + "node": "^18.17.0 || ^20.3.0 || >=21.0.0" + }, + "funding": { + "url": "https://opencollective.com/libvips" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "0.34.5", + "@img/sharp-darwin-x64": "0.34.5", + "@img/sharp-libvips-darwin-arm64": "1.2.4", + "@img/sharp-libvips-darwin-x64": "1.2.4", + "@img/sharp-libvips-linux-arm": "1.2.4", + "@img/sharp-libvips-linux-arm64": "1.2.4", + "@img/sharp-libvips-linux-ppc64": "1.2.4", + "@img/sharp-libvips-linux-riscv64": "1.2.4", + "@img/sharp-libvips-linux-s390x": "1.2.4", + "@img/sharp-libvips-linux-x64": "1.2.4", + "@img/sharp-libvips-linuxmusl-arm64": "1.2.4", + "@img/sharp-libvips-linuxmusl-x64": "1.2.4", + "@img/sharp-linux-arm": "0.34.5", + "@img/sharp-linux-arm64": "0.34.5", + "@img/sharp-linux-ppc64": "0.34.5", + "@img/sharp-linux-riscv64": "0.34.5", + "@img/sharp-linux-s390x": "0.34.5", + "@img/sharp-linux-x64": "0.34.5", + "@img/sharp-linuxmusl-arm64": "0.34.5", + "@img/sharp-linuxmusl-x64": "0.34.5", + "@img/sharp-wasm32": "0.34.5", + "@img/sharp-win32-arm64": "0.34.5", + "@img/sharp-win32-ia32": "0.34.5", + "@img/sharp-win32-x64": "0.34.5" + } + }, + "node_modules/supports-color": { + "version": "10.2.2", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-10.2.2.tgz", + "integrity": "sha512-SS+jx45GF1QjgEXQx4NJZV9ImqmO2NPz5FNsIHrsDjh2YsHnawpan7SNQ1o8NuhrbHZy9AZhIoCUiCeaW/C80g==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "url": "https://github.com/chalk/supports-color?sponsor=1" + } + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "dev": true, + "license": "0BSD", + "optional": true + }, + "node_modules/undici": { + "version": "7.24.8", + "resolved": "https://registry.npmjs.org/undici/-/undici-7.24.8.tgz", + "integrity": "sha512-6KQ/+QxK49Z/p3HO6E5ZCZWNnCasyZLa5ExaVYyvPxUwKtbCPMKELJOqh7EqOle0t9cH/7d2TaaTRRa6Nhs4YQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=20.18.1" + } + }, + "node_modules/unenv": { + "version": "2.0.0-rc.24", + "resolved": "https://registry.npmjs.org/unenv/-/unenv-2.0.0-rc.24.tgz", + "integrity": "sha512-i7qRCmY42zmCwnYlh9H2SvLEypEFGye5iRmEMKjcGi7zk9UquigRjFtTLz0TYqr0ZGLZhaMHl/foy1bZR+Cwlw==", + "dev": true, + "license": "MIT", + "dependencies": { + "pathe": "^2.0.3" + } + }, + "node_modules/workerd": { + "version": "1.20260603.1", + "resolved": "https://registry.npmjs.org/workerd/-/workerd-1.20260603.1.tgz", + "integrity": "sha512-NPcbhI1++CS+fnELyXtsIR52en+5kwr/OrKeiQeYXGy10HxmPdsQBv9N+DU7hJIOOmBHhOGAAsoGDjyiQ2YCaA==", + "dev": true, + "hasInstallScript": true, + "license": "Apache-2.0", + "bin": { + "workerd": "bin/workerd" + }, + "engines": { + "node": ">=16" + }, + "optionalDependencies": { + "@cloudflare/workerd-darwin-64": "1.20260603.1", + "@cloudflare/workerd-darwin-arm64": "1.20260603.1", + "@cloudflare/workerd-linux-64": "1.20260603.1", + "@cloudflare/workerd-linux-arm64": "1.20260603.1", + "@cloudflare/workerd-windows-64": "1.20260603.1" + } + }, + "node_modules/wrangler": { + "version": "4.98.0", + "resolved": "https://registry.npmjs.org/wrangler/-/wrangler-4.98.0.tgz", + "integrity": "sha512-cXfFUuF4rMIvE0hiMnXjEAB27ERryaCgquBJdUoPIjFzYYE1rbRdMUkEdQ18qDPUtsPvhJdqxLntixT9OfSzQw==", + "dev": true, + "license": "MIT OR Apache-2.0", + "dependencies": { + "@cloudflare/kv-asset-handler": "0.5.0", + "@cloudflare/unenv-preset": "2.16.1", + "blake3-wasm": "2.1.5", + "esbuild": "0.27.3", + "miniflare": "4.20260603.0", + "path-to-regexp": "6.3.0", + "unenv": "2.0.0-rc.24", + "workerd": "1.20260603.1" + }, + "bin": { + "wrangler": "bin/wrangler.js", + "wrangler2": "bin/wrangler.js" + }, + "engines": { + "node": ">=22.0.0" + }, + "optionalDependencies": { + "fsevents": "2.3.3" + }, + "peerDependencies": { + "@cloudflare/workers-types": "^4.20260603.1" + }, + "peerDependenciesMeta": { + "@cloudflare/workers-types": { + "optional": true + } + } + }, + "node_modules/ws": { + "version": "8.20.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-8.20.1.tgz", + "integrity": "sha512-It4dO0K5v//JtTXuPkfEOaI3uUN87iYPnqo/ZzqCoG3g8uhA66QUMs/SrM0YK7/NAu+r4LMh/9dq2A7k+rHs+w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10.0.0" + }, + "peerDependencies": { + "bufferutil": "^4.0.1", + "utf-8-validate": ">=5.0.2" + }, + "peerDependenciesMeta": { + "bufferutil": { + "optional": true + }, + "utf-8-validate": { + "optional": true + } + } + }, + "node_modules/youch": { + "version": "4.1.0-beta.10", + "resolved": "https://registry.npmjs.org/youch/-/youch-4.1.0-beta.10.tgz", + "integrity": "sha512-rLfVLB4FgQneDr0dv1oddCVZmKjcJ6yX6mS4pU82Mq/Dt9a3cLZQ62pDBL4AUO+uVrCvtWz3ZFUL2HFAFJ/BXQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/colors": "^4.1.5", + "@poppinss/dumper": "^0.6.4", + "@speed-highlight/core": "^1.2.7", + "cookie": "^1.0.2", + "youch-core": "^0.3.3" + } + }, + "node_modules/youch-core": { + "version": "0.3.3", + "resolved": "https://registry.npmjs.org/youch-core/-/youch-core-0.3.3.tgz", + "integrity": "sha512-ho7XuGjLaJ2hWHoK8yFnsUGy2Y5uDpqSTq1FkHLK4/oqKtyUU1AFbOOxY4IpC9f0fTLjwYbslUz0Po5BpD1wrA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@poppinss/exception": "^1.2.2", + "error-stack-parser-es": "^1.0.5" + } + } + } +} diff --git a/cf-worker/package.json b/cf-worker/package.json new file mode 100644 index 0000000000000000000000000000000000000000..18eb228fc722693a4749caf9afcdc410c2395a48 --- /dev/null +++ b/cf-worker/package.json @@ -0,0 +1,12 @@ +{ + "name": "ds2api-health-check", + "version": "1.0.0", + "private": true, + "scripts": { + "dev": "wrangler dev", + "deploy": "wrangler deploy" + }, + "devDependencies": { + "wrangler": "^4" + } +} diff --git a/cf-worker/src/index.js b/cf-worker/src/index.js new file mode 100644 index 0000000000000000000000000000000000000000..7b3ba99c9184a5e33c91d8a86adb238a93ccb029 --- /dev/null +++ b/cf-worker/src/index.js @@ -0,0 +1,103 @@ +/** + * Cloudflare Worker — 定时刷新 ds2api 代理健康状态 + * + * 部署步骤: + * 1. cd cf-worker && npm install + * 2. 设置 secrets: + * wrangler secret put DS2API_BASE_URL + * wrangler secret put ADMIN_KEY + * wrangler secret put VERCEL_BYPASS_TOKEN (如果开了 Vercel Deployment Protection) + * 3. wrangler deploy + * + * Cron 每 5 小时触发一次 (与 wrangler.toml 中的 crons 对应)。 + * 也可以手动 GET/POST https:///check 触发。 + */ + +const LOGIN_PATH = "/admin/login" +const CHECK_ALL_PATH = "/admin/proxies/check-all" + +function vercelHeaders(env) { + const headers = {} + const bypass = env.VERCEL_BYPASS_TOKEN + if (bypass) { + headers["x-vercel-protection-bypass"] = bypass + } + return headers +} + +async function login(baseURL, adminKey, extraHeaders) { + const res = await fetch(`${baseURL}${LOGIN_PATH}`, { + method: "POST", + headers: { "Content-Type": "application/json", ...extraHeaders }, + body: JSON.stringify({ admin_key: adminKey, expire_hours: 1 }), + }) + if (!res.ok) { + const text = await res.text() + throw new Error(`login failed (${res.status}): ${text}`) + } + const data = await res.json() + return data.token +} + +async function checkAll(baseURL, token, extraHeaders) { + const res = await fetch(`${baseURL}${CHECK_ALL_PATH}`, { + method: "POST", + headers: { + "Content-Type": "application/json", + Authorization: `Bearer ${token}`, + ...extraHeaders, + }, + }) + if (!res.ok) { + const text = await res.text() + throw new Error(`check-all failed (${res.status}): ${text}`) + } + return res.json() +} + +async function runCheck(env) { + const baseURL = (env.DS2API_BASE_URL || "").replace(/\/+$/, "") + const adminKey = env.ADMIN_KEY || "" + + if (!baseURL || !adminKey) { + throw new Error("DS2API_BASE_URL and ADMIN_KEY secrets must be set") + } + + const extraHeaders = vercelHeaders(env) + const token = await login(baseURL, adminKey, extraHeaders) + const result = await checkAll(baseURL, token, extraHeaders) + return result +} + +export default { + async scheduled(event, env, ctx) { + ctx.waitUntil( + runCheck(env) + .then((result) => { + const items = result.items || [] + const healthy = items.filter((i) => i.healthy && !i.disabled).length + const banned = items.filter((i) => i.disabled).length + console.log( + `proxy health check done: ${healthy}/${items.length} healthy, ${banned} banned` + ) + }) + .catch((err) => { + console.error(`proxy health check failed: ${err.message}`) + }) + ) + }, + + async fetch(request, env) { + const url = new URL(request.url) + if (url.pathname !== "/check") { + return new Response("not found", { status: 404 }) + } + + try { + const result = await runCheck(env) + return Response.json(result) + } catch (err) { + return Response.json({ error: err.message }, { status: 500 }) + } + }, +} diff --git a/cf-worker/wrangler.toml b/cf-worker/wrangler.toml new file mode 100644 index 0000000000000000000000000000000000000000..9eb276ce16f8f1620575a2d0c7d240e7ab9b398c --- /dev/null +++ b/cf-worker/wrangler.toml @@ -0,0 +1,9 @@ +name = "ds2api-health-check" +main = "src/index.js" +compatibility_date = "2024-12-01" + +[triggers] +crons = ["0 */5 * * *"] + +[vars] +# DS2API_BASE_URL = "https://your-app.vercel.app" diff --git a/cmd/ds2api-tests/main.go b/cmd/ds2api-tests/main.go new file mode 100644 index 0000000000000000000000000000000000000000..e2becd3aead20187960f645ffd8c06d1e0e197d6 --- /dev/null +++ b/cmd/ds2api-tests/main.go @@ -0,0 +1,37 @@ +package main + +import ( + "context" + "flag" + "fmt" + "os" + "time" + + "ds2api/internal/testsuite" +) + +func main() { + opts := testsuite.DefaultOptions() + var timeoutSeconds int + + flag.StringVar(&opts.ConfigPath, "config", opts.ConfigPath, "Path to config file (default: config.json)") + flag.StringVar(&opts.AdminKey, "admin-key", opts.AdminKey, "Admin key (default: DS2API_ADMIN_KEY or admin)") + flag.StringVar(&opts.OutputDir, "out", opts.OutputDir, "Output artifact directory") + flag.IntVar(&opts.Port, "port", opts.Port, "Server port (0 means auto-select free port)") + flag.IntVar(&timeoutSeconds, "timeout", int(opts.Timeout.Seconds()), "Per-request timeout in seconds") + flag.IntVar(&opts.Retries, "retries", opts.Retries, "Retry count for network/5xx requests") + flag.BoolVar(&opts.NoPreflight, "no-preflight", opts.NoPreflight, "Skip preflight checks") + flag.IntVar(&opts.MaxKeepRuns, "keep", opts.MaxKeepRuns, "Max test runs to keep (0 = keep all)") + flag.Parse() + + if timeoutSeconds <= 0 { + timeoutSeconds = 120 + } + opts.Timeout = time.Duration(timeoutSeconds) * time.Second + + if err := testsuite.Run(context.Background(), opts); err != nil { + _, _ = fmt.Fprintln(os.Stderr, err.Error()) + os.Exit(1) + } + _, _ = fmt.Fprintln(os.Stdout, "testsuite completed successfully") +} diff --git a/cmd/ds2api/main.go b/cmd/ds2api/main.go new file mode 100644 index 0000000000000000000000000000000000000000..a4ba77e90d5eea17f56eb00e80d268214c206287 --- /dev/null +++ b/cmd/ds2api/main.go @@ -0,0 +1,111 @@ +package main + +import ( + "context" + "fmt" + "net" + "net/http" + "os" + "os/signal" + "strings" + "syscall" + "time" + + "ds2api/internal/auth" + "ds2api/internal/config" + "ds2api/internal/server" + "ds2api/internal/webui" +) + +func main() { + if err := config.LoadDotEnv(); err != nil { + config.Logger.Warn("[dotenv] load failed", "error", err) + } + config.RefreshLogger() + webui.EnsureBuiltOnStartup() + _ = auth.AdminKey() + app, err := server.NewApp() + if err != nil { + config.Logger.Error("server initialization failed", "error", err) + os.Exit(1) + } + port := strings.TrimSpace(os.Getenv("PORT")) + if port == "" { + port = "5001" + } + + srv := &http.Server{ + Addr: "0.0.0.0:" + port, + Handler: app.Router, + ReadHeaderTimeout: 5 * time.Second, + } + localURL := fmt.Sprintf("http://127.0.0.1:%s", port) + lanIP := detectLANIPv4() + lanURL := "" + if lanIP != "" { + lanURL = fmt.Sprintf("http://%s:%s", lanIP, port) + } + + // Start server in a goroutine so we can listen for shutdown signals. + go func() { + if lanURL != "" { + config.Logger.Info("starting ds2api", "bind", srv.Addr, "port", port, "local_url", localURL, "lan_url", lanURL, "lan_ip", lanIP) + } else { + config.Logger.Info("starting ds2api", "bind", srv.Addr, "port", port, "local_url", localURL) + config.Logger.Warn("lan ip not detected; check active network interfaces") + } + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + config.Logger.Error("server stopped unexpectedly", "error", err) + os.Exit(1) + } + }() + + // Wait for interrupt signal (Ctrl+C / SIGTERM). + quit := make(chan os.Signal, 1) + signal.Notify(quit, os.Interrupt, syscall.SIGTERM) + sig := <-quit + config.Logger.Info("shutdown signal received", "signal", sig.String()) + + // Graceful shutdown: allow up to 10 seconds for in-flight requests to complete. + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + + if err := srv.Shutdown(ctx); err != nil { + config.Logger.Error("graceful shutdown failed, forcing exit", "error", err) + os.Exit(1) + } + config.Logger.Info("server gracefully stopped") +} + +func detectLANIPv4() string { + ifaces, err := net.Interfaces() + if err != nil { + return "" + } + for _, iface := range ifaces { + if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { + continue + } + addrs, err := iface.Addrs() + if err != nil { + continue + } + for _, addr := range addrs { + var ip net.IP + switch v := addr.(type) { + case *net.IPNet: + ip = v.IP + case *net.IPAddr: + ip = v.IP + default: + continue + } + ip = ip.To4() + if ip == nil || !ip.IsPrivate() { + continue + } + return ip.String() + } + } + return "" +} diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000000000000000000000000000000000000..b653716a2f0a229c11721b9e4e1d9aea55326c28 --- /dev/null +++ b/config.example.json @@ -0,0 +1,72 @@ +{ + "_comment": "DS2API 配置文件示例 - 复制为 config.json 使用", + "_doc": "详细文档: https://github.com/CJackHwang/ds2api", + "keys": [ + "your-api-key-1", + "your-api-key-2" + ], + "api_keys": [ + { + "key": "your-api-key-1", + "name": "主 API Key", + "remark": "给 OpenAI 客户端使用" + }, + { + "key": "your-api-key-2", + "name": "备用 API Key", + "remark": "压测或临时调试" + } + ], + "accounts": [ + { + "_comment": "邮箱登录方式", + "name": "主账号", + "remark": "优先用于生产流量", + "email": "example1@example.com", + "password": "your-password-1" + }, + { + "_comment": "邮箱登录方式 - 账号2", + "name": "备用账号", + "email": "example2@example.com", + "password": "your-password-2" + }, + { + "_comment": "手机号登录方式(中国大陆)", + "mobile": "12345678901", + "password": "your-password-3" + } + ], + "model_aliases": { + "gpt-4o": "deepseek-v4-flash", + "gpt-5.5": "deepseek-v4-flash", + "gpt-5.3-codex": "deepseek-v4-pro", + "o3": "deepseek-v4-pro" + }, + "responses": { + "store_ttl_seconds": 900 + }, + "current_input_file": { + "enabled": true, + "min_chars": 0 + }, + "thinking_injection": { + "enabled": false, + "prompt": "" + }, + "embeddings": { + "provider": "deterministic" + }, + "admin": { + "jwt_expire_hours": 24 + }, + "runtime": { + "account_max_inflight": 2, + "account_max_queue": 0, + "global_max_inflight": 0, + "token_refresh_interval_hours": 6 + }, + "auto_delete": { + "mode": "none" + } +} diff --git a/docker-compose.dev.yml b/docker-compose.dev.yml new file mode 100644 index 0000000000000000000000000000000000000000..c147349de05227026bca9355a9a3f3efa2130d6c --- /dev/null +++ b/docker-compose.dev.yml @@ -0,0 +1,33 @@ +# DS2API 开发环境配置 +# 特性: +# - 源代码挂载(热重载) +# - 调试日志级别 +# - 自动重启 +# +# 使用说明: +# docker-compose -f docker-compose.dev.yml up + +services: + ds2api: + build: + context: . + target: go-builder + image: ds2api:dev + container_name: ds2api-dev + command: ["go", "run", "./cmd/ds2api"] + ports: + # Host port is configurable via DS2API_HOST_PORT; container port stays fixed at 5001. + - "${DS2API_HOST_PORT:-6011}:5001" + env_file: + - .env + environment: + - HOST=0.0.0.0 + - LOG_LEVEL=DEBUG + volumes: + # 源代码挂载(开发时实时生效) + - ./:/app + # 配置文件挂载(便于本地修改) + - ./config.json:/app/config.json + restart: "no" + stdin_open: true + tty: true diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000000000000000000000000000000000000..571829ad5b6123006562d13b762482fd0ae99554 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +services: + ds2api: + image: ghcr.io/cjackhwang/ds2api:latest + container_name: ds2api + restart: always + env_file: + - .env + ports: + # Host port is configurable via DS2API_HOST_PORT; container port stays fixed at 5001. + - "${DS2API_HOST_PORT:-6011}:5001" + volumes: + - ./config.json:/data/config.json # 配置文件(持久化推荐路径) + environment: + - TZ=Asia/Shanghai + - LOG_LEVEL=INFO + - DS2API_ADMIN_KEY=${DS2API_ADMIN_KEY:-ds2api} + - DS2API_CONFIG_PATH=/data/config.json diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000000000000000000000000000000000000..2215f0d153307544673b1da79402bf5afe296757 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,22 @@ +#!/bin/bash +set -e + +# Fix /data permissions for HF Spaces Persistent Storage +if [ -d /data ]; then + chown -R ds2api:ds2api /data 2>/dev/null || true +fi + +# Create config file if not exists or is empty (with empty JSON object) +if [ ! -f /data/config.json ] || [ ! -s /data/config.json ]; then + echo {} > /data/config.json + chown ds2api:ds2api /data/config.json 2>/dev/null || true +fi + +# Create chat history file if not exists or is empty +if [ ! -f /data/chat_history.json ] || [ ! -s /data/chat_history.json ]; then + echo {} > /data/chat_history.json + chown ds2api:ds2api /data/chat_history.json 2>/dev/null || true +fi + +# Start the application as ds2api user +exec gosu ds2api /usr/local/bin/ds2api diff --git a/go.mod b/go.mod new file mode 100644 index 0000000000000000000000000000000000000000..9ce4793c4b87c312081b741f5ed862be9310d113 --- /dev/null +++ b/go.mod @@ -0,0 +1,27 @@ +module ds2api + +go 1.26.0 + +require ( + github.com/andybalholm/brotli v1.2.1 + github.com/go-chi/chi/v5 v5.2.5 + github.com/google/uuid v1.6.0 + github.com/hupe1980/go-tiktoken v0.0.10 + github.com/refraction-networking/utls v1.8.2 + github.com/router-for-me/CLIProxyAPI/v6 v6.9.14 +) + +require github.com/dlclark/regexp2 v1.11.5 // indirect + +require ( + github.com/klauspost/compress v1.18.5 // indirect + github.com/sirupsen/logrus v1.9.4 // indirect + github.com/tidwall/gjson v1.18.0 // indirect + github.com/tidwall/match v1.2.0 // indirect + github.com/tidwall/pretty v1.2.1 // indirect + github.com/tidwall/sjson v1.2.5 // indirect + golang.org/x/crypto v0.49.0 // indirect + golang.org/x/net v0.52.0 + golang.org/x/sys v0.42.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000000000000000000000000000000000000..2936ba8ff7e0c58ff1777d43ed07e61e17a3cdf1 --- /dev/null +++ b/go.sum @@ -0,0 +1,49 @@ +github.com/andybalholm/brotli v1.2.1 h1:R+f5xP285VArJDRgowrfb9DqL18yVK0gKAW/F+eTWro= +github.com/andybalholm/brotli v1.2.1/go.mod h1:rzTDkvFWvIrjDXZHkuS16NPggd91W3kUSvPlQ1pLaKY= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ= +github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8= +github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug= +github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hupe1980/go-tiktoken v0.0.10 h1:m6phOJaGyctqWdGIgwn9X8AfJvaG74tnQoDL+ntOUEQ= +github.com/hupe1980/go-tiktoken v0.0.10/go.mod h1:NME6d8hrE+Jo+kLUZHhXShYV8e40hYkm4BbSLQKtvAo= +github.com/klauspost/compress v1.18.5 h1:/h1gH5Ce+VWNLSWqPzOVn6XBO+vJbCNGvjoaGBFW2IE= +github.com/klauspost/compress v1.18.5/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/refraction-networking/utls v1.8.2 h1:j4Q1gJj0xngdeH+Ox/qND11aEfhpgoEvV+S9iJ2IdQo= +github.com/refraction-networking/utls v1.8.2/go.mod h1:jkSOEkLqn+S/jtpEHPOsVv/4V4EVnelwbMQl4vCWXAM= +github.com/router-for-me/CLIProxyAPI/v6 v6.9.14 h1:XItUHrPGE9E5xTeZIPjKGmKqfEs1AZbxl1RPfO5xtrc= +github.com/router-for-me/CLIProxyAPI/v6 v6.9.14/go.mod h1:P1jsIPFXorYGuS2N/3BlZYkpRKi/z7+oR3+1tdG0u4k= +github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= +github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/match v1.2.0 h1:0pt8FlkOwjN2fPt4bIl4BoNxb98gGHN2ObFEDkrfZnM= +github.com/tidwall/match v1.2.0/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= +github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU= +github.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E= +golang.org/x/crypto v0.49.0 h1:+Ng2ULVvLHnJ/ZFEq4KdcDd/cfjrrjjNSXNzxg0Y4U4= +golang.org/x/crypto v0.49.0/go.mod h1:ErX4dUh2UM+CFYiXZRTcMpEcN8b/1gxEuv3nODoYtCA= +golang.org/x/net v0.52.0 h1:He/TN1l0e4mmR3QqHMT2Xab3Aj3L9qjbhRm78/6jrW0= +golang.org/x/net v0.52.0/go.mod h1:R1MAz7uMZxVMualyPXb+VaqGSa3LIaUqk0eEt3w36Sw= +golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo= +golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.35.0 h1:JOVx6vVDFokkpaq1AEptVzLTpDe9KGpj5tR4/X+ybL8= +golang.org/x/text v0.35.0/go.mod h1:khi/HExzZJ2pGnjenulevKNX1W67CUy0AsXcNubPGCA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/account/pool_acquire.go b/internal/account/pool_acquire.go new file mode 100644 index 0000000000000000000000000000000000000000..6d1ec7dea9fd5c22a16283b2d39a6e69fd9ee023 --- /dev/null +++ b/internal/account/pool_acquire.go @@ -0,0 +1,99 @@ +package account + +import ( + "context" + + "ds2api/internal/config" +) + +func (p *Pool) Acquire(target string, exclude map[string]bool) (config.Account, bool) { + p.mu.Lock() + defer p.mu.Unlock() + return p.acquireLocked(target, normalizeExclude(exclude)) +} + +func (p *Pool) AcquireWait(ctx context.Context, target string, exclude map[string]bool) (config.Account, bool) { + if ctx == nil { + ctx = context.Background() + } + exclude = normalizeExclude(exclude) + for { + if ctx.Err() != nil { + return config.Account{}, false + } + + p.mu.Lock() + if acc, ok := p.acquireLocked(target, exclude); ok { + p.mu.Unlock() + return acc, true + } + if !p.canQueueLocked(target, exclude) { + p.mu.Unlock() + return config.Account{}, false + } + waiter := make(chan struct{}) + p.waiters = append(p.waiters, waiter) + p.mu.Unlock() + + select { + case <-ctx.Done(): + p.mu.Lock() + p.removeWaiterLocked(waiter) + p.mu.Unlock() + return config.Account{}, false + case <-waiter: + } + } +} + +func (p *Pool) acquireLocked(target string, exclude map[string]bool) (config.Account, bool) { + if target != "" { + if exclude[target] || !p.canAcquireIDLocked(target) { + return config.Account{}, false + } + acc, ok := p.store.FindAccount(target) + if !ok { + return config.Account{}, false + } + p.inUse[target]++ + p.bumpQueue(target) + return acc, true + } + + return p.tryAcquire(exclude) +} + +func (p *Pool) tryAcquire(exclude map[string]bool) (config.Account, bool) { + for i := 0; i < len(p.queue); i++ { + id := p.queue[i] + if exclude[id] || !p.canAcquireIDLocked(id) { + continue + } + acc, ok := p.store.FindAccount(id) + if !ok { + continue + } + p.inUse[id]++ + p.bumpQueue(id) + return acc, true + } + return config.Account{}, false +} + +func (p *Pool) bumpQueue(accountID string) { + for i, id := range p.queue { + if id != accountID { + continue + } + p.queue = append(p.queue[:i], p.queue[i+1:]...) + p.queue = append(p.queue, accountID) + return + } +} + +func normalizeExclude(exclude map[string]bool) map[string]bool { + if exclude == nil { + return map[string]bool{} + } + return exclude +} diff --git a/internal/account/pool_core.go b/internal/account/pool_core.go new file mode 100644 index 0000000000000000000000000000000000000000..90e2594d0f391801b9a9614e3383386bb6fe2b98 --- /dev/null +++ b/internal/account/pool_core.go @@ -0,0 +1,132 @@ +package account + +import ( + "sort" + "sync" + + "ds2api/internal/config" +) + +type Pool struct { + store *config.Store + mu sync.Mutex + queue []string + inUse map[string]int + waiters []chan struct{} + maxInflightPerAccount int + recommendedConcurrency int + maxQueueSize int + globalMaxInflight int +} + +func NewPool(store *config.Store) *Pool { + maxPer := 2 + if store != nil { + maxPer = store.RuntimeAccountMaxInflight() + } + p := &Pool{ + store: store, + inUse: map[string]int{}, + maxInflightPerAccount: maxPer, + } + p.Reset() + return p +} + +func (p *Pool) Reset() { + accounts := p.store.Accounts() + sort.SliceStable(accounts, func(i, j int) bool { + iHas := accounts[i].Token != "" + jHas := accounts[j].Token != "" + if iHas == jHas { + return i < j + } + return iHas + }) + ids := make([]string, 0, len(accounts)) + for _, a := range accounts { + id := a.Identifier() + if id != "" { + ids = append(ids, id) + } + } + if p.store != nil { + p.maxInflightPerAccount = p.store.RuntimeAccountMaxInflight() + } else { + p.maxInflightPerAccount = maxInflightFromEnv() + } + recommended := defaultRecommendedConcurrency(len(ids), p.maxInflightPerAccount) + queueLimit := maxQueueFromEnv(recommended) + globalLimit := recommended + if p.store != nil { + queueLimit = p.store.RuntimeAccountMaxQueue(recommended) + globalLimit = p.store.RuntimeGlobalMaxInflight(recommended) + } + p.mu.Lock() + defer p.mu.Unlock() + p.drainWaitersLocked() + p.queue = ids + p.inUse = map[string]int{} + p.recommendedConcurrency = recommended + p.maxQueueSize = queueLimit + p.globalMaxInflight = globalLimit + config.Logger.Info( + "[init_account_queue] initialized", + "total", len(ids), + "max_inflight_per_account", p.maxInflightPerAccount, + "global_max_inflight", p.globalMaxInflight, + "recommended_concurrency", p.recommendedConcurrency, + "max_queue_size", p.maxQueueSize, + ) +} + +func (p *Pool) Release(accountID string) { + if accountID == "" { + return + } + p.mu.Lock() + defer p.mu.Unlock() + count := p.inUse[accountID] + if count <= 0 { + return + } + if count == 1 { + delete(p.inUse, accountID) + p.notifyWaiterLocked() + return + } + p.inUse[accountID] = count - 1 + p.notifyWaiterLocked() +} + +func (p *Pool) Status() map[string]any { + p.mu.Lock() + defer p.mu.Unlock() + available := make([]string, 0, len(p.queue)) + inUseAccounts := make([]string, 0, len(p.inUse)) + inUseSlots := 0 + for _, id := range p.queue { + if p.inUse[id] < p.maxInflightPerAccount { + available = append(available, id) + } + } + for id, count := range p.inUse { + if count > 0 { + inUseAccounts = append(inUseAccounts, id) + inUseSlots += count + } + } + sort.Strings(inUseAccounts) + return map[string]any{ + "available": len(available), + "in_use": inUseSlots, + "total": len(p.store.Accounts()), + "available_accounts": available, + "in_use_accounts": inUseAccounts, + "max_inflight_per_account": p.maxInflightPerAccount, + "global_max_inflight": p.globalMaxInflight, + "recommended_concurrency": p.recommendedConcurrency, + "waiting": len(p.waiters), + "max_queue_size": p.maxQueueSize, + } +} diff --git a/internal/account/pool_edge_test.go b/internal/account/pool_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d8bff26677c75b6beb167e4b80680b3d04ab905f --- /dev/null +++ b/internal/account/pool_edge_test.go @@ -0,0 +1,232 @@ +package account + +import ( + "context" + "sync" + "testing" + "time" + + "ds2api/internal/config" +) + +// ─── Pool edge cases ───────────────────────────────────────────────── + +func TestPoolEmptyNoAccounts(t *testing.T) { + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", "2") + t.Setenv("DS2API_ACCOUNT_MAX_QUEUE", "") + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[]}`) + pool := NewPool(config.LoadStore()) + if _, ok := pool.Acquire("", nil); ok { + t.Fatal("expected acquire to fail with no accounts") + } + status := pool.Status() + if total, ok := status["total"].(int); !ok || total != 0 { + t.Fatalf("unexpected total: %#v", status["total"]) + } +} + +func TestPoolReleaseNonExistentAccount(t *testing.T) { + pool := newPoolForTest(t, "2") + pool.Release("nonexistent@example.com") // should not panic +} + +func TestPoolReleaseAlreadyReleased(t *testing.T) { + pool := newPoolForTest(t, "2") + acc, ok := pool.Acquire("", nil) + if !ok { + t.Fatal("expected acquire success") + } + pool.Release(acc.Identifier()) + pool.Release(acc.Identifier()) // double release should not panic +} + +func TestPoolAcquireTargetNotFound(t *testing.T) { + pool := newPoolForTest(t, "2") + if _, ok := pool.Acquire("nonexistent@example.com", nil); ok { + t.Fatal("expected acquire to fail for non-existent target") + } +} + +func TestPoolAcquireWithExclusionList(t *testing.T) { + pool := newPoolForTest(t, "2") + acc, ok := pool.Acquire("", map[string]bool{"acc1@example.com": true}) + if !ok { + t.Fatal("expected acquire success with exclusion") + } + if acc.Identifier() != "acc2@example.com" { + t.Fatalf("expected acc2 when acc1 excluded, got %q", acc.Identifier()) + } + pool.Release(acc.Identifier()) +} + +func TestPoolAcquireAllExcluded(t *testing.T) { + pool := newPoolForTest(t, "2") + if _, ok := pool.Acquire("", map[string]bool{ + "acc1@example.com": true, + "acc2@example.com": true, + }); ok { + t.Fatal("expected acquire to fail when all accounts excluded") + } +} + +func TestPoolStatusFields(t *testing.T) { + pool := newPoolForTest(t, "2") + status := pool.Status() + + // Check all expected fields are present + for _, key := range []string{"total", "available", "max_inflight_per_account", "recommended_concurrency", "available_accounts", "in_use_accounts", "waiting", "max_queue_size"} { + if _, ok := status[key]; !ok { + t.Fatalf("missing status field: %s", key) + } + } +} + +func TestPoolStatusAccountDetails(t *testing.T) { + pool := newPoolForTest(t, "2") + acc, _ := pool.Acquire("acc1@example.com", nil) + + status := pool.Status() + inUseAccounts, ok := status["in_use_accounts"].([]string) + if !ok { + t.Fatalf("unexpected in_use_accounts type: %T", status["in_use_accounts"]) + } + found := false + for _, id := range inUseAccounts { + if id == "acc1@example.com" { + found = true + break + } + } + if !found { + t.Fatalf("expected acc1 in in_use_accounts, got %v", inUseAccounts) + } + if status["in_use"] != 1 { + t.Fatalf("expected 1 in_use, got %v", status["in_use"]) + } + + pool.Release(acc.Identifier()) +} + +func TestPoolAcquireWaitContextCancelled(t *testing.T) { + pool := newSingleAccountPoolForTest(t, "1") + // Exhaust the pool + first, ok := pool.Acquire("", nil) + if !ok { + t.Fatal("expected first acquire to succeed") + } + + ctx, cancel := context.WithCancel(context.Background()) + + var wg sync.WaitGroup + wg.Add(1) + var waitOK bool + go func() { + defer wg.Done() + _, waitOK = pool.AcquireWait(ctx, "", nil) + }() + + // Wait until queued + waitForWaitingCount(t, pool, 1) + + // Cancel context + cancel() + + wg.Wait() + if waitOK { + t.Fatal("expected acquire to fail after context cancellation") + } + + pool.Release(first.Identifier()) +} + +func TestPoolAcquireWaitTargetAccount(t *testing.T) { + pool := newPoolForTest(t, "1") + // Exhaust acc1 + acc1, ok := pool.Acquire("acc1@example.com", nil) + if !ok { + t.Fatal("expected acquire acc1 success") + } + + // Acquire acc2 directly (should succeed since acc2 is free) + ctx := context.Background() + acc2, ok := pool.AcquireWait(ctx, "acc2@example.com", nil) + if !ok { + t.Fatal("expected acquire acc2 success via AcquireWait") + } + if acc2.Identifier() != "acc2@example.com" { + t.Fatalf("expected acc2, got %q", acc2.Identifier()) + } + + pool.Release(acc1.Identifier()) + pool.Release(acc2.Identifier()) +} + +func TestPoolMaxQueueSizeOverride(t *testing.T) { + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", "1") + t.Setenv("DS2API_ACCOUNT_MAX_QUEUE", "5") + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[{"email":"acc1@example.com","token":"t1"}]}`) + pool := NewPool(config.LoadStore()) + status := pool.Status() + if got, ok := status["max_queue_size"].(int); !ok || got != 5 { + t.Fatalf("expected max_queue_size=5, got %#v", status["max_queue_size"]) + } +} + +func TestPoolMultipleAcquireReleaseCycles(t *testing.T) { + pool := newSingleAccountPoolForTest(t, "1") + for i := 0; i < 10; i++ { + acc, ok := pool.Acquire("", nil) + if !ok { + t.Fatalf("acquire failed at cycle %d", i) + } + pool.Release(acc.Identifier()) + } +} + +func TestPoolConcurrentAcquireWait(t *testing.T) { + pool := newSingleAccountPoolForTest(t, "1") + first, ok := pool.Acquire("", nil) + if !ok { + t.Fatal("expected first acquire success") + } + + const waiters = 3 + results := make(chan bool, waiters) + + for i := 0; i < waiters; i++ { + go func() { + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + _, ok := pool.AcquireWait(ctx, "", nil) + results <- ok + }() + } + + // Wait for all to be queued (only 1 can queue) + time.Sleep(50 * time.Millisecond) + + // Release and allow queued requests to proceed + pool.Release(first.Identifier()) + + successCount := 0 + timeoutCount := 0 + for i := 0; i < waiters; i++ { + select { + case ok := <-results: + if ok { + successCount++ + // Release for next waiter + pool.Release("acc1@example.com") + } else { + timeoutCount++ + } + case <-time.After(3 * time.Second): + t.Fatal("timed out waiting for results") + } + } + + // At least 1 should succeed; 2 may fail due to queue limit + if successCount < 1 { + t.Fatalf("expected at least 1 success, got success=%d timeout=%d", successCount, timeoutCount) + } +} diff --git a/internal/account/pool_limits.go b/internal/account/pool_limits.go new file mode 100644 index 0000000000000000000000000000000000000000..2ddbaf4be80a97b6af79fbc22c0e9401596067c1 --- /dev/null +++ b/internal/account/pool_limits.go @@ -0,0 +1,81 @@ +package account + +import ( + "os" + "strconv" + "strings" +) + +func (p *Pool) ApplyRuntimeLimits(maxInflightPerAccount, maxQueueSize, globalMaxInflight int) { + if maxInflightPerAccount <= 0 { + maxInflightPerAccount = 1 + } + if maxQueueSize < 0 { + maxQueueSize = 0 + } + if globalMaxInflight <= 0 { + globalMaxInflight = maxInflightPerAccount * len(p.store.Accounts()) + if globalMaxInflight <= 0 { + globalMaxInflight = maxInflightPerAccount + } + } + p.mu.Lock() + defer p.mu.Unlock() + p.maxInflightPerAccount = maxInflightPerAccount + p.maxQueueSize = maxQueueSize + p.globalMaxInflight = globalMaxInflight + p.recommendedConcurrency = defaultRecommendedConcurrency(len(p.queue), p.maxInflightPerAccount) + p.notifyWaiterLocked() +} + +func maxInflightFromEnv() int { + if raw := strings.TrimSpace(os.Getenv("DS2API_ACCOUNT_MAX_INFLIGHT")); raw != "" { + if n, err := strconv.Atoi(raw); err == nil && n > 0 { + return n + } + } + return 2 +} + +func defaultRecommendedConcurrency(accountCount, maxInflightPerAccount int) int { + if accountCount <= 0 { + return 0 + } + if maxInflightPerAccount <= 0 { + maxInflightPerAccount = 2 + } + return accountCount * maxInflightPerAccount +} + +func maxQueueFromEnv(defaultSize int) int { + if raw := strings.TrimSpace(os.Getenv("DS2API_ACCOUNT_MAX_QUEUE")); raw != "" { + if n, err := strconv.Atoi(raw); err == nil && n >= 0 { + return n + } + } + if defaultSize < 0 { + return 0 + } + return defaultSize +} + +func (p *Pool) canAcquireIDLocked(accountID string) bool { + if accountID == "" { + return false + } + if p.inUse[accountID] >= p.maxInflightPerAccount { + return false + } + if p.globalMaxInflight > 0 && p.currentInUseLocked() >= p.globalMaxInflight { + return false + } + return true +} + +func (p *Pool) currentInUseLocked() int { + total := 0 + for _, n := range p.inUse { + total += n + } + return total +} diff --git a/internal/account/pool_test.go b/internal/account/pool_test.go new file mode 100644 index 0000000000000000000000000000000000000000..279cef4cb71f9a1a172db8fee9e26af79ded468b --- /dev/null +++ b/internal/account/pool_test.go @@ -0,0 +1,313 @@ +package account + +import ( + "context" + "sync" + "testing" + "time" + + "ds2api/internal/config" +) + +func newPoolForTest(t *testing.T, maxInflight string) *Pool { + t.Helper() + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", maxInflight) + t.Setenv("DS2API_ACCOUNT_MAX_QUEUE", "") + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[ + {"email":"acc1@example.com","token":"token1"}, + {"email":"acc2@example.com","token":"token2"} + ] + }`) + store := config.LoadStore() + return NewPool(store) +} + +func newSingleAccountPoolForTest(t *testing.T, maxInflight string) *Pool { + t.Helper() + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", maxInflight) + t.Setenv("DS2API_ACCOUNT_MAX_QUEUE", "") + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[{"email":"acc1@example.com","token":"token1"}] + }`) + return NewPool(config.LoadStore()) +} + +func waitForWaitingCount(t *testing.T, pool *Pool, want int) { + t.Helper() + deadline := time.Now().Add(800 * time.Millisecond) + for time.Now().Before(deadline) { + status := pool.Status() + if got, ok := status["waiting"].(int); ok && got == want { + return + } + time.Sleep(10 * time.Millisecond) + } + status := pool.Status() + t.Fatalf("waiting count did not reach %d, current status=%v", want, status) +} + +func TestPoolRoundRobinWithConcurrentSlots(t *testing.T) { + pool := newPoolForTest(t, "2") + + order := make([]string, 0, 4) + for i := 0; i < 4; i++ { + acc, ok := pool.Acquire("", nil) + if !ok { + t.Fatalf("expected acquire success at step %d", i+1) + } + order = append(order, acc.Identifier()) + } + want := []string{"acc1@example.com", "acc2@example.com", "acc1@example.com", "acc2@example.com"} + for i := range want { + if order[i] != want[i] { + t.Fatalf("unexpected order at %d: got %q want %q (full=%v)", i, order[i], want[i], order) + } + } + + if _, ok := pool.Acquire("", nil); ok { + t.Fatalf("expected acquire to fail when all inflight slots are occupied") + } + + pool.Release("acc1@example.com") + acc, ok := pool.Acquire("", nil) + if !ok || acc.Identifier() != "acc1@example.com" { + t.Fatalf("expected reacquire acc1 after releasing one slot, got ok=%v id=%q", ok, acc.Identifier()) + } +} + +func TestPoolTargetAccountInflightLimit(t *testing.T) { + pool := newPoolForTest(t, "2") + + for i := 0; i < 2; i++ { + if _, ok := pool.Acquire("acc1@example.com", nil); !ok { + t.Fatalf("expected target acquire success at step %d", i+1) + } + } + if _, ok := pool.Acquire("acc1@example.com", nil); ok { + t.Fatalf("expected third acquire on same target to fail due to inflight limit") + } +} + +func TestPoolConcurrentAcquireDistribution(t *testing.T) { + pool := newPoolForTest(t, "2") + + start := make(chan struct{}) + results := make(chan string, 6) + var wg sync.WaitGroup + for i := 0; i < 6; i++ { + wg.Add(1) + go func() { + defer wg.Done() + <-start + acc, ok := pool.Acquire("", nil) + if !ok { + results <- "FAIL" + return + } + results <- acc.Identifier() + }() + } + + close(start) + wg.Wait() + close(results) + + success := 0 + fail := 0 + perAccount := map[string]int{} + for id := range results { + if id == "FAIL" { + fail++ + continue + } + success++ + perAccount[id]++ + } + if success != 4 || fail != 2 { + t.Fatalf("unexpected concurrent acquire result: success=%d fail=%d perAccount=%v", success, fail, perAccount) + } + for id, n := range perAccount { + if n > 2 { + t.Fatalf("account %s exceeded inflight limit: %d", id, n) + } + } +} + +func TestPoolStatusRecommendedConcurrencyDefault(t *testing.T) { + pool := newPoolForTest(t, "") + status := pool.Status() + + if got, ok := status["max_inflight_per_account"].(int); !ok || got != 2 { + t.Fatalf("unexpected max_inflight_per_account: %#v", status["max_inflight_per_account"]) + } + if got, ok := status["recommended_concurrency"].(int); !ok || got != 4 { + t.Fatalf("unexpected recommended_concurrency: %#v", status["recommended_concurrency"]) + } + if got, ok := status["max_queue_size"].(int); !ok || got != 4 { + t.Fatalf("unexpected max_queue_size: %#v", status["max_queue_size"]) + } +} + +func TestPoolStatusRecommendedConcurrencyRespectsOverride(t *testing.T) { + pool := newPoolForTest(t, "3") + status := pool.Status() + + if got, ok := status["max_inflight_per_account"].(int); !ok || got != 3 { + t.Fatalf("unexpected max_inflight_per_account: %#v", status["max_inflight_per_account"]) + } + if got, ok := status["recommended_concurrency"].(int); !ok || got != 6 { + t.Fatalf("unexpected recommended_concurrency: %#v", status["recommended_concurrency"]) + } + if got, ok := status["max_queue_size"].(int); !ok || got != 6 { + t.Fatalf("unexpected max_queue_size: %#v", status["max_queue_size"]) + } +} + +func TestPoolGlobalMaxInflightEnv(t *testing.T) { + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", "1") + t.Setenv("DS2API_GLOBAL_MAX_INFLIGHT", "4") + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[ + {"email":"acc1@example.com","token":"token1"}, + {"email":"acc2@example.com","token":"token2"} + ] + }`) + + pool := NewPool(config.LoadStore()) + status := pool.Status() + if got, ok := status["global_max_inflight"].(int); !ok || got != 4 { + t.Fatalf("unexpected global_max_inflight: %#v", status["global_max_inflight"]) + } + if got, ok := status["max_inflight_per_account"].(int); !ok || got != 1 { + t.Fatalf("unexpected max_inflight_per_account: %#v", status["max_inflight_per_account"]) + } + if got, ok := status["recommended_concurrency"].(int); !ok || got != 2 { + t.Fatalf("unexpected recommended_concurrency: %#v", status["recommended_concurrency"]) + } +} + +func TestPoolDropsLegacyTokenOnlyAccountOnLoad(t *testing.T) { + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", "1") + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[{"token":"token-only-account"}] + }`) + + pool := NewPool(config.LoadStore()) + status := pool.Status() + if got, ok := status["total"].(int); !ok || got != 0 { + t.Fatalf("unexpected total in pool status: %#v", status["total"]) + } + if got, ok := status["available"].(int); !ok || got != 0 { + t.Fatalf("unexpected available in pool status: %#v", status["available"]) + } + + if _, ok := pool.Acquire("", nil); ok { + t.Fatalf("expected acquire to fail for token-only account") + } +} + +func TestPoolAcquireRotatesIntoTokenlessAccounts(t *testing.T) { + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", "1") + t.Setenv("DS2API_ACCOUNT_MAX_QUEUE", "") + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[ + {"email":"acc1@example.com","token":"token1"}, + {"email":"acc2@example.com","token":""}, + {"email":"acc3@example.com","token":""} + ] + }`) + + pool := NewPool(config.LoadStore()) + for i, want := range []string{"acc1@example.com", "acc2@example.com", "acc3@example.com"} { + acc, ok := pool.Acquire("", nil) + if !ok { + t.Fatalf("expected acquire success at step %d", i+1) + } + if got := acc.Identifier(); got != want { + t.Fatalf("unexpected account at step %d: got %q want %q", i+1, got, want) + } + pool.Release(acc.Identifier()) + } +} + +func TestPoolAcquireWaitQueuesAndSucceedsAfterRelease(t *testing.T) { + pool := newSingleAccountPoolForTest(t, "1") + first, ok := pool.Acquire("", nil) + if !ok { + t.Fatal("expected first acquire to succeed") + } + + type result struct { + id string + ok bool + } + resCh := make(chan result, 1) + ctx, cancel := context.WithTimeout(context.Background(), time.Second) + defer cancel() + go func() { + acc, ok := pool.AcquireWait(ctx, "", nil) + resCh <- result{id: acc.Identifier(), ok: ok} + }() + + waitForWaitingCount(t, pool, 1) + pool.Release(first.Identifier()) + + select { + case res := <-resCh: + if !res.ok { + t.Fatal("expected queued acquire to succeed after release") + } + if res.id != "acc1@example.com" { + t.Fatalf("unexpected account id from queued acquire: %q", res.id) + } + case <-time.After(time.Second): + t.Fatal("timed out waiting for queued acquire result") + } +} + +func TestPoolAcquireWaitQueueLimitReturnsFalse(t *testing.T) { + pool := newSingleAccountPoolForTest(t, "1") + first, ok := pool.Acquire("", nil) + if !ok { + t.Fatal("expected first acquire to succeed") + } + + type result struct { + id string + ok bool + } + firstWaiter := make(chan result, 1) + ctx1, cancel1 := context.WithTimeout(context.Background(), 1200*time.Millisecond) + defer cancel1() + go func() { + acc, ok := pool.AcquireWait(ctx1, "", nil) + firstWaiter <- result{id: acc.Identifier(), ok: ok} + }() + waitForWaitingCount(t, pool, 1) + + ctx2, cancel2 := context.WithTimeout(context.Background(), 500*time.Millisecond) + defer cancel2() + start := time.Now() + if _, ok := pool.AcquireWait(ctx2, "", nil); ok { + t.Fatal("expected second queued acquire to fail when queue is full") + } + if time.Since(start) > 120*time.Millisecond { + t.Fatalf("queue-full acquire should fail fast, took %s", time.Since(start)) + } + + pool.Release(first.Identifier()) + select { + case res := <-firstWaiter: + if !res.ok { + t.Fatal("expected first queued acquire to succeed after release") + } + case <-time.After(time.Second): + t.Fatal("timed out waiting for first queued acquire") + } +} diff --git a/internal/account/pool_waiters.go b/internal/account/pool_waiters.go new file mode 100644 index 0000000000000000000000000000000000000000..40bd146891573e124d53c3760275c3633fcd8682 --- /dev/null +++ b/internal/account/pool_waiters.go @@ -0,0 +1,43 @@ +package account + +func (p *Pool) canQueueLocked(target string, exclude map[string]bool) bool { + if target != "" { + if exclude[target] { + return false + } + if _, ok := p.store.FindAccount(target); !ok { + return false + } + } + if p.maxQueueSize <= 0 { + return false + } + return len(p.waiters) < p.maxQueueSize +} + +func (p *Pool) notifyWaiterLocked() { + if len(p.waiters) == 0 { + return + } + waiter := p.waiters[0] + p.waiters = p.waiters[1:] + close(waiter) +} + +func (p *Pool) removeWaiterLocked(waiter chan struct{}) bool { + for i, w := range p.waiters { + if w != waiter { + continue + } + p.waiters = append(p.waiters[:i], p.waiters[i+1:]...) + return true + } + return false +} + +func (p *Pool) drainWaitersLocked() { + for _, waiter := range p.waiters { + close(waiter) + } + p.waiters = nil +} diff --git a/internal/assistantturn/stream.go b/internal/assistantturn/stream.go new file mode 100644 index 0000000000000000000000000000000000000000..77c398d0780591a3f5d05adfc2047297ce310117 --- /dev/null +++ b/internal/assistantturn/stream.go @@ -0,0 +1,64 @@ +package assistantturn + +import ( + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/sse" +) + +type StreamEventType string + +const ( + StreamEventTextDelta StreamEventType = "text_delta" + StreamEventThinkingDelta StreamEventType = "thinking_delta" + StreamEventToolCall StreamEventType = "tool_call" + StreamEventDone StreamEventType = "done" + StreamEventError StreamEventType = "error" + StreamEventPing StreamEventType = "ping" +) + +type StreamEvent struct { + Type StreamEventType + Text string + Thinking string + ToolCall any + Error *OutputError + Usage *Usage +} + +type Accumulator struct { + inner shared.StreamAccumulator +} + +type AccumulatorOptions struct { + ThinkingEnabled bool + SearchEnabled bool + StripReferenceMarkers bool +} + +func NewAccumulator(opts AccumulatorOptions) *Accumulator { + return &Accumulator{ + inner: shared.StreamAccumulator{ + ThinkingEnabled: opts.ThinkingEnabled, + SearchEnabled: opts.SearchEnabled, + StripReferenceMarkers: opts.StripReferenceMarkers, + }, + } +} + +func (a *Accumulator) Apply(parsed sse.LineResult) shared.StreamAccumulatorResult { + if a == nil { + return shared.StreamAccumulatorResult{} + } + return a.inner.Apply(parsed) +} + +func (a *Accumulator) Snapshot() (rawText, text, rawThinking, thinking, detectionThinking string) { + if a == nil { + return "", "", "", "", "" + } + return a.inner.RawText.String(), + a.inner.Text.String(), + a.inner.RawThinking.String(), + a.inner.Thinking.String(), + a.inner.ToolDetectionThinking.String() +} diff --git a/internal/assistantturn/turn.go b/internal/assistantturn/turn.go new file mode 100644 index 0000000000000000000000000000000000000000..b329e65b69d6584a005b807b9f8da433e03b425e --- /dev/null +++ b/internal/assistantturn/turn.go @@ -0,0 +1,285 @@ +package assistantturn + +import ( + "net/http" + "strings" + + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" + "ds2api/internal/sse" + "ds2api/internal/toolcall" + "ds2api/internal/util" +) + +type StopReason string + +const ( + StopReasonStop StopReason = "stop" + StopReasonToolCalls StopReason = "tool_calls" + StopReasonContentFilter StopReason = "content_filter" + StopReasonError StopReason = "error" +) + +type Usage struct { + InputTokens int + OutputTokens int + ReasoningTokens int + TotalTokens int +} + +type OutputError struct { + Status int + Message string + Code string +} + +type Turn struct { + Model string + Prompt string + RawText string + RawThinking string + DetectionThinking string + Text string + Thinking string + ToolCalls []toolcall.ParsedToolCall + ParsedToolCalls toolcall.ToolCallParseResult + CitationLinks map[int]string + ContentFilter bool + ResponseMessageID int + StopReason StopReason + Usage Usage + Error *OutputError +} + +type FinalizeOptions struct { + AlreadyEmittedToolCalls bool +} + +type FinalOutcome struct { + FinishReason string + Error *OutputError + Usage Usage + HasToolCalls bool + HasVisibleText bool + HasVisibleOutput bool + ShouldFail bool +} + +type BuildOptions struct { + Model string + Prompt string + RefFileTokens int + SearchEnabled bool + StripReferenceMarkers bool + ToolNames []string + ToolsRaw any + ToolChoice promptcompat.ToolChoicePolicy +} + +type StreamSnapshot struct { + RawText string + VisibleText string + RawThinking string + VisibleThinking string + DetectionThinking string + ContentFilter bool + CitationLinks map[int]string + ResponseMessageID int + AlreadyEmittedCalls bool + AdditionalToolCalls []toolcall.ParsedToolCall + AlreadyEmittedToolRaw bool +} + +func BuildTurnFromCollected(result sse.CollectResult, opts BuildOptions) Turn { + thinking := shared.CleanVisibleOutput(result.Thinking, opts.StripReferenceMarkers) + text := shared.CleanVisibleOutput(result.Text, opts.StripReferenceMarkers) + if opts.SearchEnabled { + text = shared.ReplaceCitationMarkersWithLinks(text, result.CitationLinks) + } + + parsed := shared.DetectAssistantToolCalls(result.Text, text, result.Thinking, result.ToolDetectionThinking, opts.ToolNames) + calls := toolcall.NormalizeParsedToolCallsForSchemas(parsed.Calls, opts.ToolsRaw) + parsed.Calls = calls + + stopReason := StopReasonStop + if result.ContentFilter { + stopReason = StopReasonContentFilter + } + if len(calls) > 0 { + stopReason = StopReasonToolCalls + } + + turn := Turn{ + Model: opts.Model, + Prompt: opts.Prompt, + RawText: result.Text, + RawThinking: result.Thinking, + DetectionThinking: result.ToolDetectionThinking, + Text: text, + Thinking: thinking, + ToolCalls: calls, + ParsedToolCalls: parsed, + CitationLinks: result.CitationLinks, + ContentFilter: result.ContentFilter, + ResponseMessageID: result.ResponseMessageID, + StopReason: stopReason, + } + turn.Usage = BuildUsage(opts.Model, opts.Prompt, thinking, text, opts.RefFileTokens) + turn.Error = ValidateTurn(turn, opts.ToolChoice) + if turn.Error != nil { + turn.StopReason = StopReasonError + } + return turn +} + +func BuildTurnFromStreamSnapshot(snapshot StreamSnapshot, opts BuildOptions) Turn { + thinking := shared.CleanVisibleOutput(snapshot.VisibleThinking, opts.StripReferenceMarkers) + text := shared.CleanVisibleOutput(snapshot.VisibleText, opts.StripReferenceMarkers) + if opts.SearchEnabled { + text = shared.ReplaceCitationMarkersWithLinks(text, snapshot.CitationLinks) + } + + parsed := shared.DetectAssistantToolCalls(snapshot.RawText, text, snapshot.RawThinking, snapshot.DetectionThinking, opts.ToolNames) + calls := parsed.Calls + if len(calls) == 0 && len(snapshot.AdditionalToolCalls) > 0 { + calls = snapshot.AdditionalToolCalls + } + calls = toolcall.NormalizeParsedToolCallsForSchemas(calls, opts.ToolsRaw) + parsed.Calls = calls + + stopReason := StopReasonStop + if snapshot.ContentFilter { + stopReason = StopReasonContentFilter + } + if len(calls) > 0 || snapshot.AlreadyEmittedCalls || snapshot.AlreadyEmittedToolRaw { + stopReason = StopReasonToolCalls + } + + turn := Turn{ + Model: opts.Model, + Prompt: opts.Prompt, + RawText: snapshot.RawText, + RawThinking: snapshot.RawThinking, + DetectionThinking: snapshot.DetectionThinking, + Text: text, + Thinking: thinking, + ToolCalls: calls, + ParsedToolCalls: parsed, + CitationLinks: snapshot.CitationLinks, + ContentFilter: snapshot.ContentFilter, + ResponseMessageID: snapshot.ResponseMessageID, + StopReason: stopReason, + } + turn.Usage = BuildUsage(opts.Model, opts.Prompt, thinking, text, opts.RefFileTokens) + if !snapshot.AlreadyEmittedCalls && !snapshot.AlreadyEmittedToolRaw { + turn.Error = ValidateTurn(turn, opts.ToolChoice) + } + if turn.Error != nil && len(calls) == 0 { + turn.StopReason = StopReasonError + } + return turn +} + +func BuildUsage(model, prompt, thinking, text string, refFileTokens int) Usage { + inputTokens := util.CountPromptTokens(prompt, model) + refFileTokens + reasoningTokens := util.CountOutputTokens(thinking, model) + outputTokens := reasoningTokens + util.CountOutputTokens(text, model) + return Usage{ + InputTokens: inputTokens, + OutputTokens: outputTokens, + ReasoningTokens: reasoningTokens, + TotalTokens: inputTokens + outputTokens, + } +} + +func ValidateTurn(turn Turn, policy promptcompat.ToolChoicePolicy) *OutputError { + if policy.IsRequired() && len(turn.ToolCalls) == 0 { + return &OutputError{ + Status: http.StatusUnprocessableEntity, + Message: "tool_choice requires at least one valid tool call.", + Code: "tool_choice_violation", + } + } + if len(turn.ToolCalls) > 0 { + return nil + } + if strings.TrimSpace(turn.Text) != "" { + return nil + } + status, message, code := UpstreamEmptyOutputDetail(turn.ContentFilter, turn.Text, turn.Thinking) + return &OutputError{Status: status, Message: message, Code: code} +} + +func UpstreamEmptyOutputDetail(contentFilter bool, text, thinking string) (int, string, string) { + _ = text + if contentFilter { + return http.StatusBadRequest, "Upstream content filtered the response and returned no output.", "content_filter" + } + if strings.TrimSpace(thinking) != "" { + return http.StatusTooManyRequests, "Upstream account hit a rate limit and returned reasoning without visible output.", "upstream_empty_output" + } + return http.StatusServiceUnavailable, "Upstream service is unavailable and returned no output.", "upstream_unavailable" +} + +// ShouldRetryEmptyOutput returns true when the turn produced no visible text +// and has no tool calls or content filter. This includes thinking-only responses, +// where the model returned reasoning but no answer — a retry may yield text. +func ShouldRetryEmptyOutput(turn Turn, attempts, maxAttempts int) bool { + return attempts < maxAttempts && + !turn.ContentFilter && + len(turn.ToolCalls) == 0 && + strings.TrimSpace(turn.Text) == "" +} + +func FinalizeTurn(turn Turn, opts FinalizeOptions) FinalOutcome { + hasToolCalls := len(turn.ToolCalls) > 0 || opts.AlreadyEmittedToolCalls + hasVisibleText := strings.TrimSpace(turn.Text) != "" + hasVisibleThinking := strings.TrimSpace(turn.Thinking) != "" + err := turn.Error + if hasToolCalls { + err = nil + } + finishReason := FinishReason(turn) + if hasToolCalls { + finishReason = "tool_calls" + } + return FinalOutcome{ + FinishReason: finishReason, + Error: err, + Usage: turn.Usage, + HasToolCalls: hasToolCalls, + HasVisibleText: hasVisibleText, + HasVisibleOutput: hasVisibleText || hasVisibleThinking || hasToolCalls, + ShouldFail: err != nil, + } +} + +func OpenAIChatUsage(turn Turn) map[string]any { + return map[string]any{ + "prompt_tokens": turn.Usage.InputTokens, + "completion_tokens": turn.Usage.OutputTokens, + "total_tokens": turn.Usage.TotalTokens, + "completion_tokens_details": map[string]any{ + "reasoning_tokens": turn.Usage.ReasoningTokens, + }, + } +} + +func OpenAIResponsesUsage(turn Turn) map[string]any { + return map[string]any{ + "input_tokens": turn.Usage.InputTokens, + "output_tokens": turn.Usage.OutputTokens, + "total_tokens": turn.Usage.TotalTokens, + } +} + +func FinishReason(turn Turn) string { + switch turn.StopReason { + case StopReasonToolCalls: + return "tool_calls" + case StopReasonContentFilter: + return "content_filter" + default: + return "stop" + } +} diff --git a/internal/assistantturn/turn_test.go b/internal/assistantturn/turn_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b2f944580054148567dab0dcbf2097e8e5039168 --- /dev/null +++ b/internal/assistantturn/turn_test.go @@ -0,0 +1,149 @@ +package assistantturn + +import ( + "net/http" + "testing" + + "ds2api/internal/promptcompat" + "ds2api/internal/sse" +) + +func TestBuildTurnFromCollectedTextCitation(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{ + Text: "See [citation:1]", + CitationLinks: map[int]string{1: "https://example.com"}, + }, BuildOptions{Model: "deepseek-v4-flash", Prompt: "prompt", SearchEnabled: true}) + if turn.Text != "See [1](https://example.com)" { + t.Fatalf("text mismatch: %q", turn.Text) + } + if turn.StopReason != StopReasonStop { + t.Fatalf("stop reason mismatch: %q", turn.StopReason) + } + if turn.Error != nil { + t.Fatalf("unexpected error: %#v", turn.Error) + } +} + +func TestBuildTurnFromCollectedKeepsNonStreamReferenceLinks(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{ + Text: "结论[reference:0],补充[reference:1]。", + CitationLinks: map[int]string{ + 1: "https://example.com/a", + 2: "https://example.com/b", + }, + }, BuildOptions{Model: "deepseek-v4-flash-search", Prompt: "prompt", SearchEnabled: true}) + want := "结论[0](https://example.com/a),补充[1](https://example.com/b)。" + if turn.Text != want { + t.Fatalf("text mismatch: got %q want %q", turn.Text, want) + } +} + +func TestBuildTurnFromCollectedToolCall(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{ + Text: `{"x":1}`, + }, BuildOptions{ + ToolNames: []string{"Write"}, + ToolsRaw: []any{map[string]any{ + "name": "Write", + "input_schema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + }, + }, + }}, + }) + if len(turn.ToolCalls) != 1 { + t.Fatalf("expected one tool call, got %d", len(turn.ToolCalls)) + } + if turn.StopReason != StopReasonToolCalls { + t.Fatalf("stop reason mismatch: %q", turn.StopReason) + } + if _, ok := turn.ToolCalls[0].Input["content"].(string); !ok { + t.Fatalf("expected content coerced to string, got %#v", turn.ToolCalls[0].Input["content"]) + } +} + +func TestBuildTurnFromCollectedThinkingOnlyIsEmptyOutput(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{Thinking: "hidden"}, BuildOptions{}) + if turn.Error == nil || turn.Error.Code != "upstream_empty_output" { + t.Fatalf("expected empty output error, got %#v", turn.Error) + } +} + +func TestBuildTurnFromCollectedPureEmptyOutputIsUpstreamUnavailable(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{}, BuildOptions{}) + if turn.Error == nil || turn.Error.Status != http.StatusServiceUnavailable || turn.Error.Code != "upstream_unavailable" { + t.Fatalf("expected upstream unavailable error, got %#v", turn.Error) + } +} + +func TestBuildTurnFromCollectedToolChoiceRequired(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{Text: "hello"}, BuildOptions{ + ToolChoice: promptcompat.ToolChoicePolicy{Mode: promptcompat.ToolChoiceRequired}, + }) + if turn.Error == nil || turn.Error.Code != "tool_choice_violation" { + t.Fatalf("expected tool choice violation, got %#v", turn.Error) + } +} + +func TestBuildTurnFromStreamSnapshotUsesVisibleTextAndRawToolDetection(t *testing.T) { + turn := BuildTurnFromStreamSnapshot(StreamSnapshot{ + RawText: `{"x":1}`, + VisibleText: "", + }, BuildOptions{ + ToolNames: []string{"Write"}, + ToolsRaw: []any{map[string]any{ + "name": "Write", + "schema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + }, + }, + }}, + }) + if len(turn.ToolCalls) != 1 { + t.Fatalf("expected stream snapshot tool call, got %d", len(turn.ToolCalls)) + } + if _, ok := turn.ToolCalls[0].Input["content"].(string); !ok { + t.Fatalf("expected stream snapshot schema coercion, got %#v", turn.ToolCalls[0].Input["content"]) + } +} + +func TestBuildTurnFromStreamSnapshotAlreadyEmittedToolAvoidsEmptyError(t *testing.T) { + turn := BuildTurnFromStreamSnapshot(StreamSnapshot{AlreadyEmittedCalls: true}, BuildOptions{}) + if turn.Error != nil { + t.Fatalf("unexpected empty-output error after emitted tool call: %#v", turn.Error) + } + if turn.StopReason != StopReasonToolCalls { + t.Fatalf("stop reason mismatch: %q", turn.StopReason) + } +} + +func TestFinalizeTurnStopOutcome(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{Text: "hello"}, BuildOptions{}) + outcome := FinalizeTurn(turn, FinalizeOptions{}) + if outcome.ShouldFail { + t.Fatalf("unexpected failure: %#v", outcome.Error) + } + if outcome.FinishReason != "stop" || !outcome.HasVisibleText || !outcome.HasVisibleOutput { + t.Fatalf("unexpected outcome: %#v", outcome) + } +} + +func TestFinalizeTurnToolCallsOutcome(t *testing.T) { + turn := BuildTurnFromStreamSnapshot(StreamSnapshot{AlreadyEmittedCalls: true}, BuildOptions{}) + outcome := FinalizeTurn(turn, FinalizeOptions{AlreadyEmittedToolCalls: true}) + if outcome.ShouldFail || outcome.FinishReason != "tool_calls" || !outcome.HasToolCalls { + t.Fatalf("unexpected tool outcome: %#v", outcome) + } +} + +func TestFinalizeTurnContentFilterOutcome(t *testing.T) { + turn := BuildTurnFromCollected(sse.CollectResult{ContentFilter: true}, BuildOptions{}) + outcome := FinalizeTurn(turn, FinalizeOptions{}) + if !outcome.ShouldFail || outcome.Error == nil || outcome.Error.Code != "content_filter" { + t.Fatalf("expected content filter failure, got %#v", outcome) + } +} diff --git a/internal/auth/admin.go b/internal/auth/admin.go new file mode 100644 index 0000000000000000000000000000000000000000..8f1d2768f20e32cb4ac8dacb5d9fbe6f5bd5115e --- /dev/null +++ b/internal/auth/admin.go @@ -0,0 +1,225 @@ +package auth + +import ( + "crypto/hmac" + "crypto/sha256" + "crypto/subtle" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "log/slog" + "net/http" + "os" + "strconv" + "strings" + "sync" + "time" +) + +var warnOnce sync.Once + +type AdminConfigReader interface { + AdminPasswordHash() string + AdminJWTExpireHours() int + AdminJWTValidAfterUnix() int64 +} + +func AdminKey() string { + return effectiveAdminKey(nil) +} + +func effectiveAdminKey(store AdminConfigReader) string { + if store != nil { + if hash := strings.TrimSpace(store.AdminPasswordHash()); hash != "" { + return "" + } + } + if v := strings.TrimSpace(os.Getenv("DS2API_ADMIN_KEY")); v != "" { + return v + } + warnOnce.Do(func() { + slog.Warn("⚠️ DS2API_ADMIN_KEY is not set! Using insecure default \"admin\". Set a strong key in production!") + }) + return "admin" +} + +func jwtSecret(store AdminConfigReader) string { + if v := strings.TrimSpace(os.Getenv("DS2API_JWT_SECRET")); v != "" { + return v + } + if store != nil { + if hash := strings.TrimSpace(store.AdminPasswordHash()); hash != "" { + return hash + } + } + return effectiveAdminKey(store) +} + +func jwtExpireHours(store AdminConfigReader) int { + if store != nil { + if n := store.AdminJWTExpireHours(); n > 0 { + return n + } + } + if v := strings.TrimSpace(os.Getenv("DS2API_JWT_EXPIRE_HOURS")); v != "" { + if n, err := strconv.Atoi(v); err == nil && n > 0 { + return n + } + } + return 24 +} + +func CreateJWT(expireHours int) (string, error) { + return CreateJWTWithStore(expireHours, nil) +} + +func CreateJWTWithStore(expireHours int, store AdminConfigReader) (string, error) { + if expireHours <= 0 { + expireHours = jwtExpireHours(store) + } + issuedAt := time.Now().Unix() + // If sessions were invalidated in this same second, move iat forward by + // one second so newly minted tokens remain valid with strict cutoff checks. + if store != nil { + if validAfter := store.AdminJWTValidAfterUnix(); validAfter >= issuedAt { + issuedAt = validAfter + 1 + } + } + expireAt := time.Unix(issuedAt, 0).Add(time.Duration(expireHours) * time.Hour).Unix() + header := map[string]any{"alg": "HS256", "typ": "JWT"} + payload := map[string]any{"iat": issuedAt, "exp": expireAt, "role": "admin"} + h, _ := json.Marshal(header) + p, _ := json.Marshal(payload) + headerB64 := rawB64Encode(h) + payloadB64 := rawB64Encode(p) + msg := headerB64 + "." + payloadB64 + sig := signHS256(msg, store) + return msg + "." + rawB64Encode(sig), nil +} + +func VerifyJWT(token string) (map[string]any, error) { + return VerifyJWTWithStore(token, nil) +} + +func VerifyJWTWithStore(token string, store AdminConfigReader) (map[string]any, error) { + parts := strings.Split(token, ".") + if len(parts) != 3 { + return nil, errors.New("invalid token format") + } + msg := parts[0] + "." + parts[1] + expected := signHS256(msg, store) + actual, err := rawB64Decode(parts[2]) + if err != nil { + return nil, errors.New("invalid signature") + } + if !hmac.Equal(expected, actual) { + return nil, errors.New("invalid signature") + } + payloadBytes, err := rawB64Decode(parts[1]) + if err != nil { + return nil, errors.New("invalid payload") + } + var payload map[string]any + if err := json.Unmarshal(payloadBytes, &payload); err != nil { + return nil, errors.New("invalid payload") + } + exp, _ := payload["exp"].(float64) + if int64(exp) < time.Now().Unix() { + return nil, errors.New("token expired") + } + if store != nil { + validAfter := store.AdminJWTValidAfterUnix() + if validAfter > 0 { + iat, _ := payload["iat"].(float64) + if int64(iat) <= validAfter { + return nil, errors.New("token expired") + } + } + } + return payload, nil +} + +func VerifyAdminRequest(r *http.Request) error { + return VerifyAdminRequestWithStore(r, nil) +} + +func VerifyAdminRequestWithStore(r *http.Request, store AdminConfigReader) error { + authHeader := strings.TrimSpace(r.Header.Get("Authorization")) + if !strings.HasPrefix(strings.ToLower(authHeader), "bearer ") { + return errors.New("authentication required") + } + token := strings.TrimSpace(authHeader[7:]) + if token == "" { + return errors.New("authentication required") + } + if VerifyAdminCredential(token, store) { + return nil + } + if _, err := VerifyJWTWithStore(token, store); err == nil { + return nil + } + return errors.New("invalid credentials") +} + +func VerifyAdminCredential(candidate string, store AdminConfigReader) bool { + candidate = strings.TrimSpace(candidate) + if candidate == "" { + return false + } + if store != nil { + hash := strings.TrimSpace(store.AdminPasswordHash()) + if hash != "" { + return verifyAdminPasswordHash(candidate, hash) + } + } + key := effectiveAdminKey(store) + if key == "" { + return false + } + return subtle.ConstantTimeCompare([]byte(candidate), []byte(key)) == 1 +} + +func UsingDefaultAdminKey(store AdminConfigReader) bool { + if store != nil && strings.TrimSpace(store.AdminPasswordHash()) != "" { + return false + } + return strings.TrimSpace(os.Getenv("DS2API_ADMIN_KEY")) == "" +} + +func HashAdminPassword(raw string) string { + raw = strings.TrimSpace(raw) + if raw == "" { + return "" + } + sum := sha256.Sum256([]byte(raw)) + return "sha256:" + hex.EncodeToString(sum[:]) +} + +func verifyAdminPasswordHash(candidate, encoded string) bool { + encoded = strings.TrimSpace(strings.ToLower(encoded)) + if encoded == "" { + return false + } + if strings.HasPrefix(encoded, "sha256:") { + want := strings.TrimPrefix(encoded, "sha256:") + sum := sha256.Sum256([]byte(candidate)) + got := hex.EncodeToString(sum[:]) + return subtle.ConstantTimeCompare([]byte(got), []byte(want)) == 1 + } + return subtle.ConstantTimeCompare([]byte(candidate), []byte(encoded)) == 1 +} + +func signHS256(msg string, store AdminConfigReader) []byte { + h := hmac.New(sha256.New, []byte(jwtSecret(store))) + _, _ = h.Write([]byte(msg)) + return h.Sum(nil) +} + +func rawB64Encode(b []byte) string { + return base64.RawURLEncoding.EncodeToString(b) +} + +func rawB64Decode(s string) ([]byte, error) { + return base64.RawURLEncoding.DecodeString(s) +} diff --git a/internal/auth/admin_test.go b/internal/auth/admin_test.go new file mode 100644 index 0000000000000000000000000000000000000000..bfbd4c35fe2bd578dc63af4ecb169adfee012388 --- /dev/null +++ b/internal/auth/admin_test.go @@ -0,0 +1,86 @@ +package auth + +import ( + "net/http" + "testing" + + "ds2api/internal/config" +) + +func TestJWTCreateVerify(t *testing.T) { + token, err := CreateJWT(1) + if err != nil { + t.Fatalf("create jwt failed: %v", err) + } + payload, err := VerifyJWT(token) + if err != nil { + t.Fatalf("verify jwt failed: %v", err) + } + if payload["role"] != "admin" { + t.Fatalf("unexpected payload: %#v", payload) + } +} + +func TestVerifyAdminRequest(t *testing.T) { + token, _ := CreateJWT(1) + req, _ := http.NewRequest(http.MethodGet, "/admin/config", nil) + req.Header.Set("Authorization", "Bearer "+token) + if err := VerifyAdminRequest(req); err != nil { + t.Fatalf("expected token accepted: %v", err) + } +} + +func TestVerifyJWTWithStoreValidAfter(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"admin":{"password_hash":"`+HashAdminPassword("oldpass")+`"}}`) + store := config.LoadStore() + token, err := CreateJWTWithStore(1, store) + if err != nil { + t.Fatalf("create jwt failed: %v", err) + } + if _, err := VerifyJWTWithStore(token, store); err != nil { + t.Fatalf("verify before invalidation failed: %v", err) + } + if err := store.Update(func(c *config.Config) error { + c.Admin.JWTValidAfterUnix = 1<<62 - 1 + return nil + }); err != nil { + t.Fatalf("set valid-after failed: %v", err) + } + if _, err := VerifyJWTWithStore(token, store); err == nil { + t.Fatal("expected token invalid after valid-after update") + } +} + +func TestVerifyJWTWithStoreSameSecondInvalidationAndRelogin(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"admin":{"password_hash":"`+HashAdminPassword("oldpass")+`"}}`) + store := config.LoadStore() + + oldToken, err := CreateJWTWithStore(1, store) + if err != nil { + t.Fatalf("create old jwt failed: %v", err) + } + oldPayload, err := VerifyJWTWithStore(oldToken, store) + if err != nil { + t.Fatalf("verify old jwt before invalidation failed: %v", err) + } + oldIAT, _ := oldPayload["iat"].(float64) + + if err := store.Update(func(c *config.Config) error { + c.Admin.JWTValidAfterUnix = int64(oldIAT) + return nil + }); err != nil { + t.Fatalf("set valid-after failed: %v", err) + } + + if _, err := VerifyJWTWithStore(oldToken, store); err == nil { + t.Fatal("expected old token invalid when iat == valid-after") + } + + newToken, err := CreateJWTWithStore(1, store) + if err != nil { + t.Fatalf("create new jwt failed: %v", err) + } + if _, err := VerifyJWTWithStore(newToken, store); err != nil { + t.Fatalf("expected new token valid after invalidation cutoff: %v", err) + } +} diff --git a/internal/auth/auth_edge_test.go b/internal/auth/auth_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..73e970d982f9f7d61c4514913f75948f173240d0 --- /dev/null +++ b/internal/auth/auth_edge_test.go @@ -0,0 +1,442 @@ +package auth + +import ( + "context" + "errors" + "net/http" + "testing" + + "ds2api/internal/account" + "ds2api/internal/config" +) + +// ─── extractCallerToken edge cases ─────────────────────────────────── + +func TestExtractCallerTokenBearerPrefix(t *testing.T) { + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer my-token") + if got := extractCallerToken(req); got != "my-token" { + t.Fatalf("expected my-token, got %q", got) + } +} + +func TestExtractCallerTokenBearerCaseInsensitive(t *testing.T) { + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "BEARER My-Token") + if got := extractCallerToken(req); got != "My-Token" { + t.Fatalf("expected My-Token, got %q", got) + } +} + +func TestExtractCallerTokenBearerEmpty(t *testing.T) { + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer ") + if got := extractCallerToken(req); got != "" { + t.Fatalf("expected empty for 'Bearer ', got %q", got) + } +} + +func TestExtractCallerTokenXAPIKey(t *testing.T) { + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("x-api-key", "x-api-key-token") + if got := extractCallerToken(req); got != "x-api-key-token" { + t.Fatalf("expected x-api-key-token, got %q", got) + } +} + +func TestExtractCallerTokenBearerPreferredOverXAPIKey(t *testing.T) { + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer bearer-token") + req.Header.Set("x-api-key", "x-api-key-token") + if got := extractCallerToken(req); got != "bearer-token" { + t.Fatalf("expected bearer-token, got %q", got) + } +} + +func TestExtractCallerTokenMissingHeaders(t *testing.T) { + req, _ := http.NewRequest("POST", "/", nil) + if got := extractCallerToken(req); got != "" { + t.Fatalf("expected empty for missing headers, got %q", got) + } +} + +func TestExtractCallerTokenNonBearerAuth(t *testing.T) { + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Basic abc123") + if got := extractCallerToken(req); got != "" { + t.Fatalf("expected empty for Basic auth, got %q", got) + } +} + +// ─── Context helpers ───────────────────────────────────────────────── + +func TestWithAuthAndFromContext(t *testing.T) { + a := &RequestAuth{DeepSeekToken: "test-token"} + ctx := WithAuth(context.Background(), a) + got, ok := FromContext(ctx) + if !ok || got.DeepSeekToken != "test-token" { + t.Fatalf("expected token from context, got ok=%v token=%q", ok, got.DeepSeekToken) + } +} + +func TestFromContextMissing(t *testing.T) { + _, ok := FromContext(context.Background()) + if ok { + t.Fatal("expected not ok from empty context") + } +} + +// ─── RefreshToken edge cases ───────────────────────────────────────── + +func TestRefreshTokenNotConfigToken(t *testing.T) { + r := newTestResolver(t) + a := &RequestAuth{UseConfigToken: false, resolver: r} + if r.RefreshToken(context.Background(), a) { + t.Fatal("expected false for non-config token") + } +} + +func TestRefreshTokenEmptyAccountID(t *testing.T) { + r := newTestResolver(t) + a := &RequestAuth{UseConfigToken: true, AccountID: "", resolver: r} + if r.RefreshToken(context.Background(), a) { + t.Fatal("expected false for empty account ID") + } +} + +func TestRefreshTokenSuccess(t *testing.T) { + r := newTestResolver(t) + // First acquire an account + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer r.Release(a) + + if !r.RefreshToken(context.Background(), a) { + t.Fatal("expected refresh to succeed") + } + if a.DeepSeekToken != "fresh-token" { + t.Fatalf("expected fresh-token after refresh, got %q", a.DeepSeekToken) + } +} + +// ─── MarkTokenInvalid edge cases ───────────────────────────────────── + +func TestMarkTokenInvalidNotConfigToken(t *testing.T) { + r := newTestResolver(t) + a := &RequestAuth{UseConfigToken: false, DeepSeekToken: "direct", resolver: r} + r.MarkTokenInvalid(a) + // Should not panic, token should be unchanged for non-config + _ = a.DeepSeekToken // Actual behavior may clear it; this test only asserts no panic. +} + +func TestMarkTokenInvalidEmptyAccountID(t *testing.T) { + r := newTestResolver(t) + a := &RequestAuth{UseConfigToken: true, AccountID: "", DeepSeekToken: "tok", resolver: r} + r.MarkTokenInvalid(a) + // Should not panic +} + +func TestMarkTokenInvalidClearsToken(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer r.Release(a) + + r.MarkTokenInvalid(a) + if a.DeepSeekToken != "" { + t.Fatalf("expected empty token after invalidation, got %q", a.DeepSeekToken) + } + if a.Account.Token != "" { + t.Fatalf("expected empty account token after invalidation, got %q", a.Account.Token) + } +} + +// ─── SwitchAccount edge cases ──────────────────────────────────────── + +func TestSwitchAccountNotConfigToken(t *testing.T) { + r := newTestResolver(t) + a := &RequestAuth{UseConfigToken: false, resolver: r} + if r.SwitchAccount(context.Background(), a) { + t.Fatal("expected false for non-config token") + } +} + +func TestSwitchAccountNilTriedAccounts(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","token":"t1"}, + {"email":"acc2@test.com","token":"t2"} + ] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + r := NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "new-token", nil + }) + + // First acquire + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + + oldID := a.AccountID + a.TriedAccounts = nil // test nil initialization in SwitchAccount + if !r.SwitchAccount(context.Background(), a) { + t.Fatal("expected switch to succeed") + } + if a.AccountID == oldID { + t.Fatalf("expected different account after switch") + } + r.Release(a) +} + +func TestSwitchAccountSkipsLoginFailureAndContinues(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","password":"pwd","token":"t1"}, + {"email":"acc2@test.com","password":"pwd"}, + {"email":"acc3@test.com","password":"pwd","token":"t3"} + ] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + r := NewResolver(store, pool, func(_ context.Context, acc config.Account) (string, error) { + if acc.Email == "acc2@test.com" { + return "", errors.New("login failed") + } + return "new-token", nil + }) + + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer r.Release(a) + if a.AccountID != "acc1@test.com" { + t.Fatalf("expected first account, got %q", a.AccountID) + } + if !r.SwitchAccount(context.Background(), a) { + t.Fatal("expected switch to succeed after skipping failed account") + } + if a.AccountID != "acc3@test.com" { + t.Fatalf("expected fallback to third account, got %q", a.AccountID) + } + if !a.TriedAccounts["acc2@test.com"] { + t.Fatalf("expected failed account to be marked as tried") + } +} + +func TestSwitchAccountRespectsPinnedTargetAccount(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","token":"t1"}, + {"email":"acc2@test.com","token":"t2"} + ] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + r := NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "new-token", nil + }) + + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + req.Header.Set("X-Ds2-Target-Account", "acc1@test.com") + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer r.Release(a) + if r.SwitchAccount(context.Background(), a) { + t.Fatal("expected switch to be disabled for pinned target account") + } + if a.AccountID != "acc1@test.com" { + t.Fatalf("expected pinned account to remain selected, got %q", a.AccountID) + } +} + +// ─── Release edge cases ───────────────────────────────────────────── + +func TestReleaseNilAuth(t *testing.T) { + r := newTestResolver(t) + r.Release(nil) // should not panic +} + +func TestReleaseNonConfigToken(t *testing.T) { + r := newTestResolver(t) + a := &RequestAuth{UseConfigToken: false} + r.Release(a) // should not panic +} + +func TestReleaseEmptyAccountID(t *testing.T) { + r := newTestResolver(t) + a := &RequestAuth{UseConfigToken: true, AccountID: ""} + r.Release(a) // should not panic +} + +// ─── JWT edge cases ────────────────────────────────────────────────── + +func TestVerifyJWTInvalidFormat(t *testing.T) { + _, err := VerifyJWT("not-a-jwt") + if err == nil { + t.Fatal("expected error for invalid JWT format") + } +} + +func TestVerifyJWTInvalidSignature(t *testing.T) { + token, _ := CreateJWT(1) + // Tamper with the signature + parts := splitJWT(token) + if len(parts) == 3 { + tampered := parts[0] + "." + parts[1] + ".invalid_signature" + _, err := VerifyJWT(tampered) + if err == nil { + t.Fatal("expected error for tampered signature") + } + } +} + +func TestVerifyJWTExpired(t *testing.T) { + // Create a token with 0 hours expiry - will use default, so we can't easily test + // Instead test with bad payload + _, err := VerifyJWT("eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjF9.invalid") + if err == nil { + t.Fatal("expected error for expired/invalid JWT") + } +} + +func TestCreateJWTDefaultExpiry(t *testing.T) { + token, err := CreateJWT(0) // should use default + if err != nil { + t.Fatalf("create jwt failed: %v", err) + } + _, err = VerifyJWT(token) + if err != nil { + t.Fatalf("verify jwt failed: %v", err) + } +} + +// ─── VerifyAdminRequest edge cases ─────────────────────────────────── + +func TestVerifyAdminRequestNoHeader(t *testing.T) { + req, _ := http.NewRequest("GET", "/admin/config", nil) + if err := VerifyAdminRequest(req); err == nil { + t.Fatal("expected error for missing auth") + } +} + +func TestVerifyAdminRequestEmptyBearer(t *testing.T) { + req, _ := http.NewRequest("GET", "/admin/config", nil) + req.Header.Set("Authorization", "Bearer ") + if err := VerifyAdminRequest(req); err == nil { + t.Fatal("expected error for empty bearer") + } +} + +func TestVerifyAdminRequestWithAdminKey(t *testing.T) { + t.Setenv("DS2API_ADMIN_KEY", "test-admin-key") + req, _ := http.NewRequest("GET", "/admin/config", nil) + req.Header.Set("Authorization", "Bearer test-admin-key") + if err := VerifyAdminRequest(req); err != nil { + t.Fatalf("expected admin key accepted: %v", err) + } +} + +func TestVerifyAdminRequestInvalidCredentials(t *testing.T) { + t.Setenv("DS2API_ADMIN_KEY", "correct-key") + req, _ := http.NewRequest("GET", "/admin/config", nil) + req.Header.Set("Authorization", "Bearer wrong-key") + if err := VerifyAdminRequest(req); err == nil { + t.Fatal("expected error for wrong key") + } +} + +func TestVerifyAdminRequestBasicAuth(t *testing.T) { + req, _ := http.NewRequest("GET", "/admin/config", nil) + req.Header.Set("Authorization", "Basic abc123") + if err := VerifyAdminRequest(req); err == nil { + t.Fatal("expected error for Basic auth") + } +} + +// ─── Determine with login failure ──────────────────────────────────── + +func TestDetermineWithLoginFailure(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[{"email":"acc@test.com","password":"pwd"}] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + r := NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "", errors.New("login failed") + }) + + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + _, err := r.Determine(req) + if err == nil { + t.Fatal("expected error when login fails") + } +} + +// ─── Determine with target account ─────────────────────────────────── + +func TestDetermineWithTargetAccount(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","token":"t1"}, + {"email":"acc2@test.com","token":"t2"} + ] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + r := NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "fresh-token", nil + }) + + req, _ := http.NewRequest("POST", "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + req.Header.Set("X-Ds2-Target-Account", "acc2@test.com") + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer r.Release(a) + if a.AccountID != "acc2@test.com" { + t.Fatalf("expected target account acc2, got %q", a.AccountID) + } +} + +// helper +func splitJWT(token string) []string { + result := make([]string, 0, 3) + start := 0 + count := 0 + for i := 0; i < len(token); i++ { + if token[i] == '.' { + result = append(result, token[start:i]) + start = i + 1 + count++ + } + } + result = append(result, token[start:]) + return result +} diff --git a/internal/auth/request.go b/internal/auth/request.go new file mode 100644 index 0000000000000000000000000000000000000000..fd84a12b0de158bbc5f6c49d42e0e2acb8d051b5 --- /dev/null +++ b/internal/auth/request.go @@ -0,0 +1,314 @@ +package auth + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "net/http" + "strings" + "sync" + "time" + + "ds2api/internal/account" + "ds2api/internal/config" +) + +type ctxKey string + +const authCtxKey ctxKey = "auth_context" + +var ( + ErrUnauthorized = errors.New("unauthorized: missing auth token") + ErrNoAccount = errors.New("no accounts configured or all accounts are busy") +) + +type RequestAuth struct { + UseConfigToken bool + DeepSeekToken string + CallerID string + AccountID string + TargetAccount string + Account config.Account + TriedAccounts map[string]bool + resolver *Resolver +} + +type LoginFunc func(ctx context.Context, acc config.Account) (string, error) + +type Resolver struct { + Store *config.Store + Pool *account.Pool + Login LoginFunc + + mu sync.Mutex + tokenRefreshedAt map[string]time.Time +} + +func NewResolver(store *config.Store, pool *account.Pool, login LoginFunc) *Resolver { + return &Resolver{ + Store: store, + Pool: pool, + Login: login, + tokenRefreshedAt: map[string]time.Time{}, + } +} + +func (r *Resolver) Determine(req *http.Request) (*RequestAuth, error) { + callerKey := extractCallerToken(req) + if callerKey == "" { + return nil, ErrUnauthorized + } + callerID := callerTokenID(callerKey) + ctx := req.Context() + if !r.Store.HasAPIKey(callerKey) { + return &RequestAuth{ + UseConfigToken: false, + DeepSeekToken: callerKey, + CallerID: callerID, + resolver: r, + TriedAccounts: map[string]bool{}, + }, nil + } + target := strings.TrimSpace(req.Header.Get("X-Ds2-Target-Account")) + a, err := r.acquireManagedRequestAuth(ctx, callerID, target) + if err != nil { + return nil, err + } + return a, nil +} + +func (r *Resolver) acquireManagedRequestAuth(ctx context.Context, callerID, target string) (*RequestAuth, error) { + tried := map[string]bool{} + var lastEnsureErr error + for { + if target == "" && len(tried) >= len(r.Store.Accounts()) { + if lastEnsureErr != nil { + return nil, lastEnsureErr + } + return nil, ErrNoAccount + } + acc, ok := r.Pool.AcquireWait(ctx, target, tried) + if !ok { + if lastEnsureErr != nil { + return nil, lastEnsureErr + } + return nil, ErrNoAccount + } + + a := &RequestAuth{ + UseConfigToken: true, + CallerID: callerID, + AccountID: acc.Identifier(), + TargetAccount: target, + Account: acc, + TriedAccounts: tried, + resolver: r, + } + + if err := r.ensureManagedToken(ctx, a); err != nil { + lastEnsureErr = err + tried[a.AccountID] = true + r.Pool.Release(a.AccountID) + if target != "" { + return nil, err + } + continue + } + return a, nil + } +} + +// DetermineCaller resolves caller identity without acquiring any pooled account. +// Use this for local-cache lookup routes that only need tenant isolation. +func (r *Resolver) DetermineCaller(req *http.Request) (*RequestAuth, error) { + callerKey := extractCallerToken(req) + if callerKey == "" { + return nil, ErrUnauthorized + } + callerID := callerTokenID(callerKey) + a := &RequestAuth{ + UseConfigToken: false, + CallerID: callerID, + resolver: r, + TriedAccounts: map[string]bool{}, + } + if r == nil || r.Store == nil || !r.Store.HasAPIKey(callerKey) { + a.DeepSeekToken = callerKey + } + return a, nil +} + +func WithAuth(ctx context.Context, a *RequestAuth) context.Context { + return context.WithValue(ctx, authCtxKey, a) +} + +func FromContext(ctx context.Context) (*RequestAuth, bool) { + v := ctx.Value(authCtxKey) + a, ok := v.(*RequestAuth) + return a, ok +} + +func (r *Resolver) loginAndPersist(ctx context.Context, a *RequestAuth) error { + token, err := r.Login(ctx, a.Account) + if err != nil { + return err + } + a.Account.Token = token + a.DeepSeekToken = token + r.markTokenRefreshedNow(a.AccountID) + return r.Store.UpdateAccountToken(a.AccountID, token) +} + +func (r *Resolver) RefreshToken(ctx context.Context, a *RequestAuth) bool { + if !a.UseConfigToken || a.AccountID == "" { + return false + } + _ = r.Store.UpdateAccountToken(a.AccountID, "") + a.Account.Token = "" + if err := r.loginAndPersist(ctx, a); err != nil { + config.Logger.Error("[refresh_token] failed", "account", a.AccountID, "error", err) + return false + } + return true +} + +func (r *Resolver) MarkTokenInvalid(a *RequestAuth) { + if !a.UseConfigToken || a.AccountID == "" { + return + } + a.Account.Token = "" + a.DeepSeekToken = "" + r.clearTokenRefreshMark(a.AccountID) + _ = r.Store.UpdateAccountToken(a.AccountID, "") +} + +func (r *Resolver) SwitchAccount(ctx context.Context, a *RequestAuth) bool { + if !a.UseConfigToken { + return false + } + if strings.TrimSpace(a.TargetAccount) != "" { + return false + } + if a.TriedAccounts == nil { + a.TriedAccounts = map[string]bool{} + } + if a.AccountID != "" { + a.TriedAccounts[a.AccountID] = true + r.Pool.Release(a.AccountID) + } + for { + acc, ok := r.Pool.Acquire("", a.TriedAccounts) + if !ok { + return false + } + a.Account = acc + a.AccountID = acc.Identifier() + if err := r.ensureManagedToken(ctx, a); err != nil { + a.TriedAccounts[a.AccountID] = true + r.Pool.Release(a.AccountID) + continue + } + return true + } +} + +func (a *RequestAuth) SwitchAccount(ctx context.Context) bool { + if a == nil || a.resolver == nil { + return false + } + return a.resolver.SwitchAccount(ctx, a) +} + +func (r *Resolver) Release(a *RequestAuth) { + if a == nil || !a.UseConfigToken || a.AccountID == "" { + return + } + r.Pool.Release(a.AccountID) +} + +func extractCallerToken(req *http.Request) string { + authHeader := strings.TrimSpace(req.Header.Get("Authorization")) + if strings.HasPrefix(strings.ToLower(authHeader), "bearer ") { + token := strings.TrimSpace(authHeader[7:]) + if token != "" { + return token + } + } + if key := strings.TrimSpace(req.Header.Get("x-api-key")); key != "" { + return key + } + // Gemini/Google clients commonly send API key via x-goog-api-key. + if key := strings.TrimSpace(req.Header.Get("x-goog-api-key")); key != "" { + return key + } + // Gemini AI Studio compatibility: allow query key fallback only when no + // header-based credential is present. + if key := strings.TrimSpace(req.URL.Query().Get("key")); key != "" { + return key + } + return strings.TrimSpace(req.URL.Query().Get("api_key")) +} + +func callerTokenID(token string) string { + token = strings.TrimSpace(token) + if token == "" { + return "" + } + sum := sha256.Sum256([]byte(token)) + return "caller:" + hex.EncodeToString(sum[:8]) +} + +func (r *Resolver) ensureManagedToken(ctx context.Context, a *RequestAuth) error { + if strings.TrimSpace(a.Account.Token) == "" { + return r.loginAndPersist(ctx, a) + } + if r.shouldForceRefresh(a.AccountID) { + if err := r.loginAndPersist(ctx, a); err != nil { + return err + } + return nil + } + a.DeepSeekToken = a.Account.Token + return nil +} + +func (r *Resolver) shouldForceRefresh(accountID string) bool { + if r == nil || r.Store == nil { + return false + } + if strings.TrimSpace(accountID) == "" { + return false + } + intervalHours := r.Store.RuntimeTokenRefreshIntervalHours() + if intervalHours <= 0 { + return false + } + now := time.Now() + r.mu.Lock() + defer r.mu.Unlock() + last, ok := r.tokenRefreshedAt[accountID] + if !ok || last.IsZero() { + r.tokenRefreshedAt[accountID] = now + return false + } + return now.Sub(last) >= time.Duration(intervalHours)*time.Hour +} + +func (r *Resolver) markTokenRefreshedNow(accountID string) { + if strings.TrimSpace(accountID) == "" { + return + } + r.mu.Lock() + defer r.mu.Unlock() + r.tokenRefreshedAt[accountID] = time.Now() +} + +func (r *Resolver) clearTokenRefreshMark(accountID string) { + if strings.TrimSpace(accountID) == "" { + return + } + r.mu.Lock() + defer r.mu.Unlock() + delete(r.tokenRefreshedAt, accountID) +} diff --git a/internal/auth/request_test.go b/internal/auth/request_test.go new file mode 100644 index 0000000000000000000000000000000000000000..edf163de146f0a224b3118e806aec141717d2f66 --- /dev/null +++ b/internal/auth/request_test.go @@ -0,0 +1,397 @@ +package auth + +import ( + "context" + "errors" + "net/http" + "sync/atomic" + "testing" + "time" + + "ds2api/internal/account" + "ds2api/internal/config" +) + +func newTestResolver(t *testing.T) *Resolver { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[{"email":"acc@example.com","password":"pwd","token":"account-token"}] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + return NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "fresh-token", nil + }) +} + +func TestDetermineWithXAPIKeyUsesDirectToken(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + req.Header.Set("x-api-key", "direct-token") + + auth, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + if auth.UseConfigToken { + t.Fatalf("expected direct token mode") + } + if auth.DeepSeekToken != "direct-token" { + t.Fatalf("unexpected token: %q", auth.DeepSeekToken) + } + if auth.CallerID == "" { + t.Fatalf("expected caller id to be populated") + } +} + +func TestDetermineWithXAPIKeyManagedKeyAcquiresAccount(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + req.Header.Set("x-api-key", "managed-key") + + auth, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer r.Release(auth) + if !auth.UseConfigToken { + t.Fatalf("expected managed key mode") + } + if auth.AccountID != "acc@example.com" { + t.Fatalf("unexpected account id: %q", auth.AccountID) + } + if auth.DeepSeekToken != "fresh-token" { + t.Fatalf("unexpected account token: %q", auth.DeepSeekToken) + } + if auth.CallerID == "" { + t.Fatalf("expected caller id to be populated") + } +} + +func TestDetermineCallerWithManagedKeySkipsAccountAcquire(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodGet, "/v1/responses/resp_1", nil) + req.Header.Set("x-api-key", "managed-key") + + a, err := r.DetermineCaller(req) + if err != nil { + t.Fatalf("determine caller failed: %v", err) + } + if a.CallerID == "" { + t.Fatalf("expected caller id to be populated") + } + if a.UseConfigToken { + t.Fatalf("expected no config-token lease for caller-only auth") + } + if a.AccountID != "" { + t.Fatalf("expected empty account id, got %q", a.AccountID) + } +} + +func TestCallerTokenIDStable(t *testing.T) { + a := callerTokenID("token-a") + b := callerTokenID("token-a") + c := callerTokenID("token-b") + if a == "" || b == "" || c == "" { + t.Fatalf("expected non-empty caller ids") + } + if a != b { + t.Fatalf("expected stable caller id, got %q and %q", a, b) + } + if a == c { + t.Fatalf("expected different caller id for different tokens") + } +} + +func TestDetermineMissingToken(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + _, err := r.Determine(req) + if err == nil { + t.Fatal("expected unauthorized error") + } + if err != ErrUnauthorized { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestDetermineWithQueryKeyUsesDirectToken(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent?key=direct-query-key", nil) + + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + if a.UseConfigToken { + t.Fatalf("expected direct token mode") + } + if a.DeepSeekToken != "direct-query-key" { + t.Fatalf("unexpected token: %q", a.DeepSeekToken) + } +} + +func TestDetermineWithXGoogAPIKeyUsesDirectToken(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:streamGenerateContent?alt=sse", nil) + req.Header.Set("x-goog-api-key", "goog-header-key") + + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + if a.UseConfigToken { + t.Fatalf("expected direct token mode") + } + if a.DeepSeekToken != "goog-header-key" { + t.Fatalf("unexpected token: %q", a.DeepSeekToken) + } +} + +func TestDetermineWithAPIKeyQueryParamUsesDirectToken(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent?api_key=direct-api-key", nil) + + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + if a.UseConfigToken { + t.Fatalf("expected direct token mode") + } + if a.DeepSeekToken != "direct-api-key" { + t.Fatalf("unexpected token: %q", a.DeepSeekToken) + } +} + +func TestDetermineHeaderTokenPrecedenceOverQueryKey(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent?key=query-key", nil) + req.Header.Set("x-api-key", "managed-key") + + a, err := r.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer r.Release(a) + if !a.UseConfigToken { + t.Fatalf("expected managed key mode from header token") + } + if a.AccountID == "" { + t.Fatalf("expected managed account to be acquired") + } +} + +func TestDetermineCallerMissingToken(t *testing.T) { + r := newTestResolver(t) + req, _ := http.NewRequest(http.MethodGet, "/v1/responses/resp_1", nil) + + _, err := r.DetermineCaller(req) + if err == nil { + t.Fatal("expected unauthorized error") + } + if err != ErrUnauthorized { + t.Fatalf("unexpected error: %v", err) + } +} + +func TestDetermineManagedAccountForcesRefreshEverySixHours(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[{"email":"acc@example.com","password":"pwd","token":"seed-token"}] + }`) + store := config.LoadStore() + if err := store.UpdateAccountToken("acc@example.com", "seed-token"); err != nil { + t.Fatalf("update token failed: %v", err) + } + pool := account.NewPool(store) + + var loginCount int32 + resolver := NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + n := atomic.AddInt32(&loginCount, 1) + return "fresh-token-" + string(rune('0'+n)), nil + }) + + req, _ := http.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + req.Header.Set("x-api-key", "managed-key") + + a1, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + if a1.DeepSeekToken != "seed-token" { + t.Fatalf("expected initial token without forced refresh, got %q", a1.DeepSeekToken) + } + resolver.Release(a1) + if got := atomic.LoadInt32(&loginCount); got != 0 { + t.Fatalf("expected no login before refresh interval, got %d", got) + } + + resolver.mu.Lock() + resolver.tokenRefreshedAt["acc@example.com"] = time.Now().Add(-7 * time.Hour) + resolver.mu.Unlock() + + a2, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine after interval failed: %v", err) + } + defer resolver.Release(a2) + if a2.DeepSeekToken != "fresh-token-1" { + t.Fatalf("expected refreshed token after interval, got %q", a2.DeepSeekToken) + } + if got := atomic.LoadInt32(&loginCount); got != 1 { + t.Fatalf("expected exactly one forced refresh login, got %d", got) + } +} + +func TestDetermineManagedAccountUsesUpdatedRefreshInterval(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[{"email":"acc@example.com","password":"pwd","token":"seed-token"}], + "runtime":{"token_refresh_interval_hours":6} + }`) + store := config.LoadStore() + if err := store.UpdateAccountToken("acc@example.com", "seed-token"); err != nil { + t.Fatalf("update token failed: %v", err) + } + pool := account.NewPool(store) + + var loginCount int32 + resolver := NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + n := atomic.AddInt32(&loginCount, 1) + return "fresh-token-" + string(rune('0'+n)), nil + }) + + req, _ := http.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + req.Header.Set("x-api-key", "managed-key") + + a1, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + if a1.DeepSeekToken != "seed-token" { + t.Fatalf("expected initial token without forced refresh, got %q", a1.DeepSeekToken) + } + resolver.Release(a1) + if got := atomic.LoadInt32(&loginCount); got != 0 { + t.Fatalf("expected no login before runtime update, got %d", got) + } + + if err := store.Update(func(c *config.Config) error { + c.Runtime.TokenRefreshIntervalHours = 1 + return nil + }); err != nil { + t.Fatalf("update runtime failed: %v", err) + } + + resolver.mu.Lock() + resolver.tokenRefreshedAt["acc@example.com"] = time.Now().Add(-2 * time.Hour) + resolver.mu.Unlock() + + a2, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine after runtime update failed: %v", err) + } + defer resolver.Release(a2) + if a2.DeepSeekToken != "fresh-token-1" { + t.Fatalf("expected refreshed token after runtime update, got %q", a2.DeepSeekToken) + } + if got := atomic.LoadInt32(&loginCount); got != 1 { + t.Fatalf("expected exactly one login after runtime update, got %d", got) + } +} + +func TestDetermineManagedAccountRetriesOtherAccountOnLoginFailure(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"bad@example.com","password":"pwd"}, + {"email":"good@example.com","password":"pwd","token":"good-token"} + ] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + resolver := NewResolver(store, pool, func(_ context.Context, acc config.Account) (string, error) { + if acc.Email == "bad@example.com" { + return "", errors.New("stale account") + } + return "fresh-good-token", nil + }) + + req, _ := http.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + req.Header.Set("x-api-key", "managed-key") + + a, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer resolver.Release(a) + if a.AccountID != "good@example.com" { + t.Fatalf("expected fallback to good account, got %q", a.AccountID) + } + if a.DeepSeekToken == "" { + t.Fatal("expected non-empty token from fallback account") + } + if !a.TriedAccounts["bad@example.com"] { + t.Fatalf("expected bad account to be tracked as tried") + } +} + +func TestDetermineTargetAccountDoesNotFallbackOnLoginFailure(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"bad@example.com","password":"pwd"}, + {"email":"good@example.com","password":"pwd","token":"good-token"} + ] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + resolver := NewResolver(store, pool, func(_ context.Context, acc config.Account) (string, error) { + if acc.Email == "bad@example.com" { + return "", errors.New("stale account") + } + return "fresh-good-token", nil + }) + + req, _ := http.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + req.Header.Set("x-api-key", "managed-key") + req.Header.Set("X-Ds2-Target-Account", "bad@example.com") + + _, err := resolver.Determine(req) + if err == nil { + t.Fatal("expected determine to fail for broken target account") + } +} + +func TestDetermineManagedAccountReturnsLastEnsureErrorWhenAllFail(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"bad1@example.com","password":"pwd"}, + {"email":"bad2@example.com","password":"pwd"} + ] + }`) + store := config.LoadStore() + pool := account.NewPool(store) + ensureErr := errors.New("all credentials stale") + resolver := NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "", ensureErr + }) + + req, _ := http.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + req.Header.Set("x-api-key", "managed-key") + + _, err := resolver.Determine(req) + if err == nil { + t.Fatal("expected determine to fail") + } + if !errors.Is(err, ensureErr) { + t.Fatalf("expected ensure error, got %v", err) + } + if errors.Is(err, ErrNoAccount) { + t.Fatalf("expected auth-style ensure error, got ErrNoAccount") + } +} diff --git a/internal/chathistory/store.go b/internal/chathistory/store.go new file mode 100644 index 0000000000000000000000000000000000000000..85228dcad9978b866574ba9fc7de706d29afd9d4 --- /dev/null +++ b/internal/chathistory/store.go @@ -0,0 +1,810 @@ +package chathistory + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "sort" + "strings" + "sync" + "time" + + "github.com/google/uuid" + + "ds2api/internal/config" + "ds2api/internal/util" +) + +const ( + FileVersion = 2 + DisabledLimit = 0 + DefaultLimit = 20 + MaxLimit = 50 + defaultPreviewAt = 160 +) + +var allowedLimits = map[int]struct{}{ + DisabledLimit: {}, + 10: {}, + 20: {}, + 50: {}, +} + +var ErrDisabled = errors.New("chat history disabled") + +type Entry struct { + ID string `json:"id"` + Revision int64 `json:"revision"` + CreatedAt int64 `json:"created_at"` + UpdatedAt int64 `json:"updated_at"` + CompletedAt int64 `json:"completed_at,omitempty"` + Status string `json:"status"` + CallerID string `json:"caller_id,omitempty"` + AccountID string `json:"account_id,omitempty"` + Surface string `json:"surface,omitempty"` + Model string `json:"model,omitempty"` + Stream bool `json:"stream"` + UserInput string `json:"user_input,omitempty"` + Messages []Message `json:"messages,omitempty"` + HistoryText string `json:"history_text,omitempty"` + FinalPrompt string `json:"final_prompt,omitempty"` + ReasoningContent string `json:"reasoning_content,omitempty"` + Content string `json:"content,omitempty"` + Error string `json:"error,omitempty"` + StatusCode int `json:"status_code,omitempty"` + ElapsedMs int64 `json:"elapsed_ms,omitempty"` + FinishReason string `json:"finish_reason,omitempty"` + Usage map[string]any `json:"usage,omitempty"` +} + +type Message struct { + Role string `json:"role"` + Content string `json:"content"` +} + +type SummaryEntry struct { + ID string `json:"id"` + Revision int64 `json:"revision"` + CreatedAt int64 `json:"created_at"` + UpdatedAt int64 `json:"updated_at"` + CompletedAt int64 `json:"completed_at,omitempty"` + Status string `json:"status"` + CallerID string `json:"caller_id,omitempty"` + AccountID string `json:"account_id,omitempty"` + Surface string `json:"surface,omitempty"` + Model string `json:"model,omitempty"` + Stream bool `json:"stream"` + UserInput string `json:"user_input,omitempty"` + Preview string `json:"preview,omitempty"` + StatusCode int `json:"status_code,omitempty"` + ElapsedMs int64 `json:"elapsed_ms,omitempty"` + FinishReason string `json:"finish_reason,omitempty"` + DetailRevision int64 `json:"detail_revision"` +} + +type File struct { + Version int `json:"version"` + Limit int `json:"limit"` + Revision int64 `json:"revision"` + Items []SummaryEntry `json:"items"` +} + +type StartParams struct { + CallerID string + AccountID string + Surface string + Model string + Stream bool + UserInput string + Messages []Message + HistoryText string + FinalPrompt string +} + +type UpdateParams struct { + Status string + ReasoningContent string + Content string + Error string + StatusCode int + ElapsedMs int64 + FinishReason string + Usage map[string]any + Completed bool +} + +type detailEnvelope struct { + Version int `json:"version"` + Item Entry `json:"item"` +} + +type legacyFile struct { + Version int `json:"version"` + Limit int `json:"limit"` + Items []Entry `json:"items"` +} + +type legacyProbe struct { + Items []map[string]json.RawMessage `json:"items"` +} + +type Store struct { + mu sync.Mutex + path string + detailDir string + state File + details map[string]Entry + dirty map[string]struct{} + deleted map[string]struct{} + err error +} + +func New(path string) *Store { + s := &Store{ + path: strings.TrimSpace(path), + detailDir: strings.TrimSpace(path) + ".d", + state: File{ + Version: FileVersion, + Limit: DefaultLimit, + Revision: 0, + Items: []SummaryEntry{}, + }, + details: map[string]Entry{}, + dirty: map[string]struct{}{}, + deleted: map[string]struct{}{}, + } + s.mu.Lock() + defer s.mu.Unlock() + s.err = s.loadLocked() + return s +} + +func (s *Store) Path() string { + if s == nil { + return "" + } + return s.path +} + +func (s *Store) DetailDir() string { + if s == nil { + return "" + } + return s.detailDir +} + +func (s *Store) Err() error { + if s == nil { + return errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + return s.err +} + +func (s *Store) Snapshot() (File, error) { + if s == nil { + return File{}, errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return File{}, s.err + } + return cloneFile(s.state), nil +} + +func (s *Store) Revision() (int64, error) { + if s == nil { + return 0, errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return 0, s.err + } + return s.state.Revision, nil +} + +func (s *Store) Enabled() bool { + if s == nil { + return false + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return false + } + return s.state.Limit != DisabledLimit +} + +func (s *Store) Get(id string) (Entry, error) { + if s == nil { + return Entry{}, errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return Entry{}, s.err + } + item, ok := s.details[strings.TrimSpace(id)] + if !ok { + return Entry{}, errors.New("chat history entry not found") + } + return cloneEntry(item), nil +} + +func (s *Store) DetailRevision(id string) (int64, error) { + if s == nil { + return 0, errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return 0, s.err + } + item, ok := s.details[strings.TrimSpace(id)] + if !ok { + return 0, errors.New("chat history entry not found") + } + return item.Revision, nil +} + +func (s *Store) Start(params StartParams) (Entry, error) { + if s == nil { + return Entry{}, errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return Entry{}, s.err + } + if s.state.Limit == DisabledLimit { + return Entry{}, ErrDisabled + } + now := time.Now().UnixMilli() + revision := s.nextRevisionLocked() + entry := Entry{ + ID: "chat_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + Revision: revision, + CreatedAt: now, + UpdatedAt: now, + Status: "streaming", + CallerID: strings.TrimSpace(params.CallerID), + AccountID: strings.TrimSpace(params.AccountID), + Surface: strings.TrimSpace(params.Surface), + Model: strings.TrimSpace(params.Model), + Stream: params.Stream, + UserInput: strings.TrimSpace(params.UserInput), + Messages: cloneMessages(params.Messages), + HistoryText: params.HistoryText, + FinalPrompt: strings.TrimSpace(params.FinalPrompt), + } + s.details[entry.ID] = entry + s.markDetailDirtyLocked(entry.ID) + s.rebuildIndexLocked() + if err := s.saveLocked(); err != nil { + return cloneEntry(entry), err + } + return cloneEntry(entry), nil +} + +func (s *Store) Update(id string, params UpdateParams) (Entry, error) { + if s == nil { + return Entry{}, errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return Entry{}, s.err + } + target := strings.TrimSpace(id) + if target == "" { + return Entry{}, errors.New("history id is required") + } + item, ok := s.details[target] + if !ok { + return Entry{}, errors.New("chat history entry not found") + } + now := time.Now().UnixMilli() + item.Revision = s.nextRevisionLocked() + item.UpdatedAt = now + if params.Status != "" { + item.Status = params.Status + } + if params.ReasoningContent != "" || item.ReasoningContent == "" { + item.ReasoningContent = params.ReasoningContent + } + if params.Content != "" || item.Content == "" { + item.Content = params.Content + } + item.Error = strings.TrimSpace(params.Error) + item.StatusCode = params.StatusCode + item.ElapsedMs = params.ElapsedMs + item.FinishReason = strings.TrimSpace(params.FinishReason) + if params.Usage != nil { + item.Usage = cloneMap(params.Usage) + } + if params.Completed { + item.CompletedAt = now + } + s.details[target] = item + s.markDetailDirtyLocked(target) + s.rebuildIndexLocked() + if err := s.saveLocked(); err != nil { + return Entry{}, err + } + return cloneEntry(item), nil +} + +func (s *Store) Delete(id string) error { + if s == nil { + return errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return s.err + } + target := strings.TrimSpace(id) + if target == "" { + return errors.New("history id is required") + } + if _, ok := s.details[target]; !ok { + return errors.New("chat history entry not found") + } + s.markDetailDeletedLocked(target) + delete(s.details, target) + s.nextRevisionLocked() + s.rebuildIndexLocked() + if err := s.saveLocked(); err != nil { + return err + } + return nil +} + +func (s *Store) Clear() error { + if s == nil { + return errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return s.err + } + for id := range s.details { + s.markDetailDeletedLocked(id) + } + s.details = map[string]Entry{} + s.nextRevisionLocked() + s.rebuildIndexLocked() + if err := s.saveLocked(); err != nil { + return err + } + return nil +} + +func (s *Store) SetLimit(limit int) (File, error) { + if s == nil { + return File{}, errors.New("chat history store is nil") + } + s.mu.Lock() + defer s.mu.Unlock() + if s.err != nil { + return File{}, s.err + } + if !isAllowedLimit(limit) { + return File{}, fmt.Errorf("unsupported chat history limit: %d", limit) + } + s.state.Limit = limit + s.nextRevisionLocked() + s.rebuildIndexLocked() + if err := s.saveLocked(); err != nil { + return File{}, err + } + return cloneFile(s.state), nil +} + +func (s *Store) loadLocked() error { + if strings.TrimSpace(s.path) == "" { + return errors.New("chat history path is required") + } + if err := os.MkdirAll(filepath.Dir(s.path), 0o755); err != nil && filepath.Dir(s.path) != "." { + return fmt.Errorf("create chat history dir: %w", err) + } + if err := os.MkdirAll(s.detailDir, 0o755); err != nil { + return fmt.Errorf("create chat history detail dir: %w", err) + } + + raw, err := os.ReadFile(s.path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + if saveErr := s.saveLocked(); saveErr != nil { + config.Logger.Warn("[chat_history] bootstrap write failed", "path", s.path, "error", saveErr) + } + return nil + } + return fmt.Errorf("read chat history index: %w", err) + } + + legacy, legacyOK, legacyErr := parseLegacy(raw) + if legacyErr != nil { + return legacyErr + } + if legacyOK { + s.loadLegacyLocked(legacy) + if err := s.saveLocked(); err != nil { + config.Logger.Warn("[chat_history] legacy migration writeback failed", "path", s.path, "error", err) + } + return nil + } + + var state File + if err := json.Unmarshal(raw, &state); err != nil { + return fmt.Errorf("decode chat history index: %w", err) + } + if state.Version == 0 { + state.Version = FileVersion + } + if !isAllowedLimit(state.Limit) { + state.Limit = DefaultLimit + } + s.state = cloneFile(state) + s.details = map[string]Entry{} + for _, item := range state.Items { + detail, err := readDetailFile(filepath.Join(s.detailDir, item.ID+".json")) + if err != nil { + return err + } + s.details[item.ID] = detail + } + s.rebuildIndexLocked() + if saveErr := s.saveLocked(); saveErr != nil { + config.Logger.Warn("[chat_history] index rewrite failed", "path", s.path, "error", saveErr) + } + return nil +} + +func (s *Store) loadLegacyLocked(legacy legacyFile) { + s.state.Version = FileVersion + s.state.Limit = legacy.Limit + if !isAllowedLimit(s.state.Limit) { + s.state.Limit = DefaultLimit + } + s.details = map[string]Entry{} + s.dirty = map[string]struct{}{} + s.deleted = map[string]struct{}{} + maxRevision := int64(0) + for _, item := range legacy.Items { + if strings.TrimSpace(item.ID) == "" { + continue + } + item.Messages = cloneMessages(item.Messages) + if item.Revision == 0 { + if item.UpdatedAt > 0 { + item.Revision = item.UpdatedAt + } else { + item.Revision = time.Now().UnixNano() + } + } + if item.Revision > maxRevision { + maxRevision = item.Revision + } + s.details[item.ID] = item + s.markDetailDirtyLocked(item.ID) + } + s.state.Revision = maxRevision + s.rebuildIndexLocked() +} + +func (s *Store) saveLocked() error { + s.state.Version = FileVersion + if !isAllowedLimit(s.state.Limit) { + s.state.Limit = DefaultLimit + } + s.rebuildIndexLocked() + + if err := os.MkdirAll(s.detailDir, 0o755); err != nil { + return fmt.Errorf("create chat history detail dir: %w", err) + } + for _, id := range sortedDetailIDs(s.deleted) { + path := filepath.Join(s.detailDir, id+".json") + if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("remove stale chat history detail: %w", err) + } + } + for _, id := range sortedDetailIDs(s.dirty) { + item, ok := s.details[id] + if !ok { + continue + } + path := filepath.Join(s.detailDir, id+".json") + payload, err := json.MarshalIndent(detailEnvelope{ + Version: FileVersion, + Item: item, + }, "", " ") + if err != nil { + return fmt.Errorf("encode chat history detail: %w", err) + } + if err := writeFileAtomic(path, append(payload, '\n')); err != nil { + return err + } + } + + payload, err := json.MarshalIndent(s.state, "", " ") + if err != nil { + return fmt.Errorf("encode chat history index: %w", err) + } + if err := writeFileAtomic(s.path, append(payload, '\n')); err != nil { + return err + } + s.clearPendingDetailChangesLocked() + return nil +} + +func (s *Store) rebuildIndexLocked() { + summaries := make([]SummaryEntry, 0, len(s.details)) + for _, item := range s.details { + summaries = append(summaries, summaryFromEntry(item)) + } + sort.Slice(summaries, func(i, j int) bool { + if summaries[i].CreatedAt == summaries[j].CreatedAt { + if summaries[i].Revision == summaries[j].Revision { + return summaries[i].UpdatedAt > summaries[j].UpdatedAt + } + return summaries[i].Revision > summaries[j].Revision + } + return summaries[i].CreatedAt > summaries[j].CreatedAt + }) + if s.state.Limit < DisabledLimit || !isAllowedLimit(s.state.Limit) { + s.state.Limit = DefaultLimit + } + if s.state.Limit == DisabledLimit { + s.state.Items = summaries + return + } + if len(summaries) > s.state.Limit { + keep := make(map[string]struct{}, s.state.Limit) + for _, item := range summaries[:s.state.Limit] { + keep[item.ID] = struct{}{} + } + for id := range s.details { + if _, ok := keep[id]; !ok { + s.markDetailDeletedLocked(id) + delete(s.details, id) + } + } + summaries = summaries[:s.state.Limit] + } + s.state.Items = summaries +} + +func (s *Store) nextRevisionLocked() int64 { + next := time.Now().UnixNano() + if next <= s.state.Revision { + next = s.state.Revision + 1 + } + s.state.Revision = next + return next +} + +func summaryFromEntry(item Entry) SummaryEntry { + return SummaryEntry{ + ID: item.ID, + Revision: item.Revision, + CreatedAt: item.CreatedAt, + UpdatedAt: item.UpdatedAt, + CompletedAt: item.CompletedAt, + Status: item.Status, + CallerID: item.CallerID, + AccountID: item.AccountID, + Surface: item.Surface, + Model: item.Model, + Stream: item.Stream, + UserInput: item.UserInput, + Preview: buildPreview(item), + StatusCode: item.StatusCode, + ElapsedMs: item.ElapsedMs, + FinishReason: item.FinishReason, + DetailRevision: item.Revision, + } +} + +func buildPreview(item Entry) string { + candidate := strings.TrimSpace(item.Content) + if candidate == "" { + candidate = strings.TrimSpace(item.ReasoningContent) + } + if candidate == "" { + candidate = strings.TrimSpace(item.Error) + } + if candidate == "" { + candidate = strings.TrimSpace(item.UserInput) + } + if truncated, ok := util.TruncateRunes(candidate, defaultPreviewAt); ok { + return truncated + "..." + } + return candidate +} + +func readDetailFile(path string) (Entry, error) { + raw, err := os.ReadFile(path) + if err != nil { + return Entry{}, fmt.Errorf("read chat history detail: %w", err) + } + var env detailEnvelope + if err := json.Unmarshal(raw, &env); err != nil { + return Entry{}, fmt.Errorf("decode chat history detail: %w", err) + } + return cloneEntry(env.Item), nil +} + +func parseLegacy(raw []byte) (legacyFile, bool, error) { + var legacy legacyFile + if err := json.Unmarshal(raw, &legacy); err != nil { + return legacyFile{}, false, nil + } + if len(legacy.Items) == 0 { + return legacy, false, nil + } + var probe legacyProbe + if err := json.Unmarshal(raw, &probe); err == nil { + for _, item := range probe.Items { + if _, ok := item["detail_revision"]; ok { + return legacy, false, nil + } + } + } + return legacy, true, nil +} + +func writeFileAtomic(path string, body []byte) error { + dir := filepath.Dir(path) + if dir == "" { + dir = "." + } + if dir != "." { + if err := os.MkdirAll(dir, 0o755); err != nil { + return fmt.Errorf("create chat history dir: %w", err) + } + } + tmpFile, err := os.CreateTemp(dir, ".chat-history-*.tmp") + if err != nil { + return fmt.Errorf("create temp chat history: %w", err) + } + tmpPath := tmpFile.Name() + cleanup := func() error { + if err := os.Remove(tmpPath); err != nil && !errors.Is(err, os.ErrNotExist) { + return fmt.Errorf("remove temp chat history: %w", err) + } + return nil + } + withCleanup := func(primary error, closeErr error) error { + errs := []error{primary} + if closeErr != nil { + errs = append(errs, fmt.Errorf("close temp chat history: %w", closeErr)) + } + if cleanupErr := cleanup(); cleanupErr != nil { + errs = append(errs, cleanupErr) + } + return errors.Join(errs...) + } + if _, err := tmpFile.Write(body); err != nil { + return withCleanup(fmt.Errorf("write temp chat history: %w", err), tmpFile.Close()) + } + if err := tmpFile.Sync(); err != nil { + return withCleanup(fmt.Errorf("sync temp chat history: %w", err), tmpFile.Close()) + } + if err := tmpFile.Close(); err != nil { + if cleanupErr := cleanup(); cleanupErr != nil { + return errors.Join(fmt.Errorf("close temp chat history: %w", err), cleanupErr) + } + return fmt.Errorf("close temp chat history: %w", err) + } + if err := os.Rename(tmpPath, path); err != nil { + if cleanupErr := cleanup(); cleanupErr != nil { + return errors.Join(fmt.Errorf("promote temp chat history: %w", err), cleanupErr) + } + return fmt.Errorf("promote temp chat history: %w", err) + } + return nil +} + +func ListETag(revision int64) string { + return fmt.Sprintf(`W/"chat-history-list-%d"`, revision) +} + +func DetailETag(id string, revision int64) string { + return fmt.Sprintf(`W/"chat-history-detail-%s-%d"`, strings.TrimSpace(id), revision) +} + +func isAllowedLimit(limit int) bool { + _, ok := allowedLimits[limit] + return ok +} + +func (s *Store) markDetailDirtyLocked(id string) { + id = strings.TrimSpace(id) + if id == "" { + return + } + if s.dirty == nil { + s.dirty = map[string]struct{}{} + } + if s.deleted == nil { + s.deleted = map[string]struct{}{} + } + s.dirty[id] = struct{}{} + delete(s.deleted, id) +} + +func (s *Store) markDetailDeletedLocked(id string) { + id = strings.TrimSpace(id) + if id == "" { + return + } + if s.dirty == nil { + s.dirty = map[string]struct{}{} + } + if s.deleted == nil { + s.deleted = map[string]struct{}{} + } + s.deleted[id] = struct{}{} + delete(s.dirty, id) +} + +func (s *Store) clearPendingDetailChangesLocked() { + s.dirty = map[string]struct{}{} + s.deleted = map[string]struct{}{} +} + +func sortedDetailIDs(ids map[string]struct{}) []string { + if len(ids) == 0 { + return nil + } + out := make([]string, 0, len(ids)) + for id := range ids { + out = append(out, id) + } + sort.Strings(out) + return out +} + +func cloneFile(in File) File { + out := File{ + Version: in.Version, + Limit: in.Limit, + Revision: in.Revision, + Items: make([]SummaryEntry, len(in.Items)), + } + copy(out.Items, in.Items) + return out +} + +func cloneEntry(item Entry) Entry { + item.Usage = cloneMap(item.Usage) + item.Messages = cloneMessages(item.Messages) + return item +} + +func cloneMap(in map[string]any) map[string]any { + if in == nil { + return nil + } + out := make(map[string]any, len(in)) + for k, v := range in { + out[k] = v + } + return out +} + +func cloneMessages(messages []Message) []Message { + if len(messages) == 0 { + return []Message{} + } + out := make([]Message, len(messages)) + copy(out, messages) + return out +} diff --git a/internal/chathistory/store_test.go b/internal/chathistory/store_test.go new file mode 100644 index 0000000000000000000000000000000000000000..14da00195067fa3a6eb596e7c25db627298951ae --- /dev/null +++ b/internal/chathistory/store_test.go @@ -0,0 +1,635 @@ +package chathistory + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "strings" + "sync" + "testing" + "time" + "unicode/utf8" +) + +func blockDetailDir(t *testing.T, detailDir string) func() { + t.Helper() + blockedDir := detailDir + ".blocked" + if err := os.RemoveAll(blockedDir); err != nil { + t.Fatalf("remove blocked detail dir failed: %v", err) + } + if err := os.Rename(detailDir, blockedDir); err != nil { + t.Fatalf("move detail dir aside failed: %v", err) + } + if err := os.RemoveAll(detailDir); err != nil { + t.Fatalf("remove blocked detail path failed: %v", err) + } + if err := os.WriteFile(detailDir, []byte("blocked"), 0o644); err != nil { + t.Fatalf("write blocked detail path failed: %v", err) + } + var once sync.Once + return func() { + t.Helper() + once.Do(func() { + if err := os.RemoveAll(detailDir); err != nil { + t.Fatalf("remove blocking detail path failed: %v", err) + } + if err := os.Rename(blockedDir, detailDir); err != nil { + t.Fatalf("restore detail dir failed: %v", err) + } + }) + } +} + +func TestStoreCreatesAndPersistsEntries(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + started, err := store.Start(StartParams{ + CallerID: "caller:abc", + AccountID: "user@example.com", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "hello", + }) + if err != nil { + t.Fatalf("start entry failed: %v", err) + } + + updated, err := store.Update(started.ID, UpdateParams{ + Status: "success", + ReasoningContent: "thinking", + Content: "answer", + StatusCode: 200, + ElapsedMs: 321, + FinishReason: "stop", + Usage: map[string]any{"total_tokens": 9}, + Completed: true, + }) + if err != nil { + t.Fatalf("update entry failed: %v", err) + } + if updated.Status != "success" || updated.Content != "answer" { + t.Fatalf("unexpected updated entry: %#v", updated) + } + + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if snapshot.Limit != DefaultLimit { + t.Fatalf("unexpected default limit: %d", snapshot.Limit) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one item, got %d", len(snapshot.Items)) + } + if snapshot.Items[0].CompletedAt == 0 { + t.Fatalf("expected completed_at to be populated") + } + if snapshot.Items[0].Preview != "answer" { + t.Fatalf("expected summary preview=answer, got %#v", snapshot.Items[0]) + } + + reloaded := New(path) + reloadedSnapshot, err := reloaded.Snapshot() + if err != nil { + t.Fatalf("reload snapshot failed: %v", err) + } + if len(reloadedSnapshot.Items) != 1 { + t.Fatalf("unexpected reloaded summaries: %#v", reloadedSnapshot.Items) + } + full, err := reloaded.Get(started.ID) + if err != nil { + t.Fatalf("get detail failed: %v", err) + } + if full.Content != "answer" { + t.Fatalf("expected detail content=answer, got %#v", full) + } +} + +func TestBuildPreviewPreservesUTF8MB4Characters(t *testing.T) { + long := strings.Repeat("😀", defaultPreviewAt+1) + preview := buildPreview(Entry{Content: long}) + if !utf8.ValidString(preview) { + t.Fatalf("expected valid utf-8 preview, got %q", preview) + } + if preview != strings.Repeat("😀", defaultPreviewAt)+"..." { + t.Fatalf("unexpected preview: %q", preview) + } +} + +func TestStoreTrimsToConfiguredLimit(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + if _, err := store.SetLimit(10); err != nil { + t.Fatalf("set limit failed: %v", err) + } + + for i := 0; i < 12; i++ { + entry, err := store.Start(StartParams{Model: "deepseek-v4-flash", UserInput: "msg"}) + if err != nil { + t.Fatalf("start %d failed: %v", i, err) + } + if _, err := store.Update(entry.ID, UpdateParams{Status: "success", Content: "ok", Completed: true}); err != nil { + t.Fatalf("update %d failed: %v", i, err) + } + } + + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 10 { + t.Fatalf("expected 10 items, got %d", len(snapshot.Items)) + } +} + +func TestStoreDeleteClearAndLimitValidation(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + entry, err := store.Start(StartParams{UserInput: "hello"}) + if err != nil { + t.Fatalf("start failed: %v", err) + } + if err := store.Delete(entry.ID); err != nil { + t.Fatalf("delete failed: %v", err) + } + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 0 { + t.Fatalf("expected empty items after delete, got %d", len(snapshot.Items)) + } + if _, err := store.SetLimit(999); err == nil { + t.Fatalf("expected invalid limit error") + } + if err := store.Clear(); err != nil { + t.Fatalf("clear failed: %v", err) + } +} + +func TestStoreDisablePreservesHistoryAndBlocksNewEntries(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + entry, err := store.Start(StartParams{UserInput: "hello"}) + if err != nil { + t.Fatalf("start failed: %v", err) + } + if _, err := store.Update(entry.ID, UpdateParams{Status: "success", Content: "world", Completed: true}); err != nil { + t.Fatalf("update failed: %v", err) + } + + snapshot, err := store.SetLimit(DisabledLimit) + if err != nil { + t.Fatalf("disable failed: %v", err) + } + if snapshot.Limit != DisabledLimit { + t.Fatalf("expected disabled limit, got %d", snapshot.Limit) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected disabled mode to preserve summaries, got %d", len(snapshot.Items)) + } + if store.Enabled() { + t.Fatalf("expected store to report disabled") + } + if _, err := store.Start(StartParams{UserInput: "later"}); err != ErrDisabled { + t.Fatalf("expected ErrDisabled, got %v", err) + } +} + +func TestStoreConcurrentUpdatesKeepSplitFilesValid(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + var wg sync.WaitGroup + for i := 0; i < 8; i++ { + wg.Add(1) + go func(idx int) { + defer wg.Done() + entry, err := store.Start(StartParams{ + CallerID: "caller:test", + Model: "deepseek-v4-flash", + UserInput: "hello", + }) + if err != nil { + t.Errorf("start failed: %v", err) + return + } + _, err = store.Update(entry.ID, UpdateParams{ + Status: "success", + Content: "answer", + ElapsedMs: int64(idx), + Completed: true, + }) + if err != nil { + t.Errorf("update failed: %v", err) + } + }(i) + } + wg.Wait() + + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 8 { + t.Fatalf("expected 8 items, got %d", len(snapshot.Items)) + } + + raw, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read index failed: %v", err) + } + var persisted File + if err := json.Unmarshal(raw, &persisted); err != nil { + t.Fatalf("persisted index is invalid json: %v", err) + } + if len(persisted.Items) != 8 { + t.Fatalf("expected persisted items=8, got %d", len(persisted.Items)) + } + + detailFiles, err := os.ReadDir(path + ".d") + if err != nil { + t.Fatalf("read detail dir failed: %v", err) + } + if len(detailFiles) != 8 { + t.Fatalf("expected 8 detail files, got %d", len(detailFiles)) + } +} + +func TestStoreAutoMigratesLegacyMonolith(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + legacy := legacyFile{ + Version: 1, + Limit: 20, + Items: []Entry{{ + ID: "chat_legacy", + CreatedAt: 1, + UpdatedAt: 2, + Status: "success", + UserInput: "hello", + Content: "world", + ReasoningContent: "thinking", + }}, + } + body, _ := json.MarshalIndent(legacy, "", " ") + if err := os.WriteFile(path, body, 0o644); err != nil { + t.Fatalf("write legacy file failed: %v", err) + } + + store := New(path) + if err := store.Err(); err != nil { + t.Fatalf("expected legacy migration success, got %v", err) + } + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one migrated summary, got %#v", snapshot.Items) + } + full, err := store.Get("chat_legacy") + if err != nil { + t.Fatalf("get migrated detail failed: %v", err) + } + if full.Content != "world" { + t.Fatalf("expected migrated detail content preserved, got %#v", full) + } +} + +func TestStoreAutoMigratesMetadataOnlyLegacyMonolith(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + legacy := legacyFile{ + Version: 1, + Limit: 20, + Items: []Entry{{ + ID: "chat_metadata_only", + Revision: 0, + CreatedAt: 1, + UpdatedAt: 2, + Status: "error", + CallerID: "caller:test", + AccountID: "acct:test", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "hello", + Error: "boom", + StatusCode: 500, + ElapsedMs: 12, + FinishReason: "error", + }}, + } + body, _ := json.MarshalIndent(legacy, "", " ") + if err := os.WriteFile(path, body, 0o644); err != nil { + t.Fatalf("write legacy file failed: %v", err) + } + + store := New(path) + if err := store.Err(); err != nil { + t.Fatalf("expected legacy metadata-only migration success, got %v", err) + } + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one migrated summary, got %#v", snapshot.Items) + } + full, err := store.Get("chat_metadata_only") + if err != nil { + t.Fatalf("get migrated detail failed: %v", err) + } + if full.Error != "boom" || full.UserInput != "hello" { + t.Fatalf("expected metadata-only legacy fields preserved, got %#v", full) + } + if _, err := os.Stat(filepath.Join(store.DetailDir(), "chat_metadata_only.json")); err != nil { + t.Fatalf("expected migrated detail file to exist: %v", err) + } +} + +func TestStoreLegacyMigrationBestEffortWhenRewriteFails(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + longID := "chat_" + strings.Repeat("x", 320) + legacy := legacyFile{ + Version: 1, + Limit: 20, + Items: []Entry{{ + ID: longID, + CreatedAt: 1, + UpdatedAt: 2, + Status: "success", + UserInput: "hello", + Content: "world", + }}, + } + body, err := json.MarshalIndent(legacy, "", " ") + if err != nil { + t.Fatalf("marshal legacy file failed: %v", err) + } + if err := os.WriteFile(path, body, 0o644); err != nil { + t.Fatalf("write legacy file failed: %v", err) + } + + store := New(path) + if err := store.Err(); err != nil { + t.Fatalf("expected store to stay usable after migration writeback failure, got %v", err) + } + if !store.Enabled() { + t.Fatal("expected store to remain enabled after best-effort migration") + } + + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 || snapshot.Items[0].ID != longID { + t.Fatalf("unexpected snapshot after best-effort migration: %#v", snapshot.Items) + } + full, err := store.Get(longID) + if err != nil { + t.Fatalf("get migrated detail failed: %v", err) + } + if full.Content != "world" { + t.Fatalf("expected migrated content to stay in memory, got %#v", full) + } + if _, statErr := os.Stat(filepath.Join(store.DetailDir(), longID+".json")); statErr == nil { + t.Fatal("expected detail write to fail for overlong legacy id") + } +} + +func TestStoreTransientPersistenceFailureDoesNotLatch(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + first, err := store.Start(StartParams{UserInput: "first"}) + if err != nil { + t.Fatalf("start first failed: %v", err) + } + restore := blockDetailDir(t, store.DetailDir()) + t.Cleanup(restore) + + blocked, err := store.Start(StartParams{UserInput: "blocked"}) + if err == nil { + t.Fatalf("expected start failure while detail dir is blocked") + } + if blocked.ID == "" { + t.Fatalf("expected in-memory entry from failed start") + } + if err := store.Err(); err != nil { + t.Fatalf("transient start failure should not latch store error: %v", err) + } + if _, err := store.Update(first.ID, UpdateParams{Status: "success", Content: "one", Completed: true}); err == nil { + t.Fatalf("expected update failure while detail dir is blocked") + } + if err := store.Err(); err != nil { + t.Fatalf("transient update failure should not latch store error: %v", err) + } + + restore() + + if _, err := store.Update(blocked.ID, UpdateParams{Status: "success", Content: "two", Completed: true}); err != nil { + t.Fatalf("update after restore failed: %v", err) + } + if _, err := store.Start(StartParams{UserInput: "later"}); err != nil { + t.Fatalf("start after restore failed: %v", err) + } + full, err := store.Get(blocked.ID) + if err != nil { + t.Fatalf("get restored entry failed: %v", err) + } + if full.Content != "two" || full.Status != "success" { + t.Fatalf("expected restored entry persisted, got %#v", full) + } +} + +func TestStoreWritesOnlyChangedDetailFiles(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + first, err := store.Start(StartParams{UserInput: "one"}) + if err != nil { + t.Fatalf("start first failed: %v", err) + } + if _, err := store.Update(first.ID, UpdateParams{Status: "success", Content: "first", Completed: true}); err != nil { + t.Fatalf("update first failed: %v", err) + } + second, err := store.Start(StartParams{UserInput: "two"}) + if err != nil { + t.Fatalf("start second failed: %v", err) + } + if _, err := store.Update(second.ID, UpdateParams{Status: "success", Content: "second", Completed: true}); err != nil { + t.Fatalf("update second failed: %v", err) + } + + firstPath := filepath.Join(store.DetailDir(), first.ID+".json") + secondPath := filepath.Join(store.DetailDir(), second.ID+".json") + beforeFirst, err := os.ReadFile(firstPath) + if err != nil { + t.Fatalf("read first detail before update failed: %v", err) + } + beforeSecond, err := os.ReadFile(secondPath) + if err != nil { + t.Fatalf("read second detail before update failed: %v", err) + } + + if _, err := store.Update(first.ID, UpdateParams{Status: "success", Content: "first-updated", Completed: true}); err != nil { + t.Fatalf("update first again failed: %v", err) + } + + afterFirst, err := os.ReadFile(firstPath) + if err != nil { + t.Fatalf("read first detail after update failed: %v", err) + } + afterSecond, err := os.ReadFile(secondPath) + if err != nil { + t.Fatalf("read second detail after update failed: %v", err) + } + + if bytes.Equal(beforeFirst, afterFirst) { + t.Fatalf("expected first detail file to change after update") + } + if !bytes.Equal(beforeSecond, afterSecond) { + t.Fatalf("expected untouched detail file to remain byte-identical") + } +} + +func TestStoreOrdersByCreationTimeNotStreamingUpdates(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + first, err := store.Start(StartParams{UserInput: "first"}) + if err != nil { + t.Fatalf("start first failed: %v", err) + } + time.Sleep(time.Millisecond) + second, err := store.Start(StartParams{UserInput: "second"}) + if err != nil { + t.Fatalf("start second failed: %v", err) + } + time.Sleep(time.Millisecond) + if _, err := store.Update(first.ID, UpdateParams{Status: "streaming", Content: "still running"}); err != nil { + t.Fatalf("update first failed: %v", err) + } + + snapshot, err := store.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 2 { + t.Fatalf("expected two items, got %#v", snapshot.Items) + } + if snapshot.Items[0].ID != second.ID || snapshot.Items[1].ID != first.ID { + t.Fatalf("expected creation-time order to stay stable, got %#v", snapshot.Items) + } +} + +func TestUpdatePreservesContentWhenNewContentIsEmpty(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + started, err := store.Start(StartParams{ + CallerID: "caller:abc", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "hello", + }) + if err != nil { + t.Fatalf("start entry failed: %v", err) + } + + if _, err := store.Update(started.ID, UpdateParams{ + Status: "streaming", + ReasoningContent: "let me think", + Content: "I'll help you with that.", + }); err != nil { + t.Fatalf("progress update failed: %v", err) + } + + updated, err := store.Update(started.ID, UpdateParams{ + Status: "success", + Content: "", + Completed: true, + }) + if err != nil { + t.Fatalf("success update failed: %v", err) + } + + if updated.Content != "I'll help you with that." { + t.Fatalf("expected content to be preserved, got %q", updated.Content) + } + if updated.ReasoningContent != "let me think" { + t.Fatalf("expected reasoning content to be preserved, got %q", updated.ReasoningContent) + } + + full, err := store.Get(started.ID) + if err != nil { + t.Fatalf("get entry failed: %v", err) + } + if full.Content != "I'll help you with that." { + t.Fatalf("expected persisted content to be preserved, got %q", full.Content) + } + if full.ReasoningContent != "let me think" { + t.Fatalf("expected persisted reasoning content to be preserved, got %q", full.ReasoningContent) + } +} + +func TestUpdateAllowsSettingContentFromEmpty(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + started, err := store.Start(StartParams{ + CallerID: "caller:abc", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "hello", + }) + if err != nil { + t.Fatalf("start entry failed: %v", err) + } + + updated, err := store.Update(started.ID, UpdateParams{ + Status: "success", + Content: "final answer", + }) + if err != nil { + t.Fatalf("update failed: %v", err) + } + if updated.Content != "final answer" { + t.Fatalf("expected content to be set, got %q", updated.Content) + } +} + +func TestUpdateAllowsOverwritingContentWithNewValue(t *testing.T) { + path := filepath.Join(t.TempDir(), "chat_history.json") + store := New(path) + + started, err := store.Start(StartParams{ + CallerID: "caller:abc", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "hello", + }) + if err != nil { + t.Fatalf("start entry failed: %v", err) + } + + if _, err := store.Update(started.ID, UpdateParams{ + Status: "streaming", + Content: "partial", + }); err != nil { + t.Fatalf("first update failed: %v", err) + } + + updated, err := store.Update(started.ID, UpdateParams{ + Status: "success", + Content: "final answer", + }) + if err != nil { + t.Fatalf("second update failed: %v", err) + } + if updated.Content != "final answer" { + t.Fatalf("expected content to be overwritten, got %q", updated.Content) + } +} diff --git a/internal/claudeconv/convert.go b/internal/claudeconv/convert.go new file mode 100644 index 0000000000000000000000000000000000000000..cd6e156e30567d814c84d3e57c153c0ed4968766 --- /dev/null +++ b/internal/claudeconv/convert.go @@ -0,0 +1,37 @@ +package claudeconv + +import ( + "strings" + + "ds2api/internal/config" +) + +func ConvertClaudeToDeepSeek(claudeReq map[string]any, aliasProvider config.ModelAliasReader, defaultClaudeModel string) map[string]any { + messages, _ := claudeReq["messages"].([]any) + model, _ := claudeReq["model"].(string) + if model == "" { + model = defaultClaudeModel + } + + dsModel, ok := config.ResolveModel(aliasProvider, model) + if !ok || strings.TrimSpace(dsModel) == "" { + dsModel = "deepseek-v4-flash" + } + + convertedMessages := make([]any, 0, len(messages)+1) + if system, ok := claudeReq["system"].(string); ok && system != "" { + convertedMessages = append(convertedMessages, map[string]any{"role": "system", "content": system}) + } + convertedMessages = append(convertedMessages, messages...) + + out := map[string]any{"model": dsModel, "messages": convertedMessages} + for _, k := range []string{"temperature", "top_p", "stream"} { + if v, ok := claudeReq[k]; ok { + out[k] = v + } + } + if stopSeq, ok := claudeReq["stop_sequences"]; ok { + out["stop"] = stopSeq + } + return out +} diff --git a/internal/compat/go_compat_test.go b/internal/compat/go_compat_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f3701c50e643e8c17232f6df26a2cf201d00ce7a --- /dev/null +++ b/internal/compat/go_compat_test.go @@ -0,0 +1,120 @@ +package compat + +import ( + "encoding/json" + "os" + "path/filepath" + "reflect" + "testing" + + "ds2api/internal/sse" + "ds2api/internal/util" +) + +func TestGoCompatSSEFixtures(t *testing.T) { + files, err := filepath.Glob(compatPath("fixtures", "sse_chunks", "*.json")) + if err != nil { + t.Fatalf("glob fixtures failed: %v", err) + } + if len(files) == 0 { + t.Fatal("no sse fixtures found") + } + for _, fixturePath := range files { + name := trimExt(filepath.Base(fixturePath)) + expectedPath := compatPath("expected", "sse_"+name+".json") + + var fixture struct { + Chunk map[string]any `json:"chunk"` + ThinkingEnable bool `json:"thinking_enabled"` + CurrentType string `json:"current_type"` + } + mustLoadJSON(t, fixturePath, &fixture) + + var expected struct { + Parts []map[string]any `json:"parts"` + Finished bool `json:"finished"` + NewType string `json:"new_type"` + ContentFilter bool `json:"content_filter"` + ErrorMessage string `json:"error_message"` + } + mustLoadJSON(t, expectedPath, &expected) + + raw, err := json.Marshal(fixture.Chunk) + if err != nil { + t.Fatalf("marshal fixture %s failed: %v", name, err) + } + res := sse.ParseDeepSeekContentLine(append([]byte("data: "), raw...), fixture.ThinkingEnable, fixture.CurrentType) + gotParts := make([]map[string]any, 0, len(res.Parts)) + for _, p := range res.Parts { + gotParts = append(gotParts, map[string]any{ + "text": p.Text, + "type": p.Type, + }) + } + if !reflect.DeepEqual(gotParts, expected.Parts) || + res.Stop != expected.Finished || + res.NextType != expected.NewType || + res.ContentFilter != expected.ContentFilter || + res.ErrorMessage != expected.ErrorMessage { + t.Fatalf("fixture %s mismatch:\n got parts=%#v finished=%v newType=%q contentFilter=%v errorMessage=%q\nwant parts=%#v finished=%v newType=%q contentFilter=%v errorMessage=%q", + name, gotParts, res.Stop, res.NextType, res.ContentFilter, res.ErrorMessage, + expected.Parts, expected.Finished, expected.NewType, expected.ContentFilter, expected.ErrorMessage) + } + } +} + +func TestGoCompatTokenFixtures(t *testing.T) { + var fixture struct { + Cases []struct { + Name string `json:"name"` + Text string `json:"text"` + } `json:"cases"` + } + mustLoadJSON(t, compatPath("fixtures", "token_cases.json"), &fixture) + + var expected struct { + Cases []struct { + Name string `json:"name"` + Tokens int `json:"tokens"` + } `json:"cases"` + } + mustLoadJSON(t, compatPath("expected", "token_cases.json"), &expected) + + expectByName := map[string]int{} + for _, c := range expected.Cases { + expectByName[c.Name] = c.Tokens + } + for _, c := range fixture.Cases { + want, ok := expectByName[c.Name] + if !ok { + t.Fatalf("missing expected token case: %s", c.Name) + } + got := util.EstimateTokens(c.Text) + if got != want { + t.Fatalf("token fixture %s mismatch: got=%d want=%d", c.Name, got, want) + } + } +} + +func mustLoadJSON(t *testing.T, path string, out any) { + t.Helper() + b, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read %s failed: %v", path, err) + } + if err := json.Unmarshal(b, out); err != nil { + t.Fatalf("decode %s failed: %v", path, err) + } +} + +func trimExt(name string) string { + if len(name) > 5 && name[len(name)-5:] == ".json" { + return name[:len(name)-5] + } + return name +} + +func compatPath(parts ...string) string { + prefix := []string{"..", "..", "tests", "compat"} + return filepath.Join(append(prefix, parts...)...) +} diff --git a/internal/completionruntime/nonstream.go b/internal/completionruntime/nonstream.go new file mode 100644 index 0000000000000000000000000000000000000000..bc589c61161c135bbc04d2b094b449804028b18a --- /dev/null +++ b/internal/completionruntime/nonstream.go @@ -0,0 +1,282 @@ +package completionruntime + +import ( + "context" + "fmt" + "io" + "net/http" + "strings" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" + "ds2api/internal/sse" +) + +type DeepSeekCaller interface { + CreateSession(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + GetPow(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + UploadFile(ctx context.Context, a *auth.RequestAuth, req dsclient.UploadFileRequest, maxAttempts int) (*dsclient.UploadFileResult, error) + CallCompletion(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, maxAttempts int) (*http.Response, error) +} + +type Options struct { + StripReferenceMarkers bool + MaxAttempts int + RetryEnabled bool + RetryMaxAttempts int + CurrentInputFile history.CurrentInputConfigReader +} + +type NonStreamResult struct { + SessionID string + Payload map[string]any + Turn assistantturn.Turn + Attempts int +} + +type StartResult struct { + SessionID string + Payload map[string]any + Pow string + Response *http.Response + Request promptcompat.StandardRequest +} + +func StartCompletion(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, stdReq promptcompat.StandardRequest, opts Options) (StartResult, *assistantturn.OutputError) { + maxAttempts := opts.MaxAttempts + if maxAttempts <= 0 { + maxAttempts = 3 + } + var prepErr *assistantturn.OutputError + stdReq, prepErr = prepareCurrentInputFile(ctx, ds, a, stdReq, opts) + if prepErr != nil { + return StartResult{Request: stdReq}, prepErr + } + sessionID, err := ds.CreateSession(ctx, a, maxAttempts) + if err != nil { + return StartResult{Request: stdReq}, authOutputError(a) + } + pow, err := ds.GetPow(ctx, a, maxAttempts) + if err != nil { + return StartResult{SessionID: sessionID, Request: stdReq}, &assistantturn.OutputError{Status: http.StatusUnauthorized, Message: "Failed to get PoW (invalid token or unknown error).", Code: "error"} + } + payload := stdReq.CompletionPayload(sessionID) + resp, err := ds.CallCompletion(ctx, a, payload, pow, maxAttempts) + if err != nil { + return StartResult{SessionID: sessionID, Payload: payload, Pow: pow, Request: stdReq}, &assistantturn.OutputError{Status: http.StatusInternalServerError, Message: "Failed to get completion.", Code: "error"} + } + return StartResult{SessionID: sessionID, Payload: payload, Pow: pow, Response: resp, Request: stdReq}, nil +} + +func prepareCurrentInputFile(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, stdReq promptcompat.StandardRequest, opts Options) (promptcompat.StandardRequest, *assistantturn.OutputError) { + if opts.CurrentInputFile == nil || stdReq.CurrentInputFileApplied { + return stdReq, nil + } + out, err := (history.Service{Store: opts.CurrentInputFile, DS: ds}).ApplyCurrentInputFile(ctx, a, stdReq) + if err != nil { + status, message := history.MapError(err) + return out, &assistantturn.OutputError{Status: status, Message: message, Code: "error"} + } + return out, nil +} + +func ExecuteNonStreamWithRetry(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, stdReq promptcompat.StandardRequest, opts Options) (NonStreamResult, *assistantturn.OutputError) { + start, startErr := StartCompletion(ctx, ds, a, stdReq, opts) + if startErr != nil { + return NonStreamResult{SessionID: start.SessionID, Payload: start.Payload}, startErr + } + return ExecuteNonStreamStartedWithRetry(ctx, ds, a, start, opts) +} + +func ExecuteNonStreamStartedWithRetry(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, start StartResult, opts Options) (NonStreamResult, *assistantturn.OutputError) { + stdReq := start.Request + maxAttempts := opts.MaxAttempts + if maxAttempts <= 0 { + maxAttempts = 3 + } + sessionID := start.SessionID + payload := start.Payload + pow := start.Pow + + attempts := 0 + accountSwitchAttempted := false + currentResp := start.Response + usagePrompt := stdReq.PromptTokenText + accumulatedThinking := "" + accumulatedRawThinking := "" + accumulatedToolDetectionThinking := "" + for { + turn, outErr := collectAttempt(currentResp, stdReq, usagePrompt, opts) + if outErr != nil { + if canRetryOnAlternateAccount(ctx, a, outErr, opts.RetryEnabled, &accountSwitchAttempted) { + switched, switchErr := startStandardCompletionOnAlternateAccount(ctx, ds, a, stdReq, opts, maxAttempts) + if switchErr != nil { + return NonStreamResult{SessionID: sessionID, Payload: payload, Attempts: attempts}, switchErr + } + if switched.Response != nil { + config.Logger.Info("[completion_runtime_account_switch_retry] retrying after 429", "surface", stdReq.Surface, "stream", false, "account", a.AccountID) + sessionID = switched.SessionID + payload = switched.Payload + pow = switched.Pow + currentResp = switched.Response + usagePrompt = stdReq.PromptTokenText + accumulatedThinking = "" + accumulatedRawThinking = "" + accumulatedToolDetectionThinking = "" + continue + } + } + return NonStreamResult{SessionID: sessionID, Payload: payload, Attempts: attempts}, outErr + } + accumulatedThinking += sse.TrimContinuationOverlap(accumulatedThinking, turn.Thinking) + accumulatedRawThinking += sse.TrimContinuationOverlap(accumulatedRawThinking, turn.RawThinking) + accumulatedToolDetectionThinking += sse.TrimContinuationOverlap(accumulatedToolDetectionThinking, turn.DetectionThinking) + turn.Thinking = accumulatedThinking + turn.RawThinking = accumulatedRawThinking + turn.DetectionThinking = accumulatedToolDetectionThinking + turn = assistantturn.BuildTurnFromCollected(sse.CollectResult{ + Text: turn.RawText, + Thinking: turn.RawThinking, + ToolDetectionThinking: turn.DetectionThinking, + ContentFilter: turn.ContentFilter, + CitationLinks: turn.CitationLinks, + ResponseMessageID: turn.ResponseMessageID, + }, buildOptions(stdReq, usagePrompt, opts)) + + retryMax := opts.RetryMaxAttempts + if retryMax <= 0 { + retryMax = shared.EmptyOutputRetryMaxAttempts() + } + if !opts.RetryEnabled || !assistantturn.ShouldRetryEmptyOutput(turn, attempts, retryMax) { + if canRetryOnAlternateAccount(ctx, a, turn.Error, opts.RetryEnabled, &accountSwitchAttempted) { + switched, switchErr := startStandardCompletionOnAlternateAccount(ctx, ds, a, stdReq, opts, maxAttempts) + if switchErr != nil { + return NonStreamResult{SessionID: sessionID, Payload: payload, Turn: turn, Attempts: attempts}, switchErr + } + if switched.Response != nil { + config.Logger.Info("[completion_runtime_account_switch_retry] retrying after 429", "surface", stdReq.Surface, "stream", false, "account", a.AccountID) + sessionID = switched.SessionID + payload = switched.Payload + pow = switched.Pow + currentResp = switched.Response + usagePrompt = stdReq.PromptTokenText + accumulatedThinking = "" + accumulatedRawThinking = "" + accumulatedToolDetectionThinking = "" + continue + } + } + return NonStreamResult{SessionID: sessionID, Payload: payload, Turn: turn, Attempts: attempts}, turn.Error + } + + attempts++ + config.Logger.Info("[completion_runtime_empty_retry] attempting synthetic retry", "surface", stdReq.Surface, "stream", false, "retry_attempt", attempts, "parent_message_id", turn.ResponseMessageID) + retryPow, powErr := ds.GetPow(ctx, a, maxAttempts) + if powErr != nil { + config.Logger.Warn("[completion_runtime_empty_retry] retry PoW fetch failed, falling back to original PoW", "surface", stdReq.Surface, "retry_attempt", attempts, "error", powErr) + retryPow = pow + } + retryPayload := shared.ClonePayloadForEmptyOutputRetry(payload, turn.ResponseMessageID) + nextResp, err := ds.CallCompletion(ctx, a, retryPayload, retryPow, maxAttempts) + if err != nil { + return NonStreamResult{SessionID: sessionID, Payload: payload, Turn: turn, Attempts: attempts}, &assistantturn.OutputError{Status: http.StatusInternalServerError, Message: "Failed to get completion.", Code: "error"} + } + usagePrompt = shared.UsagePromptWithEmptyOutputRetry(usagePrompt, attempts) + currentResp = nextResp + } +} + +func canRetryOnAlternateAccount(ctx context.Context, a *auth.RequestAuth, outErr *assistantturn.OutputError, retryEnabled bool, attempted *bool) bool { + if outErr == nil || outErr.Status != http.StatusTooManyRequests { + return false + } + if !retryEnabled || attempted == nil || *attempted { + return false + } + if a == nil || !a.UseConfigToken { + return false + } + *attempted = true + return a.SwitchAccount(ctx) +} + +func startStandardCompletionOnAlternateAccount(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, stdReq promptcompat.StandardRequest, opts Options, maxAttempts int) (StartResult, *assistantturn.OutputError) { + var prepErr *assistantturn.OutputError + stdReq, prepErr = reuploadCurrentInputFileForAccount(ctx, ds, a, stdReq, opts) + if prepErr != nil { + return StartResult{Request: stdReq}, prepErr + } + sessionID, err := ds.CreateSession(ctx, a, maxAttempts) + if err != nil { + return StartResult{}, authOutputError(a) + } + pow, err := ds.GetPow(ctx, a, maxAttempts) + if err != nil { + return StartResult{SessionID: sessionID}, &assistantturn.OutputError{Status: http.StatusUnauthorized, Message: "Failed to get PoW (invalid token or unknown error).", Code: "error"} + } + payload := stdReq.CompletionPayload(sessionID) + resp, err := ds.CallCompletion(ctx, a, payload, pow, maxAttempts) + if err != nil { + return StartResult{SessionID: sessionID, Payload: payload, Pow: pow}, &assistantturn.OutputError{Status: http.StatusInternalServerError, Message: "Failed to get completion.", Code: "error"} + } + return StartResult{SessionID: sessionID, Payload: payload, Pow: pow, Response: resp, Request: stdReq}, nil +} + +func reuploadCurrentInputFileForAccount(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, stdReq promptcompat.StandardRequest, opts Options) (promptcompat.StandardRequest, *assistantturn.OutputError) { + if opts.CurrentInputFile == nil || !stdReq.CurrentInputFileApplied { + return stdReq, nil + } + out, err := (history.Service{Store: opts.CurrentInputFile, DS: ds}).ReuploadAppliedCurrentInputFile(ctx, a, stdReq) + if err != nil { + status, message := history.MapError(err) + return out, &assistantturn.OutputError{Status: status, Message: message, Code: "error"} + } + return out, nil +} + +func collectAttempt(resp *http.Response, stdReq promptcompat.StandardRequest, usagePrompt string, opts Options) (assistantturn.Turn, *assistantturn.OutputError) { + defer func() { + if err := resp.Body.Close(); err != nil { + config.Logger.Warn("[completion_runtime] response body close failed", "surface", stdReq.Surface, "error", err) + } + }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + message := strings.TrimSpace(string(body)) + if message == "" { + message = http.StatusText(resp.StatusCode) + } + return assistantturn.Turn{}, &assistantturn.OutputError{Status: resp.StatusCode, Message: message, Code: "error"} + } + result := sse.CollectStream(resp, stdReq.Thinking, false) + return assistantturn.BuildTurnFromCollected(result, buildOptions(stdReq, usagePrompt, opts)), nil +} + +func buildOptions(stdReq promptcompat.StandardRequest, prompt string, opts Options) assistantturn.BuildOptions { + return assistantturn.BuildOptions{ + Model: stdReq.ResponseModel, + Prompt: prompt, + RefFileTokens: stdReq.RefFileTokens, + SearchEnabled: stdReq.Search, + StripReferenceMarkers: opts.StripReferenceMarkers, + ToolNames: stdReq.ToolNames, + ToolsRaw: stdReq.ToolsRaw, + ToolChoice: stdReq.ToolChoice, + } +} + +func authOutputError(a *auth.RequestAuth) *assistantturn.OutputError { + if a != nil && a.UseConfigToken { + return &assistantturn.OutputError{Status: http.StatusUnauthorized, Message: "Account token is invalid. Please re-login the account in admin.", Code: "error"} + } + return &assistantturn.OutputError{Status: http.StatusUnauthorized, Message: "Invalid token. If this should be a DS2API key, add it to config.keys first.", Code: "error"} +} + +func Errorf(status int, format string, args ...any) *assistantturn.OutputError { + return &assistantturn.OutputError{Status: status, Message: fmt.Sprintf(format, args...), Code: "error"} +} diff --git a/internal/completionruntime/nonstream_test.go b/internal/completionruntime/nonstream_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0ceadf9b453132073075cddfdd4b8338229ffbdb --- /dev/null +++ b/internal/completionruntime/nonstream_test.go @@ -0,0 +1,333 @@ +package completionruntime + +import ( + "context" + "io" + "net/http" + "strings" + "testing" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/promptcompat" +) + +type fakeDeepSeekCaller struct { + responses []*http.Response + payloads []map[string]any + uploads []dsclient.UploadFileRequest + completionAccounts []string + sessionByAccount bool +} + +type currentInputRuntimeConfig struct{} + +func (currentInputRuntimeConfig) CurrentInputFileEnabled() bool { return true } +func (currentInputRuntimeConfig) CurrentInputFileMinChars() int { return 0 } + +func (f *fakeDeepSeekCaller) CreateSession(_ context.Context, a *auth.RequestAuth, _ int) (string, error) { + if f.sessionByAccount && a != nil && a.AccountID != "" { + return "session-" + a.AccountID, nil + } + return "session-1", nil +} + +func (f *fakeDeepSeekCaller) GetPow(context.Context, *auth.RequestAuth, int) (string, error) { + return "pow", nil +} + +func (f *fakeDeepSeekCaller) UploadFile(_ context.Context, a *auth.RequestAuth, req dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + f.uploads = append(f.uploads, req) + if a != nil && a.AccountID != "" { + return &dsclient.UploadFileResult{ID: "file-runtime-" + a.AccountID}, nil + } + return &dsclient.UploadFileResult{ID: "file-runtime-1"}, nil +} + +func (f *fakeDeepSeekCaller) CallCompletion(_ context.Context, a *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + f.payloads = append(f.payloads, payload) + if a != nil { + f.completionAccounts = append(f.completionAccounts, a.AccountID) + } + if len(f.responses) == 0 { + return sseHTTPResponse(http.StatusOK, `data: {"p":"response/content","v":"fallback"}`), nil + } + resp := f.responses[0] + f.responses = f.responses[1:] + return resp, nil +} + +func TestExecuteNonStreamWithRetryBuildsCanonicalTurn(t *testing.T) { + ds := &fakeDeepSeekCaller{responses: []*http.Response{sseHTTPResponse( + http.StatusOK, + `data: {"response_message_id":42,"p":"response/content","v":"{\"x\":1}"}`, + )}} + stdReq := promptcompat.StandardRequest{ + Surface: "test", + ResponseModel: "deepseek-v4-flash", + PromptTokenText: "prompt", + FinalPrompt: "final prompt", + ToolNames: []string{"Write"}, + ToolsRaw: []any{map[string]any{ + "name": "Write", + "input_schema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + }, + }, + }}, + } + + result, outErr := ExecuteNonStreamWithRetry(context.Background(), ds, &auth.RequestAuth{}, stdReq, Options{}) + if outErr != nil { + t.Fatalf("unexpected output error: %#v", outErr) + } + if result.SessionID != "session-1" { + t.Fatalf("session mismatch: %q", result.SessionID) + } + if got := result.Turn.ResponseMessageID; got != 42 { + t.Fatalf("response message id mismatch: %d", got) + } + if len(result.Turn.ToolCalls) != 1 { + t.Fatalf("expected one tool call, got %d", len(result.Turn.ToolCalls)) + } + if _, ok := result.Turn.ToolCalls[0].Input["content"].(string); !ok { + t.Fatalf("expected schema-normalized string argument, got %#v", result.Turn.ToolCalls[0].Input["content"]) + } + if result.Turn.Usage.InputTokens == 0 || result.Turn.Usage.TotalTokens == 0 { + t.Fatalf("expected usage to be populated, got %#v", result.Turn.Usage) + } +} + +func TestExecuteNonStreamWithRetrySwitchesManagedAccountBeforeFinal429(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","password":"pwd"}, + {"email":"acc2@test.com","password":"pwd"} + ] + }`) + store := config.LoadStore() + resolver := auth.NewResolver(store, account.NewPool(store), func(_ context.Context, acc config.Account) (string, error) { + return "token-" + acc.Identifier(), nil + }) + req, _ := http.NewRequest(http.MethodPost, "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + a, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer resolver.Release(a) + + ds := &fakeDeepSeekCaller{ + sessionByAccount: true, + responses: []*http.Response{ + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":11,"p":"response/thinking_content","v":"first empty"}`), + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":12,"p":"response/thinking_content","v":"retry empty"}`), + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":21,"p":"response/content","v":"ok from second account"}`), + }, + } + stdReq := promptcompat.StandardRequest{ + Surface: "test", + ResponseModel: "deepseek-v4-flash", + PromptTokenText: "prompt", + FinalPrompt: "final prompt", + Thinking: true, + } + + result, outErr := ExecuteNonStreamWithRetry(context.Background(), ds, a, stdReq, Options{RetryEnabled: true}) + if outErr != nil { + t.Fatalf("unexpected output error after account switch retry: %#v", outErr) + } + if result.Turn.Text != "ok from second account" { + t.Fatalf("text mismatch after switch retry: %q", result.Turn.Text) + } + if result.SessionID != "session-acc2@test.com" { + t.Fatalf("expected switched account session, got %q", result.SessionID) + } + wantAccounts := []string{"acc1@test.com", "acc1@test.com", "acc2@test.com"} + if len(ds.completionAccounts) != len(wantAccounts) { + t.Fatalf("completion account count mismatch: got %v want %v", ds.completionAccounts, wantAccounts) + } + for i, want := range wantAccounts { + if ds.completionAccounts[i] != want { + t.Fatalf("completion account %d = %q want %q (all=%v)", i, ds.completionAccounts[i], want, ds.completionAccounts) + } + } + if got := ds.payloads[2]["chat_session_id"]; got != "session-acc2@test.com" { + t.Fatalf("switched payload session mismatch: %#v", got) + } + if prompt, _ := ds.payloads[2]["prompt"].(string); strings.Contains(prompt, "Please provide a non-empty final answer or tool call.") { + t.Fatalf("expected fresh switched-account prompt without empty-output suffix, got %q", prompt) + } +} + +func TestExecuteNonStreamWithRetryReuploadsCurrentInputFileAfterAccountSwitch(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","password":"pwd"}, + {"email":"acc2@test.com","password":"pwd"} + ] + }`) + store := config.LoadStore() + resolver := auth.NewResolver(store, account.NewPool(store), func(_ context.Context, acc config.Account) (string, error) { + return "token-" + acc.Identifier(), nil + }) + req, _ := http.NewRequest(http.MethodPost, "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + a, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer resolver.Release(a) + + ds := &fakeDeepSeekCaller{ + sessionByAccount: true, + responses: []*http.Response{ + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":11,"p":"response/thinking_content","v":"first empty"}`), + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":12,"p":"response/thinking_content","v":"retry empty"}`), + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":21,"p":"response/content","v":"ok from second account"}`), + }, + } + stdReq := promptcompat.StandardRequest{ + Surface: "test", + RequestedModel: "deepseek-v4-flash", + ResolvedModel: "deepseek-v4-flash", + ResponseModel: "deepseek-v4-flash", + Messages: []any{ + map[string]any{"role": "user", "content": "large current input"}, + }, + PromptTokenText: "large current input", + FinalPrompt: "large current input", + Thinking: true, + } + + result, outErr := ExecuteNonStreamWithRetry(context.Background(), ds, a, stdReq, Options{ + RetryEnabled: true, + CurrentInputFile: currentInputRuntimeConfig{}, + }) + if outErr != nil { + t.Fatalf("unexpected output error after account switch retry: %#v", outErr) + } + if result.Turn.Text != "ok from second account" { + t.Fatalf("text mismatch after switch retry: %q", result.Turn.Text) + } + if len(ds.uploads) != 2 { + t.Fatalf("expected current input file uploaded once per account, got %d", len(ds.uploads)) + } + refIDs, _ := ds.payloads[2]["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-runtime-acc2@test.com" { + t.Fatalf("expected switched account ref_file_ids to use reuploaded file, got %#v", ds.payloads[2]["ref_file_ids"]) + } +} + +func TestExecuteNonStreamWithRetryUsesParentMessageForEmptyRetry(t *testing.T) { + ds := &fakeDeepSeekCaller{responses: []*http.Response{ + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":77,"p":"response/thinking_content","v":"plan"}`), + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":78,"p":"response/content","v":"ok"}`), + }} + stdReq := promptcompat.StandardRequest{ + Surface: "test", + ResponseModel: "deepseek-v4-flash", + PromptTokenText: "prompt", + FinalPrompt: "final prompt", + } + + result, outErr := ExecuteNonStreamWithRetry(context.Background(), ds, &auth.RequestAuth{}, stdReq, Options{RetryEnabled: true}) + if outErr != nil { + t.Fatalf("unexpected output error: %#v", outErr) + } + if result.Attempts != 1 { + t.Fatalf("expected one retry, got %d", result.Attempts) + } + if len(ds.payloads) != 2 { + t.Fatalf("expected two completion calls, got %d", len(ds.payloads)) + } + if got := ds.payloads[1]["parent_message_id"]; got != 77 { + t.Fatalf("retry parent_message_id mismatch: %#v", got) + } + if result.Turn.Text != "ok" { + t.Fatalf("retry text mismatch: %q", result.Turn.Text) + } +} + +func TestExecuteNonStreamWithRetryConvertsReferenceMarkers(t *testing.T) { + ds := &fakeDeepSeekCaller{responses: []*http.Response{sseHTTPResponse( + http.StatusOK, + `data: {"p":"response/content","v":"答案[reference:0]。","citation":{"cite_index":0,"url":"https://example.com/ref"}}`, + )}} + stdReq := promptcompat.StandardRequest{ + Surface: "test", + ResponseModel: "deepseek-v4-flash-search", + PromptTokenText: "prompt", + FinalPrompt: "final prompt", + Search: true, + } + + result, outErr := ExecuteNonStreamWithRetry(context.Background(), ds, &auth.RequestAuth{}, stdReq, Options{}) + if outErr != nil { + t.Fatalf("unexpected output error: %#v", outErr) + } + want := "答案[0](https://example.com/ref)。" + if result.Turn.Text != want { + t.Fatalf("text mismatch: got %q want %q", result.Turn.Text, want) + } +} + +func TestStartCompletionAppliesCurrentInputFileGlobally(t *testing.T) { + ds := &fakeDeepSeekCaller{responses: []*http.Response{sseHTTPResponse(http.StatusOK, `data: {"p":"response/content","v":"ok"}`)}} + stdReq := promptcompat.StandardRequest{ + Surface: "test_adapter", + RequestedModel: "deepseek-v4-flash", + ResolvedModel: "deepseek-v4-flash", + ResponseModel: "deepseek-v4-flash", + PromptTokenText: "first user turn", + FinalPrompt: "first user turn", + Messages: []any{ + map[string]any{"role": "user", "content": "first user turn"}, + }, + } + + start, outErr := StartCompletion(context.Background(), ds, &auth.RequestAuth{DeepSeekToken: "token"}, stdReq, Options{ + CurrentInputFile: currentInputRuntimeConfig{}, + }) + if outErr != nil { + t.Fatalf("unexpected output error: %#v", outErr) + } + if len(ds.uploads) != 1 { + t.Fatalf("expected current input upload, got %d", len(ds.uploads)) + } + if got := ds.uploads[0].Filename; strings.Contains(strings.ToLower(got), "history") || !strings.HasSuffix(got, ".txt") { + t.Fatalf("unexpected upload filename=%q", got) + } + if len(ds.payloads) != 1 { + t.Fatalf("expected one completion payload, got %d", len(ds.payloads)) + } + refIDs, _ := ds.payloads[0]["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-runtime-1" { + t.Fatalf("expected uploaded file id in ref_file_ids, got %#v", ds.payloads[0]["ref_file_ids"]) + } + prompt, _ := ds.payloads[0]["prompt"].(string) + if !strings.Contains(prompt, ds.uploads[0].Filename) { + t.Fatalf("expected continuation prompt, got %q", prompt) + } + if !start.Request.CurrentInputFileApplied || !strings.Contains(start.Request.PromptTokenText, "# context_context.txt") { + t.Fatalf("expected prepared request to carry current input file state, got %#v", start.Request) + } +} + +func sseHTTPResponse(status int, lines ...string) *http.Response { + body := strings.Join(lines, "\n") + if !strings.HasSuffix(body, "\n") { + body += "\n" + } + return &http.Response{ + StatusCode: status, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} diff --git a/internal/completionruntime/stream_retry.go b/internal/completionruntime/stream_retry.go new file mode 100644 index 0000000000000000000000000000000000000000..6007ceab4d9e6de139cc408596dbb104a64b4067 --- /dev/null +++ b/internal/completionruntime/stream_retry.go @@ -0,0 +1,190 @@ +package completionruntime + +import ( + "context" + "io" + "net/http" + "strings" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/config" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" +) + +type StreamRetryOptions struct { + Surface string + Stream bool + RetryEnabled bool + RetryMaxAttempts int + MaxAttempts int + UsagePrompt string + Request promptcompat.StandardRequest + CurrentInputFile history.CurrentInputConfigReader +} + +type StreamRetryHooks struct { + ConsumeAttempt func(resp *http.Response, allowDeferEmpty bool) (terminalWritten bool, retryable bool) + Finalize func(attempts int) + ParentMessageID func() int + OnRetry func(attempts int) + OnRetryPrompt func(prompt string) + OnRetryFailure func(status int, message, code string) + OnAccountSwitch func(sessionID string) + OnTerminal func(attempts int) +} + +func ExecuteStreamWithRetry(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, initialResp *http.Response, payload map[string]any, pow string, opts StreamRetryOptions, hooks StreamRetryHooks) { + if hooks.ConsumeAttempt == nil { + return + } + surface := strings.TrimSpace(opts.Surface) + if surface == "" { + surface = "completion" + } + maxAttempts := opts.MaxAttempts + if maxAttempts <= 0 { + maxAttempts = 3 + } + retryMax := opts.RetryMaxAttempts + if retryMax <= 0 { + retryMax = shared.EmptyOutputRetryMaxAttempts() + } + + attempts := 0 + accountSwitchAttempted := false + currentResp := initialResp + currentPayload := clonePayload(payload) + for { + allowAccountSwitch := opts.RetryEnabled && attempts >= retryMax && !accountSwitchAttempted && a != nil && a.UseConfigToken + terminalWritten, retryable := hooks.ConsumeAttempt(currentResp, opts.RetryEnabled && (attempts < retryMax || allowAccountSwitch)) + if terminalWritten { + if hooks.OnTerminal != nil { + hooks.OnTerminal(attempts) + } + return + } + if !retryable || !opts.RetryEnabled { + if hooks.Finalize != nil { + hooks.Finalize(attempts) + } + return + } + + if attempts >= retryMax { + if canRetryOnAlternateAccount(ctx, a, &assistantturn.OutputError{Status: http.StatusTooManyRequests}, opts.RetryEnabled, &accountSwitchAttempted) { + switched, switchErr := startPayloadCompletionOnAlternateAccount(ctx, ds, a, payload, opts, maxAttempts) + if switchErr != nil { + if hooks.OnRetryFailure != nil { + hooks.OnRetryFailure(switchErr.Status, switchErr.Message, switchErr.Code) + } + return + } + if switched.Response != nil { + config.Logger.Info("[completion_runtime_account_switch_retry] retrying after 429", "surface", surface, "stream", opts.Stream, "account", a.AccountID) + currentResp = switched.Response + currentPayload = switched.Payload + pow = switched.Pow + if hooks.OnAccountSwitch != nil { + hooks.OnAccountSwitch(switched.SessionID) + } + if hooks.OnRetryPrompt != nil { + hooks.OnRetryPrompt(opts.UsagePrompt) + } + continue + } + } + if hooks.Finalize != nil { + hooks.Finalize(attempts) + } + return + } + + attempts++ + parentMessageID := 0 + if hooks.ParentMessageID != nil { + parentMessageID = hooks.ParentMessageID() + } + config.Logger.Info("[completion_runtime_empty_retry] attempting synthetic retry", "surface", surface, "stream", opts.Stream, "retry_attempt", attempts, "parent_message_id", parentMessageID) + retryPow, powErr := ds.GetPow(ctx, a, maxAttempts) + if powErr != nil { + config.Logger.Warn("[completion_runtime_empty_retry] retry PoW fetch failed, falling back to original PoW", "surface", surface, "stream", opts.Stream, "retry_attempt", attempts, "error", powErr) + retryPow = pow + } + nextResp, err := ds.CallCompletion(ctx, a, shared.ClonePayloadForEmptyOutputRetry(currentPayload, parentMessageID), retryPow, maxAttempts) + if err != nil { + if hooks.OnRetryFailure != nil { + hooks.OnRetryFailure(http.StatusInternalServerError, "Failed to get completion.", "error") + } + config.Logger.Warn("[completion_runtime_empty_retry] retry request failed", "surface", surface, "stream", opts.Stream, "retry_attempt", attempts, "error", err) + return + } + if nextResp.StatusCode != http.StatusOK { + body, readErr := io.ReadAll(nextResp.Body) + if readErr != nil { + config.Logger.Warn("[completion_runtime_empty_retry] retry error body read failed", "surface", surface, "stream", opts.Stream, "retry_attempt", attempts, "error", readErr) + } + closeRetryBody(surface, nextResp.Body) + msg := strings.TrimSpace(string(body)) + if msg == "" { + msg = http.StatusText(nextResp.StatusCode) + } + if hooks.OnRetryFailure != nil { + hooks.OnRetryFailure(nextResp.StatusCode, msg, "error") + } + return + } + if hooks.OnRetry != nil { + hooks.OnRetry(attempts) + } + if hooks.OnRetryPrompt != nil { + hooks.OnRetryPrompt(shared.UsagePromptWithEmptyOutputRetry(opts.UsagePrompt, attempts)) + } + currentResp = nextResp + } +} + +func startPayloadCompletionOnAlternateAccount(ctx context.Context, ds DeepSeekCaller, a *auth.RequestAuth, payload map[string]any, opts StreamRetryOptions, maxAttempts int) (StartResult, *assistantturn.OutputError) { + sessionID, err := ds.CreateSession(ctx, a, maxAttempts) + if err != nil { + return StartResult{}, authOutputError(a) + } + pow, err := ds.GetPow(ctx, a, maxAttempts) + if err != nil { + return StartResult{SessionID: sessionID}, &assistantturn.OutputError{Status: http.StatusUnauthorized, Message: "Failed to get PoW (invalid token or unknown error).", Code: "error"} + } + nextPayload := clonePayload(payload) + if opts.CurrentInputFile != nil && opts.Request.CurrentInputFileApplied { + stdReq, prepErr := reuploadCurrentInputFileForAccount(ctx, ds, a, opts.Request, Options{CurrentInputFile: opts.CurrentInputFile}) + if prepErr != nil { + return StartResult{SessionID: sessionID}, prepErr + } + nextPayload = stdReq.CompletionPayload(sessionID) + } + nextPayload["chat_session_id"] = sessionID + delete(nextPayload, "parent_message_id") + resp, err := ds.CallCompletion(ctx, a, nextPayload, pow, maxAttempts) + if err != nil { + return StartResult{SessionID: sessionID, Payload: nextPayload, Pow: pow}, &assistantturn.OutputError{Status: http.StatusInternalServerError, Message: "Failed to get completion.", Code: "error"} + } + return StartResult{SessionID: sessionID, Payload: nextPayload, Pow: pow, Response: resp}, nil +} + +func clonePayload(payload map[string]any) map[string]any { + clone := make(map[string]any, len(payload)) + for k, v := range payload { + clone[k] = v + } + return clone +} + +func closeRetryBody(surface string, body io.Closer) { + if body == nil { + return + } + if err := body.Close(); err != nil { + config.Logger.Warn("[completion_runtime_empty_retry] retry response body close failed", "surface", surface, "error", err) + } +} diff --git a/internal/completionruntime/stream_retry_test.go b/internal/completionruntime/stream_retry_test.go new file mode 100644 index 0000000000000000000000000000000000000000..655016ca14f4e72c1e789c13ff2a0c4c2a9451ed --- /dev/null +++ b/internal/completionruntime/stream_retry_test.go @@ -0,0 +1,150 @@ +package completionruntime + +import ( + "context" + "io" + "net/http" + "strings" + "testing" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" + "ds2api/internal/httpapi/openai/shared" +) + +func TestExecuteStreamWithRetryUsesSharedRetryPayloadAndUsagePrompt(t *testing.T) { + ds := &fakeDeepSeekCaller{responses: []*http.Response{ + sseHTTPResponse(http.StatusOK, `data: {"p":"response/content","v":"ok"}`), + }} + initial := sseHTTPResponse(http.StatusOK, `data: {"response_message_id":77,"p":"response/thinking_content","v":"plan"}`) + payload := map[string]any{"prompt": "original prompt"} + attemptsSeen := 0 + retryPrompt := "" + + ExecuteStreamWithRetry(context.Background(), ds, &auth.RequestAuth{}, initial, payload, "pow", StreamRetryOptions{ + Surface: "test.stream", + Stream: true, + RetryEnabled: true, + UsagePrompt: "original prompt", + }, StreamRetryHooks{ + ConsumeAttempt: func(resp *http.Response, allowDeferEmpty bool) (bool, bool) { + defer func() { + if err := resp.Body.Close(); err != nil { + t.Fatalf("close failed: %v", err) + } + }() + _, _ = io.ReadAll(resp.Body) + attemptsSeen++ + return attemptsSeen == 2, attemptsSeen == 1 && allowDeferEmpty + }, + ParentMessageID: func() int { + return 77 + }, + OnRetryPrompt: func(prompt string) { + retryPrompt = prompt + }, + }) + + if attemptsSeen != 2 { + t.Fatalf("expected two stream attempts, got %d", attemptsSeen) + } + if len(ds.payloads) != 1 { + t.Fatalf("expected one retry completion call, got %d", len(ds.payloads)) + } + if got := ds.payloads[0]["parent_message_id"]; got != 77 { + t.Fatalf("retry parent_message_id mismatch: %#v", got) + } + if prompt, _ := ds.payloads[0]["prompt"].(string); !strings.Contains(prompt, shared.EmptyOutputRetrySuffix) { + t.Fatalf("expected retry suffix in payload prompt, got %q", prompt) + } + if !strings.Contains(retryPrompt, shared.EmptyOutputRetrySuffix) { + t.Fatalf("expected retry suffix in usage prompt, got %q", retryPrompt) + } +} + +func TestExecuteStreamWithRetrySwitchesManagedAccountBeforeFinal429(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","password":"pwd"}, + {"email":"acc2@test.com","password":"pwd"} + ] + }`) + store := config.LoadStore() + resolver := auth.NewResolver(store, account.NewPool(store), func(_ context.Context, acc config.Account) (string, error) { + return "token-" + acc.Identifier(), nil + }) + req, _ := http.NewRequest(http.MethodPost, "/", nil) + req.Header.Set("Authorization", "Bearer managed-key") + a, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer resolver.Release(a) + + ds := &fakeDeepSeekCaller{ + sessionByAccount: true, + responses: []*http.Response{ + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":12,"p":"response/thinking_content","v":"retry empty"}`), + sseHTTPResponse(http.StatusOK, `data: {"response_message_id":21,"p":"response/content","v":"ok from second account"}`), + }, + } + initial := sseHTTPResponse(http.StatusOK, `data: {"response_message_id":11,"p":"response/thinking_content","v":"first empty"}`) + payload := map[string]any{"prompt": "original prompt", "chat_session_id": "session-acc1@test.com"} + attemptsSeen := 0 + switchedSession := "" + + ExecuteStreamWithRetry(context.Background(), ds, a, initial, payload, "pow", StreamRetryOptions{ + Surface: "test.stream", + Stream: true, + RetryEnabled: true, + RetryMaxAttempts: 1, + UsagePrompt: "original prompt", + }, StreamRetryHooks{ + ConsumeAttempt: func(resp *http.Response, allowDeferEmpty bool) (bool, bool) { + defer func() { + if err := resp.Body.Close(); err != nil { + t.Fatalf("close failed: %v", err) + } + }() + body, _ := io.ReadAll(resp.Body) + attemptsSeen++ + if strings.Contains(string(body), "ok from second account") { + return true, false + } + if !allowDeferEmpty { + t.Fatalf("expected empty attempt %d to be deferred before final 429", attemptsSeen) + } + return false, true + }, + ParentMessageID: func() int { + return 11 + attemptsSeen + }, + OnAccountSwitch: func(sessionID string) { + switchedSession = sessionID + }, + }) + + if attemptsSeen != 3 { + t.Fatalf("expected three stream attempts, got %d", attemptsSeen) + } + if switchedSession != "session-acc2@test.com" { + t.Fatalf("expected switched session id, got %q", switchedSession) + } + wantAccounts := []string{"acc1@test.com", "acc2@test.com"} + if len(ds.completionAccounts) != len(wantAccounts) { + t.Fatalf("completion accounts mismatch: got %v want %v", ds.completionAccounts, wantAccounts) + } + for i, want := range wantAccounts { + if ds.completionAccounts[i] != want { + t.Fatalf("completion account %d = %q want %q (all=%v)", i, ds.completionAccounts[i], want, ds.completionAccounts) + } + } + if got := ds.payloads[1]["chat_session_id"]; got != "session-acc2@test.com" { + t.Fatalf("switched payload session mismatch: %#v", got) + } + if prompt, _ := ds.payloads[1]["prompt"].(string); strings.Contains(prompt, shared.EmptyOutputRetrySuffix) { + t.Fatalf("expected switched-account prompt without empty-output suffix, got %q", prompt) + } +} diff --git a/internal/config/account.go b/internal/config/account.go new file mode 100644 index 0000000000000000000000000000000000000000..bebb70e5c4ff73df20a1187c682c7a478989bff8 --- /dev/null +++ b/internal/config/account.go @@ -0,0 +1,13 @@ +package config + +import "strings" + +func (a Account) Identifier() string { + if strings.TrimSpace(a.Email) != "" { + return strings.TrimSpace(a.Email) + } + if mobile := NormalizeMobileForStorage(a.Mobile); mobile != "" { + return mobile + } + return "" +} diff --git a/internal/config/codec.go b/internal/config/codec.go new file mode 100644 index 0000000000000000000000000000000000000000..ac1876e2f0adb14c68eecf05bc8fbee14ac5a177 --- /dev/null +++ b/internal/config/codec.go @@ -0,0 +1,272 @@ +package config + +import ( + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "slices" + "strings" +) + +func (c Config) MarshalJSON() ([]byte, error) { + m := map[string]any{} + for k, v := range c.AdditionalFields { + m[k] = v + } + if len(c.Keys) > 0 { + m["keys"] = c.Keys + } + if len(c.APIKeys) > 0 { + m["api_keys"] = c.APIKeys + } + if len(c.Accounts) > 0 { + m["accounts"] = c.Accounts + } + if len(c.Proxies) > 0 { + m["proxies"] = c.Proxies + } + if len(c.ModelAliases) > 0 { + m["model_aliases"] = c.ModelAliases + } + if strings.TrimSpace(c.Admin.PasswordHash) != "" || c.Admin.JWTExpireHours > 0 || c.Admin.JWTValidAfterUnix > 0 { + m["admin"] = c.Admin + } + if c.Runtime.AccountMaxInflight > 0 || c.Runtime.AccountMaxQueue > 0 || c.Runtime.GlobalMaxInflight > 0 || c.Runtime.TokenRefreshIntervalHours > 0 { + m["runtime"] = c.Runtime + } + if c.Responses.StoreTTLSeconds > 0 { + m["responses"] = c.Responses + } + if strings.TrimSpace(c.Embeddings.Provider) != "" { + m["embeddings"] = c.Embeddings + } + m["auto_delete"] = c.AutoDelete + if c.CurrentInputFile.Enabled != nil || c.CurrentInputFile.MinChars != 0 { + m["current_input_file"] = c.CurrentInputFile + } + if c.ThinkingInjection.Enabled != nil || strings.TrimSpace(c.ThinkingInjection.Prompt) != "" { + m["thinking_injection"] = c.ThinkingInjection + } + if strings.TrimSpace(c.Vercel.Token) != "" || strings.TrimSpace(c.Vercel.ProjectID) != "" || strings.TrimSpace(c.Vercel.TeamID) != "" { + m["vercel"] = NormalizeVercelConfig(c.Vercel) + } + if c.VercelSyncHash != "" { + m["_vercel_sync_hash"] = c.VercelSyncHash + } + if c.VercelSyncTime != 0 { + m["_vercel_sync_time"] = c.VercelSyncTime + } + return json.Marshal(m) +} + +func (c *Config) UnmarshalJSON(b []byte) error { + raw := map[string]json.RawMessage{} + if err := json.Unmarshal(b, &raw); err != nil { + return err + } + c.AdditionalFields = map[string]any{} + for k, v := range raw { + switch k { + case "keys": + if err := json.Unmarshal(v, &c.Keys); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "api_keys": + if err := json.Unmarshal(v, &c.APIKeys); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "accounts": + if err := json.Unmarshal(v, &c.Accounts); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "proxies": + if err := json.Unmarshal(v, &c.Proxies); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "claude_mapping": + case "claude_model_mapping": + // Removed legacy mapping fields are ignored instead of persisted. + case "model_aliases": + if err := json.Unmarshal(v, &c.ModelAliases); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "admin": + if err := json.Unmarshal(v, &c.Admin); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "runtime": + if err := json.Unmarshal(v, &c.Runtime); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "compat": + // Removed field ignored instead of persisted. + if Logger != nil { + Logger.Warn("config key \"compat\" is deprecated and ignored; remove it from your configuration") + } + case "toolcall": + // Legacy field ignored. Toolcall policy is fixed and no longer configurable. + case "responses": + if err := json.Unmarshal(v, &c.Responses); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "embeddings": + if err := json.Unmarshal(v, &c.Embeddings); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "auto_delete": + if err := json.Unmarshal(v, &c.AutoDelete); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "history_split": + // Removed legacy split field is ignored instead of persisted. + case "current_input_file": + if err := json.Unmarshal(v, &c.CurrentInputFile); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "thinking_injection": + if err := json.Unmarshal(v, &c.ThinkingInjection); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "vercel": + if err := json.Unmarshal(v, &c.Vercel); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "_vercel_sync_hash": + if err := json.Unmarshal(v, &c.VercelSyncHash); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + case "_vercel_sync_time": + if err := json.Unmarshal(v, &c.VercelSyncTime); err != nil { + return fmt.Errorf("invalid field %q: %w", k, err) + } + default: + var anyVal any + if err := json.Unmarshal(v, &anyVal); err == nil { + c.AdditionalFields[k] = anyVal + } + } + } + c.NormalizeCredentials() + return nil +} + +func (c Config) Clone() Config { + clone := Config{ + Keys: slices.Clone(c.Keys), + APIKeys: slices.Clone(c.APIKeys), + Accounts: slices.Clone(c.Accounts), + Proxies: slices.Clone(c.Proxies), + ModelAliases: cloneStringMap(c.ModelAliases), + Admin: c.Admin, + Runtime: c.Runtime, + Responses: c.Responses, + Embeddings: c.Embeddings, + AutoDelete: c.AutoDelete, + CurrentInputFile: CurrentInputFileConfig{ + Enabled: cloneBoolPtr(c.CurrentInputFile.Enabled), + MinChars: c.CurrentInputFile.MinChars, + }, + ThinkingInjection: ThinkingInjectionConfig{ + Enabled: cloneBoolPtr(c.ThinkingInjection.Enabled), + Prompt: c.ThinkingInjection.Prompt, + }, + Vercel: c.Vercel, + VercelSyncHash: c.VercelSyncHash, + VercelSyncTime: c.VercelSyncTime, + AdditionalFields: map[string]any{}, + } + for k, v := range c.AdditionalFields { + clone.AdditionalFields[k] = v + } + return clone +} + +func cloneStringMap(in map[string]string) map[string]string { + if len(in) == 0 { + return nil + } + out := make(map[string]string, len(in)) + for k, v := range in { + out[k] = v + } + return out +} + +func cloneBoolPtr(in *bool) *bool { + if in == nil { + return nil + } + v := *in + return &v +} + +func parseConfigString(raw string) (Config, error) { + var cfg Config + candidates := []string{raw} + if normalized := normalizeConfigInput(raw); normalized != raw { + candidates = append(candidates, normalized) + } + for _, candidate := range candidates { + if err := json.Unmarshal([]byte(candidate), &cfg); err == nil { + return cfg, nil + } + } + + base64Input := candidates[len(candidates)-1] + decoded, err := decodeConfigBase64(base64Input) + if err != nil { + return Config{}, fmt.Errorf("invalid DS2API_CONFIG_JSON: %w", err) + } + if err := json.Unmarshal(decoded, &cfg); err != nil { + return Config{}, fmt.Errorf("invalid DS2API_CONFIG_JSON decoded JSON: %w", err) + } + return cfg, nil +} + +func normalizeConfigInput(raw string) string { + normalized := strings.TrimSpace(raw) + if normalized == "" { + return normalized + } + for { + changed := false + if len(normalized) >= 2 { + first := normalized[0] + last := normalized[len(normalized)-1] + if (first == '"' && last == '"') || (first == '\'' && last == '\'') { + normalized = strings.TrimSpace(normalized[1 : len(normalized)-1]) + changed = true + } + } + if strings.HasPrefix(strings.ToLower(normalized), "base64:") { + normalized = strings.TrimSpace(normalized[len("base64:"):]) + changed = true + } + if !changed { + break + } + } + return strings.TrimSpace(normalized) +} + +func decodeConfigBase64(raw string) ([]byte, error) { + encodings := []*base64.Encoding{ + base64.StdEncoding, + base64.RawStdEncoding, + base64.URLEncoding, + base64.RawURLEncoding, + } + var lastErr error + for _, enc := range encodings { + decoded, err := enc.DecodeString(raw) + if err == nil { + return decoded, nil + } + lastErr = err + } + if lastErr != nil { + return nil, lastErr + } + return nil, errors.New("base64 decode failed") +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 0000000000000000000000000000000000000000..8565078a2ee89bbfaeccf6bb94138ba17a248044 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,201 @@ +package config + +import ( + "crypto/sha1" + "encoding/hex" + "fmt" + "strings" +) + +type Config struct { + Keys []string `json:"keys,omitempty"` + APIKeys []APIKey `json:"api_keys,omitempty"` + Accounts []Account `json:"accounts,omitempty"` + Proxies []Proxy `json:"proxies,omitempty"` + ModelAliases map[string]string `json:"model_aliases,omitempty"` + Admin AdminConfig `json:"admin,omitempty"` + Runtime RuntimeConfig `json:"runtime,omitempty"` + Responses ResponsesConfig `json:"responses,omitempty"` + Embeddings EmbeddingsConfig `json:"embeddings,omitempty"` + AutoDelete AutoDeleteConfig `json:"auto_delete"` + CurrentInputFile CurrentInputFileConfig `json:"current_input_file,omitempty"` + ThinkingInjection ThinkingInjectionConfig `json:"thinking_injection,omitempty"` + Vercel VercelConfig `json:"vercel,omitempty"` + VercelSyncHash string `json:"_vercel_sync_hash,omitempty"` + VercelSyncTime int64 `json:"_vercel_sync_time,omitempty"` + AdditionalFields map[string]any `json:"-"` +} + +type Account struct { + Name string `json:"name,omitempty"` + Remark string `json:"remark,omitempty"` + Email string `json:"email,omitempty"` + Mobile string `json:"mobile,omitempty"` + Password string `json:"password,omitempty"` + Token string `json:"token,omitempty"` + ProxyID string `json:"proxy_id,omitempty"` +} + +type APIKey struct { + Key string `json:"key"` + Name string `json:"name,omitempty"` + Remark string `json:"remark,omitempty"` +} + +type Proxy struct { + ID string `json:"id,omitempty"` + Name string `json:"name,omitempty"` + Type string `json:"type,omitempty"` + Host string `json:"host,omitempty"` + Port int `json:"port,omitempty"` + Username string `json:"username,omitempty"` + Password string `json:"password,omitempty"` + Disabled bool `json:"disabled,omitempty"` +} + +func NormalizeProxy(p Proxy) Proxy { + p.ID = strings.TrimSpace(p.ID) + p.Name = strings.TrimSpace(p.Name) + p.Type = strings.ToLower(strings.TrimSpace(p.Type)) + p.Host = strings.TrimSpace(p.Host) + p.Username = strings.TrimSpace(p.Username) + p.Password = strings.TrimSpace(p.Password) + if p.ID == "" { + p.ID = StableProxyID(p) + } + if p.Name == "" && p.Host != "" && p.Port > 0 { + p.Name = fmt.Sprintf("%s:%d", p.Host, p.Port) + } + return p +} + +func StableProxyID(p Proxy) string { + sum := sha1.Sum([]byte(strings.ToLower(strings.TrimSpace(p.Type)) + "|" + strings.ToLower(strings.TrimSpace(p.Host)) + "|" + fmt.Sprintf("%d", p.Port) + "|" + strings.TrimSpace(p.Username))) + return "proxy_" + hex.EncodeToString(sum[:6]) +} + +func (c *Config) ClearAccountTokens() { + if c == nil { + return + } + for i := range c.Accounts { + c.Accounts[i].Token = "" + } +} + +func (c *Config) NormalizeCredentials() { + if c == nil { + return + } + normalizedAPIKeys := normalizeAPIKeys(c.APIKeys) + if len(normalizedAPIKeys) > 0 { + c.APIKeys = normalizedAPIKeys + c.Keys = apiKeysToStrings(c.APIKeys) + } else { + c.Keys = normalizeKeys(c.Keys) + c.APIKeys = apiKeysFromStrings(c.Keys, nil) + } + + for i := range c.Accounts { + c.Accounts[i].Name = strings.TrimSpace(c.Accounts[i].Name) + c.Accounts[i].Remark = strings.TrimSpace(c.Accounts[i].Remark) + } + + c.Vercel = NormalizeVercelConfig(c.Vercel) + c.normalizeModelAliases() +} + +// DropInvalidAccounts removes accounts that cannot be addressed by admin APIs +// (no email and no normalizable mobile). This prevents legacy token-only +// records from becoming orphaned empty entries after token stripping. +func (c *Config) DropInvalidAccounts() { + if c == nil || len(c.Accounts) == 0 { + return + } + kept := make([]Account, 0, len(c.Accounts)) + for _, acc := range c.Accounts { + if acc.Identifier() == "" { + continue + } + kept = append(kept, acc) + } + c.Accounts = kept +} + +func (c *Config) normalizeModelAliases() { + if c == nil { + return + } + + aliases := map[string]string{} + for k, v := range c.ModelAliases { + key := strings.TrimSpace(lower(k)) + val := strings.TrimSpace(lower(v)) + if key == "" || val == "" { + continue + } + aliases[key] = val + } + if len(aliases) == 0 { + c.ModelAliases = nil + } else { + c.ModelAliases = aliases + } +} + +type AdminConfig struct { + PasswordHash string `json:"password_hash,omitempty"` + JWTExpireHours int `json:"jwt_expire_hours,omitempty"` + JWTValidAfterUnix int64 `json:"jwt_valid_after_unix,omitempty"` +} + +type RuntimeConfig struct { + AccountMaxInflight int `json:"account_max_inflight,omitempty"` + AccountMaxQueue int `json:"account_max_queue,omitempty"` + GlobalMaxInflight int `json:"global_max_inflight,omitempty"` + TokenRefreshIntervalHours int `json:"token_refresh_interval_hours,omitempty"` +} + +type ResponsesConfig struct { + StoreTTLSeconds int `json:"store_ttl_seconds,omitempty"` +} + +type EmbeddingsConfig struct { + Provider string `json:"provider,omitempty"` +} + +type AutoDeleteConfig struct { + Mode string `json:"mode,omitempty"` + Sessions bool `json:"sessions,omitempty"` +} + +type CurrentInputFileConfig struct { + Enabled *bool `json:"enabled,omitempty"` + MinChars int `json:"min_chars,omitempty"` +} + +type ThinkingInjectionConfig struct { + Enabled *bool `json:"enabled,omitempty"` + Prompt string `json:"prompt,omitempty"` +} + +type VercelConfig struct { + Token string `json:"token,omitempty"` + ProjectID string `json:"project_id,omitempty"` + TeamID string `json:"team_id,omitempty"` +} + +func NormalizeVercelConfig(v VercelConfig) VercelConfig { + return VercelConfig{ + Token: strings.TrimSpace(v.Token), + ProjectID: strings.TrimSpace(v.ProjectID), + TeamID: strings.TrimSpace(v.TeamID), + } +} + +func (c *Config) ClearVercelCredentials() { + if c == nil { + return + } + c.Vercel = VercelConfig{} +} diff --git a/internal/config/config_edge_test.go b/internal/config/config_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ceb6faaea080e7f91846f11649e1ae12911a5d5a --- /dev/null +++ b/internal/config/config_edge_test.go @@ -0,0 +1,721 @@ +package config + +import ( + "encoding/base64" + "encoding/json" + "strings" + "testing" +) + +// ─── GetModelConfig edge cases ─────────────────────────────────────── + +func TestGetModelConfigDeepSeekChat(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-flash") + if !ok { + t.Fatal("expected ok for deepseek-v4-flash") + } + if !thinking || search { + t.Fatalf("expected thinking=true search=false for deepseek-v4-flash, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekChatNoThinking(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-flash-nothinking") + if !ok { + t.Fatal("expected ok for deepseek-v4-flash-nothinking") + } + if thinking || search { + t.Fatalf("expected thinking=false search=false for deepseek-v4-flash-nothinking, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekReasoner(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-pro") + if !ok { + t.Fatal("expected ok for deepseek-v4-pro") + } + if !thinking || search { + t.Fatalf("expected thinking=true search=false, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekChatSearch(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-flash-search") + if !ok { + t.Fatal("expected ok for deepseek-v4-flash-search") + } + if !thinking || !search { + t.Fatalf("expected thinking=true search=true, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekReasonerSearch(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-pro-search") + if !ok { + t.Fatal("expected ok for deepseek-v4-pro-search") + } + if !thinking || !search { + t.Fatalf("expected both true, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekExpertChat(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-pro") + if !ok { + t.Fatal("expected ok for deepseek-v4-pro") + } + if !thinking || search { + t.Fatalf("expected thinking=true search=false for deepseek-v4-pro, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekExpertReasonerSearch(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-pro-search") + if !ok { + t.Fatal("expected ok for deepseek-v4-pro-search") + } + if !thinking || !search { + t.Fatalf("expected both true, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekVision(t *testing.T) { + thinking, search, ok := GetModelConfig("deepseek-v4-vision") + if !ok { + t.Fatal("expected ok for deepseek-v4-vision") + } + if !thinking || search { + t.Fatalf("expected thinking=true search=false, got thinking=%v search=%v", thinking, search) + } +} + +func TestGetModelConfigDeepSeekVisionSearchUnsupported(t *testing.T) { + _, _, ok := GetModelConfig("deepseek-v4-vision-search") + if ok { + t.Fatal("expected deepseek-v4-vision-search to be unsupported") + } +} + +func TestGetModelTypeDefaultExpertAndVision(t *testing.T) { + defaultType, ok := GetModelType("deepseek-v4-flash") + if !ok || defaultType != "default" { + t.Fatalf("expected default model_type, got ok=%v model_type=%q", ok, defaultType) + } + defaultNoThinkingType, ok := GetModelType("deepseek-v4-flash-nothinking") + if !ok || defaultNoThinkingType != "default" { + t.Fatalf("expected default model_type for nothinking, got ok=%v model_type=%q", ok, defaultNoThinkingType) + } + expertType, ok := GetModelType("deepseek-v4-pro") + if !ok || expertType != "expert" { + t.Fatalf("expected expert model_type, got ok=%v model_type=%q", ok, expertType) + } + visionType, ok := GetModelType("deepseek-v4-vision") + if !ok || visionType != "vision" { + t.Fatalf("expected vision model_type, got ok=%v model_type=%q", ok, visionType) + } +} + +func TestGetModelConfigCaseInsensitive(t *testing.T) { + thinking, search, ok := GetModelConfig("DeepSeek-V4-Flash") + if !ok { + t.Fatal("expected ok for case-insensitive deepseek-v4-flash") + } + if !thinking || search { + t.Fatalf("expected thinking=true search=false for case-insensitive deepseek-v4-flash") + } +} + +func TestGetModelConfigUnknownModel(t *testing.T) { + _, _, ok := GetModelConfig("gpt-4") + if ok { + t.Fatal("expected not ok for unknown model") + } +} + +func TestGetModelConfigEmpty(t *testing.T) { + _, _, ok := GetModelConfig("") + if ok { + t.Fatal("expected not ok for empty model") + } +} + +// ─── lower function ────────────────────────────────────────────────── + +func TestLowerFunction(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {"Hello", "hello"}, + {"ALLCAPS", "allcaps"}, + {"already-lower", "already-lower"}, + {"Mixed-CASE-123", "mixed-case-123"}, + {"", ""}, + } + for _, tc := range tests { + got := lower(tc.input) + if got != tc.expected { + t.Errorf("lower(%q) = %q, want %q", tc.input, got, tc.expected) + } + } +} + +// ─── Config.MarshalJSON / UnmarshalJSON roundtrip ──────────────────── + +func TestConfigJSONRoundtrip(t *testing.T) { + cfg := Config{ + Keys: []string{"key1", "key2"}, + Accounts: []Account{{Email: "user@example.com", Password: "pass", Token: "tok"}}, + ModelAliases: map[string]string{"Claude-Sonnet-4-6": "DeepSeek-V4-Flash"}, + AutoDelete: AutoDeleteConfig{ + Mode: "single", + }, + Runtime: RuntimeConfig{ + TokenRefreshIntervalHours: 12, + }, + Vercel: VercelConfig{ + Token: " vercel-token ", + ProjectID: " prj_123 ", + TeamID: " team_123 ", + }, + VercelSyncHash: "hash123", + VercelSyncTime: 1234567890, + AdditionalFields: map[string]any{ + "custom_field": "custom_value", + }, + } + + data, err := cfg.MarshalJSON() + if err != nil { + t.Fatalf("marshal error: %v", err) + } + + var decoded Config + if err := json.Unmarshal(data, &decoded); err != nil { + t.Fatalf("unmarshal error: %v", err) + } + + if len(decoded.Keys) != 2 || decoded.Keys[0] != "key1" { + t.Fatalf("unexpected keys: %#v", decoded.Keys) + } + if len(decoded.Accounts) != 1 || decoded.Accounts[0].Email != "user@example.com" { + t.Fatalf("unexpected accounts: %#v", decoded.Accounts) + } + if decoded.ModelAliases["claude-sonnet-4-6"] != "deepseek-v4-flash" { + t.Fatalf("unexpected normalized model aliases: %#v", decoded.ModelAliases) + } + if decoded.Runtime.TokenRefreshIntervalHours != 12 { + t.Fatalf("unexpected runtime refresh interval: %#v", decoded.Runtime.TokenRefreshIntervalHours) + } + if decoded.AutoDelete.Mode != "single" { + t.Fatalf("unexpected auto delete mode: %#v", decoded.AutoDelete.Mode) + } + if decoded.Vercel.Token != "vercel-token" || decoded.Vercel.ProjectID != "prj_123" || decoded.Vercel.TeamID != "team_123" { + t.Fatalf("unexpected vercel config: %#v", decoded.Vercel) + } + if decoded.VercelSyncHash != "hash123" { + t.Fatalf("unexpected vercel sync hash: %q", decoded.VercelSyncHash) + } + if decoded.AdditionalFields["custom_field"] != "custom_value" { + t.Fatalf("unexpected additional fields: %#v", decoded.AdditionalFields) + } +} + +func TestAutoDeleteModeResolution(t *testing.T) { + tests := []struct { + name string + cfg AutoDeleteConfig + want string + }{ + {name: "default", cfg: AutoDeleteConfig{}, want: "none"}, + {name: "legacy all", cfg: AutoDeleteConfig{Sessions: true}, want: "all"}, + {name: "single", cfg: AutoDeleteConfig{Mode: "single"}, want: "single"}, + {name: "all", cfg: AutoDeleteConfig{Mode: "all"}, want: "all"}, + {name: "none", cfg: AutoDeleteConfig{Mode: "none"}, want: "none"}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + store := &Store{cfg: Config{AutoDelete: tc.cfg}} + if got := store.AutoDeleteMode(); got != tc.want { + t.Fatalf("AutoDeleteMode()=%q want=%q", got, tc.want) + } + }) + } +} + +func TestConfigUnmarshalJSONPreservesUnknownFields(t *testing.T) { + raw := `{"keys":["k1"],"accounts":[],"my_custom_field":"hello","number_field":42}` + var cfg Config + if err := json.Unmarshal([]byte(raw), &cfg); err != nil { + t.Fatalf("unmarshal error: %v", err) + } + if cfg.AdditionalFields["my_custom_field"] != "hello" { + t.Fatalf("expected custom field preserved, got %#v", cfg.AdditionalFields) + } + // number_field should also be preserved + if cfg.AdditionalFields["number_field"] != float64(42) { + t.Fatalf("expected number field preserved, got %#v", cfg.AdditionalFields["number_field"]) + } +} + +func TestConfigUnmarshalJSONIgnoresRemovedLegacyModelMappings(t *testing.T) { + raw := `{"keys":["k1"],"accounts":[],"claude_mapping":{"fast":"deepseek-v4-pro"},"claude_model_mapping":{"slow":"deepseek-v4-pro"}}` + var cfg Config + if err := json.Unmarshal([]byte(raw), &cfg); err != nil { + t.Fatalf("unmarshal error: %v", err) + } + if len(cfg.ModelAliases) != 0 { + t.Fatalf("expected removed legacy mappings to be ignored, got %#v", cfg.ModelAliases) + } + if _, ok := cfg.AdditionalFields["claude_mapping"]; ok { + t.Fatalf("expected removed legacy field not to persist in additional fields: %#v", cfg.AdditionalFields) + } + if _, ok := cfg.AdditionalFields["claude_model_mapping"]; ok { + t.Fatalf("expected removed legacy field not to persist in additional fields: %#v", cfg.AdditionalFields) + } +} + +func TestConfigUnmarshalJSONIgnoresRemovedHistorySplit(t *testing.T) { + raw := `{"keys":["k1"],"history_split":{"enabled":true,"trigger_after_turns":2}}` + var cfg Config + if err := json.Unmarshal([]byte(raw), &cfg); err != nil { + t.Fatalf("unmarshal error: %v", err) + } + if _, ok := cfg.AdditionalFields["history_split"]; ok { + t.Fatalf("expected removed legacy field not to persist in additional fields: %#v", cfg.AdditionalFields) + } + out, err := json.Marshal(cfg) + if err != nil { + t.Fatalf("marshal error: %v", err) + } + if strings.Contains(string(out), "history_split") { + t.Fatalf("expected removed history_split field not to marshal, got %s", out) + } +} + +// ─── Config.Clone ──────────────────────────────────────────────────── + +func TestConfigCloneIsDeepCopy(t *testing.T) { + cfg := Config{ + Keys: []string{"key1"}, + Accounts: []Account{{Email: "user@test.com", Token: "token"}}, + ModelAliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}, + AdditionalFields: map[string]any{"custom": "value"}, + } + + cloned := cfg.Clone() + + // Modify original + cfg.Keys[0] = "modified" + cfg.Accounts[0].Email = "modified@test.com" + cfg.ModelAliases["claude-sonnet-4-6"] = "modified-model" + + // Cloned should not be affected + if cloned.Keys[0] != "key1" { + t.Fatalf("clone keys was affected by original change: %#v", cloned.Keys) + } + if cloned.Accounts[0].Email != "user@test.com" { + t.Fatalf("clone accounts was affected: %#v", cloned.Accounts) + } + if cloned.ModelAliases["claude-sonnet-4-6"] != "deepseek-v4-flash" { + t.Fatalf("clone model aliases was affected: %#v", cloned.ModelAliases) + } +} + +func TestConfigCloneNilMaps(t *testing.T) { + cfg := Config{ + Keys: []string{"k"}, + Accounts: nil, + } + cloned := cfg.Clone() + if len(cloned.Keys) != 1 { + t.Fatalf("unexpected keys length: %d", len(cloned.Keys)) + } + if cloned.Accounts != nil { + t.Fatalf("expected nil accounts in clone, got %#v", cloned.Accounts) + } +} + +// ─── Account.Identifier edge cases ─────────────────────────────────── + +func TestAccountIdentifierPreferenceMobileOverToken(t *testing.T) { + acc := Account{Mobile: "13800138000", Token: "tok"} + if acc.Identifier() != "+8613800138000" { + t.Fatalf("expected mobile identifier, got %q", acc.Identifier()) + } +} + +func TestAccountIdentifierPreferenceEmailOverMobile(t *testing.T) { + acc := Account{Email: "user@test.com", Mobile: "13800138000"} + if acc.Identifier() != "user@test.com" { + t.Fatalf("expected email identifier, got %q", acc.Identifier()) + } +} + +func TestAccountIdentifierEmptyAccount(t *testing.T) { + acc := Account{} + if acc.Identifier() != "" { + t.Fatalf("expected empty identifier for empty account, got %q", acc.Identifier()) + } +} + +// ─── normalizeConfigInput ──────────────────────────────────────────── + +func TestNormalizeConfigInputStripsQuotes(t *testing.T) { + got := normalizeConfigInput(`"base64:abc"`) + if strings.HasPrefix(got, `"`) || strings.HasSuffix(got, `"`) { + t.Fatalf("expected quotes stripped, got %q", got) + } +} + +func TestNormalizeConfigInputStripsSingleQuotes(t *testing.T) { + got := normalizeConfigInput("'some-value'") + if strings.HasPrefix(got, "'") || strings.HasSuffix(got, "'") { + t.Fatalf("expected single quotes stripped, got %q", got) + } +} + +func TestNormalizeConfigInputTrimsWhitespace(t *testing.T) { + got := normalizeConfigInput(" hello ") + if got != "hello" { + t.Fatalf("expected trimmed, got %q", got) + } +} + +// ─── parseConfigString edge cases ──────────────────────────────────── + +func TestParseConfigStringPlainJSON(t *testing.T) { + cfg, err := parseConfigString(`{"keys":["k1"],"accounts":[]}`) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(cfg.Keys) != 1 || cfg.Keys[0] != "k1" { + t.Fatalf("unexpected keys: %#v", cfg.Keys) + } +} + +func TestParseConfigStringBase64Prefix(t *testing.T) { + rawJSON := `{"keys":["base64-key"],"accounts":[]}` + b64 := base64.StdEncoding.EncodeToString([]byte(rawJSON)) + cfg, err := parseConfigString("base64:" + b64) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(cfg.Keys) != 1 || cfg.Keys[0] != "base64-key" { + t.Fatalf("unexpected keys: %#v", cfg.Keys) + } +} + +func TestParseConfigStringInvalidBase64(t *testing.T) { + _, err := parseConfigString("base64:!!!invalid!!!") + if err == nil { + t.Fatal("expected error for invalid base64") + } +} + +func TestParseConfigStringEmptyString(t *testing.T) { + _, err := parseConfigString("") + if err == nil { + t.Fatal("expected error for empty string") + } +} + +// ─── Store methods ─────────────────────────────────────────────────── + +func TestStoreSnapshotReturnsClone(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[{"email":"u@test.com","token":"t1"}]}`) + store := LoadStore() + snap := store.Snapshot() + snap.Keys[0] = "modified" + if store.Keys()[0] != "k1" { + t.Fatal("snapshot modification should not affect store") + } +} + +func TestStoreHasAPIKeyMultipleKeys(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["key1","key2","key3"],"accounts":[]}`) + store := LoadStore() + if !store.HasAPIKey("key1") { + t.Fatal("expected key1 found") + } + if !store.HasAPIKey("key2") { + t.Fatal("expected key2 found") + } + if !store.HasAPIKey("key3") { + t.Fatal("expected key3 found") + } + if store.HasAPIKey("nonexistent") { + t.Fatal("expected nonexistent key not found") + } +} + +func TestStoreFindAccountNotFound(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[{"email":"u@test.com"}]}`) + store := LoadStore() + _, ok := store.FindAccount("nonexistent@test.com") + if ok { + t.Fatal("expected account not found") + } +} + +func TestStoreIgnoresRemovedCompatConfig(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[],"compat":{"strip_reference_markers":false}}`) + store := LoadStore() + + snap := store.Snapshot() + data, err := snap.MarshalJSON() + if err != nil { + t.Fatalf("marshal failed: %v", err) + } + var out map[string]any + if err := json.Unmarshal(data, &out); err != nil { + t.Fatalf("decode failed: %v", err) + } + if _, ok := out["compat"]; ok { + t.Fatalf("expected removed compat field not to marshal, got %#v", out) + } +} + +func TestStoreIsEnvBacked(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[]}`) + store := LoadStore() + if !store.IsEnvBacked() { + t.Fatal("expected env-backed store") + } +} + +func TestStoreReplace(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[]}`) + store := LoadStore() + newCfg := Config{ + Keys: []string{"new-key"}, + Accounts: []Account{{Email: "new@test.com"}}, + } + if err := store.Replace(newCfg); err != nil { + t.Fatalf("replace error: %v", err) + } + if !store.HasAPIKey("new-key") { + t.Fatal("expected new key after replace") + } + if store.HasAPIKey("k1") { + t.Fatal("expected old key removed after replace") + } +} + +func TestStoreUpdate(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[]}`) + store := LoadStore() + err := store.Update(func(cfg *Config) error { + cfg.Keys = append(cfg.Keys, "k2") + return nil + }) + if err != nil { + t.Fatalf("update error: %v", err) + } + if !store.HasAPIKey("k2") { + t.Fatal("expected k2 after update") + } +} + +func TestStoreUpdateReconcilesAPIKeyMutations(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "api_keys":[{"key":"k1","name":"primary","remark":"prod"}], + "accounts":[] + }`) + store := LoadStore() + + if err := store.Update(func(cfg *Config) error { + cfg.APIKeys = append(cfg.APIKeys, APIKey{Key: "k2", Name: "secondary", Remark: "staging"}) + return nil + }); err != nil { + t.Fatalf("add api key failed: %v", err) + } + + snap := store.Snapshot() + if len(snap.Keys) != 2 || snap.Keys[0] != "k1" || snap.Keys[1] != "k2" { + t.Fatalf("unexpected keys after api key add: %#v", snap.Keys) + } + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys length after add: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary" || snap.APIKeys[0].Remark != "prod" { + t.Fatalf("metadata for existing key was lost: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Name != "secondary" || snap.APIKeys[1].Remark != "staging" { + t.Fatalf("metadata for new key was lost: %#v", snap.APIKeys[1]) + } + + if err := store.Update(func(cfg *Config) error { + cfg.APIKeys = append([]APIKey(nil), cfg.APIKeys[1:]...) + return nil + }); err != nil { + t.Fatalf("delete api key failed: %v", err) + } + + snap = store.Snapshot() + if len(snap.Keys) != 1 || snap.Keys[0] != "k2" { + t.Fatalf("unexpected keys after api key delete: %#v", snap.Keys) + } + if len(snap.APIKeys) != 1 || snap.APIKeys[0].Key != "k2" { + t.Fatalf("unexpected api keys after delete: %#v", snap.APIKeys) + } +} + +func TestStoreUpdateReconcilesLegacyKeyMutations(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "api_keys":[{"key":"k1","name":"primary","remark":"prod"}], + "accounts":[] + }`) + store := LoadStore() + + if err := store.Update(func(cfg *Config) error { + cfg.Keys = append(cfg.Keys, "k2") + return nil + }); err != nil { + t.Fatalf("legacy key update failed: %v", err) + } + + snap := store.Snapshot() + if len(snap.Keys) != 2 || snap.Keys[0] != "k1" || snap.Keys[1] != "k2" { + t.Fatalf("unexpected keys after legacy update: %#v", snap.Keys) + } + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after legacy update: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary" || snap.APIKeys[0].Remark != "prod" { + t.Fatalf("metadata for preserved key was lost: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Key != "k2" || snap.APIKeys[1].Name != "" || snap.APIKeys[1].Remark != "" { + t.Fatalf("new legacy key should stay metadata-free: %#v", snap.APIKeys[1]) + } +} + +func TestNormalizeCredentialsPrefersStructuredAPIKeys(t *testing.T) { + cfg := Config{ + Keys: []string{"legacy-key"}, + APIKeys: []APIKey{ + {Key: "structured-key", Name: "primary", Remark: "prod"}, + }, + } + cfg.NormalizeCredentials() + + if len(cfg.Keys) != 1 || cfg.Keys[0] != "structured-key" { + t.Fatalf("unexpected normalized keys: %#v", cfg.Keys) + } + if len(cfg.APIKeys) != 1 { + t.Fatalf("unexpected normalized api keys: %#v", cfg.APIKeys) + } + if cfg.APIKeys[0].Key != "structured-key" || cfg.APIKeys[0].Name != "primary" || cfg.APIKeys[0].Remark != "prod" { + t.Fatalf("unexpected structured api key metadata: %#v", cfg.APIKeys[0]) + } +} + +func TestStoreModelAliasesIncludesDefaultsAndOverrides(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":[],"accounts":[],"model_aliases":{"claude-opus-4-6":"deepseek-v4-pro-search"}}`) + store := LoadStore() + aliases := store.ModelAliases() + if aliases["claude-sonnet-4-6"] != "deepseek-v4-flash" { + t.Fatalf("expected default alias to remain available, got %q", aliases["claude-sonnet-4-6"]) + } + if aliases["claude-opus-4-6"] != "deepseek-v4-pro-search" { + t.Fatalf("expected custom alias override, got %q", aliases["claude-opus-4-6"]) + } +} + +func TestStoreModelAliasesDefault(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":[],"accounts":[]}`) + store := LoadStore() + aliases := store.ModelAliases() + if aliases == nil { + t.Fatal("expected non-nil aliases") + } + if aliases["claude-sonnet-4-6"] != "deepseek-v4-flash" { + t.Fatalf("expected built-in alias, got %q", aliases["claude-sonnet-4-6"]) + } +} + +func TestStoreSetVercelSync(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":[],"accounts":[]}`) + store := LoadStore() + if err := store.SetVercelSync("hash123", 1234567890); err != nil { + t.Fatalf("setVercelSync error: %v", err) + } + snap := store.Snapshot() + if snap.VercelSyncHash != "hash123" || snap.VercelSyncTime != 1234567890 { + t.Fatalf("unexpected vercel sync: hash=%q time=%d", snap.VercelSyncHash, snap.VercelSyncTime) + } +} + +func TestStoreExportJSONAndBase64(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["export-key"],"accounts":[]}`) + store := LoadStore() + jsonStr, b64Str, err := store.ExportJSONAndBase64() + if err != nil { + t.Fatalf("export error: %v", err) + } + if !strings.Contains(jsonStr, "export-key") { + t.Fatalf("expected JSON to contain key: %q", jsonStr) + } + decoded, err := base64.StdEncoding.DecodeString(b64Str) + if err != nil { + t.Fatalf("base64 decode error: %v", err) + } + if !strings.Contains(string(decoded), "export-key") { + t.Fatalf("expected base64-decoded to contain key: %q", string(decoded)) + } +} + +// ─── OpenAIModelsResponse / ClaudeModelsResponse ───────────────────── + +func TestOpenAIModelsResponse(t *testing.T) { + resp := OpenAIModelsResponse() + if resp["object"] != "list" { + t.Fatalf("unexpected object: %v", resp["object"]) + } + data, ok := resp["data"].([]ModelInfo) + if !ok { + t.Fatalf("unexpected data type: %T", resp["data"]) + } + if len(data) == 0 { + t.Fatal("expected non-empty models list") + } + expected := map[string]bool{ + "deepseek-v4-flash": false, + "deepseek-v4-flash-nothinking": false, + "deepseek-v4-pro": false, + "deepseek-v4-pro-nothinking": false, + "deepseek-v4-flash-search": false, + "deepseek-v4-flash-search-nothinking": false, + "deepseek-v4-pro-search": false, + "deepseek-v4-pro-search-nothinking": false, + "deepseek-v4-vision": false, + "deepseek-v4-vision-nothinking": false, + } + for _, model := range data { + if _, ok := expected[model.ID]; ok { + expected[model.ID] = true + } + } + for id, seen := range expected { + if !seen { + t.Fatalf("expected OpenAI model list to include %s", id) + } + } +} + +func TestClaudeModelsResponse(t *testing.T) { + resp := ClaudeModelsResponse() + if resp["object"] != "list" { + t.Fatalf("unexpected object: %v", resp["object"]) + } + data, ok := resp["data"].([]ModelInfo) + if !ok { + t.Fatalf("unexpected data type: %T", resp["data"]) + } + if len(data) == 0 { + t.Fatal("expected non-empty models list") + } +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d6954034f203c3ba85ab0722a27e1a0f14a011bc --- /dev/null +++ b/internal/config/config_test.go @@ -0,0 +1,464 @@ +package config + +import ( + "encoding/base64" + "errors" + "os" + "strings" + "testing" +) + +func TestAccountIdentifierRequiresEmailOrMobile(t *testing.T) { + acc := Account{Token: "example-token-value"} + id := acc.Identifier() + if id != "" { + t.Fatalf("expected empty identifier when only token is present, got %q", id) + } +} + +func TestLoadStoreClearsTokensFromConfigInput(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[{"email":"u@example.com","password":"p","token":"token-only-account"}] + }`) + + store := LoadStore() + accounts := store.Accounts() + if len(accounts) != 1 { + t.Fatalf("expected 1 account, got %d", len(accounts)) + } + if accounts[0].Token != "" { + t.Fatalf("expected token to be cleared after loading, got %q", accounts[0].Token) + } +} + +func TestLoadStorePreservesProxiesAndAccountProxyAssignment(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "proxies":[ + { + "id":"proxy-sh-1", + "name":"Shanghai Exit", + "type":"socks5h", + "host":"127.0.0.1", + "port":1080, + "username":"demo", + "password":"secret" + } + ], + "accounts":[ + { + "email":"u@example.com", + "password":"p", + "proxy_id":"proxy-sh-1" + } + ] + }`) + + store := LoadStore() + snap := store.Snapshot() + if len(snap.Proxies) != 1 { + t.Fatalf("expected 1 proxy, got %d", len(snap.Proxies)) + } + if snap.Proxies[0].ID != "proxy-sh-1" { + t.Fatalf("unexpected proxy id: %#v", snap.Proxies[0]) + } + if snap.Proxies[0].Type != "socks5h" { + t.Fatalf("unexpected proxy type: %#v", snap.Proxies[0]) + } + if len(snap.Accounts) != 1 { + t.Fatalf("expected 1 account, got %d", len(snap.Accounts)) + } + if snap.Accounts[0].ProxyID != "proxy-sh-1" { + t.Fatalf("expected account proxy assignment preserved, got %#v", snap.Accounts[0]) + } +} + +func TestLoadStoreDropsLegacyTokenOnlyAccounts(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "accounts":[ + {"token":"legacy-token-only"}, + {"email":"u@example.com","password":"p","token":"runtime-token"} + ] + }`) + + store := LoadStore() + accounts := store.Accounts() + if len(accounts) != 1 { + t.Fatalf("expected token-only account to be dropped, got %d accounts", len(accounts)) + } + if accounts[0].Identifier() != "u@example.com" { + t.Fatalf("unexpected remaining account: %#v", accounts[0]) + } + if accounts[0].Token != "" { + t.Fatalf("expected persisted token to be cleared, got %q", accounts[0].Token) + } +} + +func TestLoadStorePreservesFileBackedTokensForRuntime(t *testing.T) { + tmp, err := os.CreateTemp(t.TempDir(), "config-*.json") + if err != nil { + t.Fatalf("create temp config: %v", err) + } + defer func() { _ = tmp.Close() }() + if _, err := tmp.WriteString(`{ + "accounts":[{"email":"u@example.com","password":"p","token":"persisted-token"}] + }`); err != nil { + t.Fatalf("write temp config: %v", err) + } + + t.Setenv("DS2API_CONFIG_JSON", "") + t.Setenv("DS2API_CONFIG_PATH", tmp.Name()) + + store := LoadStore() + accounts := store.Accounts() + if len(accounts) != 1 { + t.Fatalf("expected 1 account, got %d", len(accounts)) + } + if accounts[0].Token != "persisted-token" { + t.Fatalf("expected file-backed token preserved for runtime use, got %q", accounts[0].Token) + } +} + +func TestLoadStoreIgnoresLegacyConfigJSONEnv(t *testing.T) { + tmp, err := os.CreateTemp(t.TempDir(), "config-*.json") + if err != nil { + t.Fatalf("create temp config: %v", err) + } + path := tmp.Name() + _ = tmp.Close() + _ = os.Remove(path) + + t.Setenv("DS2API_CONFIG_JSON", "") + t.Setenv("CONFIG_JSON", `{"keys":["legacy-key"],"accounts":[{"email":"legacy@example.com","password":"p"}]}`) + t.Setenv("DS2API_CONFIG_PATH", path) + + store := LoadStore() + if store.HasEnvConfigSource() { + t.Fatal("expected legacy CONFIG_JSON to be ignored") + } + if store.IsEnvBacked() { + t.Fatal("expected store to remain file-backed/empty when only CONFIG_JSON is set") + } + if len(store.Keys()) != 0 || len(store.Accounts()) != 0 { + t.Fatalf("expected ignored legacy env to leave store empty, got keys=%d accounts=%d", len(store.Keys()), len(store.Accounts())) + } +} + +func TestExplicitMissingConfigPathBootstrapsEmptyFileBackedStore(t *testing.T) { + path := t.TempDir() + "/config.json" + + t.Setenv("DS2API_CONFIG_JSON", "") + t.Setenv("DS2API_CONFIG_PATH", path) + + store, err := LoadStoreWithError() + if err != nil { + t.Fatalf("expected missing explicit config path to bootstrap, got: %v", err) + } + if store.IsEnvBacked() { + t.Fatal("expected bootstrap store to be file-backed") + } + if store.ConfigPath() != path { + t.Fatalf("ConfigPath() = %q, want %q", store.ConfigPath(), path) + } + if len(store.Keys()) != 0 || len(store.Accounts()) != 0 { + t.Fatalf("expected empty bootstrap config, got keys=%d accounts=%d", len(store.Keys()), len(store.Accounts())) + } + if _, statErr := os.Stat(path); !errors.Is(statErr, os.ErrNotExist) { + t.Fatalf("expected bootstrap not to create config until first save, stat err=%v", statErr) + } + + if err := store.Update(func(c *Config) error { + c.Keys = []string{"first-key"} + return nil + }); err != nil { + t.Fatalf("update should persist bootstrap config: %v", err) + } + content, err := os.ReadFile(path) + if err != nil { + t.Fatalf("expected first update to write config: %v", err) + } + if !strings.Contains(string(content), "first-key") { + t.Fatalf("expected saved config to contain first key, got: %s", content) + } +} + +func TestEnvBackedStoreWritebackBootstrapsMissingConfigFile(t *testing.T) { + tmp, err := os.CreateTemp(t.TempDir(), "config-*.json") + if err != nil { + t.Fatalf("create temp config: %v", err) + } + path := tmp.Name() + _ = tmp.Close() + _ = os.Remove(path) + + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[{"email":"seed@example.com","password":"p"}]}`) + t.Setenv("DS2API_CONFIG_PATH", path) + t.Setenv("DS2API_ENV_WRITEBACK", "1") + + store := LoadStore() + if store.IsEnvBacked() { + t.Fatalf("expected writeback bootstrap to become file-backed immediately") + } + if err := store.Update(func(c *Config) error { + c.Accounts = append(c.Accounts, Account{Email: "new@example.com", Password: "p2"}) + return nil + }); err != nil { + t.Fatalf("update failed: %v", err) + } + content, err := os.ReadFile(path) + if err != nil { + t.Fatalf("read written config: %v", err) + } + if !strings.Contains(string(content), "seed@example.com") { + t.Fatalf("expected bootstrapped config to contain seed account, got: %s", content) + } + if !strings.Contains(string(content), "new@example.com") { + t.Fatalf("expected persisted config to contain added account, got: %s", content) + } + + reloaded := LoadStore() + if reloaded.IsEnvBacked() { + t.Fatalf("expected reloaded store to prefer persisted config file") + } + accounts := reloaded.Accounts() + if len(accounts) != 2 { + t.Fatalf("expected 2 accounts after reload, got %d", len(accounts)) + } +} + +func TestEnvBackedStoreWritebackDoesNotBootstrapOnInvalidEnvJSON(t *testing.T) { + tmp, err := os.CreateTemp(t.TempDir(), "config-*.json") + if err != nil { + t.Fatalf("create temp config: %v", err) + } + path := tmp.Name() + _ = tmp.Close() + _ = os.Remove(path) + + t.Setenv("DS2API_CONFIG_JSON", "{invalid-json") + t.Setenv("DS2API_CONFIG_PATH", path) + t.Setenv("DS2API_ENV_WRITEBACK", "1") + + cfg, fromEnv, loadErr := loadConfig() + if loadErr == nil { + t.Fatalf("expected loadConfig error for invalid env json") + } + if !fromEnv { + t.Fatalf("expected fromEnv=true when parsing env config fails") + } + if len(cfg.Keys) != 0 || len(cfg.Accounts) != 0 { + t.Fatalf("expected empty config on parse failure, got keys=%d accounts=%d", len(cfg.Keys), len(cfg.Accounts)) + } + if _, statErr := os.Stat(path); !errors.Is(statErr, os.ErrNotExist) { + t.Fatalf("expected no bootstrapped config file, stat err=%v", statErr) + } +} + +func TestEnvBackedStoreWritebackDoesNotBootstrapOnInvalidSemanticConfig(t *testing.T) { + tmp, err := os.CreateTemp(t.TempDir(), "config-*.json") + if err != nil { + t.Fatalf("create temp config: %v", err) + } + path := tmp.Name() + _ = tmp.Close() + _ = os.Remove(path) + + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[{"email":"seed@example.com","password":"p"}], + "runtime":{"account_max_inflight":300} + }`) + t.Setenv("DS2API_CONFIG_PATH", path) + t.Setenv("DS2API_ENV_WRITEBACK", "1") + + cfg, fromEnv, loadErr := loadConfig() + if loadErr == nil { + t.Fatalf("expected loadConfig error for invalid runtime config") + } + if !fromEnv { + t.Fatalf("expected fromEnv=true when env config is the source") + } + if !strings.Contains(loadErr.Error(), "runtime.account_max_inflight") { + t.Fatalf("expected runtime validation error, got %v", loadErr) + } + if len(cfg.Keys) != 1 || len(cfg.Accounts) != 1 { + t.Fatalf("expected env config to be parsed before validation, got keys=%d accounts=%d", len(cfg.Keys), len(cfg.Accounts)) + } + if _, statErr := os.Stat(path); !errors.Is(statErr, os.ErrNotExist) { + t.Fatalf("expected invalid config not to be bootstrapped, stat err=%v", statErr) + } +} + +func TestLoadStoreWithErrorRejectsInvalidRuntimeConfig(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[{"email":"u@example.com","password":"p"}], + "runtime":{"account_max_inflight":300} + }`) + t.Setenv("DS2API_ENV_WRITEBACK", "0") + + if _, err := LoadStoreWithError(); err == nil { + t.Fatal("expected LoadStoreWithError to reject invalid runtime config") + } else if !strings.Contains(err.Error(), "runtime.account_max_inflight") { + t.Fatalf("expected runtime validation error, got %v", err) + } +} + +func TestEnvBackedStoreWritebackFallsBackToPersistedFileOnInvalidEnvJSON(t *testing.T) { + tmp, err := os.CreateTemp(t.TempDir(), "config-*.json") + if err != nil { + t.Fatalf("create temp config: %v", err) + } + path := tmp.Name() + if _, err := tmp.WriteString(`{"keys":["file-key"],"accounts":[{"email":"persisted@example.com","password":"p"}]}`); err != nil { + t.Fatalf("write temp config: %v", err) + } + _ = tmp.Close() + + t.Setenv("DS2API_CONFIG_JSON", "{invalid-json") + t.Setenv("DS2API_CONFIG_PATH", path) + t.Setenv("DS2API_ENV_WRITEBACK", "1") + + cfg, fromEnv, loadErr := loadConfig() + if loadErr != nil { + t.Fatalf("expected fallback to persisted file, got error: %v", loadErr) + } + if fromEnv { + t.Fatalf("expected fallback to file-backed mode") + } + if len(cfg.Keys) != 1 || cfg.Keys[0] != "file-key" { + t.Fatalf("unexpected keys after fallback: %#v", cfg.Keys) + } + if len(cfg.Accounts) != 1 || cfg.Accounts[0].Email != "persisted@example.com" { + t.Fatalf("unexpected accounts after fallback: %#v", cfg.Accounts) + } +} + +func TestRuntimeTokenRefreshIntervalHoursDefaultsToSix(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[{"email":"u@example.com","password":"p"}] + }`) + + store := LoadStore() + if got := store.RuntimeTokenRefreshIntervalHours(); got != 6 { + t.Fatalf("expected default refresh interval 6, got %d", got) + } +} + +func TestRuntimeTokenRefreshIntervalHoursUsesConfigValue(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["k1"], + "accounts":[{"email":"u@example.com","password":"p"}], + "runtime":{"token_refresh_interval_hours":9} + }`) + + store := LoadStore() + if got := store.RuntimeTokenRefreshIntervalHours(); got != 9 { + t.Fatalf("expected configured refresh interval 9, got %d", got) + } +} + +func TestStoreUpdateAccountTokenKeepsIdentifierResolvable(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{ + "accounts":[{"email":"user@example.com","password":"p"}] + }`) + + store := LoadStore() + before := store.Accounts() + if len(before) != 1 { + t.Fatalf("expected 1 account, got %d", len(before)) + } + oldID := before[0].Identifier() + if err := store.UpdateAccountToken(oldID, "new-token"); err != nil { + t.Fatalf("update token failed: %v", err) + } + + if got, ok := store.FindAccount(oldID); !ok || got.Token != "new-token" { + t.Fatalf("expected find by stable account identifier") + } +} + +func TestLoadStoreRejectsInvalidFieldType(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":"not-array","accounts":[]}`) + store := LoadStore() + if len(store.Keys()) != 0 || len(store.Accounts()) != 0 { + t.Fatalf("expected empty store when config type is invalid") + } +} + +func TestParseConfigStringSupportsQuotedBase64Prefix(t *testing.T) { + rawJSON := `{"keys":["k1"],"accounts":[{"email":"u@example.com","password":"p"}]}` + b64 := base64.StdEncoding.EncodeToString([]byte(rawJSON)) + cfg, err := parseConfigString(`"base64:` + b64 + `"`) + if err != nil { + t.Fatalf("unexpected parse error: %v", err) + } + if len(cfg.Keys) != 1 || cfg.Keys[0] != "k1" { + t.Fatalf("unexpected keys: %#v", cfg.Keys) + } +} + +func TestParseConfigStringSupportsRawURLBase64(t *testing.T) { + rawJSON := `{"keys":["k-url"],"accounts":[]}` + b64 := base64.RawURLEncoding.EncodeToString([]byte(rawJSON)) + cfg, err := parseConfigString(b64) + if err != nil { + t.Fatalf("unexpected parse error: %v", err) + } + if len(cfg.Keys) != 1 || cfg.Keys[0] != "k-url" { + t.Fatalf("unexpected keys: %#v", cfg.Keys) + } +} + +func TestLoadConfigOnVercelWithoutConfigFileFallsBackToMemory(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_CONFIG_JSON", "") + t.Setenv("DS2API_CONFIG_PATH", "testdata/does-not-exist.json") + + cfg, fromEnv, err := loadConfig() + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if !fromEnv { + t.Fatalf("expected fromEnv=true for vercel fallback") + } + if len(cfg.Keys) != 0 || len(cfg.Accounts) != 0 { + t.Fatalf("expected empty bootstrap config, got keys=%d accounts=%d", len(cfg.Keys), len(cfg.Accounts)) + } +} + +func TestAccountTestStatusIsRuntimeOnlyAndNotPersisted(t *testing.T) { + tmp, err := os.CreateTemp(t.TempDir(), "config-*.json") + if err != nil { + t.Fatalf("create temp config: %v", err) + } + defer func() { _ = tmp.Close() }() + if _, err := tmp.WriteString(`{ + "accounts":[{"email":"u@example.com","password":"p","test_status":"ok"}] + }`); err != nil { + t.Fatalf("write temp config: %v", err) + } + + t.Setenv("DS2API_CONFIG_JSON", "") + t.Setenv("DS2API_CONFIG_PATH", tmp.Name()) + + store := LoadStore() + if got, ok := store.AccountTestStatus("u@example.com"); ok || got != "" { + t.Fatalf("expected no runtime status loaded from config, got %q", got) + } + if err := store.UpdateAccountTestStatus("u@example.com", "ok"); err != nil { + t.Fatalf("update test status: %v", err) + } + if got, ok := store.AccountTestStatus("u@example.com"); !ok || got != "ok" { + t.Fatalf("expected runtime status to be available, got %q (ok=%v)", got, ok) + } + + content, err := os.ReadFile(tmp.Name()) + if err != nil { + t.Fatalf("read config: %v", err) + } + if strings.Contains(string(content), "test_status") { + t.Fatalf("expected test_status to stay out of persisted config, got: %s", content) + } +} diff --git a/internal/config/credentials.go b/internal/config/credentials.go new file mode 100644 index 0000000000000000000000000000000000000000..a29f314262f158b72fb0afe60f257fed591a9d9e --- /dev/null +++ b/internal/config/credentials.go @@ -0,0 +1,158 @@ +package config + +import ( + "slices" + "strings" +) + +func (c *Config) ReconcileCredentials(base Config) { + if c == nil { + return + } + currKeys := normalizeKeys(c.Keys) + currAPIKeys := normalizeAPIKeys(c.APIKeys) + baseKeys := normalizeKeys(base.Keys) + baseAPIKeys := normalizeAPIKeys(base.APIKeys) + + keysChanged := !slices.Equal(currKeys, baseKeys) + apiKeysChanged := !equalAPIKeys(currAPIKeys, baseAPIKeys) + + if keysChanged && !apiKeysChanged { + c.APIKeys = apiKeysFromStrings(currKeys, apiKeyMap(baseAPIKeys)) + } else { + c.APIKeys = currAPIKeys + } + c.Keys = apiKeysToStrings(c.APIKeys) +} + +func normalizeKeys(keys []string) []string { + if len(keys) == 0 { + return nil + } + out := make([]string, 0, len(keys)) + seen := make(map[string]struct{}, len(keys)) + for _, key := range keys { + key = strings.TrimSpace(key) + if key == "" { + continue + } + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + out = append(out, key) + } + if len(out) == 0 { + return nil + } + return out +} + +func normalizeAPIKeys(items []APIKey) []APIKey { + if len(items) == 0 { + return nil + } + out := make([]APIKey, 0, len(items)) + seen := make(map[string]struct{}, len(items)) + for _, item := range items { + key := strings.TrimSpace(item.Key) + if key == "" { + continue + } + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + out = append(out, APIKey{ + Key: key, + Name: strings.TrimSpace(item.Name), + Remark: strings.TrimSpace(item.Remark), + }) + } + if len(out) == 0 { + return nil + } + return out +} + +func apiKeysFromStrings(keys []string, meta map[string]APIKey) []APIKey { + if len(keys) == 0 { + return nil + } + out := make([]APIKey, 0, len(keys)) + seen := make(map[string]struct{}, len(keys)) + for _, key := range keys { + key = strings.TrimSpace(key) + if key == "" { + continue + } + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + if item, ok := meta[key]; ok { + out = append(out, APIKey{ + Key: key, + Name: strings.TrimSpace(item.Name), + Remark: strings.TrimSpace(item.Remark), + }) + continue + } + out = append(out, APIKey{Key: key}) + } + if len(out) == 0 { + return nil + } + return out +} + +func apiKeysToStrings(items []APIKey) []string { + if len(items) == 0 { + return nil + } + keys := make([]string, 0, len(items)) + for _, item := range items { + key := strings.TrimSpace(item.Key) + if key == "" { + continue + } + keys = append(keys, key) + } + if len(keys) == 0 { + return nil + } + return keys +} + +func apiKeyMap(items []APIKey) map[string]APIKey { + if len(items) == 0 { + return nil + } + out := make(map[string]APIKey, len(items)) + for _, item := range items { + key := strings.TrimSpace(item.Key) + if key == "" { + continue + } + if _, ok := out[key]; ok { + continue + } + out[key] = APIKey{ + Key: key, + Name: strings.TrimSpace(item.Name), + Remark: strings.TrimSpace(item.Remark), + } + } + return out +} + +func equalAPIKeys(a, b []APIKey) bool { + if len(a) != len(b) { + return false + } + return slices.EqualFunc(a, b, func(x, y APIKey) bool { + return strings.TrimSpace(x.Key) == strings.TrimSpace(y.Key) && + strings.TrimSpace(x.Name) == strings.TrimSpace(y.Name) && + strings.TrimSpace(x.Remark) == strings.TrimSpace(y.Remark) + }) +} diff --git a/internal/config/dotenv.go b/internal/config/dotenv.go new file mode 100644 index 0000000000000000000000000000000000000000..c33d2b09a1c69de888555d3fae17b519680d95cb --- /dev/null +++ b/internal/config/dotenv.go @@ -0,0 +1,137 @@ +package config + +import ( + "errors" + "fmt" + "os" + "path/filepath" + "strings" +) + +// LoadDotEnv loads environment variables from .env in the current working +// directory without overriding variables that are already set. +func LoadDotEnv() error { + return loadDotEnvFromPath(filepath.Join(BaseDir(), ".env")) +} + +func loadDotEnvFromPath(path string) error { + content, err := os.ReadFile(path) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + return nil + } + return err + } + + lines := strings.Split(strings.ReplaceAll(string(content), "\r\n", "\n"), "\n") + for i, rawLine := range lines { + line := strings.TrimSpace(rawLine) + if i == 0 { + line = strings.TrimPrefix(line, "\ufeff") + } + if line == "" || strings.HasPrefix(line, "#") { + continue + } + if strings.HasPrefix(line, "export ") { + line = strings.TrimSpace(strings.TrimPrefix(line, "export ")) + } + + key, value, ok := strings.Cut(line, "=") + if !ok { + return fmt.Errorf("%s:%d invalid env assignment", path, i+1) + } + key = strings.TrimSpace(key) + if key == "" { + return fmt.Errorf("%s:%d empty env key", path, i+1) + } + if _, exists := os.LookupEnv(key); exists { + continue + } + if err := os.Setenv(key, normalizeDotEnvValue(trimDotEnvValue(strings.TrimSpace(value)))); err != nil { + return fmt.Errorf("%s:%d set env %q: %w", path, i+1, key, err) + } + } + + return nil +} + +// Preserve quoted values, but drop Compose-style inline comments from unquoted values. +func trimDotEnvValue(raw string) string { + if raw == "" { + return raw + } + + switch raw[0] { + case '"': + if trimmed, ok := trimQuotedDotEnvValue(raw, '"'); ok { + return trimmed + } + case '\'': + if trimmed, ok := trimQuotedDotEnvValue(raw, '\''); ok { + return trimmed + } + default: + if idx := inlineDotEnvCommentStart(raw); idx >= 0 { + return strings.TrimSpace(raw[:idx]) + } + } + + return raw +} + +func trimQuotedDotEnvValue(raw string, quote byte) (string, bool) { + escaped := false + for i := 1; i < len(raw); i++ { + ch := raw[i] + if quote == '"' && escaped { + escaped = false + continue + } + if quote == '"' && ch == '\\' { + escaped = true + continue + } + if ch == quote { + return strings.TrimSpace(raw[:i+1]), true + } + } + return raw, false +} + +func inlineDotEnvCommentStart(raw string) int { + for i := 1; i < len(raw); i++ { + if raw[i] == '#' && isDotEnvCommentSpacer(raw[i-1]) { + return i + } + } + return -1 +} + +func isDotEnvCommentSpacer(b byte) bool { + return b == ' ' || b == '\t' +} + +func normalizeDotEnvValue(raw string) string { + if len(raw) < 2 { + return raw + } + first := raw[0] + last := raw[len(raw)-1] + if (first != '"' || last != '"') && (first != '\'' || last != '\'') { + return raw + } + + raw = raw[1 : len(raw)-1] + if first == '\'' { + return raw + } + + replacer := strings.NewReplacer( + `\\`, `\`, + `\n`, "\n", + `\r`, "\r", + `\t`, "\t", + `\"`, `"`, + ) + return replacer.Replace(raw) +} diff --git a/internal/config/dotenv_test.go b/internal/config/dotenv_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2e8a3a8e19d6ea86a6b0b02085e0739d87f9ed8c --- /dev/null +++ b/internal/config/dotenv_test.go @@ -0,0 +1,135 @@ +package config + +import ( + "os" + "path/filepath" + "strings" + "testing" +) + +func TestLoadDotEnvLoadsWorkingDirectoryFileWithoutOverridingExistingEnv(t *testing.T) { + dir := t.TempDir() + oldWD, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %v", err) + } + if err := os.Chdir(dir); err != nil { + t.Fatalf("chdir temp dir: %v", err) + } + t.Cleanup(func() { + _ = os.Chdir(oldWD) + }) + + const newKey = "DS2API_TEST_DOTENV_NEW" + const keepKey = "DS2API_TEST_DOTENV_KEEP" + const quotedKey = "DS2API_TEST_DOTENV_QUOTED" + + unsetEnv(t, newKey) + unsetEnv(t, quotedKey) + t.Setenv(keepKey, "from-env") + + content := "DS2API_TEST_DOTENV_NEW=from-file\n" + + "DS2API_TEST_DOTENV_KEEP=from-file\n" + + "DS2API_TEST_DOTENV_QUOTED=\"line1\\nline2\"\n" + if err := os.WriteFile(filepath.Join(dir, ".env"), []byte(content), 0o644); err != nil { + t.Fatalf("write .env: %v", err) + } + + if err := LoadDotEnv(); err != nil { + t.Fatalf("LoadDotEnv() error: %v", err) + } + + if got := os.Getenv(newKey); got != "from-file" { + t.Fatalf("expected %s from .env, got %q", newKey, got) + } + if got := os.Getenv(keepKey); got != "from-env" { + t.Fatalf("expected existing env to win, got %q", got) + } + if got := os.Getenv(quotedKey); got != "line1\nline2" { + t.Fatalf("expected quoted newline decoding, got %q", got) + } +} + +func TestLoadDotEnvIgnoresMissingFile(t *testing.T) { + dir := t.TempDir() + oldWD, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %v", err) + } + if err := os.Chdir(dir); err != nil { + t.Fatalf("chdir temp dir: %v", err) + } + t.Cleanup(func() { + _ = os.Chdir(oldWD) + }) + + if err := LoadDotEnv(); err != nil { + t.Fatalf("expected missing .env to be ignored, got %v", err) + } +} + +func TestLoadDotEnvStripsInlineCommentsFromUnquotedValues(t *testing.T) { + dir := t.TempDir() + oldWD, err := os.Getwd() + if err != nil { + t.Fatalf("getwd: %v", err) + } + if err := os.Chdir(dir); err != nil { + t.Fatalf("chdir temp dir: %v", err) + } + t.Cleanup(func() { + _ = os.Chdir(oldWD) + }) + + const plainKey = "DS2API_TEST_DOTENV_PLAIN" + const hashKey = "DS2API_TEST_DOTENV_HASH" + const quotedKey = "DS2API_TEST_DOTENV_QUOTED_COMMENT" + const exportKey = "DS2API_TEST_DOTENV_EXPORT" + + unsetEnv(t, plainKey) + unsetEnv(t, hashKey) + unsetEnv(t, quotedKey) + unsetEnv(t, exportKey) + + content := strings.Join([]string{ + plainKey + "=5001 # local", + hashKey + "=5001#local", + quotedKey + `="5001 # local" # keep the inner hash`, + "export " + exportKey + "=enabled # exported", + }, "\n") + "\n" + if err := os.WriteFile(filepath.Join(dir, ".env"), []byte(content), 0o644); err != nil { + t.Fatalf("write .env: %v", err) + } + + if err := LoadDotEnv(); err != nil { + t.Fatalf("LoadDotEnv() error: %v", err) + } + + if got := os.Getenv(plainKey); got != "5001" { + t.Fatalf("expected inline comment to be stripped, got %q", got) + } + if got := os.Getenv(hashKey); got != "5001#local" { + t.Fatalf("expected hash without preceding whitespace to remain, got %q", got) + } + if got := os.Getenv(quotedKey); got != "5001 # local" { + t.Fatalf("expected quoted value to preserve hash text, got %q", got) + } + if got := os.Getenv(exportKey); got != "enabled" { + t.Fatalf("expected export syntax to load, got %q", got) + } +} + +func unsetEnv(t *testing.T, key string) { + t.Helper() + old, had := os.LookupEnv(key) + if err := os.Unsetenv(key); err != nil { + t.Fatalf("unset %s: %v", key, err) + } + t.Cleanup(func() { + if had { + _ = os.Setenv(key, old) + return + } + _ = os.Unsetenv(key) + }) +} diff --git a/internal/config/logger.go b/internal/config/logger.go new file mode 100644 index 0000000000000000000000000000000000000000..e88fee1c988a9b37678407deef27e640d0a795f9 --- /dev/null +++ b/internal/config/logger.go @@ -0,0 +1,29 @@ +package config + +import ( + "log/slog" + "os" + "strings" +) + +var Logger = newLogger() + +func newLogger() *slog.Logger { + level := new(slog.LevelVar) + switch strings.ToUpper(strings.TrimSpace(os.Getenv("LOG_LEVEL"))) { + case "DEBUG": + level.Set(slog.LevelDebug) + case "WARN": + level.Set(slog.LevelWarn) + case "ERROR": + level.Set(slog.LevelError) + default: + level.Set(slog.LevelInfo) + } + h := slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: level}) + return slog.New(h) +} + +func RefreshLogger() { + Logger = newLogger() +} diff --git a/internal/config/mobile.go b/internal/config/mobile.go new file mode 100644 index 0000000000000000000000000000000000000000..7e2158be52dd77e8407d9aa37ab7afa1cbb1a0b6 --- /dev/null +++ b/internal/config/mobile.go @@ -0,0 +1,82 @@ +package config + +import "strings" + +// NormalizeMobileForStorage normalizes user input to a stable storage format. +// It keeps existing country codes and auto-prefixes mainland China numbers with +86. +func NormalizeMobileForStorage(raw string) string { + digits, hasPlus := extractMobileDigits(raw) + if digits == "" { + return "" + } + if hasPlus { + return "+" + digits + } + if isChinaMobileWithCountryCode(digits) { + return "+86" + digits[2:] + } + if isChinaMainlandMobileDigits(digits) { + return "+86" + digits + } + // For non-China numbers without a leading +, preserve semantics by adding it. + return "+" + digits +} + +// CanonicalMobileKey returns the comparison key used by dedupe/matching logic. +func CanonicalMobileKey(raw string) string { + return NormalizeMobileForStorage(raw) +} + +func extractMobileDigits(raw string) (digits string, hasPlus bool) { + s := strings.TrimSpace(raw) + if s == "" { + return "", false + } + + for _, r := range s { + switch { + case r >= '0' && r <= '9': + goto collect + case isMobileSeparator(r): + continue + case r == '+': + hasPlus = true + goto collect + default: + goto collect + } + } + +collect: + var b strings.Builder + b.Grow(len(s)) + for _, r := range s { + if r >= '0' && r <= '9' { + b.WriteRune(r) + } + } + return b.String(), hasPlus +} + +func isChinaMainlandMobileDigits(digits string) bool { + if len(digits) != 11 || digits[0] != '1' { + return false + } + return digits[1] >= '3' && digits[1] <= '9' +} + +func isChinaMobileWithCountryCode(digits string) bool { + if len(digits) != 13 || !strings.HasPrefix(digits, "86") { + return false + } + return isChinaMainlandMobileDigits(digits[2:]) +} + +func isMobileSeparator(r rune) bool { + switch r { + case ' ', '\t', '\n', '\r', '-', '(', ')', '.', '/': + return true + default: + return false + } +} diff --git a/internal/config/mobile_test.go b/internal/config/mobile_test.go new file mode 100644 index 0000000000000000000000000000000000000000..96a98b6a3613802d67d9675c34d339e249e7a8bb --- /dev/null +++ b/internal/config/mobile_test.go @@ -0,0 +1,36 @@ +package config + +import "testing" + +func TestNormalizeMobileForStorageChinaMainlandAddsPlus86(t *testing.T) { + if got := NormalizeMobileForStorage("13800138000"); got != "+8613800138000" { + t.Fatalf("got %q", got) + } +} + +func TestNormalizeMobileForStorageChinaWithCountryCode(t *testing.T) { + if got := NormalizeMobileForStorage("8613800138000"); got != "+8613800138000" { + t.Fatalf("got %q", got) + } +} + +func TestNormalizeMobileForStorageKeepsExistingCountryCode(t *testing.T) { + if got := NormalizeMobileForStorage(" +1 (415) 555-2671 "); got != "+14155552671" { + t.Fatalf("got %q", got) + } +} + +func TestCanonicalMobileKeyMatchesChinaAliases(t *testing.T) { + a := CanonicalMobileKey("+8613800138000") + b := CanonicalMobileKey("13800138000") + c := CanonicalMobileKey("86 13800138000") + if a == "" || a != b || b != c { + t.Fatalf("alias mismatch: a=%q b=%q c=%q", a, b, c) + } +} + +func TestCanonicalMobileKeyEmptyForInvalidInput(t *testing.T) { + if got := CanonicalMobileKey("() --"); got != "" { + t.Fatalf("got %q", got) + } +} diff --git a/internal/config/model_alias_test.go b/internal/config/model_alias_test.go new file mode 100644 index 0000000000000000000000000000000000000000..573c6e3b0ab2a2c50945959cad906855cccb83c6 --- /dev/null +++ b/internal/config/model_alias_test.go @@ -0,0 +1,158 @@ +package config + +import "testing" + +type mockModelAliasReader map[string]string + +func (m mockModelAliasReader) ModelAliases() map[string]string { return m } + +func TestResolveModelDirectDeepSeek(t *testing.T) { + got, ok := ResolveModel(nil, "deepseek-v4-flash") + if !ok || got != "deepseek-v4-flash" { + t.Fatalf("expected deepseek-v4-flash, got ok=%v model=%q", ok, got) + } +} + +func TestResolveModelDirectDeepSeekNoThinking(t *testing.T) { + got, ok := ResolveModel(nil, "deepseek-v4-flash-nothinking") + if !ok || got != "deepseek-v4-flash-nothinking" { + t.Fatalf("expected deepseek-v4-flash-nothinking, got ok=%v model=%q", ok, got) + } +} + +func TestResolveModelAlias(t *testing.T) { + got, ok := ResolveModel(nil, "gpt-4.1") + if !ok || got != "deepseek-v4-flash" { + t.Fatalf("expected alias gpt-4.1 -> deepseek-v4-flash, got ok=%v model=%q", ok, got) + } +} + +func TestResolveLatestOpenAIAlias(t *testing.T) { + got, ok := ResolveModel(nil, "gpt-5.5") + if !ok || got != "deepseek-v4-flash" { + t.Fatalf("expected alias gpt-5.5 -> deepseek-v4-flash, got ok=%v model=%q", ok, got) + } +} + +func TestResolveLatestClaudeAlias(t *testing.T) { + got, ok := ResolveModel(nil, "claude-sonnet-4-6") + if !ok || got != "deepseek-v4-flash" { + t.Fatalf("expected alias claude-sonnet-4-6 -> deepseek-v4-flash, got ok=%v model=%q", ok, got) + } +} + +func TestResolveLatestClaudeAliasNoThinking(t *testing.T) { + got, ok := ResolveModel(nil, "claude-sonnet-4-6-nothinking") + if !ok || got != "deepseek-v4-flash-nothinking" { + t.Fatalf("expected alias claude-sonnet-4-6-nothinking -> deepseek-v4-flash-nothinking, got ok=%v model=%q", ok, got) + } +} + +func TestResolveExpandedHistoricalAliases(t *testing.T) { + cases := []struct { + name string + model string + want string + }{ + {name: "openai old chatgpt", model: "chatgpt-4o", want: "deepseek-v4-flash"}, + {name: "openai codex max", model: "gpt-5.1-codex-max", want: "deepseek-v4-pro"}, + {name: "openai deep research", model: "o3-deep-research", want: "deepseek-v4-pro-search"}, + {name: "openai historical reasoning", model: "o1-preview", want: "deepseek-v4-pro"}, + {name: "claude latest historical", model: "claude-3-5-sonnet-latest", want: "deepseek-v4-flash"}, + {name: "claude historical opus", model: "claude-3-opus-20240229", want: "deepseek-v4-pro"}, + {name: "claude historical haiku", model: "claude-3-haiku-20240307", want: "deepseek-v4-flash"}, + {name: "gemini latest alias", model: "gemini-flash-latest", want: "deepseek-v4-flash"}, + {name: "gemini historical pro", model: "gemini-1.5-pro", want: "deepseek-v4-pro"}, + {name: "gemini vision legacy", model: "gemini-pro-vision", want: "deepseek-v4-vision"}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got, ok := ResolveModel(nil, tc.model) + if !ok || got != tc.want { + t.Fatalf("expected alias %s -> %s, got ok=%v model=%q", tc.model, tc.want, ok, got) + } + }) + } +} + +func TestResolveModelUnknown(t *testing.T) { + _, ok := ResolveModel(nil, "totally-custom-model") + if ok { + t.Fatal("expected unknown model to fail resolve") + } +} + +func TestResolveModelUnknownKnownFamilyName(t *testing.T) { + _, ok := ResolveModel(nil, "gpt-5.5-pro-search") + if ok { + t.Fatal("expected unknown known-family model to fail resolve without alias") + } +} + +func TestResolveModelRejectsLegacyDeepSeekIDs(t *testing.T) { + legacyModels := []string{ + "deepseek-chat", + "deepseek-reasoner", + "deepseek-chat-search", + "deepseek-reasoner-search", + "deepseek-expert-chat", + "deepseek-expert-reasoner", + "deepseek-vision-chat", + } + for _, model := range legacyModels { + if got, ok := ResolveModel(nil, model); ok { + t.Fatalf("expected legacy model %q to be rejected, got %q", model, got) + } + } +} + +func TestResolveModelRejectsRetiredHistoricalModels(t *testing.T) { + retiredModels := []string{ + "claude-2.1", + "claude-instant-1.2", + "gpt-3.5-turbo", + } + for _, model := range retiredModels { + if got, ok := ResolveModel(nil, model); ok { + t.Fatalf("expected retired model %q to be rejected, got %q", model, got) + } + } +} + +func TestResolveModelDirectDeepSeekExpert(t *testing.T) { + got, ok := ResolveModel(nil, "deepseek-v4-pro") + if !ok || got != "deepseek-v4-pro" { + t.Fatalf("expected deepseek-v4-pro, got ok=%v model=%q", ok, got) + } +} + +func TestResolveModelCustomAliasToExpert(t *testing.T) { + got, ok := ResolveModel(mockModelAliasReader{ + "my-expert-model": "deepseek-v4-pro-search", + }, "my-expert-model") + if !ok || got != "deepseek-v4-pro-search" { + t.Fatalf("expected alias -> deepseek-v4-pro-search, got ok=%v model=%q", ok, got) + } +} + +func TestResolveModelCustomAliasToVision(t *testing.T) { + got, ok := ResolveModel(mockModelAliasReader{ + "my-vision-model": "deepseek-v4-vision", + }, "my-vision-model") + if !ok || got != "deepseek-v4-vision" { + t.Fatalf("expected alias -> deepseek-v4-vision, got ok=%v model=%q", ok, got) + } +} + +func TestClaudeModelsResponsePaginationFields(t *testing.T) { + resp := ClaudeModelsResponse() + if _, ok := resp["first_id"]; !ok { + t.Fatalf("expected first_id in response: %#v", resp) + } + if _, ok := resp["last_id"]; !ok { + t.Fatalf("expected last_id in response: %#v", resp) + } + if _, ok := resp["has_more"]; !ok { + t.Fatalf("expected has_more in response: %#v", resp) + } +} diff --git a/internal/config/models.go b/internal/config/models.go new file mode 100644 index 0000000000000000000000000000000000000000..a9c22b0e99a3159346a057cf31e4664f3c0798cc --- /dev/null +++ b/internal/config/models.go @@ -0,0 +1,361 @@ +package config + +import ( + "strings" + "time" +) + +type ModelInfo struct { + ID string `json:"id"` + Object string `json:"object"` + Created int64 `json:"created"` + OwnedBy string `json:"owned_by"` + Permission []any `json:"permission,omitempty"` +} +type OllamaModelInfo struct { + Name string `json:"name"` + Model string `json:"model"` + Size int64 `json:"size"` + ModifiedAt string `json:"modified_at"` +} +type OllamaCapabilitiesModelInfo struct { + ID string `json:"id"` + Capabilities []string `json:"capabilities"` +} + +type ModelAliasReader interface { + ModelAliases() map[string]string +} + +const noThinkingModelSuffix = "-nothinking" + +var deepSeekBaseModels = []ModelInfo{ + {ID: "deepseek-v4-flash", Object: "model", Created: 1677610602, OwnedBy: "deepseek", Permission: []any{}}, + {ID: "deepseek-v4-pro", Object: "model", Created: 1677610602, OwnedBy: "deepseek", Permission: []any{}}, + {ID: "deepseek-v4-flash-search", Object: "model", Created: 1677610602, OwnedBy: "deepseek", Permission: []any{}}, + {ID: "deepseek-v4-pro-search", Object: "model", Created: 1677610602, OwnedBy: "deepseek", Permission: []any{}}, + {ID: "deepseek-v4-vision", Object: "model", Created: 1677610602, OwnedBy: "deepseek", Permission: []any{}}, +} + +var OllamaCapabilitiesModels = []OllamaCapabilitiesModelInfo{ + {ID: "deepseek-v4-flash", Capabilities: []string{"tools", "thinking"}}, + {ID: "deepseek-v4-pro", Capabilities: []string{"tools", "thinking"}}, + {ID: "deepseek-v4-flash-search", Capabilities: []string{"tools", "thinking"}}, + {ID: "deepseek-v4-pro-search", Capabilities: []string{"tools", "thinking"}}, + {ID: "deepseek-v4-vision", Capabilities: []string{"tools", "thinking", "vision"}}, + {ID: "deepseek-v4-flash-nothinking", Capabilities: []string{"tools"}}, + {ID: "deepseek-v4-pro-nothinking", Capabilities: []string{"tools"}}, + {ID: "deepseek-v4-flash-search-nothinking", Capabilities: []string{"tools"}}, + {ID: "deepseek-v4-pro-search-nothinking", Capabilities: []string{"tools"}}, + {ID: "deepseek-v4-vision-nothinking", Capabilities: []string{"tools", "vision"}}, +} + +var DeepSeekModels = appendNoThinkingVariants(deepSeekBaseModels) +var OllamaModels = mapToOllamaModels(DeepSeekModels) +var claudeBaseModels = []ModelInfo{ + // Current aliases + {ID: "claude-opus-4-6", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-sonnet-4-6", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-haiku-4-5", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + + // Claude 4.x snapshots and prior aliases kept for compatibility + {ID: "claude-sonnet-4-5", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-opus-4-1", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-opus-4-1-20250805", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-opus-4-0", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-opus-4-20250514", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-sonnet-4-5-20250929", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-sonnet-4-0", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-sonnet-4-20250514", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-haiku-4-5-20251001", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + + // Claude 3.x (legacy/deprecated snapshots and aliases) + {ID: "claude-3-7-sonnet-latest", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-7-sonnet-20250219", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-5-sonnet-latest", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-5-sonnet-20240620", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-5-sonnet-20241022", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-opus-20240229", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-sonnet-20240229", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-5-haiku-latest", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-5-haiku-20241022", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, + {ID: "claude-3-haiku-20240307", Object: "model", Created: 1715635200, OwnedBy: "anthropic"}, +} + +var ClaudeModels = appendNoThinkingVariants(claudeBaseModels) + +func GetModelConfig(model string) (thinking bool, search bool, ok bool) { + baseModel, noThinking := splitNoThinkingModel(model) + if baseModel == "" { + return false, false, false + } + switch baseModel { + case "deepseek-v4-flash", "deepseek-v4-pro", "deepseek-v4-vision": + return !noThinking, false, true + case "deepseek-v4-flash-search", "deepseek-v4-pro-search": + return !noThinking, true, true + default: + return false, false, false + } +} + +func GetModelType(model string) (modelType string, ok bool) { + baseModel, _ := splitNoThinkingModel(model) + switch baseModel { + case "deepseek-v4-flash", "deepseek-v4-flash-search": + return "default", true + case "deepseek-v4-pro", "deepseek-v4-pro-search": + return "expert", true + case "deepseek-v4-vision": + return "vision", true + default: + return "", false + } +} + +func IsSupportedDeepSeekModel(model string) bool { + _, _, ok := GetModelConfig(model) + return ok +} + +func IsNoThinkingModel(model string) bool { + _, noThinking := splitNoThinkingModel(model) + return noThinking +} + +func DefaultModelAliases() map[string]string { + return map[string]string{ + // OpenAI GPT / ChatGPT families + "chatgpt-4o": "deepseek-v4-flash", + "gpt-4": "deepseek-v4-flash", + "gpt-4-turbo": "deepseek-v4-flash", + "gpt-4-turbo-preview": "deepseek-v4-flash", + "gpt-4.5-preview": "deepseek-v4-flash", + "gpt-4o": "deepseek-v4-flash", + "gpt-4o-mini": "deepseek-v4-flash", + "gpt-4.1": "deepseek-v4-flash", + "gpt-4.1-mini": "deepseek-v4-flash", + "gpt-4.1-nano": "deepseek-v4-flash", + "gpt-5": "deepseek-v4-flash", + "gpt-5-chat": "deepseek-v4-flash", + "gpt-5.1": "deepseek-v4-flash", + "gpt-5.1-chat": "deepseek-v4-flash", + "gpt-5.2": "deepseek-v4-flash", + "gpt-5.2-chat": "deepseek-v4-flash", + "gpt-5.3-chat": "deepseek-v4-flash", + "gpt-5.4": "deepseek-v4-flash", + "gpt-5.5": "deepseek-v4-flash", + "gpt-5-mini": "deepseek-v4-flash", + "gpt-5-nano": "deepseek-v4-flash", + "gpt-5.4-mini": "deepseek-v4-flash", + "gpt-5.4-nano": "deepseek-v4-flash", + "gpt-5-pro": "deepseek-v4-pro", + "gpt-5.2-pro": "deepseek-v4-pro", + "gpt-5.4-pro": "deepseek-v4-pro", + "gpt-5.5-pro": "deepseek-v4-pro", + "gpt-5-codex": "deepseek-v4-pro", + "gpt-5.1-codex": "deepseek-v4-pro", + "gpt-5.1-codex-mini": "deepseek-v4-pro", + "gpt-5.1-codex-max": "deepseek-v4-pro", + "gpt-5.2-codex": "deepseek-v4-pro", + "gpt-5.3-codex": "deepseek-v4-pro", + "codex-mini-latest": "deepseek-v4-pro", + + // OpenAI reasoning / research families + "o1": "deepseek-v4-pro", + "o1-preview": "deepseek-v4-pro", + "o1-mini": "deepseek-v4-pro", + "o1-pro": "deepseek-v4-pro", + "o3": "deepseek-v4-pro", + "o3-mini": "deepseek-v4-pro", + "o3-pro": "deepseek-v4-pro", + "o3-deep-research": "deepseek-v4-pro-search", + "o4-mini": "deepseek-v4-pro", + "o4-mini-deep-research": "deepseek-v4-pro-search", + + // Claude current and historical aliases + "claude-opus-4-6": "deepseek-v4-pro", + "claude-opus-4-1": "deepseek-v4-pro", + "claude-opus-4-1-20250805": "deepseek-v4-pro", + "claude-opus-4-0": "deepseek-v4-pro", + "claude-opus-4-20250514": "deepseek-v4-pro", + "claude-sonnet-4-6": "deepseek-v4-flash", + "claude-sonnet-4-5": "deepseek-v4-flash", + "claude-sonnet-4-5-20250929": "deepseek-v4-flash", + "claude-sonnet-4-0": "deepseek-v4-flash", + "claude-sonnet-4-20250514": "deepseek-v4-flash", + "claude-haiku-4-5": "deepseek-v4-flash", + "claude-haiku-4-5-20251001": "deepseek-v4-flash", + "claude-3-7-sonnet": "deepseek-v4-flash", + "claude-3-7-sonnet-latest": "deepseek-v4-flash", + "claude-3-7-sonnet-20250219": "deepseek-v4-flash", + "claude-3-5-sonnet": "deepseek-v4-flash", + "claude-3-5-sonnet-latest": "deepseek-v4-flash", + "claude-3-5-sonnet-20240620": "deepseek-v4-flash", + "claude-3-5-sonnet-20241022": "deepseek-v4-flash", + "claude-3-5-haiku": "deepseek-v4-flash", + "claude-3-5-haiku-latest": "deepseek-v4-flash", + "claude-3-5-haiku-20241022": "deepseek-v4-flash", + "claude-3-opus": "deepseek-v4-pro", + "claude-3-opus-20240229": "deepseek-v4-pro", + "claude-3-sonnet": "deepseek-v4-flash", + "claude-3-sonnet-20240229": "deepseek-v4-flash", + "claude-3-haiku": "deepseek-v4-flash", + "claude-3-haiku-20240307": "deepseek-v4-flash", + + // Gemini current and historical text / multimodal models + "gemini-pro": "deepseek-v4-pro", + "gemini-pro-vision": "deepseek-v4-vision", + "gemini-pro-latest": "deepseek-v4-pro", + "gemini-flash-latest": "deepseek-v4-flash", + "gemini-1.5-pro": "deepseek-v4-pro", + "gemini-1.5-flash": "deepseek-v4-flash", + "gemini-1.5-flash-8b": "deepseek-v4-flash", + "gemini-2.0-flash": "deepseek-v4-flash", + "gemini-2.0-flash-lite": "deepseek-v4-flash", + "gemini-2.5-pro": "deepseek-v4-pro", + "gemini-2.5-flash": "deepseek-v4-flash", + "gemini-2.5-flash-lite": "deepseek-v4-flash", + "gemini-3.1-pro": "deepseek-v4-pro", + "gemini-3-pro": "deepseek-v4-pro", + "gemini-3-flash": "deepseek-v4-flash", + "gemini-3.1-flash": "deepseek-v4-flash", + "gemini-3.1-flash-lite": "deepseek-v4-flash", + + "llama-3.1-70b-instruct": "deepseek-v4-flash", + "qwen-max": "deepseek-v4-flash", + } +} + +func ResolveModel(store ModelAliasReader, requested string) (string, bool) { + model := lower(strings.TrimSpace(requested)) + if model == "" { + return "", false + } + aliases := loadModelAliases(store) + if IsSupportedDeepSeekModel(model) { + return model, true + } + if mapped, ok := aliases[model]; ok && IsSupportedDeepSeekModel(mapped) { + return mapped, true + } + baseModel, noThinking := splitNoThinkingModel(model) + if mapped, ok := aliases[baseModel]; ok && IsSupportedDeepSeekModel(mapped) { + return withNoThinkingVariant(mapped, noThinking), true + } + return "", false +} + +func lower(s string) string { + b := []byte(s) + for i, c := range b { + if c >= 'A' && c <= 'Z' { + b[i] = c + 32 + } + } + return string(b) +} + +func OpenAIModelsResponse() map[string]any { + return map[string]any{"object": "list", "data": DeepSeekModels} +} + +func OpenAIModelByID(store ModelAliasReader, id string) (ModelInfo, bool) { + canonical, ok := ResolveModel(store, id) + if !ok { + return ModelInfo{}, false + } + for _, model := range DeepSeekModels { + if model.ID == canonical { + return model, true + } + } + return ModelInfo{}, false +} + +func OllamaModelsResponse() map[string]any { + return map[string]any{"models": OllamaModels} +} + +func OllamaModelByID(store ModelAliasReader, id string) (OllamaCapabilitiesModelInfo, bool) { + canonical, ok := ResolveModel(store, id) + if !ok { + return OllamaCapabilitiesModelInfo{}, false + } + for _, model := range OllamaCapabilitiesModels { + if model.ID == canonical { + return model, true + } + } + return OllamaCapabilitiesModelInfo{}, false +} + +func ClaudeModelsResponse() map[string]any { + resp := map[string]any{"object": "list", "data": ClaudeModels} + if len(ClaudeModels) > 0 { + resp["first_id"] = ClaudeModels[0].ID + resp["last_id"] = ClaudeModels[len(ClaudeModels)-1].ID + } else { + resp["first_id"] = nil + resp["last_id"] = nil + } + resp["has_more"] = false + return resp +} + +func appendNoThinkingVariants(models []ModelInfo) []ModelInfo { + out := make([]ModelInfo, 0, len(models)*2) + for _, model := range models { + out = append(out, model) + variant := model + variant.ID = withNoThinkingVariant(model.ID, true) + out = append(out, variant) + } + return out +} +func mapToOllamaModels(models []ModelInfo) []OllamaModelInfo { + out := make([]OllamaModelInfo, 0, len(models)) + for _, model := range models { + var modifiedAt string + if model.Created > 0 { + modifiedAt = time.Unix(model.Created, 0).Format(time.RFC3339) + } + ollamaModel := OllamaModelInfo{ + Name: model.ID, + Model: model.ID, + Size: 0, + ModifiedAt: modifiedAt, + } + out = append(out, ollamaModel) + } + return out +} + +func splitNoThinkingModel(model string) (string, bool) { + model = lower(strings.TrimSpace(model)) + if strings.HasSuffix(model, noThinkingModelSuffix) { + return strings.TrimSuffix(model, noThinkingModelSuffix), true + } + return model, false +} + +func withNoThinkingVariant(model string, enabled bool) string { + baseModel, _ := splitNoThinkingModel(model) + if !enabled { + return baseModel + } + if baseModel == "" { + return "" + } + return baseModel + noThinkingModelSuffix +} + +func loadModelAliases(store ModelAliasReader) map[string]string { + aliases := DefaultModelAliases() + if store != nil { + for k, v := range store.ModelAliases() { + aliases[lower(strings.TrimSpace(k))] = lower(strings.TrimSpace(v)) + } + } + return aliases +} diff --git a/internal/config/paths.go b/internal/config/paths.go new file mode 100644 index 0000000000000000000000000000000000000000..60f2829cbc0d6bf0edecc4f492c68fab0be98d47 --- /dev/null +++ b/internal/config/paths.go @@ -0,0 +1,71 @@ +package config + +import ( + "os" + "path/filepath" + "strings" +) + +func BaseDir() string { + cwd, err := os.Getwd() + if err != nil { + return "." + } + return cwd +} + +func IsVercel() bool { + return strings.TrimSpace(os.Getenv("VERCEL")) != "" || strings.TrimSpace(os.Getenv("NOW_REGION")) != "" +} + +func ResolvePath(envKey, defaultRel string) string { + raw := strings.TrimSpace(os.Getenv(envKey)) + if raw != "" { + if filepath.IsAbs(raw) { + return raw + } + return filepath.Join(BaseDir(), raw) + } + return filepath.Join(BaseDir(), defaultRel) +} + +func ConfigPath() string { + if strings.TrimSpace(os.Getenv("DS2API_CONFIG_PATH")) == "" && BaseDir() == "/app" { + return containerDefaultConfigPath() + } + return ResolvePath("DS2API_CONFIG_PATH", "config.json") +} + +func containerDefaultConfigPath() string { + // Container images run as non-root by default. Only use /data when mounted/provisioned. + // Otherwise keep /app/config.json so admin-side save does not fail on MkdirAll("/data"). + if st, err := os.Stat("/data"); err == nil && st.IsDir() { + return "/data/config.json" + } + return "/app/config.json" +} + +func legacyContainerConfigPath() string { + return "/app/config.json" +} + +func shouldTryLegacyContainerConfigPath() bool { + return strings.TrimSpace(os.Getenv("DS2API_CONFIG_PATH")) == "" && BaseDir() == "/app" +} + +func RawStreamSampleRoot() string { + return ResolvePath("DS2API_RAW_STREAM_SAMPLE_ROOT", "tests/raw_stream_samples") +} + +func ChatHistoryPath() string { + // On Vercel, /var/task is read-only at runtime. If no explicit path is set, + // default to /tmp/chat_history.json (the only writable directory). + if IsVercel() && strings.TrimSpace(os.Getenv("DS2API_CHAT_HISTORY_PATH")) == "" { + return "/tmp/chat_history.json" + } + return ResolvePath("DS2API_CHAT_HISTORY_PATH", "data/chat_history.json") +} + +func StaticAdminDir() string { + return ResolvePath("DS2API_STATIC_ADMIN_DIR", "static/admin") +} diff --git a/internal/config/paths_test.go b/internal/config/paths_test.go new file mode 100644 index 0000000000000000000000000000000000000000..00fa51a9ea0d28278d9db6929e42ef572444df06 --- /dev/null +++ b/internal/config/paths_test.go @@ -0,0 +1,28 @@ +package config + +import ( + "os" + "testing" +) + +func TestContainerDefaultConfigPath(t *testing.T) { + t.Run("fallback to /app when /data is missing", func(t *testing.T) { + // This test environment does not guarantee a writable/mounted /data. + // If /data is absent we must keep /app fallback to avoid persistence failures. + if _, err := os.Stat("/data"); err == nil { + t.Skip("/data exists in this environment; cannot validate missing-/data fallback") + } + if got := containerDefaultConfigPath(); got != "/app/config.json" { + t.Fatalf("containerDefaultConfigPath() = %q, want %q", got, "/app/config.json") + } + }) + + t.Run("prefer /data when /data directory exists", func(t *testing.T) { + if _, err := os.Stat("/data"); err != nil { + t.Skip("/data does not exist in this environment") + } + if got := containerDefaultConfigPath(); got != "/data/config.json" { + t.Fatalf("containerDefaultConfigPath() = %q, want %q", got, "/data/config.json") + } + }) +} diff --git a/internal/config/store.go b/internal/config/store.go new file mode 100644 index 0000000000000000000000000000000000000000..b28c143c34bc47680b9a99b8f830173d4d66fe21 --- /dev/null +++ b/internal/config/store.go @@ -0,0 +1,314 @@ +package config + +import ( + "encoding/base64" + "encoding/json" + "errors" + "os" + "slices" + "strings" + "sync" +) + +type Store struct { + mu sync.RWMutex + cfg Config + path string + fromEnv bool + keyMap map[string]struct{} // O(1) API key lookup index + accMap map[string]int // O(1) account lookup: identifier -> slice index + accTest map[string]string // runtime-only account test status cache +} + +func LoadStore() *Store { + store, err := loadStore() + if err != nil { + Logger.Warn("[config] load failed", "error", err) + } + if len(store.cfg.Keys) == 0 && len(store.cfg.Accounts) == 0 { + Logger.Warn("[config] empty config loaded") + } + store.rebuildIndexes() + return store +} + +func LoadStoreWithError() (*Store, error) { + store, err := loadStore() + if err != nil { + return nil, err + } + store.rebuildIndexes() + return store, nil +} + +func loadStore() (*Store, error) { + cfg, fromEnv, err := loadConfig() + cfg.NormalizeCredentials() + if validateErr := ValidateConfig(cfg); validateErr != nil { + err = errors.Join(err, validateErr) + } + return &Store{cfg: cfg, path: ConfigPath(), fromEnv: fromEnv}, err +} + +func loadConfig() (Config, bool, error) { + rawCfg := strings.TrimSpace(os.Getenv("DS2API_CONFIG_JSON")) + path := ConfigPath() + if rawCfg != "" { + cfg, err := parseConfigString(rawCfg) + if err != nil { + if !IsVercel() && envWritebackEnabled() { + if fileCfg, fileErr := loadConfigFromFile(path); fileErr == nil { + return fileCfg, false, nil + } + } + return cfg, true, err + } + cfg.ClearAccountTokens() + cfg.DropInvalidAccounts() + if IsVercel() || !envWritebackEnabled() { + return cfg, true, err + } + content, fileErr := os.ReadFile(path) + if fileErr == nil { + var fileCfg Config + if unmarshalErr := json.Unmarshal(content, &fileCfg); unmarshalErr == nil { + fileCfg.DropInvalidAccounts() + return fileCfg, false, err + } + } + if errors.Is(fileErr, os.ErrNotExist) { + if validateErr := ValidateConfig(cfg); validateErr != nil { + return cfg, true, validateErr + } + if writeErr := writeConfigFile(path, cfg.Clone()); writeErr == nil { + return cfg, false, err + } else { + Logger.Warn("[config] env writeback bootstrap failed", "error", writeErr) + } + } + return cfg, true, err + } + cfg, err := loadConfigFromFile(path) + if err != nil { + if shouldTryLegacyContainerConfigPath() { + legacyPath := legacyContainerConfigPath() + if legacyCfg, legacyErr := loadConfigFromFile(legacyPath); legacyErr == nil { + Logger.Info("[config] loaded legacy container config path", "path", legacyPath) + return legacyCfg, false, nil + } + } + if IsVercel() { + // Vercel may start without writable/present config; keep in-memory bootstrap config. + return Config{}, true, nil + } + if shouldBootstrapMissingConfigFile(err) { + Logger.Warn("[config] config file missing; starting with empty file-backed config", "path", path) + return Config{}, false, nil + } + return Config{}, false, err + } + if IsVercel() { + // Vercel filesystem is ephemeral/read-only for runtime writes; avoid save errors. + return cfg, true, nil + } + return cfg, false, nil +} + +func shouldBootstrapMissingConfigFile(err error) bool { + return errors.Is(err, os.ErrNotExist) && strings.TrimSpace(os.Getenv("DS2API_CONFIG_PATH")) != "" +} + +func loadConfigFromFile(path string) (Config, error) { + content, err := os.ReadFile(path) + if err != nil { + return Config{}, err + } + // Treat empty or whitespace-only files as valid empty config, matching the + // bootstrap behaviour of entrypoint.sh (`echo {} > config.json`). + if len(strings.TrimSpace(string(content))) == 0 { + return Config{}, nil + } + var cfg Config + if err := json.Unmarshal(content, &cfg); err != nil { + return Config{}, err + } + cfg.NormalizeCredentials() + cfg.DropInvalidAccounts() + if strings.Contains(string(content), `"test_status"`) && !IsVercel() { + if b, err := json.MarshalIndent(cfg, "", " "); err == nil { + _ = os.WriteFile(path, b, 0o644) + } + } + return cfg, nil +} + +func (s *Store) Snapshot() Config { + s.mu.RLock() + defer s.mu.RUnlock() + return s.cfg.Clone() +} + +func (s *Store) HasAPIKey(k string) bool { + s.mu.RLock() + defer s.mu.RUnlock() + _, ok := s.keyMap[k] + return ok +} + +func (s *Store) Keys() []string { + s.mu.RLock() + defer s.mu.RUnlock() + return slices.Clone(s.cfg.Keys) +} + +func (s *Store) Accounts() []Account { + s.mu.RLock() + defer s.mu.RUnlock() + return slices.Clone(s.cfg.Accounts) +} + +func (s *Store) FindAccount(identifier string) (Account, bool) { + identifier = strings.TrimSpace(identifier) + s.mu.RLock() + defer s.mu.RUnlock() + if idx, ok := s.findAccountIndexLocked(identifier); ok { + return s.cfg.Accounts[idx], true + } + return Account{}, false +} + +func (s *Store) UpdateAccountTestStatus(identifier, status string) error { + identifier = strings.TrimSpace(identifier) + s.mu.Lock() + defer s.mu.Unlock() + idx, ok := s.findAccountIndexLocked(identifier) + if !ok { + return errors.New("account not found") + } + s.setAccountTestStatusLocked(s.cfg.Accounts[idx], status, identifier) + return nil +} + +func (s *Store) AccountTestStatus(identifier string) (string, bool) { + identifier = strings.TrimSpace(identifier) + if identifier == "" { + return "", false + } + s.mu.RLock() + defer s.mu.RUnlock() + status, ok := s.accTest[identifier] + return status, ok +} + +func (s *Store) UpdateAccountToken(identifier, token string) error { + identifier = strings.TrimSpace(identifier) + s.mu.Lock() + defer s.mu.Unlock() + idx, ok := s.findAccountIndexLocked(identifier) + if !ok { + return errors.New("account not found") + } + oldID := s.cfg.Accounts[idx].Identifier() + s.cfg.Accounts[idx].Token = token + newID := s.cfg.Accounts[idx].Identifier() + // Keep historical aliases usable for long-lived queues while also adding + // the latest identifier after token refresh. + if identifier != "" { + s.accMap[identifier] = idx + } + if oldID != "" { + s.accMap[oldID] = idx + } + if newID != "" { + s.accMap[newID] = idx + } + return s.saveLocked() +} + +func (s *Store) Replace(cfg Config) error { + s.mu.Lock() + defer s.mu.Unlock() + cfg.NormalizeCredentials() + s.cfg = cfg.Clone() + s.rebuildIndexes() + return s.saveLocked() +} + +func (s *Store) Update(mutator func(*Config) error) error { + s.mu.Lock() + defer s.mu.Unlock() + base := s.cfg.Clone() + cfg := base.Clone() + if err := mutator(&cfg); err != nil { + return err + } + cfg.ReconcileCredentials(base) + cfg.NormalizeCredentials() + s.cfg = cfg + s.rebuildIndexes() + return s.saveLocked() +} + +func (s *Store) Save() error { + s.mu.Lock() + defer s.mu.Unlock() + if s.fromEnv && (IsVercel() || !envWritebackEnabled()) { + Logger.Info("[save_config] source from env, skip write") + return nil + } + persistCfg := s.cfg.Clone() + persistCfg.ClearAccountTokens() + b, err := json.MarshalIndent(persistCfg, "", " ") + if err != nil { + return err + } + if err := writeConfigBytes(s.path, b); err != nil { + return err + } + s.fromEnv = false + return nil +} + +func (s *Store) saveLocked() error { + if s.fromEnv && (IsVercel() || !envWritebackEnabled()) { + Logger.Info("[save_config] source from env, skip write") + return nil + } + persistCfg := s.cfg.Clone() + persistCfg.ClearAccountTokens() + b, err := json.MarshalIndent(persistCfg, "", " ") + if err != nil { + return err + } + if err := writeConfigBytes(s.path, b); err != nil { + return err + } + s.fromEnv = false + return nil +} + +func (s *Store) IsEnvBacked() bool { + s.mu.RLock() + defer s.mu.RUnlock() + return s.fromEnv +} + +func (s *Store) SetVercelSync(hash string, ts int64) error { + return s.Update(func(c *Config) error { + c.VercelSyncHash = hash + c.VercelSyncTime = ts + return nil + }) +} + +func (s *Store) ExportJSONAndBase64() (string, string, error) { + s.mu.RLock() + defer s.mu.RUnlock() + exportCfg := s.cfg.Clone() + exportCfg.ClearAccountTokens() + b, err := json.Marshal(exportCfg) + if err != nil { + return "", "", err + } + return string(b), base64.StdEncoding.EncodeToString(b), nil +} diff --git a/internal/config/store_accessors.go b/internal/config/store_accessors.go new file mode 100644 index 0000000000000000000000000000000000000000..61f509936ea63be873f9093befa7d9173edd9c21 --- /dev/null +++ b/internal/config/store_accessors.go @@ -0,0 +1,176 @@ +package config + +import ( + "os" + "strconv" + "strings" +) + +func (s *Store) ModelAliases() map[string]string { + s.mu.RLock() + defer s.mu.RUnlock() + out := DefaultModelAliases() + for k, v := range s.cfg.ModelAliases { + key := strings.TrimSpace(lower(k)) + val := strings.TrimSpace(lower(v)) + if key == "" || val == "" { + continue + } + out[key] = val + } + return out +} + +func (s *Store) ToolcallMode() string { + return "feature_match" +} + +func (s *Store) ToolcallEarlyEmitConfidence() string { + return "high" +} + +func (s *Store) ResponsesStoreTTLSeconds() int { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.Responses.StoreTTLSeconds > 0 { + return s.cfg.Responses.StoreTTLSeconds + } + return 900 +} + +func (s *Store) EmbeddingsProvider() string { + s.mu.RLock() + defer s.mu.RUnlock() + return strings.TrimSpace(s.cfg.Embeddings.Provider) +} + +func (s *Store) AutoDeleteMode() string { + s.mu.RLock() + defer s.mu.RUnlock() + mode := strings.ToLower(strings.TrimSpace(s.cfg.AutoDelete.Mode)) + switch mode { + case "none", "single", "all": + return mode + } + if s.cfg.AutoDelete.Sessions { + return "all" + } + return "none" +} + +func (s *Store) AdminPasswordHash() string { + s.mu.RLock() + defer s.mu.RUnlock() + return strings.TrimSpace(s.cfg.Admin.PasswordHash) +} + +func (s *Store) AdminJWTExpireHours() int { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.Admin.JWTExpireHours > 0 { + return s.cfg.Admin.JWTExpireHours + } + if raw := strings.TrimSpace(os.Getenv("DS2API_JWT_EXPIRE_HOURS")); raw != "" { + if n, err := strconv.Atoi(raw); err == nil && n > 0 { + return n + } + } + return 24 +} + +func (s *Store) AdminJWTValidAfterUnix() int64 { + s.mu.RLock() + defer s.mu.RUnlock() + return s.cfg.Admin.JWTValidAfterUnix +} + +func (s *Store) RuntimeAccountMaxInflight() int { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.Runtime.AccountMaxInflight > 0 { + return s.cfg.Runtime.AccountMaxInflight + } + if raw := strings.TrimSpace(os.Getenv("DS2API_ACCOUNT_MAX_INFLIGHT")); raw != "" { + if n, err := strconv.Atoi(raw); err == nil && n > 0 { + return n + } + } + return 2 +} + +func (s *Store) RuntimeAccountMaxQueue(defaultSize int) int { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.Runtime.AccountMaxQueue > 0 { + return s.cfg.Runtime.AccountMaxQueue + } + if raw := strings.TrimSpace(os.Getenv("DS2API_ACCOUNT_MAX_QUEUE")); raw != "" { + if n, err := strconv.Atoi(raw); err == nil && n >= 0 { + return n + } + } + if defaultSize < 0 { + return 0 + } + return defaultSize +} + +func (s *Store) RuntimeGlobalMaxInflight(defaultSize int) int { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.Runtime.GlobalMaxInflight > 0 { + return s.cfg.Runtime.GlobalMaxInflight + } + if raw := strings.TrimSpace(os.Getenv("DS2API_GLOBAL_MAX_INFLIGHT")); raw != "" { + if n, err := strconv.Atoi(raw); err == nil && n > 0 { + return n + } + } + if defaultSize < 0 { + return 0 + } + return defaultSize +} + +func (s *Store) RuntimeTokenRefreshIntervalHours() int { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.Runtime.TokenRefreshIntervalHours > 0 { + return s.cfg.Runtime.TokenRefreshIntervalHours + } + return 6 +} + +func (s *Store) AutoDeleteSessions() bool { + return s.AutoDeleteMode() != "none" +} + +func (s *Store) CurrentInputFileEnabled() bool { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.CurrentInputFile.Enabled == nil { + return true + } + return *s.cfg.CurrentInputFile.Enabled +} + +func (s *Store) CurrentInputFileMinChars() int { + s.mu.RLock() + defer s.mu.RUnlock() + return s.cfg.CurrentInputFile.MinChars +} + +func (s *Store) ThinkingInjectionEnabled() bool { + s.mu.RLock() + defer s.mu.RUnlock() + if s.cfg.ThinkingInjection.Enabled == nil { + return false + } + return *s.cfg.ThinkingInjection.Enabled +} + +func (s *Store) ThinkingInjectionPrompt() string { + s.mu.RLock() + defer s.mu.RUnlock() + return strings.TrimSpace(s.cfg.ThinkingInjection.Prompt) +} diff --git a/internal/config/store_accessors_test.go b/internal/config/store_accessors_test.go new file mode 100644 index 0000000000000000000000000000000000000000..7667d6127d8064d3dca05a0dd110af682bf18503 --- /dev/null +++ b/internal/config/store_accessors_test.go @@ -0,0 +1,46 @@ +package config + +import "testing" + +func TestStoreCurrentInputFileAccessors(t *testing.T) { + store := &Store{cfg: Config{}} + if !store.CurrentInputFileEnabled() { + t.Fatal("expected current input file enabled by default") + } + if got := store.CurrentInputFileMinChars(); got != 0 { + t.Fatalf("default current input file min_chars=%d want=0", got) + } + + enabled := false + store.cfg.CurrentInputFile = CurrentInputFileConfig{Enabled: &enabled, MinChars: 12345} + if store.CurrentInputFileEnabled() { + t.Fatal("expected current input file disabled") + } + + enabled = true + store.cfg.CurrentInputFile.Enabled = &enabled + if !store.CurrentInputFileEnabled() { + t.Fatal("expected current input file enabled") + } + if got := store.CurrentInputFileMinChars(); got != 12345 { + t.Fatalf("current input file min_chars=%d want=12345", got) + } +} + +func TestStoreThinkingInjectionAccessors(t *testing.T) { + store := &Store{cfg: Config{}} + if !store.ThinkingInjectionEnabled() { + t.Fatal("expected thinking injection enabled by default") + } + + disabled := false + store.cfg.ThinkingInjection.Enabled = &disabled + if store.ThinkingInjectionEnabled() { + t.Fatal("expected thinking injection disabled by explicit config") + } + + store.cfg.ThinkingInjection.Prompt = " custom thinking prompt " + if got := store.ThinkingInjectionPrompt(); got != "custom thinking prompt" { + t.Fatalf("thinking injection prompt=%q want custom thinking prompt", got) + } +} diff --git a/internal/config/store_env_writeback.go b/internal/config/store_env_writeback.go new file mode 100644 index 0000000000000000000000000000000000000000..1872317296741543a5ed21c9ba6dc6221d5d9190 --- /dev/null +++ b/internal/config/store_env_writeback.go @@ -0,0 +1,48 @@ +package config + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "strings" +) + +func envWritebackEnabled() bool { + v := strings.ToLower(strings.TrimSpace(os.Getenv("DS2API_ENV_WRITEBACK"))) + return v == "1" || v == "true" || v == "yes" || v == "on" +} + +func (s *Store) IsEnvWritebackEnabled() bool { + return envWritebackEnabled() +} + +func (s *Store) HasEnvConfigSource() bool { + rawCfg := strings.TrimSpace(os.Getenv("DS2API_CONFIG_JSON")) + return rawCfg != "" +} + +func (s *Store) ConfigPath() string { + return s.path +} + +func writeConfigFile(path string, cfg Config) error { + persistCfg := cfg.Clone() + persistCfg.ClearAccountTokens() + b, err := json.MarshalIndent(persistCfg, "", " ") + if err != nil { + return err + } + return writeConfigBytes(path, b) +} + +func writeConfigBytes(path string, b []byte) error { + dir := filepath.Dir(path) + if dir == "." || dir == "" { + return os.WriteFile(path, b, 0o644) + } + if err := os.MkdirAll(dir, 0o755); err != nil { + return fmt.Errorf("mkdir config dir: %w", err) + } + return os.WriteFile(path, b, 0o644) +} diff --git a/internal/config/store_index.go b/internal/config/store_index.go new file mode 100644 index 0000000000000000000000000000000000000000..a0e663849f468ee16fb6c8f9565b6a2676116bcf --- /dev/null +++ b/internal/config/store_index.go @@ -0,0 +1,55 @@ +package config + +// rebuildIndexes must be called with the lock already held (or during init). +func (s *Store) rebuildIndexes() { + prevStatus := s.accTest + s.keyMap = make(map[string]struct{}, len(s.cfg.Keys)) + for _, k := range s.cfg.Keys { + s.keyMap[k] = struct{}{} + } + s.accMap = make(map[string]int, len(s.cfg.Accounts)) + s.accTest = make(map[string]string, len(s.cfg.Accounts)) + for i, acc := range s.cfg.Accounts { + id := acc.Identifier() + if id != "" { + s.accMap[id] = i + if status, ok := prevStatus[id]; ok { + s.setAccountTestStatusLocked(acc, status, "") + } + } + } +} + +// findAccountIndexLocked expects the store lock to already be held. +func (s *Store) findAccountIndexLocked(identifier string) (int, bool) { + if idx, ok := s.accMap[identifier]; ok && idx >= 0 && idx < len(s.cfg.Accounts) { + return idx, true + } + // Fallback for token-only accounts whose derived identifier changed after + // a token refresh; this preserves correctness on map misses. + for i, acc := range s.cfg.Accounts { + if acc.Identifier() == identifier { + return i, true + } + } + return -1, false +} + +func (s *Store) setAccountTestStatusLocked(acc Account, status, hintedIdentifier string) { + status = lower(status) + if status == "" { + return + } + if id := acc.Identifier(); id != "" { + s.accTest[id] = status + } + if email := acc.Email; email != "" { + s.accTest[email] = status + } + if mobile := CanonicalMobileKey(acc.Mobile); mobile != "" { + s.accTest[mobile] = status + } + if hintedIdentifier = lower(hintedIdentifier); hintedIdentifier != "" { + s.accTest[hintedIdentifier] = status + } +} diff --git a/internal/config/validation.go b/internal/config/validation.go new file mode 100644 index 0000000000000000000000000000000000000000..0ae41d38e53f988c3465459578580d5a5a1aafb5 --- /dev/null +++ b/internal/config/validation.go @@ -0,0 +1,153 @@ +package config + +import ( + "fmt" + "strings" +) + +func ValidateConfig(c Config) error { + if err := ValidateProxyConfig(c.Proxies); err != nil { + return err + } + if err := ValidateAdminConfig(c.Admin); err != nil { + return err + } + if err := ValidateRuntimeConfig(c.Runtime); err != nil { + return err + } + if err := ValidateResponsesConfig(c.Responses); err != nil { + return err + } + if err := ValidateEmbeddingsConfig(c.Embeddings); err != nil { + return err + } + if err := ValidateAutoDeleteConfig(c.AutoDelete); err != nil { + return err + } + if err := ValidateCurrentInputFileConfig(c.CurrentInputFile); err != nil { + return err + } + if err := ValidateAccountProxyReferences(c.Accounts, c.Proxies); err != nil { + return err + } + return nil +} + +func ValidateProxyConfig(proxies []Proxy) error { + seen := make(map[string]struct{}, len(proxies)) + for _, proxy := range proxies { + proxy = NormalizeProxy(proxy) + if err := ValidateTrimmedString("proxies.id", proxy.ID, true); err != nil { + return err + } + switch proxy.Type { + case "socks5", "socks5h": + default: + return fmt.Errorf("proxies.type must be one of socks5, socks5h") + } + if err := ValidateTrimmedString("proxies.host", proxy.Host, true); err != nil { + return err + } + if err := ValidateIntRange("proxies.port", proxy.Port, 1, 65535, true); err != nil { + return err + } + if _, ok := seen[proxy.ID]; ok { + return fmt.Errorf("duplicate proxy id: %s", proxy.ID) + } + seen[proxy.ID] = struct{}{} + } + return nil +} + +func ValidateAccountProxyReferences(accounts []Account, proxies []Proxy) error { + if len(accounts) == 0 { + return nil + } + ids := make(map[string]struct{}, len(proxies)) + for _, proxy := range proxies { + ids[NormalizeProxy(proxy).ID] = struct{}{} + } + for _, acc := range accounts { + proxyID := strings.TrimSpace(acc.ProxyID) + if proxyID == "" { + continue + } + if _, ok := ids[proxyID]; !ok { + return fmt.Errorf("account proxy_id references unknown proxy: %s", proxyID) + } + } + return nil +} + +func ValidateAdminConfig(admin AdminConfig) error { + return ValidateIntRange("admin.jwt_expire_hours", admin.JWTExpireHours, 1, 720, false) +} + +func ValidateRuntimeConfig(runtime RuntimeConfig) error { + if err := ValidateIntRange("runtime.account_max_inflight", runtime.AccountMaxInflight, 1, 256, false); err != nil { + return err + } + if err := ValidateIntRange("runtime.account_max_queue", runtime.AccountMaxQueue, 1, 200000, false); err != nil { + return err + } + if err := ValidateIntRange("runtime.global_max_inflight", runtime.GlobalMaxInflight, 1, 200000, false); err != nil { + return err + } + if err := ValidateIntRange("runtime.token_refresh_interval_hours", runtime.TokenRefreshIntervalHours, 1, 720, false); err != nil { + return err + } + if runtime.AccountMaxInflight > 0 && runtime.GlobalMaxInflight > 0 && runtime.GlobalMaxInflight < runtime.AccountMaxInflight { + return fmt.Errorf("runtime.global_max_inflight must be >= runtime.account_max_inflight") + } + return nil +} + +func ValidateResponsesConfig(responses ResponsesConfig) error { + return ValidateIntRange("responses.store_ttl_seconds", responses.StoreTTLSeconds, 30, 86400, false) +} + +func ValidateEmbeddingsConfig(embeddings EmbeddingsConfig) error { + return ValidateTrimmedString("embeddings.provider", embeddings.Provider, false) +} + +func ValidateAutoDeleteConfig(autoDelete AutoDeleteConfig) error { + return ValidateAutoDeleteMode(autoDelete.Mode) +} + +func ValidateCurrentInputFileConfig(currentInputFile CurrentInputFileConfig) error { + if currentInputFile.MinChars != 0 { + return ValidateIntRange("current_input_file.min_chars", currentInputFile.MinChars, 1, 100000000, true) + } + return nil +} + +func ValidateIntRange(name string, value, min, max int, required bool) error { + if value == 0 && !required { + return nil + } + if value < min || value > max { + return fmt.Errorf("%s must be between %d and %d", name, min, max) + } + return nil +} + +func ValidateTrimmedString(name, value string, required bool) error { + trimmed := strings.TrimSpace(value) + if trimmed == "" { + if !required && value == "" { + return nil + } + return fmt.Errorf("%s cannot be empty", name) + } + return nil +} + +func ValidateAutoDeleteMode(mode string) error { + mode = strings.ToLower(strings.TrimSpace(mode)) + switch mode { + case "", "none", "single", "all": + return nil + default: + return fmt.Errorf("auto_delete.mode must be one of none, single, all") + } +} diff --git a/internal/config/validation_test.go b/internal/config/validation_test.go new file mode 100644 index 0000000000000000000000000000000000000000..46546b09f2710f02c1f5c630baba998f1c502e32 --- /dev/null +++ b/internal/config/validation_test.go @@ -0,0 +1,66 @@ +package config + +import ( + "strings" + "testing" +) + +func TestValidateConfigRejectsInvalidValues(t *testing.T) { + tests := []struct { + name string + cfg Config + want string + }{ + { + name: "admin", + cfg: Config{Admin: AdminConfig{JWTExpireHours: 721}}, + want: "admin.jwt_expire_hours", + }, + { + name: "runtime relation", + cfg: Config{Runtime: RuntimeConfig{ + AccountMaxInflight: 8, + GlobalMaxInflight: 4, + }}, + want: "runtime.global_max_inflight must be >= runtime.account_max_inflight", + }, + { + name: "responses", + cfg: Config{Responses: ResponsesConfig{StoreTTLSeconds: 10}}, + want: "responses.store_ttl_seconds", + }, + { + name: "embeddings", + cfg: Config{Embeddings: EmbeddingsConfig{Provider: " "}}, + want: "embeddings.provider", + }, + { + name: "auto delete", + cfg: Config{AutoDelete: AutoDeleteConfig{Mode: "maybe"}}, + want: "auto_delete.mode", + }, + { + name: "current input file", + cfg: Config{CurrentInputFile: CurrentInputFileConfig{MinChars: -1}}, + want: "current_input_file.min_chars", + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + err := ValidateConfig(tc.cfg) + if err == nil { + t.Fatal("expected validation error") + } + if !strings.Contains(err.Error(), tc.want) { + t.Fatalf("expected %q in error, got %v", tc.want, err) + } + }) + } +} + +func TestValidateConfigAcceptsLegacyAutoDeleteSessions(t *testing.T) { + if err := ValidateConfig(Config{AutoDelete: AutoDeleteConfig{Sessions: true}}); err != nil { + t.Fatalf("expected legacy auto_delete.sessions config to remain valid, got %v", err) + } +} diff --git a/internal/deepseek/client/client_auth.go b/internal/deepseek/client/client_auth.go new file mode 100644 index 0000000000000000000000000000000000000000..e64c9536dd1da49eb6f971ae4fee28bc725e7ee8 --- /dev/null +++ b/internal/deepseek/client/client_auth.go @@ -0,0 +1,295 @@ +package client + +import ( + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "errors" + "fmt" + "net/http" + "strings" + "unicode" + + "ds2api/internal/auth" + "ds2api/internal/config" +) + +func (c *Client) Login(ctx context.Context, acc config.Account) (string, error) { + clients := c.requestClientsForAccount(acc) + payload := map[string]any{ + "password": strings.TrimSpace(acc.Password), + "device_id": "android_device", + "os": "android", + } + if email := strings.TrimSpace(acc.Email); email != "" { + payload["email"] = email + } else if mobile := strings.TrimSpace(acc.Mobile); mobile != "" { + loginMobile, areaCode := normalizeMobileForLogin(mobile) + payload["mobile"] = loginMobile + payload["area_code"] = areaCode + } else { + return "", errors.New("missing email/mobile") + } + resp, err := c.postJSON(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekLoginURL, dsprotocol.BaseHeaders, payload) + if err != nil { + return "", err + } + code := intFrom(resp["code"]) + if code != 0 { + return "", fmt.Errorf("login failed: %v", resp["msg"]) + } + data, _ := resp["data"].(map[string]any) + if intFrom(data["biz_code"]) != 0 { + return "", fmt.Errorf("login failed: %v", data["biz_msg"]) + } + bizData, _ := data["biz_data"].(map[string]any) + user, _ := bizData["user"].(map[string]any) + token, _ := user["token"].(string) + if strings.TrimSpace(token) == "" { + return "", errors.New("missing login token") + } + return token, nil +} + +func (c *Client) CreateSession(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) { + if maxAttempts <= 0 { + maxAttempts = c.maxRetries + } + clients := c.requestClientsForAuth(ctx, a) + attempts := 0 + refreshed := false + for attempts < maxAttempts { + headers := c.authHeaders(a.DeepSeekToken) + resp, status, err := c.postJSONWithStatus(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekCreateSessionURL, headers, map[string]any{"agent": "chat"}) + if err != nil { + config.Logger.Warn("[create_session] request error", "error", err, "account", a.AccountID) + attempts++ + continue + } + code, bizCode, msg, bizMsg := extractResponseStatus(resp) + if status == http.StatusOK && code == 0 && bizCode == 0 { + sessionID := extractCreateSessionID(resp) + if sessionID != "" { + return sessionID, nil + } + } + config.Logger.Warn("[create_session] failed", "status", status, "code", code, "biz_code", bizCode, "msg", msg, "biz_msg", bizMsg, "use_config_token", a.UseConfigToken, "account", a.AccountID) + if a.UseConfigToken { + if !refreshed && shouldAttemptRefresh(status, code, bizCode, msg, bizMsg) { + if c.Auth.RefreshToken(ctx, a) { + refreshed = true + continue + } + } + if c.Auth.SwitchAccount(ctx, a) { + refreshed = false + attempts++ + continue + } + } + attempts++ + } + return "", errors.New("create session failed") +} + +func (c *Client) GetPow(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) { + return c.GetPowForTarget(ctx, a, dsprotocol.DeepSeekCompletionTargetPath, maxAttempts) +} + +func (c *Client) GetPowForTarget(ctx context.Context, a *auth.RequestAuth, targetPath string, maxAttempts int) (string, error) { + if maxAttempts <= 0 { + maxAttempts = c.maxRetries + } + targetPath = strings.TrimSpace(targetPath) + if targetPath == "" { + targetPath = dsprotocol.DeepSeekCompletionTargetPath + } + clients := c.requestClientsForAuth(ctx, a) + attempts := 0 + refreshed := false + lastFailureKind := FailureUnknown + lastFailureMessage := "" + for attempts < maxAttempts { + headers := c.authHeaders(a.DeepSeekToken) + resp, status, err := c.postJSONWithStatus(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekCreatePowURL, headers, map[string]any{"target_path": targetPath}) + if err != nil { + config.Logger.Warn("[get_pow] request error", "error", err, "account", a.AccountID, "target_path", targetPath) + lastFailureKind = FailureUnknown + lastFailureMessage = err.Error() + attempts++ + continue + } + code, bizCode, msg, bizMsg := extractResponseStatus(resp) + if status == http.StatusOK && code == 0 && bizCode == 0 { + data, _ := resp["data"].(map[string]any) + bizData, _ := data["biz_data"].(map[string]any) + challenge, _ := bizData["challenge"].(map[string]any) + answer, err := ComputePow(ctx, challenge) + if err != nil { + attempts++ + continue + } + return BuildPowHeader(challenge, answer) + } + config.Logger.Warn("[get_pow] failed", "status", status, "code", code, "biz_code", bizCode, "msg", msg, "biz_msg", bizMsg, "use_config_token", a.UseConfigToken, "account", a.AccountID, "target_path", targetPath) + lastFailureMessage = failureMessage(msg, bizMsg, "get pow failed") + if isTokenInvalid(status, code, bizCode, msg, bizMsg) || isAuthIndicativeBizFailure(msg, bizMsg) { + lastFailureKind = authFailureKind(a.UseConfigToken) + } else { + lastFailureKind = FailureUnknown + } + if a.UseConfigToken { + if !refreshed && shouldAttemptRefresh(status, code, bizCode, msg, bizMsg) { + if c.Auth.RefreshToken(ctx, a) { + refreshed = true + continue + } + } + if c.Auth.SwitchAccount(ctx, a) { + refreshed = false + attempts++ + continue + } + } + attempts++ + } + if lastFailureKind != FailureUnknown { + return "", &RequestFailure{Op: "get pow", Kind: lastFailureKind, Message: lastFailureMessage} + } + return "", errors.New("get pow failed") +} + +func (c *Client) authHeaders(token string) map[string]string { + headers := make(map[string]string, len(dsprotocol.BaseHeaders)+1) + for k, v := range dsprotocol.BaseHeaders { + headers[k] = v + } + headers["authorization"] = "Bearer " + token + return headers +} + +func isTokenInvalid(status int, code int, bizCode int, msg string, bizMsg string) bool { + msg = strings.ToLower(strings.TrimSpace(msg) + " " + strings.TrimSpace(bizMsg)) + if status == http.StatusUnauthorized || status == http.StatusForbidden { + return true + } + if code == 40001 || code == 40002 || code == 40003 || bizCode == 40001 || bizCode == 40002 || bizCode == 40003 { + return true + } + return strings.Contains(msg, "token") || + strings.Contains(msg, "unauthorized") || + strings.Contains(msg, "expired") || + strings.Contains(msg, "not login") || + strings.Contains(msg, "login required") || + strings.Contains(msg, "invalid jwt") +} + +func shouldAttemptRefresh(status int, code int, bizCode int, msg string, bizMsg string) bool { + if isTokenInvalid(status, code, bizCode, msg, bizMsg) { + return true + } + // Some DeepSeek failures come back as HTTP 200/code=0 but with non-zero biz_code. + // Only attempt refresh when these biz failures still look auth-related. + return status == http.StatusOK && + code == 0 && + bizCode != 0 && + isAuthIndicativeBizFailure(msg, bizMsg) +} + +func isAuthIndicativeBizFailure(msg string, bizMsg string) bool { + combined := strings.ToLower(strings.TrimSpace(msg) + " " + strings.TrimSpace(bizMsg)) + authKeywords := []string{ + "auth", + "authorization", + "credential", + "expired", + "invalid jwt", + "jwt", + "login", + "not login", + "session expired", + "token", + "unauthorized", + "登录", + "未登录", + "认证", + "凭证", + "会话过期", + "令牌", + } + for _, keyword := range authKeywords { + if strings.Contains(combined, keyword) { + return true + } + } + return false +} + +func authFailureKind(useConfigToken bool) FailureKind { + if useConfigToken { + return FailureManagedUnauthorized + } + return FailureDirectUnauthorized +} + +func failureMessage(msg string, bizMsg string, fallback string) string { + if trimmed := strings.TrimSpace(bizMsg); trimmed != "" { + return trimmed + } + if trimmed := strings.TrimSpace(msg); trimmed != "" { + return trimmed + } + return strings.TrimSpace(fallback) +} + +// DeepSeek has returned create-session ids in both biz_data.id and +// biz_data.chat_session.id across observed response variants; accept either. +func extractCreateSessionID(resp map[string]any) string { + data, _ := resp["data"].(map[string]any) + bizData, _ := data["biz_data"].(map[string]any) + if sessionID, _ := bizData["id"].(string); strings.TrimSpace(sessionID) != "" { + return strings.TrimSpace(sessionID) + } + if chatSession, ok := bizData["chat_session"].(map[string]any); ok { + if sessionID, _ := chatSession["id"].(string); strings.TrimSpace(sessionID) != "" { + return strings.TrimSpace(sessionID) + } + } + return "" +} + +func extractResponseStatus(resp map[string]any) (code int, bizCode int, msg string, bizMsg string) { + code = intFrom(resp["code"]) + msg, _ = resp["msg"].(string) + data, _ := resp["data"].(map[string]any) + bizCode = intFrom(data["biz_code"]) + bizMsg, _ = data["biz_msg"].(string) + if strings.TrimSpace(bizMsg) == "" { + if bizData, ok := data["biz_data"].(map[string]any); ok { + bizMsg, _ = bizData["msg"].(string) + } + } + return code, bizCode, msg, bizMsg +} + +func normalizeMobileForLogin(raw string) (mobile string, areaCode any) { + s := strings.TrimSpace(raw) + if s == "" { + return "", nil + } + hasPlus := strings.HasPrefix(s, "+") + var b strings.Builder + b.Grow(len(s)) + for _, r := range s { + if unicode.IsDigit(r) { + b.WriteRune(r) + } + } + digits := b.String() + if digits == "" { + return "", nil + } + if (hasPlus || strings.HasPrefix(digits, "86")) && strings.HasPrefix(digits, "86") && len(digits) == 13 { + return digits[2:], nil + } + return digits, nil +} diff --git a/internal/deepseek/client/client_auth_mobile_test.go b/internal/deepseek/client/client_auth_mobile_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e676b4effc5e5282c6fa7f23ab4f243cf2b479ab --- /dev/null +++ b/internal/deepseek/client/client_auth_mobile_test.go @@ -0,0 +1,33 @@ +package client + +import "testing" + +func TestNormalizeMobileForLogin_ChinaWithPlus86(t *testing.T) { + mobile, areaCode := normalizeMobileForLogin("+8613800138000") + if mobile != "13800138000" { + t.Fatalf("unexpected mobile: %q", mobile) + } + if areaCode != nil { + t.Fatalf("expected nil areaCode, got %#v", areaCode) + } +} + +func TestNormalizeMobileForLogin_ChinaWith86Prefix(t *testing.T) { + mobile, areaCode := normalizeMobileForLogin("8613800138000") + if mobile != "13800138000" { + t.Fatalf("unexpected mobile: %q", mobile) + } + if areaCode != nil { + t.Fatalf("expected nil areaCode, got %#v", areaCode) + } +} + +func TestNormalizeMobileForLogin_KeepPlainDigits(t *testing.T) { + mobile, areaCode := normalizeMobileForLogin("13800138000") + if mobile != "13800138000" { + t.Fatalf("unexpected mobile: %q", mobile) + } + if areaCode != nil { + t.Fatalf("expected nil areaCode, got %#v", areaCode) + } +} diff --git a/internal/deepseek/client/client_auth_refresh_test.go b/internal/deepseek/client/client_auth_refresh_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2cc1ff123202e12b800355d9e38e280da423a975 --- /dev/null +++ b/internal/deepseek/client/client_auth_refresh_test.go @@ -0,0 +1,27 @@ +package client + +import "testing" + +func TestShouldAttemptRefreshOnTokenInvalidSignal(t *testing.T) { + if !shouldAttemptRefresh(401, 0, 0, "unauthorized", "") { + t.Fatal("expected refresh when response indicates invalid token") + } +} + +func TestShouldAttemptRefreshOnAuthIndicativeBizCodeFailure(t *testing.T) { + if !shouldAttemptRefresh(200, 0, 400123, "", "login expired, token invalid") { + t.Fatal("expected refresh on auth-indicative biz_code failure") + } +} + +func TestShouldAttemptRefreshFalseOnNonAuthBizCodeFailure(t *testing.T) { + if shouldAttemptRefresh(200, 0, 400123, "", "session create failed: quota reached") { + t.Fatal("did not expect refresh on non-auth biz_code failure") + } +} + +func TestShouldAttemptRefreshFalseOnGenericServerError(t *testing.T) { + if shouldAttemptRefresh(500, 500, 0, "internal error", "") { + t.Fatal("did not expect refresh on generic server error") + } +} diff --git a/internal/deepseek/client/client_auth_test.go b/internal/deepseek/client/client_auth_test.go new file mode 100644 index 0000000000000000000000000000000000000000..6e238776a2e4d8ea102ac4cf4013b68a6fe2a52d --- /dev/null +++ b/internal/deepseek/client/client_auth_test.go @@ -0,0 +1,34 @@ +package client + +import "testing" + +func TestExtractCreateSessionIDSupportsLegacyShape(t *testing.T) { + resp := map[string]any{ + "data": map[string]any{ + "biz_data": map[string]any{ + "id": "legacy-session-id", + }, + }, + } + + if got := extractCreateSessionID(resp); got != "legacy-session-id" { + t.Fatalf("expected legacy session id, got %q", got) + } +} + +func TestExtractCreateSessionIDSupportsNestedChatSessionShape(t *testing.T) { + resp := map[string]any{ + "data": map[string]any{ + "biz_data": map[string]any{ + "chat_session": map[string]any{ + "id": "nested-session-id", + "model_type": "default", + }, + }, + }, + } + + if got := extractCreateSessionID(resp); got != "nested-session-id" { + t.Fatalf("expected nested session id, got %q", got) + } +} diff --git a/internal/deepseek/client/client_completion.go b/internal/deepseek/client/client_completion.go new file mode 100644 index 0000000000000000000000000000000000000000..0563d3349fa2b9157d069bfb450459f613e5e903 --- /dev/null +++ b/internal/deepseek/client/client_completion.go @@ -0,0 +1,72 @@ +package client + +import ( + "bytes" + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "encoding/json" + "net/http" + + "ds2api/internal/auth" + "ds2api/internal/config" + trans "ds2api/internal/deepseek/transport" +) + +func (c *Client) CallCompletion(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, maxAttempts int) (*http.Response, error) { + _ = maxAttempts + clients := c.requestClientsForAuth(ctx, a) + headers := c.authHeaders(a.DeepSeekToken) + headers["x-ds-pow-response"] = powResp + captureSession := c.capture.Start("deepseek_completion", dsprotocol.DeepSeekCompletionURL, a.AccountID, payload) + resp, err := c.streamPostOnce(ctx, clients.stream, dsprotocol.DeepSeekCompletionURL, headers, payload) + if err != nil { + return nil, err + } + if captureSession != nil { + resp.Body = captureSession.WrapBody(resp.Body, resp.StatusCode) + } + if resp.StatusCode == http.StatusOK { + resp = c.wrapCompletionWithAutoContinue(ctx, a, payload, powResp, resp) + } + return resp, nil +} + +func (c *Client) streamPost(ctx context.Context, doer trans.Doer, url string, headers map[string]string, payload any) (*http.Response, error) { + return c.streamPostWithFallback(ctx, doer, url, headers, payload, true) +} + +func (c *Client) streamPostOnce(ctx context.Context, doer trans.Doer, url string, headers map[string]string, payload any) (*http.Response, error) { + return c.streamPostWithFallback(ctx, doer, url, headers, payload, false) +} + +func (c *Client) streamPostWithFallback(ctx context.Context, doer trans.Doer, url string, headers map[string]string, payload any, allowFallback bool) (*http.Response, error) { + b, err := json.Marshal(payload) + if err != nil { + return nil, err + } + headers = c.jsonHeaders(headers) + clients := c.requestClientsFromContext(ctx) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b)) + if err != nil { + return nil, err + } + for k, v := range headers { + req.Header.Set(k, v) + } + resp, err := doer.Do(req) + if err != nil { + if allowFallback { + config.Logger.Warn("[deepseek] fingerprint stream request failed, fallback to std transport", "url", url, "error", err) + req2, reqErr := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b)) + if reqErr != nil { + return nil, reqErr + } + for k, v := range headers { + req2.Header.Set(k, v) + } + return clients.fallbackS.Do(req2) + } + return nil, err + } + return resp, nil +} diff --git a/internal/deepseek/client/client_completion_test.go b/internal/deepseek/client/client_completion_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5244c8007e0f420885a2a3797e909f1d675166c1 --- /dev/null +++ b/internal/deepseek/client/client_completion_test.go @@ -0,0 +1,36 @@ +package client + +import ( + "context" + "errors" + "net/http" + "testing" + + "ds2api/internal/auth" +) + +func TestCallCompletionDoesNotFallbackForNonIdempotentCompletion(t *testing.T) { + var fallbackCalled bool + client := &Client{ + stream: doerFunc(func(*http.Request) (*http.Response, error) { + return nil, errors.New("ambiguous completion write failure") + }), + fallbackS: &http.Client{Transport: roundTripperFunc(func(*http.Request) (*http.Response, error) { + fallbackCalled = true + return &http.Response{StatusCode: http.StatusOK}, nil + })}, + } + _, err := client.CallCompletion( + context.Background(), + &auth.RequestAuth{DeepSeekToken: "token"}, + map[string]any{"prompt": "hello"}, + "pow", + 3, + ) + if err == nil { + t.Fatal("expected completion error") + } + if fallbackCalled { + t.Fatal("completion fallback should not be called for a non-idempotent request") + } +} diff --git a/internal/deepseek/client/client_continue.go b/internal/deepseek/client/client_continue.go new file mode 100644 index 0000000000000000000000000000000000000000..009c0274ae0a53c390f094e23f0edbf9468c8d50 --- /dev/null +++ b/internal/deepseek/client/client_continue.go @@ -0,0 +1,329 @@ +package client + +import ( + "bufio" + "bytes" + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "strings" + + "ds2api/internal/auth" + "ds2api/internal/config" +) + +const defaultAutoContinueLimit = 8 + +type continueOpenFunc func(context.Context, string, int) (*http.Response, error) + +type continueState struct { + sessionID string + responseMessageID int + lastStatus string + finished bool +} + +// wrapCompletionWithAutoContinue wraps the completion response body so that +// if the upstream indicates the response is incomplete (INCOMPLETE / +// AUTO_CONTINUE), ds2api will automatically call the DeepSeek continue +// endpoint and splice the continuation SSE stream onto the original. +// The caller sees a single, seamless SSE stream. +func (c *Client) wrapCompletionWithAutoContinue(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, resp *http.Response) *http.Response { + if resp == nil || resp.Body == nil { + return resp + } + sessionID, _ := payload["chat_session_id"].(string) + sessionID = strings.TrimSpace(sessionID) + if sessionID == "" { + return resp + } + config.Logger.Debug("[auto_continue] wrapping completion response", "session_id", sessionID) + resp.Body = newAutoContinueBody(ctx, resp.Body, sessionID, defaultAutoContinueLimit, func(ctx context.Context, sessionID string, responseMessageID int) (*http.Response, error) { + return c.callContinue(ctx, a, sessionID, responseMessageID, powResp) + }) + return resp +} + +// callContinue sends a continue request to DeepSeek to resume generation. +func (c *Client) callContinue(ctx context.Context, a *auth.RequestAuth, sessionID string, responseMessageID int, powResp string) (*http.Response, error) { + if strings.TrimSpace(sessionID) == "" || responseMessageID <= 0 { + return nil, errors.New("missing continue identifiers") + } + clients := c.requestClientsForAuth(ctx, a) + headers := c.authHeaders(a.DeepSeekToken) + headers["x-ds-pow-response"] = powResp + payload := map[string]any{ + "chat_session_id": sessionID, + "message_id": responseMessageID, + "fallback_to_resume": true, + } + config.Logger.Info("[auto_continue] calling continue", "session_id", sessionID, "message_id", responseMessageID) + captureSession := c.capture.Start("deepseek_continue", dsprotocol.DeepSeekContinueURL, a.AccountID, payload) + resp, err := c.streamPost(ctx, clients.stream, dsprotocol.DeepSeekContinueURL, headers, payload) + if err != nil { + return nil, err + } + if captureSession != nil { + resp.Body = captureSession.WrapBody(resp.Body, resp.StatusCode) + } + if resp.StatusCode != http.StatusOK { + _ = resp.Body.Close() + return nil, errors.New("continue failed") + } + return resp, nil +} + +// newAutoContinueBody returns a new ReadCloser that transparently pumps +// continuation rounds via an io.Pipe. +func newAutoContinueBody(ctx context.Context, initial io.ReadCloser, sessionID string, maxRounds int, openContinue continueOpenFunc) io.ReadCloser { + if initial == nil || strings.TrimSpace(sessionID) == "" || openContinue == nil { + return initial + } + if maxRounds <= 0 { + maxRounds = defaultAutoContinueLimit + } + pr, pw := io.Pipe() + go pumpAutoContinue(ctx, pw, initial, continueState{sessionID: sessionID}, maxRounds, openContinue) + return pr +} + +// pumpAutoContinue is the goroutine that drives the auto-continue loop. +// It reads the initial SSE body, checks whether a continue is required, +// and if so opens a new continue stream and splices it onto the pipe writer. +func pumpAutoContinue(ctx context.Context, pw *io.PipeWriter, initial io.ReadCloser, state continueState, maxRounds int, openContinue continueOpenFunc) { + defer func() { _ = pw.Close() }() + current := initial + rounds := 0 + for { + hadDone, err := streamBodyWithContinueState(ctx, pw, current, &state) + _ = current.Close() + if err != nil { + _ = pw.CloseWithError(err) + return + } + if state.shouldContinue() && rounds < maxRounds { + rounds++ + config.Logger.Info("[auto_continue] continuing", "round", rounds, "session_id", state.sessionID, "message_id", state.responseMessageID, "status", state.lastStatus) + nextResp, err := openContinue(ctx, state.sessionID, state.responseMessageID) + if err != nil { + config.Logger.Warn("[auto_continue] continue request failed", "round", rounds, "error", err) + _ = pw.CloseWithError(err) + return + } + current = nextResp.Body + state.prepareForNextRound() + continue + } + // Emit the final [DONE] sentinel if the upstream had one. + if hadDone { + if _, err := io.Copy(pw, bytes.NewBufferString("data: [DONE]\n")); err != nil { + _ = pw.CloseWithError(err) + } + } + return + } +} + +// streamBodyWithContinueState scans an SSE body line-by-line, writing each +// line through to pw while observing state signals. Intermediate [DONE] +// sentinels are consumed (not forwarded) so that the downstream only sees +// one final [DONE] at the very end. +func streamBodyWithContinueState(ctx context.Context, pw *io.PipeWriter, body io.Reader, state *continueState) (bool, error) { + reader := bufio.NewReaderSize(body, 64*1024) + hadDone := false + for { + select { + case <-ctx.Done(): + return hadDone, ctx.Err() + default: + } + line, err := reader.ReadBytes('\n') + if len(line) == 0 && err != nil { + if err == io.EOF { + return hadDone, nil + } + return hadDone, err + } + trimmed := strings.TrimSpace(string(line)) + if trimmed != "" { + if strings.HasPrefix(trimmed, "data:") { + data := strings.TrimSpace(strings.TrimPrefix(trimmed, "data:")) + if data == "[DONE]" { + hadDone = true + if err != nil && err != io.EOF { + return hadDone, err + } + if err == io.EOF { + return hadDone, nil + } + continue + } + state.observe(data) + } + if !strings.HasSuffix(string(line), "\n") { + line = append(line, '\n') + } + if _, copyErr := io.Copy(pw, bytes.NewReader(line)); copyErr != nil { + return hadDone, copyErr + } + } + if err != nil { + if err == io.EOF { + return hadDone, nil + } + return hadDone, err + } + } +} + +// observe extracts continue-relevant signals from an SSE JSON chunk. +func (s *continueState) observe(data string) { + if s == nil || strings.TrimSpace(data) == "" { + return + } + var chunk map[string]any + if err := json.Unmarshal([]byte(data), &chunk); err != nil { + return + } + // Top-level response_message_id + if id := intFrom(chunk["response_message_id"]); id > 0 { + s.responseMessageID = id + } + s.observeDirectPatch(asString(chunk["p"]), chunk["v"]) + if p, _ := chunk["p"].(string); p == "response" { + s.observeBatchPatches("response", chunk["v"]) + } else { + s.observeBatchPatches("", chunk["v"]) + } + if v, _ := chunk["v"].(map[string]any); v != nil { + s.observeResponseObject(v["response"]) + } + if message, _ := chunk["message"].(map[string]any); message != nil { + s.observeResponseObject(message["response"]) + } +} + +func (s *continueState) observeDirectPatch(path string, value any) { + if s == nil { + return + } + switch strings.Trim(strings.TrimSpace(path), "/") { + case "response/status", "status", "response/quasi_status", "quasi_status": + s.setStatus(asString(value)) + case "response/auto_continue", "auto_continue": + if v, ok := value.(bool); ok && v { + s.lastStatus = "AUTO_CONTINUE" + } + } +} + +func (s *continueState) observeResponseObject(raw any) { + if s == nil { + return + } + response, _ := raw.(map[string]any) + if response == nil { + return + } + if id := intFrom(response["message_id"]); id > 0 { + s.responseMessageID = id + } + s.setStatus(asString(response["status"])) + if autoContinue, ok := response["auto_continue"].(bool); ok && autoContinue { + s.lastStatus = "AUTO_CONTINUE" + } +} + +func (s *continueState) observeBatchPatches(parentPath string, raw any) { + if s == nil { + return + } + patches, ok := raw.([]any) + if !ok { + return + } + for _, patch := range patches { + m, ok := patch.(map[string]any) + if !ok { + continue + } + path := strings.TrimSpace(asString(m["p"])) + if path == "" { + continue + } + fullPath := path + if parent := strings.Trim(strings.TrimSpace(parentPath), "/"); parent != "" && !strings.Contains(path, "/") { + fullPath = parent + "/" + path + } + switch strings.Trim(strings.TrimSpace(fullPath), "/") { + case "response/status", "status", "response/quasi_status", "quasi_status": + s.setStatus(asString(m["v"])) + case "response/auto_continue", "auto_continue": + if v, ok := m["v"].(bool); ok && v { + s.lastStatus = "AUTO_CONTINUE" + } + } + } +} + +func (s *continueState) setStatus(status string) { + if s == nil { + return + } + normalized := strings.TrimSpace(status) + if normalized == "" { + return + } + s.lastStatus = normalized + if strings.EqualFold(normalized, "FINISHED") || strings.EqualFold(normalized, "CONTENT_FILTER") { + s.finished = true + } +} + +// shouldContinue returns true when the upstream explicitly indicates the +// response is incomplete and we have enough information to issue a continue +// request. Plain WIP is not sufficient because normal streams begin in WIP. +func (s *continueState) shouldContinue() bool { + if s == nil { + return false + } + if s.finished || s.responseMessageID <= 0 || strings.TrimSpace(s.sessionID) == "" { + return false + } + switch strings.ToUpper(strings.TrimSpace(s.lastStatus)) { + case "INCOMPLETE", "AUTO_CONTINUE": + return true + default: + return false + } +} + +// prepareForNextRound resets ephemeral state before processing the next +// continuation stream. +func (s *continueState) prepareForNextRound() { + if s == nil { + return + } + s.finished = false + s.lastStatus = "" +} + +func asString(v any) string { + if v == nil { + return "" + } + switch x := v.(type) { + case string: + return x + default: + s := strings.TrimSpace(strings.ReplaceAll(strings.TrimSpace(fmt.Sprint(v)), "\u0000", "")) + if s == "" { + return "" + } + return s + } +} diff --git a/internal/deepseek/client/client_continue_test.go b/internal/deepseek/client/client_continue_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b79dbcac41126f0ded4a7ec1ce6eeda39de5d87e --- /dev/null +++ b/internal/deepseek/client/client_continue_test.go @@ -0,0 +1,307 @@ +package client + +import ( + "bytes" + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "errors" + "io" + "net/http" + "strings" + "sync/atomic" + "testing" + + "ds2api/internal/auth" +) + +type failingDoer struct { + err error +} + +func (d failingDoer) Do(_ *http.Request) (*http.Response, error) { + return nil, d.err +} + +type roundTripperFunc func(*http.Request) (*http.Response, error) + +func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) { + return f(req) +} + +func TestCallContinuePropagatesPowHeaderToFallbackRequest(t *testing.T) { + var seenPow string + var seenURL string + + client := &Client{ + stream: failingDoer{err: errors.New("stream transport failed")}, + fallbackS: &http.Client{ + Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) { + seenPow = req.Header.Get("x-ds-pow-response") + seenURL = req.URL.String() + body := io.NopCloser(strings.NewReader("data: {\"p\":\"response/content\",\"v\":\"continued\"}\n" + "data: [DONE]\n")) + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: body, + Request: req, + }, nil + }), + }, + } + + resp, err := client.callContinue(context.Background(), &auth.RequestAuth{ + DeepSeekToken: "token", + AccountID: "acct", + }, "session-123", 99, "pow-response-abc") + if err != nil { + t.Fatalf("callContinue returned error: %v", err) + } + defer func() { _ = resp.Body.Close() }() + if seenPow != "pow-response-abc" { + t.Fatalf("continue request pow header=%q want=%q", seenPow, "pow-response-abc") + } + if seenURL != dsprotocol.DeepSeekContinueURL { + t.Fatalf("continue request url=%q want=%q", seenURL, dsprotocol.DeepSeekContinueURL) + } +} + +func TestCallCompletionAutoContinueThreadsPowHeader(t *testing.T) { + var seenPow string + var seenContinueURL string + + initialBody := strings.Join([]string{ + `data: {"response_message_id":321,"v":{"response":{"message_id":321,"status":"WIP","auto_continue":true}}}`, + `data: [DONE]`, + }, "\n") + "\n" + + client := &Client{ + stream: failingOrCompletionDoer{ + completionResp: &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(initialBody)), + }, + }, + fallbackS: &http.Client{ + Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) { + seenPow = req.Header.Get("x-ds-pow-response") + seenContinueURL = req.URL.String() + body := io.NopCloser(strings.NewReader("data: {\"response_message_id\":322,\"v\":{\"response\":{\"message_id\":322,\"status\":\"FINISHED\"}}}\n" + "data: [DONE]\n")) + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: body, + Request: req, + }, nil + }), + }, + } + + resp, err := client.CallCompletion(context.Background(), &auth.RequestAuth{ + DeepSeekToken: "token", + AccountID: "acct", + }, map[string]any{ + "chat_session_id": "session-123", + }, "pow-response-xyz", 1) + if err != nil { + t.Fatalf("CallCompletion returned error: %v", err) + } + defer func() { _ = resp.Body.Close() }() + out, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read auto-continued body failed: %v", err) + } + if seenPow != "pow-response-xyz" { + t.Fatalf("threaded continue pow header=%q want=%q", seenPow, "pow-response-xyz") + } + if seenContinueURL != dsprotocol.DeepSeekContinueURL { + t.Fatalf("continue url=%q want=%q", seenContinueURL, dsprotocol.DeepSeekContinueURL) + } + if !bytes.Contains(out, []byte(`"status":"WIP"`)) { + t.Fatalf("expected initial stream content in body, got=%s", string(out)) + } + if !bytes.Contains(out, []byte(`data: [DONE]`)) { + t.Fatalf("expected final DONE sentinel in body, got=%s", string(out)) + } +} + +func TestAutoContinueDoesNotTriggerOnPlainWIPWithoutExplicitContinuationSignal(t *testing.T) { + initialBody := strings.Join([]string{ + `data: {"response_message_id":321,"v":{"response":{"message_id":321,"status":"WIP","auto_continue":false}}}`, + `data: [DONE]`, + }, "\n") + "\n" + + var continueCalls atomic.Int32 + body := newAutoContinueBody(context.Background(), io.NopCloser(strings.NewReader(initialBody)), "session-123", 8, func(context.Context, string, int) (*http.Response, error) { + continueCalls.Add(1) + return nil, errors.New("continue should not have been called") + }) + defer func() { _ = body.Close() }() + + out, err := io.ReadAll(body) + if err != nil { + t.Fatalf("read body failed: %v", err) + } + if continueCalls.Load() != 0 { + t.Fatalf("expected no continue calls, got %d", continueCalls.Load()) + } + if !bytes.Contains(out, []byte(`"status":"WIP"`)) || !bytes.Contains(out, []byte(`data: [DONE]`)) { + t.Fatalf("expected original body to pass through unchanged, got=%s", string(out)) + } +} + +func TestAutoContinuePassesThroughLongSingleSSELine(t *testing.T) { + payload := strings.Repeat("x", 2*1024*1024+4096) + initialBody := `data: {"p":"response/content","v":"` + payload + `"}` + "\n" + + `data: [DONE]` + "\n" + + body := newAutoContinueBody(context.Background(), io.NopCloser(strings.NewReader(initialBody)), "session-123", 8, func(context.Context, string, int) (*http.Response, error) { + return nil, errors.New("continue should not have been called") + }) + defer func() { _ = body.Close() }() + + out, err := io.ReadAll(body) + if err != nil { + t.Fatalf("read body failed: %v", err) + } + if !bytes.Contains(out, []byte(payload)) { + t.Fatalf("expected long SSE payload to pass through, got len=%d want payload len=%d", len(out), len(payload)) + } + if !bytes.Contains(out, []byte(`data: [DONE]`)) { + t.Fatalf("expected final DONE sentinel in body, got len=%d", len(out)) + } +} + +func TestAutoContinueTriggersOnDirectQuasiStatusIncomplete(t *testing.T) { + initialBody := strings.Join([]string{ + `data: {"response_message_id":321,"p":"response/content","v":""}` + "\n" + + `data: {"p":"response/status","v":"FINISHED"}` + "\n" + + `data: [DONE]` + "\n", + )), + }, nil + }) + defer func() { _ = body.Close() }() + + out, err := io.ReadAll(body) + if err != nil { + t.Fatalf("read body failed: %v", err) + } + if continueCalls.Load() != 1 { + t.Fatalf("expected exactly one continue call, got %d", continueCalls.Load()) + } + if !bytes.Contains(out, []byte("part-one")) || !bytes.Contains(out, []byte("-part-two")) { + t.Fatalf("expected continued tool content in body, got=%s", string(out)) + } +} + +func TestAutoContinueTriggersOnResponseBatchQuasiStatusIncomplete(t *testing.T) { + initialBody := strings.Join([]string{ + `data: {"response_message_id":321,"v":{"response":{"message_id":321,"status":"WIP","auto_continue":false}}}`, + `data: {"p":"response","o":"BATCH","v":[{"p":"accumulated_token_usage","v":2413},{"p":"quasi_status","v":"INCOMPLETE"}]}`, + `data: [DONE]`, + }, "\n") + "\n" + + var continueCalls atomic.Int32 + body := newAutoContinueBody(context.Background(), io.NopCloser(strings.NewReader(initialBody)), "session-123", 8, func(context.Context, string, int) (*http.Response, error) { + continueCalls.Add(1) + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader( + `data: {"response_message_id":322,"p":"response/status","v":"FINISHED"}` + "\n" + + `data: [DONE]` + "\n", + )), + }, nil + }) + defer func() { _ = body.Close() }() + + out, err := io.ReadAll(body) + if err != nil { + t.Fatalf("read body failed: %v", err) + } + if continueCalls.Load() != 1 { + t.Fatalf("expected exactly one continue call, got %d", continueCalls.Load()) + } + if !bytes.Contains(out, []byte(`"quasi_status","v":"INCOMPLETE"`)) || !bytes.Contains(out, []byte(`"v":"FINISHED"`)) { + t.Fatalf("expected continued output to include initial and final rounds, got=%s", string(out)) + } +} + +func TestAutoContinueDoesNotTriggerWhenResponseBatchQuasiStatusFinished(t *testing.T) { + initialBody := strings.Join([]string{ + `data: {"response_message_id":321,"v":{"response":{"message_id":321,"status":"WIP","auto_continue":false}}}`, + `data: {"p":"response","o":"BATCH","v":[{"p":"accumulated_token_usage","v":2413},{"p":"quasi_status","v":"FINISHED"}]}`, + `data: [DONE]`, + }, "\n") + "\n" + + var continueCalls atomic.Int32 + body := newAutoContinueBody(context.Background(), io.NopCloser(strings.NewReader(initialBody)), "session-123", 8, func(context.Context, string, int) (*http.Response, error) { + continueCalls.Add(1) + return nil, errors.New("continue should not have been called") + }) + defer func() { _ = body.Close() }() + + out, err := io.ReadAll(body) + if err != nil { + t.Fatalf("read body failed: %v", err) + } + if continueCalls.Load() != 0 { + t.Fatalf("expected no continue calls, got %d", continueCalls.Load()) + } + if !bytes.Contains(out, []byte(`"quasi_status","v":"FINISHED"`)) || !bytes.Contains(out, []byte(`data: [DONE]`)) { + t.Fatalf("expected original finished body to pass through unchanged, got=%s", string(out)) + } +} + +type failingOrCompletionDoer struct { + completionResp *http.Response +} + +func (d failingOrCompletionDoer) Do(req *http.Request) (*http.Response, error) { + if strings.Contains(req.URL.Path, "/chat/completion") { + return d.completionResp, nil + } + return nil, errors.New("forced stream failure") +} + +func TestAutoContinuePreservesIncompleteStateWhenNextChunkOmitsStatus(t *testing.T) { + initialBody := strings.Join([]string{ + `data: {"response_message_id":321,"v":{"response":{"message_id":321,"status":"INCOMPLETE"}}}`, + `data: {"p":"response/content","v":{"text":"continued"}}`, + `data: [DONE]`, + }, "\n") + "\n" + + var continueCalls atomic.Int32 + body := newAutoContinueBody(context.Background(), io.NopCloser(strings.NewReader(initialBody)), "session-123", 8, func(context.Context, string, int) (*http.Response, error) { + continueCalls.Add(1) + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader( + `data: {"response_message_id":322,"p":"response/status","v":"FINISHED"}` + "\n" + + `data: [DONE]` + "\n", + )), + }, nil + }) + defer func() { _ = body.Close() }() + + _, err := io.ReadAll(body) + if err != nil { + t.Fatalf("read body failed: %v", err) + } + if continueCalls.Load() != 1 { + t.Fatalf("expected exactly one continue call, got %d", continueCalls.Load()) + } +} diff --git a/internal/deepseek/client/client_core.go b/internal/deepseek/client/client_core.go new file mode 100644 index 0000000000000000000000000000000000000000..f730e88f71ea02ae1dd419707c95c77d48952c1a --- /dev/null +++ b/internal/deepseek/client/client_core.go @@ -0,0 +1,50 @@ +package client + +import ( + "context" + "net/http" + "sync" + "time" + + "ds2api/internal/auth" + "ds2api/internal/config" + trans "ds2api/internal/deepseek/transport" + "ds2api/internal/devcapture" + "ds2api/internal/util" +) + +// intFrom is a package-internal alias for the shared util version. +var intFrom = util.IntFrom + +type Client struct { + Store *config.Store + Auth *auth.Resolver + capture *devcapture.Store + regular trans.Doer + stream trans.Doer + fallback *http.Client + fallbackS *http.Client + maxRetries int + + proxyClientsMu sync.RWMutex + proxyClients map[string]requestClients +} + +func NewClient(store *config.Store, resolver *auth.Resolver) *Client { + return &Client{ + Store: store, + Auth: resolver, + capture: devcapture.Global(), + regular: trans.New(60 * time.Second), + stream: trans.New(0), + fallback: &http.Client{Timeout: 60 * time.Second}, + fallbackS: &http.Client{Timeout: 0}, + maxRetries: 3, + proxyClients: map[string]requestClients{}, + } +} + +// PreloadPow 保留兼容接口,纯 Go 实现无需预加载。 +func (c *Client) PreloadPow(_ context.Context) error { + return nil +} diff --git a/internal/deepseek/client/client_file_status.go b/internal/deepseek/client/client_file_status.go new file mode 100644 index 0000000000000000000000000000000000000000..07acf87f992e91b17ca0f6952d8cf22521d26fbd --- /dev/null +++ b/internal/deepseek/client/client_file_status.go @@ -0,0 +1,193 @@ +package client + +import ( + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "errors" + "fmt" + "net/http" + "net/url" + "strings" + "time" + + "ds2api/internal/auth" + "ds2api/internal/config" +) + +const ( + fileReadyPollAttempts = 60 + fileReadyPollInterval = time.Second + fileReadyPollTimeout = 65 * time.Second +) + +var fileReadySleep = time.Sleep + +// ErrUploadFileNotFound indicates that DeepSeek returned no matching uploaded file. +var ErrUploadFileNotFound = errors.New("uploaded file not found") + +func (c *Client) waitForUploadedFile(ctx context.Context, a *auth.RequestAuth, result *UploadFileResult) error { + if result == nil || strings.TrimSpace(result.ID) == "" { + return nil + } + if isReadyUploadFileStatus(result.Status) { + return nil + } + + pollCtx, cancel := context.WithTimeout(ctx, fileReadyPollTimeout) + defer cancel() + + var lastErr error + for attempt := 0; attempt < fileReadyPollAttempts; attempt++ { + if err := pollCtx.Err(); err != nil { + if lastErr != nil { + return fmt.Errorf("waiting for file %s to become ready: %w", result.ID, lastErr) + } + return fmt.Errorf("waiting for file %s to become ready: %w", result.ID, err) + } + + fetched, err := c.FetchUploadedFile(pollCtx, a, result.ID) + if err == nil && fetched != nil { + mergeUploadFileResults(result, fetched) + if isReadyUploadFileStatus(result.Status) { + return nil + } + lastErr = fmt.Errorf("status=%s", strings.TrimSpace(result.Status)) + } else if err != nil { + lastErr = err + config.Logger.Debug("[upload_file] waiting for file readiness", "file_id", result.ID, "attempt", attempt+1, "error", err) + } + + if attempt < fileReadyPollAttempts-1 { + fileReadySleep(fileReadyPollInterval) + } + } + + if lastErr == nil { + lastErr = fmt.Errorf("status=%s", strings.TrimSpace(result.Status)) + } + return fmt.Errorf("file %s did not become ready: %w", result.ID, lastErr) +} + +// FetchUploadedFile returns metadata for an uploaded DeepSeek file by ID. +func (c *Client) FetchUploadedFile(ctx context.Context, a *auth.RequestAuth, fileID string) (*UploadFileResult, error) { + fileID = strings.TrimSpace(fileID) + if fileID == "" { + return nil, errors.New("file id is required") + } + clients := c.requestClientsForAuth(ctx, a) + reqURL := dsprotocol.DeepSeekFetchFilesURL + "?file_ids=" + url.QueryEscape(fileID) + headers := c.authHeaders(a.DeepSeekToken) + + resp, status, err := c.getJSONWithStatus(ctx, clients.regular, reqURL, headers) + if err != nil { + return nil, err + } + + code, bizCode, msg, bizMsg := extractResponseStatus(resp) + if status != http.StatusOK || code != 0 || bizCode != 0 { + if strings.TrimSpace(bizMsg) != "" { + msg = bizMsg + } + if msg == "" { + msg = http.StatusText(status) + } + return nil, fmt.Errorf("request failed: status=%d, code=%d, msg=%s", status, code, msg) + } + + result := extractFetchedUploadFileResult(resp, fileID) + if result == nil || strings.TrimSpace(result.ID) == "" { + return nil, ErrUploadFileNotFound + } + result.Raw = resp + return result, nil +} + +func extractFetchedUploadFileResult(resp map[string]any, targetID string) *UploadFileResult { + targetID = strings.TrimSpace(targetID) + if resp == nil || targetID == "" { + return nil + } + + var walk func(any) *UploadFileResult + walk = func(v any) *UploadFileResult { + switch x := v.(type) { + case map[string]any: + if result := buildUploadFileResultFromMap(x, targetID); result != nil { + return result + } + for _, nested := range x { + if result := walk(nested); result != nil { + return result + } + } + case []any: + for _, item := range x { + if result := walk(item); result != nil { + return result + } + } + } + return nil + } + + if result := walk(resp); result != nil { + return result + } + return nil +} + +func buildUploadFileResultFromMap(m map[string]any, targetID string) *UploadFileResult { + fileID := strings.TrimSpace(firstNonEmptyString(m, "id", "file_id")) + if fileID == "" || !strings.EqualFold(fileID, targetID) { + return nil + } + result := &UploadFileResult{ + ID: fileID, + Filename: firstNonEmptyString(m, "name", "filename", "file_name"), + Status: firstNonEmptyString(m, "status", "file_status"), + Purpose: firstNonEmptyString(m, "purpose"), + IsImage: firstBool(m, "is_image", "isImage"), + Bytes: firstPositiveInt64(m, "bytes", "size", "file_size"), + } + if result.Status == "" { + result.Status = "uploaded" + } + return result +} + +func mergeUploadFileResults(dst, src *UploadFileResult) { + if dst == nil || src == nil { + return + } + if strings.TrimSpace(src.ID) != "" { + dst.ID = strings.TrimSpace(src.ID) + } + if strings.TrimSpace(src.Filename) != "" { + dst.Filename = strings.TrimSpace(src.Filename) + } + if src.Bytes > 0 { + dst.Bytes = src.Bytes + } + if strings.TrimSpace(src.Status) != "" { + dst.Status = strings.TrimSpace(src.Status) + } + if strings.TrimSpace(src.Purpose) != "" { + dst.Purpose = strings.TrimSpace(src.Purpose) + } + dst.IsImage = src.IsImage + if len(src.Raw) > 0 { + dst.Raw = src.Raw + } + if src.RawHeaders != nil { + dst.RawHeaders = src.RawHeaders.Clone() + } +} + +func isReadyUploadFileStatus(status string) bool { + switch strings.ToLower(strings.TrimSpace(status)) { + case "processed", "ready", "done", "available", "success", "completed", "finished": + return true + default: + return false + } +} diff --git a/internal/deepseek/client/client_http_helpers.go b/internal/deepseek/client/client_http_helpers.go new file mode 100644 index 0000000000000000000000000000000000000000..dd690d9689b1ba7ed854d8a8510184a53525afc7 --- /dev/null +++ b/internal/deepseek/client/client_http_helpers.go @@ -0,0 +1,49 @@ +package client + +import ( + "compress/gzip" + "io" + "net/http" + "strings" + + "github.com/andybalholm/brotli" +) + +func readResponseBody(resp *http.Response) ([]byte, error) { + encoding := strings.ToLower(strings.TrimSpace(resp.Header.Get("Content-Encoding"))) + var reader io.Reader = resp.Body + switch encoding { + case "gzip": + gz, err := gzip.NewReader(resp.Body) + if err != nil { + return nil, err + } + defer func() { _ = gz.Close() }() + reader = gz + case "br": + reader = brotli.NewReader(resp.Body) + } + return io.ReadAll(reader) +} + +func preview(b []byte) string { + s := strings.TrimSpace(string(b)) + if len(s) > 160 { + return s[:160] + } + return s +} + +func (c *Client) jsonHeaders(headers map[string]string) map[string]string { + out := cloneStringMap(headers) + out["Content-Type"] = "application/json" + return out +} + +func cloneStringMap(in map[string]string) map[string]string { + out := make(map[string]string, len(in)) + for k, v := range in { + out[k] = v + } + return out +} diff --git a/internal/deepseek/client/client_http_json.go b/internal/deepseek/client/client_http_json.go new file mode 100644 index 0000000000000000000000000000000000000000..06c8138a205f40b487bf59fe85bdd457b6b8711e --- /dev/null +++ b/internal/deepseek/client/client_http_json.go @@ -0,0 +1,103 @@ +package client + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "net/http" + + "ds2api/internal/config" + trans "ds2api/internal/deepseek/transport" +) + +func (c *Client) postJSON(ctx context.Context, doer trans.Doer, fallback trans.Doer, url string, headers map[string]string, payload any) (map[string]any, error) { + body, status, err := c.postJSONWithStatus(ctx, doer, fallback, url, headers, payload) + if err != nil { + return nil, err + } + if status == 0 { + return nil, errors.New("request failed") + } + return body, nil +} + +func (c *Client) postJSONWithStatus(ctx context.Context, doer trans.Doer, fallback trans.Doer, url string, headers map[string]string, payload any) (map[string]any, int, error) { + b, err := json.Marshal(payload) + if err != nil { + return nil, 0, err + } + headers = c.jsonHeaders(headers) + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b)) + if err != nil { + return nil, 0, err + } + for k, v := range headers { + req.Header.Set(k, v) + } + resp, err := doer.Do(req) + if err != nil { + config.Logger.Warn("[deepseek] fingerprint request failed, fallback to std transport", "url", url, "error", err) + req2, reqErr := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b)) + if reqErr != nil { + return nil, 0, reqErr + } + for k, v := range headers { + req2.Header.Set(k, v) + } + resp, err = fallback.Do(req2) + if err != nil { + return nil, 0, err + } + } + defer func() { _ = resp.Body.Close() }() + payloadBytes, err := readResponseBody(resp) + if err != nil { + return nil, resp.StatusCode, err + } + out := map[string]any{} + if len(payloadBytes) > 0 { + if err := json.Unmarshal(payloadBytes, &out); err != nil { + config.Logger.Warn("[deepseek] json parse failed", "url", url, "status", resp.StatusCode, "content_encoding", resp.Header.Get("Content-Encoding"), "preview", preview(payloadBytes)) + } + } + return out, resp.StatusCode, nil +} + +func (c *Client) getJSONWithStatus(ctx context.Context, doer trans.Doer, url string, headers map[string]string) (map[string]any, int, error) { + clients := c.requestClientsFromContext(ctx) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + return nil, 0, err + } + for k, v := range headers { + req.Header.Set(k, v) + } + resp, err := doer.Do(req) + if err != nil { + config.Logger.Warn("[deepseek] fingerprint GET request failed, fallback to std transport", "url", url, "error", err) + req2, reqErr := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if reqErr != nil { + return nil, 0, reqErr + } + for k, v := range headers { + req2.Header.Set(k, v) + } + resp, err = clients.fallback.Do(req2) + if err != nil { + return nil, 0, err + } + } + defer func() { _ = resp.Body.Close() }() + payloadBytes, err := readResponseBody(resp) + if err != nil { + return nil, resp.StatusCode, err + } + out := map[string]any{} + if len(payloadBytes) > 0 { + if err := json.Unmarshal(payloadBytes, &out); err != nil { + config.Logger.Warn("[deepseek] json parse failed", "url", url, "status", resp.StatusCode, "content_encoding", resp.Header.Get("Content-Encoding"), "preview", preview(payloadBytes)) + } + } + return out, resp.StatusCode, nil +} diff --git a/internal/deepseek/client/client_http_json_test.go b/internal/deepseek/client/client_http_json_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d2188e996b36a191620d3df8f79982d8041f8539 --- /dev/null +++ b/internal/deepseek/client/client_http_json_test.go @@ -0,0 +1,52 @@ +package client + +import ( + "context" + "errors" + "io" + "net/http" + "strings" + "testing" +) + +func TestPostJSONWithStatusUsesProvidedFallbackClient(t *testing.T) { + var fallbackCalled bool + client := &Client{} + primary := failingDoer{err: errors.New("primary failed")} + fallbackDoer := doerFunc(func(req *http.Request) (*http.Response, error) { + fallbackCalled = true + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(`{"ok":true}`)), + Request: req, + }, nil + }) + + resp, status, err := client.postJSONWithStatus( + context.Background(), + primary, + fallbackDoer, + "https://example.com/api", + map[string]string{"x-test": "1"}, + map[string]any{"foo": "bar"}, + ) + if err != nil { + t.Fatalf("postJSONWithStatus error: %v", err) + } + if status != http.StatusOK { + t.Fatalf("status=%d want=%d", status, http.StatusOK) + } + if !fallbackCalled { + t.Fatal("expected provided fallback doer to be called") + } + if ok, _ := resp["ok"].(bool); !ok { + t.Fatalf("unexpected response body: %#v", resp) + } +} + +type doerFunc func(*http.Request) (*http.Response, error) + +func (f doerFunc) Do(req *http.Request) (*http.Response, error) { + return f(req) +} diff --git a/internal/deepseek/client/client_session.go b/internal/deepseek/client/client_session.go new file mode 100644 index 0000000000000000000000000000000000000000..98a7feb12fb3dc797a30a3cb569c57b1cc3fe119 --- /dev/null +++ b/internal/deepseek/client/client_session.go @@ -0,0 +1,261 @@ +package client + +import ( + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "errors" + "fmt" + "net/http" + "net/url" + "strings" + + "ds2api/internal/auth" + "ds2api/internal/config" +) + +// SessionInfo 会话信息 +type SessionInfo struct { + ID string `json:"id"` + Title string `json:"title"` + TitleType string `json:"title_type"` + Pinned bool `json:"pinned"` + UpdatedAt float64 `json:"updated_at"` +} + +// SessionStats 会话统计结果 +type SessionStats struct { + AccountID string // 账号标识 (email 或 mobile) + FirstPageCount int // 第一页会话数量(当 HasMore 为 true 时,真实总数可能更大) + PinnedCount int // 置顶会话数量 + HasMore bool // 是否还有更多页 + Success bool // 请求是否成功 + ErrorMessage string // 错误信息 +} + +// GetSessionCount 获取单个账号的会话数量 +func (c *Client) GetSessionCount(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (*SessionStats, error) { + if maxAttempts <= 0 { + maxAttempts = c.maxRetries + } + clients := c.requestClientsForAuth(ctx, a) + + stats := &SessionStats{ + AccountID: a.AccountID, + } + + attempts := 0 + refreshed := false + + for attempts < maxAttempts { + headers := c.authHeaders(a.DeepSeekToken) + + // 构建请求 URL + reqURL := dsprotocol.DeepSeekFetchSessionURL + "?lte_cursor.pinned=false" + + resp, status, err := c.getJSONWithStatus(ctx, clients.regular, reqURL, headers) + if err != nil { + config.Logger.Warn("[get_session_count] request error", "error", err, "account", a.AccountID) + attempts++ + continue + } + + code, bizCode, msg, bizMsg := extractResponseStatus(resp) + if status == http.StatusOK && code == 0 && bizCode == 0 { + data, _ := resp["data"].(map[string]any) + bizData, _ := data["biz_data"].(map[string]any) + chatSessions, _ := bizData["chat_sessions"].([]any) + hasMore, _ := bizData["has_more"].(bool) + + stats.FirstPageCount = len(chatSessions) + stats.HasMore = hasMore + stats.Success = true + + // 统计置顶会话数量 + for _, session := range chatSessions { + if s, ok := session.(map[string]any); ok { + if pinned, ok := s["pinned"].(bool); ok && pinned { + stats.PinnedCount++ + } + } + } + + return stats, nil + } + + stats.ErrorMessage = fmt.Sprintf("status=%d, code=%d, msg=%s", status, code, msg) + config.Logger.Warn("[get_session_count] failed", "status", status, "code", code, "biz_code", bizCode, "msg", msg, "biz_msg", bizMsg, "account", a.AccountID) + + if a.UseConfigToken { + if isTokenInvalid(status, code, bizCode, msg, bizMsg) && !refreshed { + if c.Auth.RefreshToken(ctx, a) { + refreshed = true + continue + } + } + if c.Auth.SwitchAccount(ctx, a) { + refreshed = false + attempts++ + continue + } + } + attempts++ + } + + stats.Success = false + stats.ErrorMessage = "get session count failed after retries" + return stats, errors.New(stats.ErrorMessage) +} + +// GetSessionCountForToken 直接使用 token 获取会话数量(直通模式) +func (c *Client) GetSessionCountForToken(ctx context.Context, token string) (*SessionStats, error) { + clients := c.requestClientsFromContext(ctx) + headers := c.authHeaders(token) + reqURL := dsprotocol.DeepSeekFetchSessionURL + "?lte_cursor.pinned=false" + + resp, status, err := c.getJSONWithStatus(ctx, clients.regular, reqURL, headers) + if err != nil { + return nil, err + } + + code, bizCode, msg, bizMsg := extractResponseStatus(resp) + if status != http.StatusOK || code != 0 || bizCode != 0 { + if strings.TrimSpace(bizMsg) != "" { + msg = bizMsg + } + return nil, fmt.Errorf("request failed: status=%d, code=%d, msg=%s", status, code, msg) + } + + data, _ := resp["data"].(map[string]any) + bizData, _ := data["biz_data"].(map[string]any) + chatSessions, _ := bizData["chat_sessions"].([]any) + hasMore, _ := bizData["has_more"].(bool) + + stats := &SessionStats{ + FirstPageCount: len(chatSessions), + HasMore: hasMore, + Success: true, + } + + // 统计置顶会话数量 + for _, session := range chatSessions { + if s, ok := session.(map[string]any); ok { + if pinned, ok := s["pinned"].(bool); ok && pinned { + stats.PinnedCount++ + } + } + } + + return stats, nil +} + +// GetSessionCountAll 获取所有账号的会话数量统计 +func (c *Client) GetSessionCountAll(ctx context.Context) []*SessionStats { + accounts := c.Store.Accounts() + results := make([]*SessionStats, 0, len(accounts)) + + for _, acc := range accounts { + token := acc.Token + accountID := acc.Email + if accountID == "" { + accountID = acc.Mobile + } + + // 如果没有 token,尝试登录获取 + if token == "" { + var err error + token, err = c.Login(auth.WithAuth(ctx, &auth.RequestAuth{AccountID: acc.Identifier(), Account: acc}), acc) + if err != nil { + results = append(results, &SessionStats{ + AccountID: accountID, + Success: false, + ErrorMessage: fmt.Sprintf("login failed: %v", err), + }) + continue + } + } + + ctxWithAuth := auth.WithAuth(ctx, &auth.RequestAuth{AccountID: acc.Identifier(), Account: acc, DeepSeekToken: token}) + stats, err := c.GetSessionCountForToken(ctxWithAuth, token) + if err != nil { + results = append(results, &SessionStats{ + AccountID: accountID, + Success: false, + ErrorMessage: err.Error(), + }) + continue + } + + stats.AccountID = accountID + results = append(results, stats) + } + + return results +} + +// FetchSessionPage 获取会话列表(支持分页) +func (c *Client) FetchSessionPage(ctx context.Context, a *auth.RequestAuth, cursor string) ([]SessionInfo, bool, error) { + clients := c.requestClientsForAuth(ctx, a) + headers := c.authHeaders(a.DeepSeekToken) + + // 构建请求 URL + params := url.Values{} + params.Set("lte_cursor.pinned", "false") + if cursor != "" { + params.Set("lte_cursor", cursor) + } + reqURL := dsprotocol.DeepSeekFetchSessionURL + "?" + params.Encode() + + resp, status, err := c.getJSONWithStatus(ctx, clients.regular, reqURL, headers) + if err != nil { + return nil, false, err + } + + code := intFrom(resp["code"]) + if status != http.StatusOK || code != 0 { + msg, _ := resp["msg"].(string) + return nil, false, fmt.Errorf("request failed: status=%d, code=%d, msg=%s", status, code, msg) + } + + data, _ := resp["data"].(map[string]any) + bizData, _ := data["biz_data"].(map[string]any) + chatSessions, _ := bizData["chat_sessions"].([]any) + hasMore, _ := bizData["has_more"].(bool) + + sessions := make([]SessionInfo, 0, len(chatSessions)) + for _, s := range chatSessions { + if m, ok := s.(map[string]any); ok { + session := SessionInfo{ + ID: stringFromMap(m, "id"), + Title: stringFromMap(m, "title"), + TitleType: stringFromMap(m, "title_type"), + Pinned: boolFromMap(m, "pinned"), + UpdatedAt: floatFromMap(m, "updated_at"), + } + sessions = append(sessions, session) + } + } + + return sessions, hasMore, nil +} + +// 辅助函数 +func stringFromMap(m map[string]any, key string) string { + if v, ok := m[key].(string); ok { + return v + } + return "" +} + +func boolFromMap(m map[string]any, key string) bool { + if v, ok := m[key].(bool); ok { + return v + } + return false +} + +func floatFromMap(m map[string]any, key string) float64 { + if v, ok := m[key].(float64); ok { + return v + } + return 0 +} diff --git a/internal/deepseek/client/client_session_delete.go b/internal/deepseek/client/client_session_delete.go new file mode 100644 index 0000000000000000000000000000000000000000..fa810fd3d1b81b256526101c8813eeb914a5cef3 --- /dev/null +++ b/internal/deepseek/client/client_session_delete.go @@ -0,0 +1,160 @@ +package client + +import ( + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "errors" + "fmt" + "net/http" + + "ds2api/internal/auth" + "ds2api/internal/config" +) + +// DeleteSessionResult 删除会话结果 +type DeleteSessionResult struct { + SessionID string // 会话 ID + Success bool // 是否成功 + ErrorMessage string // 错误信息 +} + +// DeleteSession 删除单个会话 +func (c *Client) DeleteSession(ctx context.Context, a *auth.RequestAuth, sessionID string, maxAttempts int) (*DeleteSessionResult, error) { + if maxAttempts <= 0 { + maxAttempts = c.maxRetries + } + clients := c.requestClientsForAuth(ctx, a) + + result := &DeleteSessionResult{ + SessionID: sessionID, + } + + if sessionID == "" { + result.ErrorMessage = "session_id is required" + return result, errors.New(result.ErrorMessage) + } + + attempts := 0 + refreshed := false + + for attempts < maxAttempts { + headers := c.authHeaders(a.DeepSeekToken) + + payload := map[string]any{ + "chat_session_id": sessionID, + } + + resp, status, err := c.postJSONWithStatus(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekDeleteSessionURL, headers, payload) + if err != nil { + config.Logger.Warn("[delete_session] request error", "error", err, "session_id", sessionID) + attempts++ + continue + } + + code, bizCode, msg, bizMsg := extractResponseStatus(resp) + if status == http.StatusOK && code == 0 && bizCode == 0 { + result.Success = true + return result, nil + } + + result.ErrorMessage = fmt.Sprintf("status=%d, code=%d, msg=%s", status, code, msg) + config.Logger.Warn("[delete_session] failed", "status", status, "code", code, "biz_code", bizCode, "msg", msg, "biz_msg", bizMsg, "session_id", sessionID) + + if a.UseConfigToken { + if isTokenInvalid(status, code, bizCode, msg, bizMsg) && !refreshed { + if c.Auth.RefreshToken(ctx, a) { + refreshed = true + continue + } + } + if c.Auth.SwitchAccount(ctx, a) { + refreshed = false + attempts++ + continue + } + } + attempts++ + } + + result.Success = false + result.ErrorMessage = "delete session failed after retries" + return result, errors.New(result.ErrorMessage) +} + +// DeleteSessionForToken 直接使用 token 删除会话(直通模式) +func (c *Client) DeleteSessionForToken(ctx context.Context, token string, sessionID string) (*DeleteSessionResult, error) { + clients := c.requestClientsFromContext(ctx) + result := &DeleteSessionResult{ + SessionID: sessionID, + } + + if sessionID == "" { + result.ErrorMessage = "session_id is required" + return result, errors.New(result.ErrorMessage) + } + + headers := c.authHeaders(token) + payload := map[string]any{ + "chat_session_id": sessionID, + } + + resp, status, err := c.postJSONWithStatus(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekDeleteSessionURL, headers, payload) + if err != nil { + result.ErrorMessage = err.Error() + return result, err + } + + code := intFrom(resp["code"]) + if status != http.StatusOK || code != 0 { + msg, _ := resp["msg"].(string) + result.ErrorMessage = fmt.Sprintf("request failed: status=%d, code=%d, msg=%s", status, code, msg) + return result, errors.New(result.ErrorMessage) + } + + result.Success = true + return result, nil +} + +// DeleteAllSessions 删除所有会话(谨慎使用) +func (c *Client) DeleteAllSessions(ctx context.Context, a *auth.RequestAuth) error { + clients := c.requestClientsForAuth(ctx, a) + headers := c.authHeaders(a.DeepSeekToken) + payload := map[string]any{} + + resp, status, err := c.postJSONWithStatus(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekDeleteAllSessionsURL, headers, payload) + if err != nil { + config.Logger.Warn("[delete_all_sessions] request error", "error", err) + return err + } + + code := intFrom(resp["code"]) + if status != http.StatusOK || code != 0 { + msg, _ := resp["msg"].(string) + config.Logger.Warn("[delete_all_sessions] failed", "status", status, "code", code, "msg", msg) + return fmt.Errorf("request failed: status=%d, code=%d, msg=%s", status, code, msg) + } + + return nil +} + +// DeleteAllSessionsForToken 直接使用 token 删除所有会话(直通模式) +func (c *Client) DeleteAllSessionsForToken(ctx context.Context, token string) error { + clients := c.requestClientsFromContext(ctx) + headers := c.authHeaders(token) + payload := map[string]any{} + + resp, status, err := c.postJSONWithStatus(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekDeleteAllSessionsURL, headers, payload) + if err != nil { + config.Logger.Warn("[delete_all_sessions_for_token] request error", "error", err) + return err + } + + code := intFrom(resp["code"]) + if status != http.StatusOK || code != 0 { + msg, _ := resp["msg"].(string) + config.Logger.Warn("[delete_all_sessions_for_token] failed", "status", status, "code", code, "msg", msg) + return fmt.Errorf("request failed: status=%d, code=%d, msg=%s", status, code, msg) + } + + return nil +} diff --git a/internal/deepseek/client/client_upload.go b/internal/deepseek/client/client_upload.go new file mode 100644 index 0000000000000000000000000000000000000000..3dc778dd37e289abd3de7426839faf5c80e4d558 --- /dev/null +++ b/internal/deepseek/client/client_upload.go @@ -0,0 +1,292 @@ +package client + +import ( + "bytes" + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "encoding/json" + "errors" + "fmt" + "mime/multipart" + "net/http" + "net/textproto" + "path/filepath" + "strconv" + "strings" + + "ds2api/internal/auth" + "ds2api/internal/config" + trans "ds2api/internal/deepseek/transport" +) + +type UploadFileRequest struct { + Filename string + ContentType string + Purpose string + ModelType string + Data []byte +} + +type UploadFileResult struct { + ID string + Filename string + Bytes int64 + Status string + Purpose string + AccountID string + IsImage bool + Raw map[string]any + RawHeaders http.Header +} + +func (c *Client) UploadFile(ctx context.Context, a *auth.RequestAuth, req UploadFileRequest, maxAttempts int) (*UploadFileResult, error) { + if maxAttempts <= 0 { + maxAttempts = c.maxRetries + } + if len(req.Data) == 0 { + return nil, errors.New("file is required") + } + filename := strings.TrimSpace(req.Filename) + if filename == "" { + filename = "upload.bin" + } + contentType := strings.TrimSpace(req.ContentType) + if contentType == "" { + contentType = "application/octet-stream" + } + purpose := strings.TrimSpace(req.Purpose) + modelType := strings.ToLower(strings.TrimSpace(req.ModelType)) + body, contentTypeHeader, err := buildUploadMultipartBody(filename, contentType, req.Data) + if err != nil { + return nil, err + } + capturePayload := map[string]any{ + "filename": filename, + "content_type": contentType, + "purpose": purpose, + "bytes": len(req.Data), + } + if modelType != "" { + capturePayload["model_type"] = modelType + } + captureSession := c.capture.Start("deepseek_upload_file", dsprotocol.DeepSeekUploadFileURL, a.AccountID, capturePayload) + attempts := 0 + refreshed := false + powHeader := "" + lastFailureKind := FailureUnknown + lastFailureMessage := "" + for attempts < maxAttempts { + clients := c.requestClientsForAuth(ctx, a) + if strings.TrimSpace(powHeader) == "" { + powHeader, err = c.GetPowForTarget(ctx, a, dsprotocol.DeepSeekUploadTargetPath, maxAttempts) + if err != nil { + return nil, err + } + clients = c.requestClientsForAuth(ctx, a) + } + headers := c.authHeaders(a.DeepSeekToken) + headers["Content-Type"] = contentTypeHeader + if modelType != "" { + headers["x-model-type"] = modelType + } + headers["x-ds-pow-response"] = powHeader + headers["x-file-size"] = strconv.Itoa(len(req.Data)) + headers["x-thinking-enabled"] = "1" + resp, err := c.doUpload(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekUploadFileURL, headers, body) + if err != nil { + config.Logger.Warn("[upload_file] request error", "error", err, "account", a.AccountID, "filename", filename) + return nil, err + } + if captureSession != nil { + resp.Body = captureSession.WrapBody(resp.Body, resp.StatusCode) + } + payloadBytes, readErr := readResponseBody(resp) + _ = resp.Body.Close() + if readErr != nil { + powHeader = "" + attempts++ + continue + } + parsed := map[string]any{} + if len(payloadBytes) > 0 { + if err := json.Unmarshal(payloadBytes, &parsed); err != nil { + config.Logger.Warn("[upload_file] json parse failed", "status", resp.StatusCode, "preview", preview(payloadBytes)) + } + } + code, bizCode, msg, bizMsg := extractResponseStatus(parsed) + if resp.StatusCode == http.StatusOK && code == 0 && bizCode == 0 { + result := extractUploadFileResult(parsed) + result.Raw = parsed + result.RawHeaders = resp.Header.Clone() + if result.Filename == "" { + result.Filename = filename + } + if result.Bytes == 0 { + result.Bytes = int64(len(req.Data)) + } + if result.Purpose == "" { + result.Purpose = purpose + } + if result.AccountID == "" { + result.AccountID = a.AccountID + } + if result.ID == "" { + return nil, errors.New("upload file succeeded without file id") + } + if err := c.waitForUploadedFile(ctx, a, result); err != nil { + return nil, err + } + return result, nil + } + config.Logger.Warn("[upload_file] failed", "status", resp.StatusCode, "code", code, "biz_code", bizCode, "msg", msg, "biz_msg", bizMsg, "account", a.AccountID, "filename", filename) + powHeader = "" + lastFailureMessage = failureMessage(msg, bizMsg, "upload file failed") + if isTokenInvalid(resp.StatusCode, code, bizCode, msg, bizMsg) || isAuthIndicativeBizFailure(msg, bizMsg) { + lastFailureKind = authFailureKind(a.UseConfigToken) + } else { + lastFailureKind = FailureUnknown + } + if a.UseConfigToken { + if !refreshed && shouldAttemptRefresh(resp.StatusCode, code, bizCode, msg, bizMsg) { + if c.Auth.RefreshToken(ctx, a) { + refreshed = true + attempts++ + continue + } + } + if c.Auth.SwitchAccount(ctx, a) { + refreshed = false + attempts++ + continue + } + } + attempts++ + } + if lastFailureKind != FailureUnknown { + return nil, &RequestFailure{Op: "upload file", Kind: lastFailureKind, Message: lastFailureMessage} + } + return nil, errors.New("upload file failed") +} + +func buildUploadMultipartBody(filename, contentType string, data []byte) ([]byte, string, error) { + var buf bytes.Buffer + writer := multipart.NewWriter(&buf) + partHeader := textproto.MIMEHeader{} + partHeader.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename=%q`, escapeMultipartFilename(filename))) + partHeader.Set("Content-Type", contentType) + part, err := writer.CreatePart(partHeader) + if err != nil { + return nil, "", err + } + if _, err := part.Write(data); err != nil { + return nil, "", err + } + if err := writer.Close(); err != nil { + return nil, "", err + } + return buf.Bytes(), writer.FormDataContentType(), nil +} + +func escapeMultipartFilename(filename string) string { + filename = filepath.Base(strings.TrimSpace(filename)) + filename = strings.ReplaceAll(filename, `\`, "_") + filename = strings.ReplaceAll(filename, `"`, "_") + if filename == "." || filename == "" { + return "upload.bin" + } + return filename +} + +func (c *Client) doUpload(ctx context.Context, doer trans.Doer, _ trans.Doer, url string, headers map[string]string, body []byte) (*http.Response, error) { + req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body)) + if err != nil { + return nil, err + } + for k, v := range headers { + req.Header.Set(k, v) + } + resp, err := doer.Do(req) + if err == nil { + return resp, nil + } + return nil, err +} + +func extractUploadFileResult(resp map[string]any) *UploadFileResult { + result := &UploadFileResult{Status: "uploaded"} + data, _ := resp["data"].(map[string]any) + bizData, _ := data["biz_data"].(map[string]any) + searchMaps := []map[string]any{resp, data, bizData} + for _, parent := range []map[string]any{resp, data, bizData} { + if parent == nil { + continue + } + for _, key := range []string{"file", "biz_data", "data"} { + if nested, ok := parent[key].(map[string]any); ok { + searchMaps = append(searchMaps, nested) + } + } + } + for _, m := range searchMaps { + if m == nil { + continue + } + if result.ID == "" { + result.ID = firstNonEmptyString(m, "id", "file_id") + } + if result.Filename == "" { + result.Filename = firstNonEmptyString(m, "name", "filename", "file_name") + } + if result.Status == "uploaded" { + if status := firstNonEmptyString(m, "status", "file_status"); status != "" { + result.Status = status + } + } + if !result.IsImage { + result.IsImage = firstBool(m, "is_image", "isImage") + } + if result.Purpose == "" { + result.Purpose = firstNonEmptyString(m, "purpose") + } + if result.AccountID == "" { + result.AccountID = firstNonEmptyString(m, "account_id", "accountId", "owner_account_id", "ownerAccountId") + } + if result.Bytes == 0 { + result.Bytes = firstPositiveInt64(m, "bytes", "size", "file_size") + } + } + return result +} + +func firstBool(m map[string]any, keys ...string) bool { + for _, key := range keys { + switch v := m[key].(type) { + case bool: + return v + case string: + switch strings.ToLower(strings.TrimSpace(v)) { + case "true", "1", "yes", "y": + return true + } + } + } + return false +} + +func firstNonEmptyString(m map[string]any, keys ...string) string { + for _, key := range keys { + if v, _ := m[key].(string); strings.TrimSpace(v) != "" { + return strings.TrimSpace(v) + } + } + return "" +} + +func firstPositiveInt64(m map[string]any, keys ...string) int64 { + for _, key := range keys { + if v := toInt64(m[key], 0); v > 0 { + return v + } + } + return 0 +} diff --git a/internal/deepseek/client/client_upload_test.go b/internal/deepseek/client/client_upload_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ff547da3e41924018db525a1bd7c5350a378a96a --- /dev/null +++ b/internal/deepseek/client/client_upload_test.go @@ -0,0 +1,249 @@ +package client + +import ( + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "io" + "net/http" + "strings" + "testing" + "time" + + "ds2api/internal/auth" + powpkg "ds2api/pow" +) + +func TestBuildUploadMultipartBodyOmitsPurposeAndIncludesFilePart(t *testing.T) { + body, contentType, err := buildUploadMultipartBody(`../demo.txt`, "text/plain", []byte("hello")) + if err != nil { + t.Fatalf("buildUploadMultipartBody error: %v", err) + } + if !strings.HasPrefix(contentType, "multipart/form-data; boundary=") { + t.Fatalf("unexpected content type: %q", contentType) + } + payload := string(body) + if strings.Contains(payload, `name="purpose"`) || strings.Contains(payload, "assistants") { + t.Fatalf("expected purpose to be omitted from payload: %q", payload) + } + if !strings.Contains(payload, `name="file"; filename="demo.txt"`) { + t.Fatalf("expected sanitized filename in payload: %q", payload) + } + if !strings.Contains(payload, "Content-Type: text/plain") { + t.Fatalf("expected file content type in payload: %q", payload) + } + if !strings.Contains(payload, "hello") { + t.Fatalf("expected file content in payload: %q", payload) + } +} + +func TestDoUploadDoesNotFallbackForNonIdempotentUpload(t *testing.T) { + var fallbackCalled bool + client := &Client{} + _, err := client.doUpload( + context.Background(), + doerFunc(func(req *http.Request) (*http.Response, error) { + _, _ = io.ReadAll(req.Body) + return nil, errors.New("ambiguous upload write failure") + }), + doerFunc(func(*http.Request) (*http.Response, error) { + fallbackCalled = true + return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader("{}"))}, nil + }), + dsprotocol.DeepSeekUploadFileURL, + map[string]string{"Content-Type": "multipart/form-data"}, + []byte("body"), + ) + if err == nil { + t.Fatal("expected upload error") + } + if fallbackCalled { + t.Fatal("upload fallback should not be called for a non-idempotent request") + } +} + +func TestExtractUploadFileResultSupportsNestedShapes(t *testing.T) { + got := extractUploadFileResult(map[string]any{ + "data": map[string]any{ + "biz_data": map[string]any{ + "file": map[string]any{ + "file_id": "file_123", + "file_name": "report.pdf", + "file_size": 99, + "status": "processed", + "purpose": "assistants", + "is_image": true, + }, + }, + }, + }) + if got.ID != "file_123" { + t.Fatalf("expected id file_123, got %#v", got) + } + if got.Filename != "report.pdf" { + t.Fatalf("expected filename report.pdf, got %#v", got) + } + if got.Bytes != 99 { + t.Fatalf("expected bytes 99, got %#v", got) + } + if got.Status != "processed" { + t.Fatalf("expected status processed, got %#v", got) + } + if got.Purpose != "assistants" { + t.Fatalf("expected purpose assistants, got %#v", got) + } + if !got.IsImage { + t.Fatalf("expected image flag true, got %#v", got) + } +} + +func TestUploadFileUsesUploadTargetPowAndMultipartHeaders(t *testing.T) { + challengeHash := powpkg.DeepSeekHashV1([]byte(powpkg.BuildPrefix("salt", 1712345678) + "42")) + powResponse := `{"code":0,"msg":"ok","data":{"biz_code":0,"biz_data":{"challenge":{"algorithm":"DeepSeekHashV1","challenge":"` + hex.EncodeToString(challengeHash[:]) + `","salt":"salt","expire_at":1712345678,"difficulty":1000,"signature":"sig","target_path":"` + dsprotocol.DeepSeekUploadTargetPath + `"}}}}` + uploadResponse := `{"code":0,"msg":"ok","data":{"biz_code":0,"biz_data":{"file":{"file_id":"file_789","filename":"demo.txt","bytes":5,"status":"processed","purpose":"assistants","is_image":false}}}}` + var seenPow string + var seenTargetPath string + var seenContentType string + var seenFileSize string + var seenModelType string + var seenBody string + call := 0 + client := &Client{ + regular: doerFunc(func(req *http.Request) (*http.Response, error) { + call++ + bodyBytes, _ := io.ReadAll(req.Body) + switch call { + case 1: + seenTargetPath = string(bodyBytes) + return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(powResponse)), Request: req}, nil + case 2: + seenPow = req.Header.Get("x-ds-pow-response") + seenContentType = req.Header.Get("Content-Type") + seenFileSize = req.Header.Get("x-file-size") + seenModelType = req.Header.Get("x-model-type") + seenBody = string(bodyBytes) + return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(uploadResponse)), Request: req}, nil + default: + t.Fatalf("unexpected request count %d", call) + return nil, nil + } + }), + fallback: &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) { + return nil, nil + })}, + maxRetries: 1, + } + result, err := client.UploadFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token", TriedAccounts: map[string]bool{}}, UploadFileRequest{ + Filename: "demo.txt", + ContentType: "text/plain", + Purpose: "assistants", + ModelType: "vision", + Data: []byte("hello"), + }, 1) + if err != nil { + t.Fatalf("UploadFile error: %v", err) + } + if result.ID != "file_789" { + t.Fatalf("expected uploaded file id file_789, got %#v", result) + } + if !strings.Contains(seenTargetPath, `"target_path":"`+dsprotocol.DeepSeekUploadTargetPath+`"`) { + t.Fatalf("expected upload target_path in pow request, got %q", seenTargetPath) + } + if strings.TrimSpace(seenPow) == "" { + t.Fatal("expected x-ds-pow-response header") + } + rawPow, err := base64.StdEncoding.DecodeString(seenPow) + if err != nil { + t.Fatalf("decode pow header failed: %v", err) + } + var powHeader map[string]any + if err := json.Unmarshal(rawPow, &powHeader); err != nil { + t.Fatalf("unmarshal pow header failed: %v", err) + } + if powHeader["target_path"] != dsprotocol.DeepSeekUploadTargetPath { + t.Fatalf("expected pow target_path %q, got %#v", dsprotocol.DeepSeekUploadTargetPath, powHeader["target_path"]) + } + if seenFileSize != "5" { + t.Fatalf("expected x-file-size=5, got %q", seenFileSize) + } + if seenModelType != "vision" { + t.Fatalf("expected x-model-type=vision, got %q", seenModelType) + } + if !strings.HasPrefix(seenContentType, "multipart/form-data; boundary=") { + t.Fatalf("expected multipart content type, got %q", seenContentType) + } + if !strings.Contains(seenBody, `name="file"; filename="demo.txt"`) { + t.Fatalf("expected file part in upload body: %q", seenBody) + } +} + +func TestUploadFileWaitsForProcessedFetchFiles(t *testing.T) { + oldSleep := fileReadySleep + fileReadySleep = func(time.Duration) {} + defer func() { fileReadySleep = oldSleep }() + + challengeHash := powpkg.DeepSeekHashV1([]byte(powpkg.BuildPrefix("salt", 1712345678) + "42")) + powResponse := `{"code":0,"msg":"ok","data":{"biz_code":0,"biz_data":{"challenge":{"algorithm":"DeepSeekHashV1","challenge":"` + hex.EncodeToString(challengeHash[:]) + `","salt":"salt","expire_at":1712345678,"difficulty":1000,"signature":"sig","target_path":"` + dsprotocol.DeepSeekUploadTargetPath + `"}}}}` + uploadResponse := `{"code":0,"msg":"ok","data":{"biz_code":0,"biz_data":{"file":{"file_id":"file_789","filename":"demo.txt","bytes":5,"status":"PENDING","purpose":"assistants","is_image":false}}}}` + pendingFetchResponse := `{"code":0,"msg":"ok","data":{"biz_code":0,"biz_data":{"files":[{"file_id":"file_789","filename":"demo.txt","bytes":5,"status":"PENDING","purpose":"assistants","is_image":false}]}}}` + processedFetchResponse := `{"code":0,"msg":"ok","data":{"biz_code":0,"biz_data":{"files":[{"file_id":"file_789","filename":"demo.txt","bytes":5,"status":"processed","purpose":"assistants","is_image":true}]}}}` + + var call int + client := &Client{ + regular: doerFunc(func(req *http.Request) (*http.Response, error) { + call++ + switch call { + case 1: + bodyBytes, _ := io.ReadAll(req.Body) + if !strings.Contains(string(bodyBytes), `"target_path":"`+dsprotocol.DeepSeekUploadTargetPath+`"`) { + t.Fatalf("expected pow target path request, got %s", string(bodyBytes)) + } + return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(powResponse)), Request: req}, nil + case 2: + return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(uploadResponse)), Request: req}, nil + case 3, 4: + if req.Method != http.MethodGet { + t.Fatalf("expected GET fetch request, got %s", req.Method) + } + if req.URL.Path != "/api/v0/file/fetch_files" { + t.Fatalf("expected fetch files path /api/v0/file/fetch_files, got %q", req.URL.Path) + } + if got := req.URL.Query().Get("file_ids"); got != "file_789" { + t.Fatalf("expected file_ids=file_789, got %q", got) + } + respBody := pendingFetchResponse + if call == 4 { + respBody = processedFetchResponse + } + return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(respBody)), Request: req}, nil + default: + t.Fatalf("unexpected request count %d", call) + return nil, nil + } + }), + fallback: &http.Client{Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) { return nil, nil })}, + maxRetries: 1, + } + + result, err := client.UploadFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token", TriedAccounts: map[string]bool{}}, UploadFileRequest{ + Filename: "demo.txt", + ContentType: "text/plain", + Purpose: "assistants", + Data: []byte("hello"), + }, 1) + if err != nil { + t.Fatalf("UploadFile error: %v", err) + } + if result.ID != "file_789" { + t.Fatalf("expected uploaded file id file_789, got %#v", result) + } + if result.Status != "processed" { + t.Fatalf("expected final status processed, got %#v", result.Status) + } + if call != 4 { + t.Fatalf("expected 4 requests, got %d", call) + } +} diff --git a/internal/deepseek/client/deepseek_edge_test.go b/internal/deepseek/client/deepseek_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..fb0b41333fe8be69b248d5511cbff4ef639fa4a3 --- /dev/null +++ b/internal/deepseek/client/deepseek_edge_test.go @@ -0,0 +1,125 @@ +package client + +import ( + "context" + "testing" +) + +// ─── toFloat64 edge cases ──────────────────────────────────────────── + +func TestToFloat64FromFloat64(t *testing.T) { + if got := toFloat64(float64(3.14), 0); got != 3.14 { + t.Fatalf("expected 3.14, got %f", got) + } +} + +func TestToFloat64FromInt(t *testing.T) { + if got := toFloat64(42, 0); got != 42.0 { + t.Fatalf("expected 42.0, got %f", got) + } +} + +func TestToFloat64FromInt64(t *testing.T) { + if got := toFloat64(int64(100), 0); got != 100.0 { + t.Fatalf("expected 100.0, got %f", got) + } +} + +func TestToFloat64FromStringDefault(t *testing.T) { + if got := toFloat64("42", 99.0); got != 99.0 { + t.Fatalf("expected default 99.0, got %f", got) + } +} + +func TestToFloat64FromNilDefault(t *testing.T) { + if got := toFloat64(nil, 5.5); got != 5.5 { + t.Fatalf("expected default 5.5, got %f", got) + } +} + +func TestToFloat64FromBoolDefault(t *testing.T) { + if got := toFloat64(true, 1.0); got != 1.0 { + t.Fatalf("expected default 1.0, got %f", got) + } +} + +// ─── toInt64 edge cases ────────────────────────────────────────────── + +func TestToInt64FromFloat64(t *testing.T) { + if got := toInt64(float64(42.9), 0); got != 42 { + t.Fatalf("expected 42, got %d", got) + } +} + +func TestToInt64FromInt(t *testing.T) { + if got := toInt64(42, 0); got != 42 { + t.Fatalf("expected 42, got %d", got) + } +} + +func TestToInt64FromInt64(t *testing.T) { + if got := toInt64(int64(100), 0); got != 100 { + t.Fatalf("expected 100, got %d", got) + } +} + +func TestToInt64FromStringDefault(t *testing.T) { + if got := toInt64("42", 99); got != 99 { + t.Fatalf("expected default 99, got %d", got) + } +} + +func TestToInt64FromNilDefault(t *testing.T) { + if got := toInt64(nil, 7); got != 7 { + t.Fatalf("expected default 7, got %d", got) + } +} + +// ─── BuildPowHeader edge cases ─────────────────────────────────────── + +func TestBuildPowHeaderBasicChallenge(t *testing.T) { + challenge := map[string]any{ + "algorithm": "DeepSeekHashV1", + "challenge": "abc123", + "salt": "salt456", + "signature": "sig789", + "target_path": "/path", + } + result, err := BuildPowHeader(challenge, 42) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + if result == "" { + t.Fatal("expected non-empty result") + } +} + +func TestBuildPowHeaderEmptyChallenge(t *testing.T) { + result, err := BuildPowHeader(map[string]any{}, 0) + if err != nil { + t.Fatalf("unexpected error: %v", err) + } + // Should produce a base64 encoded JSON with nil values + if result == "" { + t.Fatal("expected non-empty result for empty challenge") + } +} + +// ─── NewClient ─────────────────────────────────────────────────────── + +func TestNewClientInitialState(t *testing.T) { + client := NewClient(nil, nil) + if client == nil { + t.Fatal("expected non-nil client") + } +} + +func TestNewClientPreloadPowIdempotent(t *testing.T) { + client := NewClient(nil, nil) + if err := client.PreloadPow(context.Background()); err != nil { + t.Fatalf("first preload failed: %v", err) + } + if err := client.PreloadPow(context.Background()); err != nil { + t.Fatalf("second preload failed: %v", err) + } +} diff --git a/internal/deepseek/client/errors.go b/internal/deepseek/client/errors.go new file mode 100644 index 0000000000000000000000000000000000000000..0c2c18a2473e954f0f964c50dc7643959d439110 --- /dev/null +++ b/internal/deepseek/client/errors.go @@ -0,0 +1,46 @@ +package client + +import ( + "errors" + "fmt" +) + +type FailureKind string + +const ( + FailureUnknown FailureKind = "" + FailureDirectUnauthorized FailureKind = "direct_unauthorized" + FailureManagedUnauthorized FailureKind = "managed_unauthorized" +) + +type RequestFailure struct { + Op string + Kind FailureKind + Message string +} + +func (e *RequestFailure) Error() string { + if e == nil { + return "" + } + switch { + case e.Op != "" && e.Message != "": + return fmt.Sprintf("%s: %s", e.Op, e.Message) + case e.Op != "": + return e.Op + " failed" + case e.Message != "": + return e.Message + default: + return "request failed" + } +} + +func IsManagedUnauthorizedError(err error) bool { + var failure *RequestFailure + return errors.As(err, &failure) && failure.Kind == FailureManagedUnauthorized +} + +func IsDirectUnauthorizedError(err error) bool { + var failure *RequestFailure + return errors.As(err, &failure) && failure.Kind == FailureDirectUnauthorized +} diff --git a/internal/deepseek/client/pow.go b/internal/deepseek/client/pow.go new file mode 100644 index 0000000000000000000000000000000000000000..6a58fe1fdc0c76b06a6771d23e43a6bfe3ac75ad --- /dev/null +++ b/internal/deepseek/client/pow.go @@ -0,0 +1,72 @@ +package client + +import ( + "context" + "encoding/base64" + "encoding/json" + "errors" + + "ds2api/pow" +) + +// ComputePow 使用纯 Go 实现求解 PoW challenge (DeepSeekHashV1)。 +func ComputePow(ctx context.Context, challenge map[string]any) (int64, error) { + algo, _ := challenge["algorithm"].(string) + if algo != "DeepSeekHashV1" { + return 0, errors.New("unsupported algorithm") + } + challengeStr, _ := challenge["challenge"].(string) + salt, _ := challenge["salt"].(string) + expireAt := toInt64(challenge["expire_at"], 1680000000) + difficulty := toInt64FromFloat(challenge["difficulty"], 144000) + + return pow.SolvePow(ctx, challengeStr, salt, expireAt, difficulty) +} + +// BuildPowHeader 序列化 {algorithm,challenge,salt,answer,signature,target_path} 为 base64(JSON)。 +func BuildPowHeader(challenge map[string]any, answer int64) (string, error) { + payload := map[string]any{ + "algorithm": challenge["algorithm"], + "challenge": challenge["challenge"], + "salt": challenge["salt"], + "answer": answer, + "signature": challenge["signature"], + "target_path": challenge["target_path"], + } + b, err := json.Marshal(payload) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(b), nil +} + +func toFloat64(v any, d float64) float64 { + switch n := v.(type) { + case float64: + return n + case int: + return float64(n) + case int64: + return float64(n) + default: + return d + } +} + +func toInt64(v any, d int64) int64 { + switch n := v.(type) { + case float64: + return int64(n) + case int: + return int64(n) + case int64: + return n + default: + return d + } +} + +// toInt64FromFloat 与 toInt64 等价,仅名称区分用途。 +func toInt64FromFloat(v any, d int64) int64 { + return toInt64(v, d) +} diff --git a/internal/deepseek/client/pow_test.go b/internal/deepseek/client/pow_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5367e0a2417ddc4348657d7ad38a6103cbc284f2 --- /dev/null +++ b/internal/deepseek/client/pow_test.go @@ -0,0 +1,20 @@ +package client + +import ( + "context" + "testing" +) + +func TestPreloadPowNoOp(t *testing.T) { + client := NewClient(nil, nil) + if err := client.PreloadPow(context.Background()); err != nil { + t.Fatalf("PreloadPow should be no-op, got error: %v", err) + } +} + +func TestComputePowUnsupportedAlgorithm(t *testing.T) { + _, err := ComputePow(context.Background(), map[string]any{"algorithm": "unknown"}) + if err == nil { + t.Fatal("expected error for unsupported algorithm") + } +} diff --git a/internal/deepseek/client/proxy.go b/internal/deepseek/client/proxy.go new file mode 100644 index 0000000000000000000000000000000000000000..7e1dfa8d9884cff388ab6f103dbd1896c49b2878 --- /dev/null +++ b/internal/deepseek/client/proxy.go @@ -0,0 +1,244 @@ +package client + +import ( + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "fmt" + "net" + "net/http" + "strconv" + "strings" + "time" + + "golang.org/x/net/proxy" + + "ds2api/internal/auth" + "ds2api/internal/config" + trans "ds2api/internal/deepseek/transport" +) + +type requestClients struct { + regular trans.Doer + stream trans.Doer + fallback *http.Client + fallbackS *http.Client +} + +type hostLookupFunc func(ctx context.Context, network, host string) ([]string, error) + +var proxyConnectivityTestURL = "https://chat.deepseek.com/" + +var defaultHostLookup hostLookupFunc = func(ctx context.Context, _ string, host string) ([]string, error) { + return net.DefaultResolver.LookupHost(ctx, host) +} + +func proxyDialAddress(ctx context.Context, proxyType, address string, lookup hostLookupFunc) (string, error) { + proxyType = strings.ToLower(strings.TrimSpace(proxyType)) + if proxyType != "socks5" { + return address, nil + } + host, port, err := net.SplitHostPort(address) + if err != nil { + return "", err + } + if net.ParseIP(host) != nil { + return address, nil + } + if lookup == nil { + lookup = defaultHostLookup + } + addrs, err := lookup(ctx, "ip", host) + if err != nil { + return "", err + } + if len(addrs) == 0 { + return "", fmt.Errorf("no ip address resolved for %s", host) + } + return net.JoinHostPort(addrs[0], port), nil +} + +func proxyCacheKey(proxyCfg config.Proxy) string { + proxyCfg = config.NormalizeProxy(proxyCfg) + return strings.Join([]string{ + proxyCfg.ID, + proxyCfg.Type, + strings.ToLower(proxyCfg.Host), + strconv.Itoa(proxyCfg.Port), + proxyCfg.Username, + proxyCfg.Password, + }, "|") +} + +func proxyDialContext(proxyCfg config.Proxy) (trans.DialContextFunc, error) { + proxyCfg = config.NormalizeProxy(proxyCfg) + var authCfg *proxy.Auth + if proxyCfg.Username != "" || proxyCfg.Password != "" { + authCfg = &proxy.Auth{User: proxyCfg.Username, Password: proxyCfg.Password} + } + forward := &net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second} + dialer, err := proxy.SOCKS5("tcp", net.JoinHostPort(proxyCfg.Host, strconv.Itoa(proxyCfg.Port)), authCfg, forward) + if err != nil { + return nil, err + } + return func(ctx context.Context, network, address string) (net.Conn, error) { + target, err := proxyDialAddress(ctx, proxyCfg.Type, address, defaultHostLookup) + if err != nil { + return nil, err + } + if ctxDialer, ok := dialer.(proxy.ContextDialer); ok { + return ctxDialer.DialContext(ctx, network, target) + } + return dialer.Dial(network, target) + }, nil +} + +func (c *Client) defaultRequestClients() requestClients { + return requestClients{ + regular: c.regular, + stream: c.stream, + fallback: c.fallback, + fallbackS: c.fallbackS, + } +} + +func (c *Client) resolveProxyForAccount(acc config.Account) (config.Proxy, bool) { + if c == nil || c.Store == nil { + return config.Proxy{}, false + } + proxyID := strings.TrimSpace(acc.ProxyID) + if proxyID == "" { + return config.Proxy{}, false + } + snap := c.Store.Snapshot() + for _, proxyCfg := range snap.Proxies { + proxyCfg = config.NormalizeProxy(proxyCfg) + if proxyCfg.ID == proxyID { + if proxyCfg.Disabled { + config.Logger.Warn("[proxy] skipping disabled proxy", "proxy_id", proxyCfg.ID) + return config.Proxy{}, false + } + return proxyCfg, true + } + } + return config.Proxy{}, false +} + +func (c *Client) requestClientsFromContext(ctx context.Context) requestClients { + if a, ok := auth.FromContext(ctx); ok { + return c.requestClientsForAccount(a.Account) + } + return c.defaultRequestClients() +} + +func (c *Client) requestClientsForAuth(ctx context.Context, a *auth.RequestAuth) requestClients { + if a != nil { + return c.requestClientsForAccount(a.Account) + } + return c.requestClientsFromContext(ctx) +} + +func (c *Client) requestClientsForAccount(acc config.Account) requestClients { + proxyCfg, ok := c.resolveProxyForAccount(acc) + if !ok { + return c.defaultRequestClients() + } + + key := proxyCacheKey(proxyCfg) + c.proxyClientsMu.RLock() + cached, ok := c.proxyClients[key] + c.proxyClientsMu.RUnlock() + if ok { + return cached + } + + dialContext, err := proxyDialContext(proxyCfg) + if err != nil { + config.Logger.Warn("[proxy] build dialer failed", "proxy_id", proxyCfg.ID, "error", err) + return c.defaultRequestClients() + } + + bundle := requestClients{ + regular: trans.NewWithDialContext(60*time.Second, dialContext), + stream: trans.NewWithDialContext(0, dialContext), + fallback: trans.NewFallbackClient(60*time.Second, dialContext), + fallbackS: trans.NewFallbackClient(0, dialContext), + } + + c.proxyClientsMu.Lock() + if c.proxyClients == nil { + c.proxyClients = make(map[string]requestClients) + } + c.proxyClients[key] = bundle + c.proxyClientsMu.Unlock() + return bundle +} + +func applyProxyConnectivityHeaders(req *http.Request) { + if req == nil { + return + } + for key, value := range dsprotocol.BaseHeaders { + key = strings.TrimSpace(key) + value = strings.TrimSpace(value) + if key == "" || value == "" { + continue + } + req.Header.Set(key, value) + } +} + +func proxyConnectivityStatus(statusCode int) (bool, string) { + switch { + case statusCode >= 200 && statusCode < 300: + return true, fmt.Sprintf("代理可达,目标返回 HTTP %d", statusCode) + case statusCode >= 300 && statusCode < 500: + return true, fmt.Sprintf("代理可达,但目标返回 HTTP %d(可能是风控或挑战)", statusCode) + default: + return false, fmt.Sprintf("目标返回 HTTP %d", statusCode) + } +} + +func TestProxyConnectivity(ctx context.Context, proxyCfg config.Proxy) map[string]any { + start := time.Now() + proxyCfg = config.NormalizeProxy(proxyCfg) + result := map[string]any{ + "success": false, + "proxy_id": proxyCfg.ID, + "proxy_type": proxyCfg.Type, + "response_time": 0, + } + + if err := config.ValidateProxyConfig([]config.Proxy{proxyCfg}); err != nil { + result["message"] = "代理配置无效: " + err.Error() + return result + } + dialContext, err := proxyDialContext(proxyCfg) + if err != nil { + result["message"] = "代理拨号器初始化失败: " + err.Error() + return result + } + + client := trans.NewFallbackClient(15*time.Second, dialContext) + req, err := http.NewRequestWithContext(ctx, http.MethodGet, proxyConnectivityTestURL, nil) + if err != nil { + result["message"] = err.Error() + return result + } + applyProxyConnectivityHeaders(req) + + resp, err := client.Do(req) + result["response_time"] = int(time.Since(start).Milliseconds()) + if err != nil { + result["message"] = err.Error() + return result + } + defer func() { + if closeErr := resp.Body.Close(); closeErr != nil { + config.Logger.Warn("[proxy] close response body failed", "proxy_id", proxyCfg.ID, "error", closeErr) + } + }() + + result["status_code"] = resp.StatusCode + result["success"], result["message"] = proxyConnectivityStatus(resp.StatusCode) + return result +} diff --git a/internal/deepseek/client/proxy_test.go b/internal/deepseek/client/proxy_test.go new file mode 100644 index 0000000000000000000000000000000000000000..cbb931dc2541f09b40650437d9535b3eb9f07ea7 --- /dev/null +++ b/internal/deepseek/client/proxy_test.go @@ -0,0 +1,86 @@ +package client + +import ( + "context" + dsprotocol "ds2api/internal/deepseek/protocol" + "net/http" + "strings" + "testing" +) + +func TestProxyDialAddressUsesLocalResolutionForSocks5(t *testing.T) { + ctx := context.Background() + resolved, err := proxyDialAddress(ctx, "socks5", "example.com:443", func(_ context.Context, network, host string) ([]string, error) { + if network != "ip" { + t.Fatalf("unexpected lookup network: %q", network) + } + if host != "example.com" { + t.Fatalf("unexpected lookup host: %q", host) + } + return []string{"203.0.113.10"}, nil + }) + if err != nil { + t.Fatalf("proxyDialAddress returned error: %v", err) + } + if resolved != "203.0.113.10:443" { + t.Fatalf("expected locally resolved address, got %q", resolved) + } +} + +func TestProxyDialAddressKeepsHostnameForSocks5h(t *testing.T) { + ctx := context.Background() + lookups := 0 + resolved, err := proxyDialAddress(ctx, "socks5h", "example.com:443", func(_ context.Context, network, host string) ([]string, error) { + lookups++ + return []string{"203.0.113.10"}, nil + }) + if err != nil { + t.Fatalf("proxyDialAddress returned error: %v", err) + } + if resolved != "example.com:443" { + t.Fatalf("expected hostname preserved for remote DNS, got %q", resolved) + } + if lookups != 0 { + t.Fatalf("expected no local DNS lookup for socks5h, got %d", lookups) + } +} + +func TestApplyProxyConnectivityHeadersUsesBaseHeaders(t *testing.T) { + req, err := http.NewRequest(http.MethodGet, "https://chat.deepseek.com/", nil) + if err != nil { + t.Fatalf("http.NewRequest returned error: %v", err) + } + + applyProxyConnectivityHeaders(req) + + for key, want := range dsprotocol.BaseHeaders { + if got := req.Header.Get(key); got != want { + t.Fatalf("expected header %q=%q, got %q", key, want, got) + } + } +} + +func TestProxyConnectivityStatus(t *testing.T) { + cases := []struct { + name string + statusCode int + success bool + wantText string + }{ + {name: "ok", statusCode: 200, success: true, wantText: "HTTP 200"}, + {name: "challenge", statusCode: 403, success: true, wantText: "风控或挑战"}, + {name: "upstream error", statusCode: 502, success: false, wantText: "HTTP 502"}, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + success, message := proxyConnectivityStatus(tc.statusCode) + if success != tc.success { + t.Fatalf("expected success=%v, got %v", tc.success, success) + } + if message == "" || !strings.Contains(message, tc.wantText) { + t.Fatalf("expected message to contain %q, got %q", tc.wantText, message) + } + }) + } +} diff --git a/internal/deepseek/protocol/constants.go b/internal/deepseek/protocol/constants.go new file mode 100644 index 0000000000000000000000000000000000000000..83daa31962b2b08eb609e177a8e91dc56794a02c --- /dev/null +++ b/internal/deepseek/protocol/constants.go @@ -0,0 +1,164 @@ +package protocol + +import ( + _ "embed" + "encoding/json" + "fmt" +) + +const ( + DeepSeekHost = "chat.deepseek.com" + DeepSeekLoginURL = "https://chat.deepseek.com/api/v0/users/login" + DeepSeekCreateSessionURL = "https://chat.deepseek.com/api/v0/chat_session/create" + DeepSeekCreatePowURL = "https://chat.deepseek.com/api/v0/chat/create_pow_challenge" + DeepSeekCompletionURL = "https://chat.deepseek.com/api/v0/chat/completion" + DeepSeekContinueURL = "https://chat.deepseek.com/api/v0/chat/continue" + DeepSeekUploadFileURL = "https://chat.deepseek.com/api/v0/file/upload_file" + DeepSeekFetchFilesURL = "https://chat.deepseek.com/api/v0/file/fetch_files" + DeepSeekFetchSessionURL = "https://chat.deepseek.com/api/v0/chat_session/fetch_page" + DeepSeekDeleteSessionURL = "https://chat.deepseek.com/api/v0/chat_session/delete" + DeepSeekDeleteAllSessionsURL = "https://chat.deepseek.com/api/v0/chat_session/delete_all" + DeepSeekCompletionTargetPath = "/api/v0/chat/completion" + DeepSeekUploadTargetPath = "/api/v0/file/upload_file" +) + +var defaultStaticBaseHeaders = map[string]string{ + "Host": "chat.deepseek.com", + "Accept": "application/json", + "Content-Type": "application/json", + "accept-charset": "UTF-8", +} + +var defaultSkipContainsPatterns = []string{ + "quasi_status", + "elapsed_secs", + "token_usage", + "pending_fragment", + "conversation_mode", + "fragments/-1/status", + "fragments/-2/status", + "fragments/-3/status", +} + +var defaultSkipExactPaths = []string{ + "response/search_status", +} + +var ClientVersion string +var BaseHeaders = map[string]string{} +var SkipContainsPatterns = cloneStringSlice(defaultSkipContainsPatterns) +var SkipExactPathSet = toStringSet(defaultSkipExactPaths) + +type clientConstants struct { + Name string `json:"name"` + Platform string `json:"platform"` + Version string `json:"version"` + AndroidAPILevel string `json:"android_api_level"` + Locale string `json:"locale"` +} + +type sharedConstants struct { + Client clientConstants `json:"client"` + BaseHeaders map[string]string `json:"base_headers"` + SkipContainsPattern []string `json:"skip_contains_patterns"` + SkipExactPaths []string `json:"skip_exact_paths"` +} + +//go:embed constants_shared.json +var sharedConstantsJSON []byte + +func init() { + cfg := sharedConstants{} + if err := json.Unmarshal(sharedConstantsJSON, &cfg); err != nil { + panic(fmt.Errorf("load DeepSeek shared constants: %w", err)) + } + applySharedConstants(cfg) +} + +func applySharedConstants(cfg sharedConstants) { + client := normalizeClientConstants(cfg.Client) + ClientVersion = client.Version + BaseHeaders = buildBaseHeaders(client, cfg.BaseHeaders) + SkipContainsPatterns = cloneStringSlice(defaultSkipContainsPatterns) + if len(cfg.SkipContainsPattern) > 0 { + SkipContainsPatterns = cloneStringSlice(cfg.SkipContainsPattern) + } + SkipExactPathSet = toStringSet(defaultSkipExactPaths) + if len(cfg.SkipExactPaths) > 0 { + SkipExactPathSet = toStringSet(cfg.SkipExactPaths) + } +} + +func normalizeClientConstants(in clientConstants) clientConstants { + if in.Name == "" { + in.Name = "DeepSeek" + } + if in.Platform == "" { + in.Platform = "android" + } + if in.AndroidAPILevel == "" { + in.AndroidAPILevel = "35" + } + if in.Locale == "" { + in.Locale = "zh_CN" + } + return in +} + +func buildBaseHeaders(client clientConstants, overrides map[string]string) map[string]string { + out := cloneStringMap(defaultStaticBaseHeaders) + for k, v := range overrides { + if k == "" || v == "" { + continue + } + out[k] = v + } + if client.Name != "" && client.Version != "" { + userAgent := client.Name + "/" + client.Version + if client.Platform == "android" && client.AndroidAPILevel != "" { + userAgent += " Android/" + client.AndroidAPILevel + } + out["User-Agent"] = userAgent + } + if client.Platform != "" { + out["x-client-platform"] = client.Platform + } + if client.Version != "" { + out["x-client-version"] = client.Version + } + if client.Locale != "" { + out["x-client-locale"] = client.Locale + } + return out +} + +func cloneStringMap(in map[string]string) map[string]string { + out := make(map[string]string, len(in)) + for k, v := range in { + out[k] = v + } + return out +} + +func cloneStringSlice(in []string) []string { + out := make([]string, len(in)) + copy(out, in) + return out +} + +func toStringSet(in []string) map[string]struct{} { + out := make(map[string]struct{}, len(in)) + for _, v := range in { + if v == "" { + continue + } + out[v] = struct{}{} + } + return out +} + +const ( + KeepAliveTimeout = 5 + StreamIdleTimeout = 300 + MaxKeepaliveCount = 40 +) diff --git a/internal/deepseek/protocol/constants_shared.json b/internal/deepseek/protocol/constants_shared.json new file mode 100644 index 0000000000000000000000000000000000000000..390104ede47174019dcbcded2c254a9b7f861124 --- /dev/null +++ b/internal/deepseek/protocol/constants_shared.json @@ -0,0 +1,31 @@ +{ + "client": { + "name": "DeepSeek", + "platform": "android", + "version": "2.1.2", + "android_api_level": "32", + "locale": "zh_CN" + }, + "base_headers": { + "Host": "chat.deepseek.com", + "Accept": "application/json", + "Content-Type": "application/json", + "accept-encoding": "gzip", + "accept-charset": "UTF-8", + "x-client-bundle-id": "com.deepseek.chat", + "x-rangers-id": "7639874692114816004", + "x-client-timezone-offset": "28800" + }, + "skip_contains_patterns": [ + "quasi_status", + "elapsed_secs", + "pending_fragment", + "conversation_mode", + "fragments/-1/status", + "fragments/-2/status", + "fragments/-3/status" + ], + "skip_exact_paths": [ + "response/search_status" + ] +} \ No newline at end of file diff --git a/internal/deepseek/protocol/constants_test.go b/internal/deepseek/protocol/constants_test.go new file mode 100644 index 0000000000000000000000000000000000000000..1f278f1254e2624b964265bfab0cf8f859c44627 --- /dev/null +++ b/internal/deepseek/protocol/constants_test.go @@ -0,0 +1,56 @@ +package protocol + +import ( + "encoding/json" + "testing" +) + +func TestSharedConstantsLoaded(t *testing.T) { + cfg := sharedConstants{} + if err := json.Unmarshal(sharedConstantsJSON, &cfg); err != nil { + t.Fatalf("failed to parse shared constants: %v", err) + } + client := normalizeClientConstants(cfg.Client) + if ClientVersion != client.Version { + t.Fatalf("unexpected client version=%q", ClientVersion) + } + wantUserAgent := client.Name + "/" + client.Version + " Android/" + client.AndroidAPILevel + if BaseHeaders["User-Agent"] != wantUserAgent { + t.Fatalf("unexpected user agent=%q", BaseHeaders["User-Agent"]) + } + if BaseHeaders["x-client-platform"] != "android" { + t.Fatalf("unexpected base header x-client-platform=%q", BaseHeaders["x-client-platform"]) + } + if BaseHeaders["x-client-version"] != ClientVersion { + t.Fatalf("unexpected base header x-client-version=%q", BaseHeaders["x-client-version"]) + } + if BaseHeaders["Content-Type"] != "application/json" { + t.Fatalf("unexpected base header Content-Type=%q", BaseHeaders["Content-Type"]) + } + if len(SkipContainsPatterns) == 0 { + t.Fatal("expected skip contains patterns to be loaded") + } + if _, ok := SkipExactPathSet["response/search_status"]; !ok { + t.Fatal("expected response/search_status in exact skip path set") + } +} + +func TestClientHeadersDerivedFromSharedVersion(t *testing.T) { + client := normalizeClientConstants(clientConstants{ + Name: "DeepSeek", + Platform: "android", + Version: "9.8.7", + AndroidAPILevel: "35", + Locale: "zh_CN", + }) + headers := buildBaseHeaders(client, map[string]string{ + "User-Agent": "stale", + "x-client-version": "stale", + }) + if headers["User-Agent"] != "DeepSeek/9.8.7 Android/35" { + t.Fatalf("unexpected derived user agent=%q", headers["User-Agent"]) + } + if headers["x-client-version"] != "9.8.7" { + t.Fatalf("unexpected derived client version=%q", headers["x-client-version"]) + } +} diff --git a/internal/deepseek/protocol/sse.go b/internal/deepseek/protocol/sse.go new file mode 100644 index 0000000000000000000000000000000000000000..af942aa93948da3aa5aa1e2e5447c90034869959 --- /dev/null +++ b/internal/deepseek/protocol/sse.go @@ -0,0 +1,25 @@ +package protocol + +import ( + "bufio" + "io" + "net/http" +) + +func ScanSSELines(resp *http.Response, onLine func([]byte) bool) error { + reader := bufio.NewReaderSize(resp.Body, 64*1024) + for { + line, err := reader.ReadBytes('\n') + if len(line) > 0 { + if !onLine(line) { + return nil + } + } + if err != nil { + if err == io.EOF { + return nil + } + return err + } + } +} diff --git a/internal/deepseek/protocol/sse_test.go b/internal/deepseek/protocol/sse_test.go new file mode 100644 index 0000000000000000000000000000000000000000..17589c71e3ed24592fd560cfcf251a921c3850ec --- /dev/null +++ b/internal/deepseek/protocol/sse_test.go @@ -0,0 +1,26 @@ +package protocol + +import ( + "io" + "net/http" + "strings" + "testing" +) + +func TestScanSSELinesHandlesLongSingleLine(t *testing.T) { + payload := strings.Repeat("x", 2*1024*1024+4096) + body := "data: {\"p\":\"response/content\",\"v\":\"" + payload + "\"}\n" + resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))} + + var got string + err := ScanSSELines(resp, func(line []byte) bool { + got = string(line) + return true + }) + if err != nil { + t.Fatalf("ScanSSELines returned error: %v", err) + } + if !strings.Contains(got, payload) { + t.Fatalf("long SSE line was not preserved: got len=%d want payload len=%d", len(got), len(payload)) + } +} diff --git a/internal/deepseek/transport/transport.go b/internal/deepseek/transport/transport.go new file mode 100644 index 0000000000000000000000000000000000000000..76a80042586f05cf47ea689e9dda6bec054a1d93 --- /dev/null +++ b/internal/deepseek/transport/transport.go @@ -0,0 +1,113 @@ +package transport + +import ( + "context" + "crypto/tls" + "fmt" + "net" + "net/http" + "time" + + utls "github.com/refraction-networking/utls" +) + +type Doer interface { + Do(req *http.Request) (*http.Response, error) +} + +type DialContextFunc func(ctx context.Context, network, addr string) (net.Conn, error) + +type Client struct { + http *http.Client +} + +func New(timeout time.Duration) *Client { + return NewWithDialContext(timeout, nil) +} + +func NewWithDialContext(timeout time.Duration, dialContext DialContextFunc) *Client { + useEnvProxy := dialContext == nil + if dialContext == nil { + dialContext = (&net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second}).DialContext + } + base := &http.Transport{ + ForceAttemptHTTP2: false, + MaxIdleConns: 200, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: 90 * time.Second, + DialContext: dialContext, + DialTLSContext: safariTLSDialer(dialContext), + TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12}, + } + if useEnvProxy { + base.Proxy = http.ProxyFromEnvironment + } + return &Client{http: &http.Client{Timeout: timeout, Transport: base}} +} + +func (c *Client) Do(req *http.Request) (*http.Response, error) { + return c.http.Do(req) +} + +func NewFallbackClient(timeout time.Duration, dialContext DialContextFunc) *http.Client { + useEnvProxy := dialContext == nil + if dialContext == nil { + dialContext = (&net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second}).DialContext + } + base := &http.Transport{ + ForceAttemptHTTP2: false, + MaxIdleConns: 200, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: 90 * time.Second, + DialContext: dialContext, + TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS12}, + } + if useEnvProxy { + base.Proxy = http.ProxyFromEnvironment + } + return &http.Client{Timeout: timeout, Transport: base} +} + +func safariTLSDialer(dialContext DialContextFunc) func(ctx context.Context, network, addr string) (net.Conn, error) { + if dialContext == nil { + dialContext = (&net.Dialer{Timeout: 15 * time.Second, KeepAlive: 30 * time.Second}).DialContext + } + return func(ctx context.Context, network, addr string) (net.Conn, error) { + plainConn, err := dialContext(ctx, network, addr) + if err != nil { + return nil, err + } + host, _, _ := net.SplitHostPort(addr) + uCfg := &utls.Config{ServerName: host} + uConn := utls.UClient(plainConn, uCfg, utls.HelloSafari_Auto) + if err := forceHTTP11ALPN(uConn); err != nil { + _ = plainConn.Close() + return nil, err + } + err = uConn.HandshakeContext(ctx) + if err != nil { + _ = plainConn.Close() + return nil, err + } + if negotiated := uConn.ConnectionState().NegotiatedProtocol; negotiated != "" && negotiated != "http/1.1" { + _ = uConn.Close() + return nil, fmt.Errorf("unexpected ALPN protocol negotiated: %s", negotiated) + } + return uConn, nil + } +} + +func forceHTTP11ALPN(uConn *utls.UConn) error { + if err := uConn.BuildHandshakeState(); err != nil { + return err + } + for _, ext := range uConn.Extensions { + alpnExt, ok := ext.(*utls.ALPNExtension) + if !ok { + continue + } + alpnExt.AlpnProtocols = []string{"http/1.1"} + return nil + } + return nil +} diff --git a/internal/devcapture/store.go b/internal/devcapture/store.go new file mode 100644 index 0000000000000000000000000000000000000000..64561c5983d5344ad6c27261c92f0635d79d1552 --- /dev/null +++ b/internal/devcapture/store.go @@ -0,0 +1,262 @@ +package devcapture + +import ( + "encoding/json" + "fmt" + "io" + "os" + "strconv" + "strings" + "sync" + "time" + + "ds2api/internal/util" + + "github.com/google/uuid" +) + +const ( + defaultLimit = 20 + defaultMaxBodyBytes = 5 * 1024 * 1024 + maxLimit = 50 +) + +type Entry struct { + ID string `json:"id"` + CreatedAt int64 `json:"created_at"` + Label string `json:"label"` + URL string `json:"url"` + AccountID string `json:"account_id,omitempty"` + StatusCode int `json:"status_code"` + RequestBody string `json:"request_body"` + ResponseBody string `json:"response_body"` + ResponseTruncated bool `json:"response_truncated"` +} + +type Store struct { + mu sync.Mutex + enabled bool + limit int + maxBodyBytes int + items []Entry +} + +type Session struct { + store *Store + id string + createdAt int64 + label string + url string + accountID string + requestRaw string +} + +type captureBody struct { + rc io.ReadCloser + s *Session + statusCode int + buf strings.Builder + truncated bool + finalized bool +} + +var ( + globalOnce sync.Once + globalInst *Store +) + +func Global() *Store { + globalOnce.Do(func() { + globalInst = NewFromEnv() + }) + return globalInst +} + +func NewFromEnv() *Store { + enabled := !isVercelRuntime() + if raw, ok := os.LookupEnv("DS2API_DEV_PACKET_CAPTURE"); ok { + enabled = parseBool(raw) + } + limit := parseIntWithDefault(os.Getenv("DS2API_DEV_PACKET_CAPTURE_LIMIT"), defaultLimit) + if limit < 1 { + limit = defaultLimit + } + if limit > maxLimit { + limit = maxLimit + } + maxBodyBytes := parseIntWithDefault(os.Getenv("DS2API_DEV_PACKET_CAPTURE_MAX_BODY_BYTES"), defaultMaxBodyBytes) + if maxBodyBytes < 1024 { + maxBodyBytes = defaultMaxBodyBytes + } + return &Store{ + enabled: enabled, + limit: limit, + maxBodyBytes: maxBodyBytes, + items: make([]Entry, 0, limit), + } +} + +func isVercelRuntime() bool { + return strings.TrimSpace(os.Getenv("VERCEL")) != "" || strings.TrimSpace(os.Getenv("NOW_REGION")) != "" +} + +func (s *Store) Enabled() bool { + if s == nil { + return false + } + return s.enabled +} + +func (s *Store) Limit() int { + if s == nil { + return defaultLimit + } + return s.limit +} + +func (s *Store) MaxBodyBytes() int { + if s == nil { + return defaultMaxBodyBytes + } + return s.maxBodyBytes +} + +func (s *Store) Snapshot() []Entry { + if s == nil { + return nil + } + s.mu.Lock() + defer s.mu.Unlock() + out := make([]Entry, len(s.items)) + copy(out, s.items) + return out +} + +func (s *Store) Clear() { + if s == nil { + return + } + s.mu.Lock() + defer s.mu.Unlock() + s.items = s.items[:0] +} + +func (s *Store) Start(label, url, accountID string, requestPayload any) *Session { + if s == nil || !s.enabled { + return nil + } + return &Session{ + store: s, + id: "cap_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + createdAt: time.Now().Unix(), + label: strings.TrimSpace(label), + url: strings.TrimSpace(url), + accountID: strings.TrimSpace(accountID), + requestRaw: marshalPayload(requestPayload), + } +} + +func (s *Session) WrapBody(rc io.ReadCloser, statusCode int) io.ReadCloser { + if s == nil || rc == nil { + return rc + } + return &captureBody{ + rc: rc, + s: s, + statusCode: statusCode, + } +} + +func (c *captureBody) Read(p []byte) (int, error) { + n, err := c.rc.Read(p) + if n > 0 { + c.append(string(p[:n])) + } + if err == io.EOF { + c.finalize() + } + return n, err +} + +func (c *captureBody) Close() error { + err := c.rc.Close() + c.finalize() + return err +} + +func (c *captureBody) append(chunk string) { + if chunk == "" || c.s == nil || c.s.store == nil { + return + } + maxLen := c.s.store.maxBodyBytes + current := c.buf.Len() + if current >= maxLen { + c.truncated = true + return + } + remain := maxLen - current + if len(chunk) > remain { + truncated, _ := util.TruncateUTF8Bytes(chunk, remain) + c.buf.WriteString(truncated) + c.truncated = true + return + } + c.buf.WriteString(chunk) +} + +func (c *captureBody) finalize() { + if c.finalized || c.s == nil || c.s.store == nil { + return + } + c.finalized = true + entry := Entry{ + ID: c.s.id, + CreatedAt: c.s.createdAt, + Label: c.s.label, + URL: c.s.url, + AccountID: c.s.accountID, + StatusCode: c.statusCode, + RequestBody: c.s.requestRaw, + ResponseBody: c.buf.String(), + ResponseTruncated: c.truncated, + } + c.s.store.push(entry) +} + +func (s *Store) push(entry Entry) { + s.mu.Lock() + defer s.mu.Unlock() + s.items = append([]Entry{entry}, s.items...) + if len(s.items) > s.limit { + s.items = s.items[:s.limit] + } +} + +func marshalPayload(v any) string { + b, err := json.Marshal(v) + if err != nil { + return fmt.Sprintf("%v", v) + } + return string(b) +} + +func parseBool(v string) bool { + switch strings.ToLower(strings.TrimSpace(v)) { + case "1", "true", "yes", "on": + return true + default: + return false + } +} + +func parseIntWithDefault(raw string, d int) int { + raw = strings.TrimSpace(raw) + if raw == "" { + return d + } + n, err := strconv.Atoi(raw) + if err != nil { + return d + } + return n +} diff --git a/internal/devcapture/store_test.go b/internal/devcapture/store_test.go new file mode 100644 index 0000000000000000000000000000000000000000..91854c5c06166354d36b1619f7063666840ddd1a --- /dev/null +++ b/internal/devcapture/store_test.go @@ -0,0 +1,110 @@ +package devcapture + +import ( + "io" + "strings" + "testing" + "unicode/utf8" +) + +func TestNewFromEnvDefaults(t *testing.T) { + t.Setenv("DS2API_DEV_PACKET_CAPTURE_LIMIT", "") + t.Setenv("DS2API_DEV_PACKET_CAPTURE_MAX_BODY_BYTES", "") + t.Setenv("VERCEL", "") + t.Setenv("NOW_REGION", "") + + s := NewFromEnv() + if s.Limit() != 20 { + t.Fatalf("expected default limit 20, got %d", s.Limit()) + } + if s.MaxBodyBytes() != 5*1024*1024 { + t.Fatalf("expected default max body bytes 5MB, got %d", s.MaxBodyBytes()) + } +} + +func TestNewFromEnvHonorsOverrides(t *testing.T) { + t.Setenv("DS2API_DEV_PACKET_CAPTURE_LIMIT", "7") + t.Setenv("DS2API_DEV_PACKET_CAPTURE_MAX_BODY_BYTES", "8192") + t.Setenv("VERCEL", "") + t.Setenv("NOW_REGION", "") + s := NewFromEnv() + if s.Limit() != 7 { + t.Fatalf("expected override limit 7, got %d", s.Limit()) + } + if s.MaxBodyBytes() != 8192 { + t.Fatalf("expected override max body bytes 8192, got %d", s.MaxBodyBytes()) + } +} + +func TestStorePushKeepsNewestWithinLimit(t *testing.T) { + s := &Store{enabled: true, limit: 2, maxBodyBytes: 1024} + for i := 0; i < 3; i++ { + session := s.Start("test", "http://x", "", map[string]any{"seq": i}) + if session == nil { + t.Fatal("expected session") + } + rc := session.WrapBody(io.NopCloser(strings.NewReader("ok")), 200) + _, _ = io.ReadAll(rc) + _ = rc.Close() + } + items := s.Snapshot() + if len(items) != 2 { + t.Fatalf("expected 2 items, got %d", len(items)) + } + if !strings.Contains(items[0].RequestBody, `"seq":2`) { + t.Fatalf("expected newest first, got %#v", items[0].RequestBody) + } + if !strings.Contains(items[1].RequestBody, `"seq":1`) { + t.Fatalf("expected second newest, got %#v", items[1].RequestBody) + } +} + +func TestWrapBodyTruncatesByLimit(t *testing.T) { + s := &Store{enabled: true, limit: 5, maxBodyBytes: 4} + session := s.Start("test", "http://x", "acc1", map[string]any{"x": 1}) + if session == nil { + t.Fatal("expected session") + } + rc := session.WrapBody(io.NopCloser(strings.NewReader("abcdef")), 200) + _, _ = io.ReadAll(rc) + _ = rc.Close() + + items := s.Snapshot() + if len(items) != 1 { + t.Fatalf("expected 1 item, got %d", len(items)) + } + if items[0].ResponseBody != "abcd" { + t.Fatalf("expected truncated body, got %q", items[0].ResponseBody) + } + if !items[0].ResponseTruncated { + t.Fatal("expected truncated flag true") + } + if items[0].AccountID != "acc1" { + t.Fatalf("expected account id, got %q", items[0].AccountID) + } +} + +func TestWrapBodyTruncatesUTF8WithoutBreakingRune(t *testing.T) { + s := &Store{enabled: true, limit: 5, maxBodyBytes: 5} + session := s.Start("test", "http://x", "acc1", map[string]any{"x": 1}) + if session == nil { + t.Fatal("expected session") + } + rc := session.WrapBody(io.NopCloser(strings.NewReader("😀xy")), 200) + _, _ = io.ReadAll(rc) + _ = rc.Close() + + items := s.Snapshot() + if len(items) != 1 { + t.Fatalf("expected 1 item, got %d", len(items)) + } + if !utf8.ValidString(items[0].ResponseBody) { + t.Fatalf("expected valid utf-8 response body, got %q", items[0].ResponseBody) + } + if items[0].ResponseBody != "😀x" { + t.Fatalf("expected rune-safe truncation, got %q", items[0].ResponseBody) + } + if !items[0].ResponseTruncated { + t.Fatal("expected truncated flag true") + } +} diff --git a/internal/format/claude/render.go b/internal/format/claude/render.go new file mode 100644 index 0000000000000000000000000000000000000000..3912f41a5d527b95e514b3e483d07ed51522740d --- /dev/null +++ b/internal/format/claude/render.go @@ -0,0 +1,108 @@ +package claude + +import ( + "ds2api/internal/assistantturn" + "ds2api/internal/toolcall" + "fmt" + "time" + + "ds2api/internal/prompt" + "ds2api/internal/util" +) + +func BuildMessageResponseFromTurn(messageID, model string, turn assistantturn.Turn, exposeThinking bool) map[string]any { + content := make([]map[string]any, 0, 4) + if exposeThinking && turn.Thinking != "" { + content = append(content, map[string]any{"type": "thinking", "thinking": turn.Thinking}) + } + stopReason := "end_turn" + if len(turn.ToolCalls) > 0 { + stopReason = "tool_use" + for i, tc := range turn.ToolCalls { + content = append(content, map[string]any{ + "type": "tool_use", + "id": fmt.Sprintf("toolu_%d_%d", time.Now().Unix(), i), + "name": tc.Name, + "input": tc.Input, + }) + } + } else { + text := turn.Text + if text == "" && exposeThinking { + text = turn.Thinking + } + if text == "" { + text = "抱歉,没有生成有效的响应内容。" + } + content = append(content, map[string]any{"type": "text", "text": text}) + } + return map[string]any{ + "id": messageID, + "type": "message", + "role": "assistant", + "model": model, + "content": content, + "stop_reason": stopReason, + "stop_sequence": nil, + "usage": map[string]any{ + "input_tokens": turn.Usage.InputTokens, + "output_tokens": turn.Usage.OutputTokens, + }, + } +} + +func BuildMessageResponse(messageID, model string, normalizedMessages []any, finalThinking, finalText string, toolNames []string) map[string]any { + detected := toolcall.ParseToolCalls(finalText, toolNames) + if len(detected) == 0 && finalText == "" && finalThinking != "" { + detected = toolcall.ParseToolCalls(finalThinking, toolNames) + } + content := make([]map[string]any, 0, 4) + if finalThinking != "" { + content = append(content, map[string]any{"type": "thinking", "thinking": finalThinking}) + } + stopReason := "end_turn" + if len(detected) > 0 { + stopReason = "tool_use" + for i, tc := range detected { + content = append(content, map[string]any{ + "type": "tool_use", + "id": fmt.Sprintf("toolu_%d_%d", time.Now().Unix(), i), + "name": tc.Name, + "input": tc.Input, + }) + } + } else { + if finalText == "" { + finalText = "抱歉,没有生成有效的响应内容。" + } + content = append(content, map[string]any{"type": "text", "text": finalText}) + } + return map[string]any{ + "id": messageID, + "type": "message", + "role": "assistant", + "model": model, + "content": content, + "stop_reason": stopReason, + "stop_sequence": nil, + "usage": map[string]any{ + "input_tokens": util.CountPromptTokens(prompt.MessagesPrepareWithThinking(claudeMessageMaps(normalizedMessages), false), model), + "output_tokens": util.CountOutputTokens(finalThinking, model) + util.CountOutputTokens(finalText, model), + }, + } +} + +func claudeMessageMaps(messages []any) []map[string]any { + if len(messages) == 0 { + return nil + } + out := make([]map[string]any, 0, len(messages)) + for _, item := range messages { + msg, ok := item.(map[string]any) + if !ok { + continue + } + out = append(out, msg) + } + return out +} diff --git a/internal/format/claude/render_test.go b/internal/format/claude/render_test.go new file mode 100644 index 0000000000000000000000000000000000000000..da6066269a70751756775071f7c94a94b43e8a60 --- /dev/null +++ b/internal/format/claude/render_test.go @@ -0,0 +1,36 @@ +package claude + +import "testing" + +func TestBuildMessageResponseSkipsThinkingFallbackWhenFinalTextExists(t *testing.T) { + resp := BuildMessageResponse( + "msg_1", + "claude-sonnet-4-5", + []any{map[string]any{"role": "user", "content": "hi"}}, + `{"tool_calls":[{"name":"search","input":{"q":"go"}}]}`, + "normal answer", + []string{"search"}, + ) + + if resp["stop_reason"] != "end_turn" { + t.Fatalf("expected stop_reason=end_turn, got=%#v", resp["stop_reason"]) + } + + content, _ := resp["content"].([]map[string]any) + foundText := false + foundTool := false + for _, block := range content { + if block["type"] == "text" && block["text"] == "normal answer" { + foundText = true + } + if block["type"] == "tool_use" { + foundTool = true + } + } + if !foundText { + t.Fatalf("expected text block with finalText, got=%#v", resp["content"]) + } + if foundTool { + t.Fatalf("unexpected tool_use block when finalText exists, got=%#v", resp["content"]) + } +} diff --git a/internal/format/openai/render_chat.go b/internal/format/openai/render_chat.go new file mode 100644 index 0000000000000000000000000000000000000000..3fa6e53f804295388fb141ec47b95ab30e469f28 --- /dev/null +++ b/internal/format/openai/render_chat.go @@ -0,0 +1,63 @@ +package openai + +import ( + "ds2api/internal/toolcall" + "strings" + "time" +) + +func BuildChatCompletion(completionID, model, finalPrompt, finalThinking, finalText string, toolNames []string, toolsRaw any) map[string]any { + detected := toolcall.ParseAssistantToolCallsDetailed(finalText, finalThinking, toolNames) + return BuildChatCompletionWithToolCalls(completionID, model, finalPrompt, finalThinking, finalText, detected.Calls, toolsRaw) +} + +func BuildChatCompletionWithToolCalls(completionID, model, finalPrompt, finalThinking, finalText string, detected []toolcall.ParsedToolCall, toolsRaw any) map[string]any { + finishReason := "stop" + messageObj := map[string]any{"role": "assistant", "content": finalText} + if strings.TrimSpace(finalThinking) != "" { + messageObj["reasoning_content"] = finalThinking + } + if len(detected) > 0 { + finishReason = "tool_calls" + messageObj["tool_calls"] = toolcall.FormatOpenAIToolCalls(detected, toolsRaw) + messageObj["content"] = nil + } + + return map[string]any{ + "id": completionID, + "object": "chat.completion", + "created": time.Now().Unix(), + "model": model, + "choices": []map[string]any{{"index": 0, "message": messageObj, "finish_reason": finishReason}}, + "usage": BuildChatUsageForModel(model, finalPrompt, finalThinking, finalText, 0), + } +} + +func BuildChatStreamDeltaChoice(index int, delta map[string]any) map[string]any { + return map[string]any{ + "delta": delta, + "index": index, + } +} + +func BuildChatStreamFinishChoice(index int, finishReason string) map[string]any { + return map[string]any{ + "delta": map[string]any{}, + "index": index, + "finish_reason": finishReason, + } +} + +func BuildChatStreamChunk(completionID string, created int64, model string, choices []map[string]any, usage map[string]any) map[string]any { + out := map[string]any{ + "id": completionID, + "object": "chat.completion.chunk", + "created": created, + "model": model, + "choices": choices, + } + if len(usage) > 0 { + out["usage"] = usage + } + return out +} diff --git a/internal/format/openai/render_responses.go b/internal/format/openai/render_responses.go new file mode 100644 index 0000000000000000000000000000000000000000..5635f96a47faada3907dd80fe2da0c2aa07051d3 --- /dev/null +++ b/internal/format/openai/render_responses.go @@ -0,0 +1,127 @@ +package openai + +import ( + "ds2api/internal/toolcall" + "encoding/json" + "strings" + "time" + + "github.com/google/uuid" +) + +func BuildResponseObject(responseID, model, finalPrompt, finalThinking, finalText string, toolNames []string, toolsRaw any) map[string]any { + // Strict mode: only standalone, structured tool-call payloads are treated + // as executable tool calls. + detected := toolcall.ParseAssistantToolCallsDetailed(finalText, finalThinking, toolNames) + return BuildResponseObjectWithToolCalls(responseID, model, finalPrompt, finalThinking, finalText, detected.Calls, toolsRaw) +} + +func BuildResponseObjectWithToolCalls(responseID, model, finalPrompt, finalThinking, finalText string, detected []toolcall.ParsedToolCall, toolsRaw any) map[string]any { + exposedOutputText := finalText + output := make([]any, 0, 2) + if len(detected) > 0 { + exposedOutputText = "" + if strings.TrimSpace(finalThinking) != "" { + output = append(output, map[string]any{ + "type": "message", + "id": "msg_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + "role": "assistant", + "status": "completed", + "content": []any{map[string]any{ + "type": "reasoning", + "text": finalThinking, + }}, + }) + } + output = append(output, toResponsesFunctionCallItems(detected, toolsRaw)...) + } else { + content := make([]any, 0, 2) + if finalThinking != "" { + content = append([]any{map[string]any{ + "type": "reasoning", + "text": finalThinking, + }}, content...) + } + if strings.TrimSpace(finalText) != "" { + content = append(content, map[string]any{ + "type": "output_text", + "text": finalText, + }) + } + if strings.TrimSpace(finalText) == "" && strings.TrimSpace(finalThinking) != "" { + exposedOutputText = finalThinking + } + output = append(output, map[string]any{ + "type": "message", + "id": "msg_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + "role": "assistant", + "content": content, + }) + } + return BuildResponseObjectFromItems( + responseID, + model, + finalPrompt, + finalThinking, + finalText, + output, + exposedOutputText, + ) +} + +func BuildResponseObjectFromItems(responseID, model, finalPrompt, finalThinking, finalText string, output []any, outputText string) map[string]any { + if output == nil { + output = []any{} + } + return map[string]any{ + "id": responseID, + "type": "response", + "object": "response", + "created_at": time.Now().Unix(), + "status": "completed", + "model": model, + "output": output, + "output_text": outputText, + "usage": BuildResponsesUsageForModel(model, finalPrompt, finalThinking, finalText, 0), + } +} + +func toResponsesFunctionCallItems(toolCalls []toolcall.ParsedToolCall, toolsRaw any) []any { + if len(toolCalls) == 0 { + return nil + } + normalizedCalls := toolcall.NormalizeParsedToolCallsForSchemas(toolCalls, toolsRaw) + out := make([]any, 0, len(toolCalls)) + for _, tc := range normalizedCalls { + if strings.TrimSpace(tc.Name) == "" { + continue + } + argsBytes, _ := json.Marshal(tc.Input) + args := normalizeJSONString(string(argsBytes)) + out = append(out, map[string]any{ + "id": "fc_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + "type": "function_call", + "call_id": "call_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + "name": tc.Name, + "arguments": args, + "status": "completed", + }) + } + return out +} + +func normalizeJSONString(raw string) string { + s := strings.TrimSpace(raw) + if s == "" { + return "{}" + } + var v any + if err := json.Unmarshal([]byte(s), &v); err != nil { + return raw + } + b, err := json.Marshal(v) + if err != nil { + return raw + } + return string(b) +} diff --git a/internal/format/openai/render_stream_events.go b/internal/format/openai/render_stream_events.go new file mode 100644 index 0000000000000000000000000000000000000000..33c7c09c97a9bc5aebb8a0f2f0051295fbc4d285 --- /dev/null +++ b/internal/format/openai/render_stream_events.go @@ -0,0 +1,169 @@ +package openai + +import "strings" + +func BuildResponsesCreatedPayload(responseID, model string) map[string]any { + return map[string]any{ + "type": "response.created", + "id": responseID, + "response_id": responseID, + "object": "response", + "model": model, + "status": "in_progress", + } +} + +func BuildResponsesOutputItemAddedPayload(responseID, itemID string, outputIndex int, item map[string]any) map[string]any { + return map[string]any{ + "type": "response.output_item.added", + "id": responseID, + "response_id": responseID, + "output_index": outputIndex, + "item_id": itemID, + "item": item, + } +} + +func BuildResponsesOutputItemDonePayload(responseID, itemID string, outputIndex int, item map[string]any) map[string]any { + return map[string]any{ + "type": "response.output_item.done", + "id": responseID, + "response_id": responseID, + "output_index": outputIndex, + "item_id": itemID, + "item": item, + } +} + +func BuildResponsesContentPartAddedPayload(responseID, itemID string, outputIndex, contentIndex int, part map[string]any) map[string]any { + return map[string]any{ + "type": "response.content_part.added", + "id": responseID, + "response_id": responseID, + "item_id": itemID, + "output_index": outputIndex, + "content_index": contentIndex, + "part": part, + } +} + +func BuildResponsesContentPartDonePayload(responseID, itemID string, outputIndex, contentIndex int, part map[string]any) map[string]any { + return map[string]any{ + "type": "response.content_part.done", + "id": responseID, + "response_id": responseID, + "item_id": itemID, + "output_index": outputIndex, + "content_index": contentIndex, + "part": part, + } +} + +func BuildResponsesTextDeltaPayload(responseID, itemID string, outputIndex, contentIndex int, delta string) map[string]any { + return map[string]any{ + "type": "response.output_text.delta", + "id": responseID, + "response_id": responseID, + "item_id": itemID, + "output_index": outputIndex, + "content_index": contentIndex, + "delta": delta, + } +} + +func BuildResponsesTextDonePayload(responseID, itemID string, outputIndex, contentIndex int, text string) map[string]any { + return map[string]any{ + "type": "response.output_text.done", + "id": responseID, + "response_id": responseID, + "item_id": itemID, + "output_index": outputIndex, + "content_index": contentIndex, + "text": text, + } +} + +func BuildResponsesReasoningDeltaPayload(responseID, delta string) map[string]any { + return map[string]any{ + "type": "response.reasoning.delta", + "id": responseID, + "response_id": responseID, + "delta": delta, + } +} + +func BuildResponsesFunctionCallArgumentsDeltaPayload(responseID, itemID string, outputIndex int, callID, delta string) map[string]any { + return map[string]any{ + "type": "response.function_call_arguments.delta", + "id": responseID, + "response_id": responseID, + "item_id": itemID, + "output_index": outputIndex, + "call_id": callID, + "delta": delta, + } +} + +func BuildResponsesFunctionCallArgumentsDonePayload(responseID, itemID string, outputIndex int, callID, name, arguments string) map[string]any { + return map[string]any{ + "type": "response.function_call_arguments.done", + "id": responseID, + "response_id": responseID, + "item_id": itemID, + "output_index": outputIndex, + "call_id": callID, + "name": name, + "arguments": normalizeJSONString(arguments), + } +} + +func BuildResponsesFailedPayload(responseID, model string, status int, message, code string) map[string]any { + code = strings.TrimSpace(code) + if code == "" { + code = "api_error" + } + return map[string]any{ + "type": "response.failed", + "id": responseID, + "response_id": responseID, + "object": "response", + "model": model, + "status": "failed", + "status_code": status, + "error": map[string]any{ + "message": message, + "type": responsesErrorType(status), + "code": code, + "param": nil, + }, + } +} + +func responsesErrorType(status int) string { + switch status { + case 400, 404, 422: + return "invalid_request_error" + case 401: + return "authentication_error" + case 403: + return "permission_error" + case 429: + return "rate_limit_error" + case 503: + return "service_unavailable_error" + default: + if status >= 500 { + return "api_error" + } + return "invalid_request_error" + } +} + +func BuildResponsesCompletedPayload(response map[string]any) map[string]any { + responseID, _ := response["id"].(string) + return map[string]any{ + "type": "response.completed", + "response_id": responseID, + "response": response, + } +} diff --git a/internal/format/openai/render_test.go b/internal/format/openai/render_test.go new file mode 100644 index 0000000000000000000000000000000000000000..1c14f51f07d906be38733e99e20f5b7edcdd9474 --- /dev/null +++ b/internal/format/openai/render_test.go @@ -0,0 +1,206 @@ +package openai + +import ( + "encoding/json" + "strings" + "testing" + + "ds2api/internal/toolcall" + "ds2api/internal/util" +) + +func TestBuildResponseObjectKeepsFencedToolPayloadAsText(t *testing.T) { + obj := BuildResponseObject( + "resp_test", + "gpt-4o", + "prompt", + "", + "```json\n{\"tool_calls\":[{\"name\":\"search\",\"input\":{\"q\":\"golang\"}}]}\n```", + []string{"search"}, + nil, + ) + + outputText, _ := obj["output_text"].(string) + if !strings.Contains(outputText, "\"tool_calls\"") { + t.Fatalf("expected output_text to preserve fenced tool payload, got %q", outputText) + } + output, _ := obj["output"].([]any) + if len(output) != 1 { + t.Fatalf("expected one message output item, got %#v", obj["output"]) + } + first, _ := output[0].(map[string]any) + if first["type"] != "message" { + t.Fatalf("expected message output type, got %#v", first["type"]) + } +} + +// Backward-compatible alias for historical test name used in CI logs. +func TestBuildResponseObjectPromotesFencedToolPayloadToFunctionCall(t *testing.T) { + TestBuildResponseObjectKeepsFencedToolPayloadAsText(t) +} + +func TestBuildResponseObjectReasoningOnlyFallsBackToOutputText(t *testing.T) { + obj := BuildResponseObject( + "resp_test", + "gpt-4o", + "prompt", + "internal thinking content", + "", + nil, + nil, + ) + + outputText, _ := obj["output_text"].(string) + if outputText == "" { + t.Fatalf("expected output_text fallback from reasoning when final text is empty") + } + + output, _ := obj["output"].([]any) + if len(output) != 1 { + t.Fatalf("expected one output item, got %#v", obj["output"]) + } + first, _ := output[0].(map[string]any) + if first["type"] != "message" { + t.Fatalf("expected output type message, got %#v", first["type"]) + } + content, _ := first["content"].([]any) + if len(content) == 0 { + t.Fatalf("expected reasoning content, got %#v", first["content"]) + } + block0, _ := content[0].(map[string]any) + if block0["type"] != "reasoning" { + t.Fatalf("expected first content block reasoning, got %#v", block0["type"]) + } +} + +func TestBuildResponseObjectPromotesToolCallFromThinkingWhenTextEmpty(t *testing.T) { + obj := BuildResponseObject( + "resp_test", + "gpt-4o", + "prompt", + `from-thinking`, + "", + []string{"search"}, + nil, + ) + + output, _ := obj["output"].([]any) + if len(output) != 2 { + t.Fatalf("expected reasoning message plus function_call output, got %#v", obj["output"]) + } + first, _ := output[0].(map[string]any) + if first["type"] != "message" { + t.Fatalf("expected reasoning message output first, got %#v", first["type"]) + } + content, _ := first["content"].([]any) + if len(content) != 1 { + t.Fatalf("expected reasoning content, got %#v", first["content"]) + } + block0, _ := content[0].(map[string]any) + if block0["type"] != "reasoning" { + t.Fatalf("expected reasoning block, got %#v", block0["type"]) + } + second, _ := output[1].(map[string]any) + if second["type"] != "function_call" { + t.Fatalf("expected function_call output, got %#v", second["type"]) + } +} + +func TestBuildChatCompletionWithToolCallsCoercesSchemaDeclaredStringArguments(t *testing.T) { + toolsRaw := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "Write", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + "taskId": map[string]any{"type": "string"}, + }, + }, + }, + }, + } + obj := BuildChatCompletionWithToolCalls( + "chat_test", + "gpt-4o", + "prompt", + "", + "", + []toolcall.ParsedToolCall{{ + Name: "Write", + Input: map[string]any{ + "content": map[string]any{"message": "hi"}, + "taskId": 1, + }, + }}, + toolsRaw, + ) + choices, _ := obj["choices"].([]map[string]any) + message, _ := choices[0]["message"].(map[string]any) + toolCalls, _ := message["tool_calls"].([]map[string]any) + fn, _ := toolCalls[0]["function"].(map[string]any) + args := map[string]any{} + if err := json.Unmarshal([]byte(fn["arguments"].(string)), &args); err != nil { + t.Fatalf("decode arguments failed: %v", err) + } + if args["content"] != `{"message":"hi"}` { + t.Fatalf("expected content stringified by schema, got %#v", args["content"]) + } + if args["taskId"] != "1" { + t.Fatalf("expected taskId stringified by schema, got %#v", args["taskId"]) + } +} + +func TestBuildResponseObjectWithToolCallsCoercesSchemaDeclaredStringArguments(t *testing.T) { + toolsRaw := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "Write", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + }, + }, + }, + }, + } + obj := BuildResponseObjectWithToolCalls( + "resp_test", + "gpt-4o", + "prompt", + "", + "", + []toolcall.ParsedToolCall{{ + Name: "Write", + Input: map[string]any{"content": []any{"a", 1}}, + }}, + toolsRaw, + ) + output, _ := obj["output"].([]any) + first, _ := output[0].(map[string]any) + args := map[string]any{} + if err := json.Unmarshal([]byte(first["arguments"].(string)), &args); err != nil { + t.Fatalf("decode response arguments failed: %v", err) + } + if args["content"] != `["a",1]` { + t.Fatalf("expected response content stringified by schema, got %#v", args["content"]) + } +} + +func TestBuildChatUsageForModelUsesConservativePromptCount(t *testing.T) { + prompt := strings.Repeat("上下文token ", 40) + usage := BuildChatUsageForModel("deepseek-v4-flash", prompt, "", "ok", 0) + promptTokens, _ := usage["prompt_tokens"].(int) + if promptTokens <= util.EstimateTokens(prompt) { + t.Fatalf("expected conservative prompt token count > rough estimate, got=%d estimate=%d", promptTokens, util.EstimateTokens(prompt)) + } + totalTokens, _ := usage["total_tokens"].(int) + completionTokens, _ := usage["completion_tokens"].(int) + if totalTokens != promptTokens+completionTokens { + t.Fatalf("expected total tokens to add up, got usage=%#v", usage) + } +} diff --git a/internal/format/openai/render_usage.go b/internal/format/openai/render_usage.go new file mode 100644 index 0000000000000000000000000000000000000000..08541a4a5ec28c012f58f7d0244552dd53233662 --- /dev/null +++ b/internal/format/openai/render_usage.go @@ -0,0 +1,36 @@ +package openai + +import "ds2api/internal/util" + +func BuildChatUsageForModel(model, finalPrompt, finalThinking, finalText string, refFileTokens int) map[string]any { + promptTokens := util.CountPromptTokens(finalPrompt, model) + refFileTokens + reasoningTokens := util.CountOutputTokens(finalThinking, model) + completionTokens := util.CountOutputTokens(finalText, model) + return map[string]any{ + "prompt_tokens": promptTokens, + "completion_tokens": reasoningTokens + completionTokens, + "total_tokens": promptTokens + reasoningTokens + completionTokens, + "completion_tokens_details": map[string]any{ + "reasoning_tokens": reasoningTokens, + }, + } +} + +func BuildChatUsage(finalPrompt, finalThinking, finalText string) map[string]any { + return BuildChatUsageForModel("", finalPrompt, finalThinking, finalText, 0) +} + +func BuildResponsesUsageForModel(model, finalPrompt, finalThinking, finalText string, refFileTokens int) map[string]any { + promptTokens := util.CountPromptTokens(finalPrompt, model) + refFileTokens + reasoningTokens := util.CountOutputTokens(finalThinking, model) + completionTokens := util.CountOutputTokens(finalText, model) + return map[string]any{ + "input_tokens": promptTokens, + "output_tokens": reasoningTokens + completionTokens, + "total_tokens": promptTokens + reasoningTokens + completionTokens, + } +} + +func BuildResponsesUsage(finalPrompt, finalThinking, finalText string) map[string]any { + return BuildResponsesUsageForModel("", finalPrompt, finalThinking, finalText, 0) +} diff --git a/internal/httpapi/admin/accounts/deps.go b/internal/httpapi/admin/accounts/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..568487caf4ef7ed15bc1155d7de506233bec210d --- /dev/null +++ b/internal/httpapi/admin/accounts/deps.go @@ -0,0 +1,46 @@ +package accounts + +import ( + "net/http" + + "ds2api/internal/chathistory" + "ds2api/internal/config" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON + +func reverseAccounts(a []config.Account) { adminshared.ReverseAccounts(a) } +func intFromQuery(r *http.Request, key string, d int) int { + return adminshared.IntFromQuery(r, key, d) +} +func maskSecretPreview(secret string) string { + return adminshared.MaskSecretPreview(secret) +} +func toAccount(m map[string]any) config.Account { + return adminshared.ToAccount(m) +} +func fieldStringOptional(m map[string]any, key string) (string, bool) { + return adminshared.FieldStringOptional(m, key) +} +func accountMatchesIdentifier(acc config.Account, identifier string) bool { + return adminshared.AccountMatchesIdentifier(acc, identifier) +} +func findProxyByID(c config.Config, proxyID string) (config.Proxy, bool) { + return adminshared.FindProxyByID(c, proxyID) +} +func findAccountByIdentifier(store adminshared.ConfigStore, identifier string) (config.Account, bool) { + return adminshared.FindAccountByIdentifier(store, identifier) +} +func newRequestError(detail string) error { return adminshared.NewRequestError(detail) } +func requestErrorDetail(err error) (string, bool) { + return adminshared.RequestErrorDetail(err) +} diff --git a/internal/httpapi/admin/accounts/handler_accounts_crud.go b/internal/httpapi/admin/accounts/handler_accounts_crud.go new file mode 100644 index 0000000000000000000000000000000000000000..7375b403c91f1a361a33fffc15a49a020d39312d --- /dev/null +++ b/internal/httpapi/admin/accounts/handler_accounts_crud.go @@ -0,0 +1,176 @@ +package accounts + +import ( + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/config" +) + +func (h *Handler) listAccounts(w http.ResponseWriter, r *http.Request) { + page := intFromQuery(r, "page", 1) + pageSize := intFromQuery(r, "page_size", 10) + if page < 1 { + page = 1 + } + if pageSize < 1 { + pageSize = 1 + } + if pageSize > 5000 { + pageSize = 5000 + } + accounts := h.Store.Snapshot().Accounts + reverseAccounts(accounts) + q := strings.TrimSpace(strings.ToLower(r.URL.Query().Get("q"))) + if q != "" { + filtered := make([]config.Account, 0, len(accounts)) + for _, acc := range accounts { + id := strings.ToLower(acc.Identifier()) + if strings.Contains(id, q) || + strings.Contains(strings.ToLower(acc.Name), q) || + strings.Contains(strings.ToLower(acc.Remark), q) || + strings.Contains(strings.ToLower(acc.Email), q) || + strings.Contains(strings.ToLower(acc.Mobile), q) { + filtered = append(filtered, acc) + } + } + accounts = filtered + } + total := len(accounts) + totalPages := 1 + if total > 0 { + totalPages = (total + pageSize - 1) / pageSize + } + start := (page - 1) * pageSize + if start > total { + start = total + } + end := start + pageSize + if end > total { + end = total + } + items := make([]map[string]any, 0, end-start) + for _, acc := range accounts[start:end] { + testStatus, _ := h.Store.AccountTestStatus(acc.Identifier()) + token := strings.TrimSpace(acc.Token) + items = append(items, map[string]any{ + "identifier": acc.Identifier(), + "name": acc.Name, + "remark": acc.Remark, + "email": acc.Email, + "mobile": acc.Mobile, + "proxy_id": acc.ProxyID, + "has_password": acc.Password != "", + "has_token": token != "", + "token_preview": maskSecretPreview(token), + "test_status": testStatus, + }) + } + writeJSON(w, http.StatusOK, map[string]any{"items": items, "total": total, "page": page, "page_size": pageSize, "total_pages": totalPages}) +} + +func (h *Handler) addAccount(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + acc := toAccount(req) + if acc.Identifier() == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "需要 email 或 mobile"}) + return + } + err := h.Store.Update(func(c *config.Config) error { + if acc.ProxyID != "" { + if _, ok := findProxyByID(*c, acc.ProxyID); !ok { + return fmt.Errorf("代理不存在") + } + } + mobileKey := config.CanonicalMobileKey(acc.Mobile) + for _, a := range c.Accounts { + if acc.Email != "" && a.Email == acc.Email { + return fmt.Errorf("邮箱已存在") + } + if mobileKey != "" && config.CanonicalMobileKey(a.Mobile) == mobileKey { + return fmt.Errorf("手机号已存在") + } + } + c.Accounts = append(c.Accounts, acc) + return nil + }) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + h.Pool.Reset() + writeJSON(w, http.StatusOK, map[string]any{"success": true, "total_accounts": len(h.Store.Snapshot().Accounts)}) +} + +func (h *Handler) updateAccount(w http.ResponseWriter, r *http.Request) { + identifier := chi.URLParam(r, "identifier") + if decoded, err := url.PathUnescape(identifier); err == nil { + identifier = decoded + } + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + name, nameOK := fieldStringOptional(req, "name") + remark, remarkOK := fieldStringOptional(req, "remark") + + err := h.Store.Update(func(c *config.Config) error { + for i, acc := range c.Accounts { + if !accountMatchesIdentifier(acc, identifier) { + continue + } + if nameOK { + c.Accounts[i].Name = name + } + if remarkOK { + c.Accounts[i].Remark = remark + } + return nil + } + return newRequestError("账号不存在") + }) + if err != nil { + if detail, ok := requestErrorDetail(err); ok { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": detail}) + return + } + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "total_accounts": len(h.Store.Snapshot().Accounts)}) +} + +func (h *Handler) deleteAccount(w http.ResponseWriter, r *http.Request) { + identifier := chi.URLParam(r, "identifier") + if decoded, err := url.PathUnescape(identifier); err == nil { + identifier = decoded + } + err := h.Store.Update(func(c *config.Config) error { + idx := -1 + for i, a := range c.Accounts { + if accountMatchesIdentifier(a, identifier) { + idx = i + break + } + } + if idx < 0 { + return fmt.Errorf("账号不存在") + } + c.Accounts = append(c.Accounts[:idx], c.Accounts[idx+1:]...) + return nil + }) + if err != nil { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": err.Error()}) + return + } + h.Pool.Reset() + writeJSON(w, http.StatusOK, map[string]any{"success": true, "total_accounts": len(h.Store.Snapshot().Accounts)}) +} diff --git a/internal/httpapi/admin/accounts/handler_accounts_crud_test.go b/internal/httpapi/admin/accounts/handler_accounts_crud_test.go new file mode 100644 index 0000000000000000000000000000000000000000..be2b0ba81c41d1e5f1039b7ccee1b1bedece870c --- /dev/null +++ b/internal/httpapi/admin/accounts/handler_accounts_crud_test.go @@ -0,0 +1,118 @@ +package accounts + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/go-chi/chi/v5" +) + +func TestListAccountsPageSizeCapIs5000(t *testing.T) { + accounts := make([]string, 0, 150) + for i := range 150 { + accounts = append(accounts, fmt.Sprintf(`{"email":"u%d@example.com","password":"pwd"}`, i)) + } + raw := fmt.Sprintf(`{"accounts":[%s]}`, strings.Join(accounts, ",")) + router := newHTTPAdminHarness(t, raw, &testingDSMock{}) + + rec := httptest.NewRecorder() + router.ServeHTTP(rec, adminReq(http.MethodGet, "/accounts?page=1&page_size=200", nil)) + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response: %v", err) + } + items, _ := payload["items"].([]any) + if len(items) != 150 { + t.Fatalf("expected all 150 accounts with page_size=200, got %d", len(items)) + } + if ps, _ := payload["page_size"].(float64); ps != 200 { + t.Fatalf("expected page_size=200 in response, got %v", payload["page_size"]) + } +} + +func TestListAccountsPageSizeAbove5000ClampedTo5000(t *testing.T) { + router := newHTTPAdminHarness(t, `{"accounts":[{"email":"u@example.com","password":"pwd"}]}`, &testingDSMock{}) + + rec := httptest.NewRecorder() + router.ServeHTTP(rec, adminReq(http.MethodGet, "/accounts?page=1&page_size=9999", nil)) + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response: %v", err) + } + if ps, _ := payload["page_size"].(float64); ps != 5000 { + t.Fatalf("expected page_size clamped to 5000, got %v", payload["page_size"]) + } +} + +func TestUpdateAccountMetadataPreservesCredentials(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[{"email":"u@example.com","name":"old name","remark":"old remark","password":"secret"}] + }`) + + r := chi.NewRouter() + r.Put("/admin/accounts/{identifier}", h.updateAccount) + + body := []byte(`{"name":"new name","remark":"new remark"}`) + req := httptest.NewRequest(http.MethodPut, "/admin/accounts/u@example.com", strings.NewReader(string(body))) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.Accounts) != 1 { + t.Fatalf("unexpected accounts after update: %#v", snap.Accounts) + } + acc := snap.Accounts[0] + if acc.Email != "u@example.com" { + t.Fatalf("identifier changed unexpectedly: %#v", acc) + } + if acc.Name != "new name" || acc.Remark != "new remark" { + t.Fatalf("metadata update did not persist: %#v", acc) + } + if acc.Password != "secret" { + t.Fatalf("password should be preserved, got %#v", acc) + } +} + +func TestListAccountsMasksTokenPreview(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[{"email":"u@example.com","password":"pwd"}] + }`) + if err := h.Store.UpdateAccountToken("u@example.com", "abcdefgh"); err != nil { + t.Fatalf("seed runtime token: %v", err) + } + + req := httptest.NewRequest(http.MethodGet, "/admin/accounts?page=1&page_size=10", nil) + rec := httptest.NewRecorder() + h.listAccounts(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response failed: %v", err) + } + items, _ := payload["items"].([]any) + if len(items) != 1 { + t.Fatalf("expected 1 item, got %d", len(items)) + } + first, _ := items[0].(map[string]any) + if got, _ := first["token_preview"].(string); got != "ab****gh" { + t.Fatalf("expected masked token preview, got %q", got) + } +} diff --git a/internal/httpapi/admin/accounts/handler_accounts_identifier_test.go b/internal/httpapi/admin/accounts/handler_accounts_identifier_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5edaf27fb0df9675498bb112bf0f41e61f89cc66 --- /dev/null +++ b/internal/httpapi/admin/accounts/handler_accounts_identifier_test.go @@ -0,0 +1,135 @@ +package accounts + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/account" + "ds2api/internal/config" +) + +func newAdminTestHandler(t *testing.T, raw string) *Handler { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", raw) + store := config.LoadStore() + return &Handler{ + Store: store, + Pool: account.NewPool(store), + } +} + +func TestListAccountsUsesEmailIdentifier(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[{"email":"u@example.com","password":"pwd"}] + }`) + + req := httptest.NewRequest(http.MethodGet, "/admin/accounts?page=1&page_size=10", nil) + rec := httptest.NewRecorder() + h.listAccounts(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response failed: %v", err) + } + items, _ := payload["items"].([]any) + if len(items) != 1 { + t.Fatalf("expected 1 item, got %d", len(items)) + } + first, _ := items[0].(map[string]any) + identifier, _ := first["identifier"].(string) + if identifier != "u@example.com" { + t.Fatalf("expected email identifier, got %q", identifier) + } +} + +func TestDeleteAccountSupportsMobileAlias(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[{"email":"u@example.com","mobile":"13800138000","password":"pwd"}] + }`) + + r := chi.NewRouter() + r.Delete("/admin/accounts/{identifier}", h.deleteAccount) + req := httptest.NewRequest(http.MethodDelete, "/admin/accounts/13800138000", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + if got := len(h.Store.Accounts()); got != 0 { + t.Fatalf("expected account removed, remaining=%d", got) + } +} + +func TestDeleteAccountSupportsEncodedPlusMobile(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[{"mobile":"+8613800138000","password":"pwd"}] + }`) + + r := chi.NewRouter() + r.Delete("/admin/accounts/{identifier}", h.deleteAccount) + req := httptest.NewRequest(http.MethodDelete, "/admin/accounts/"+url.PathEscape("+8613800138000"), nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + if got := len(h.Store.Accounts()); got != 0 { + t.Fatalf("expected account removed, remaining=%d", got) + } +} + +func TestAddAccountRejectsCanonicalMobileDuplicate(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[{"mobile":"+8613800138000","password":"pwd"}] + }`) + + r := chi.NewRouter() + r.Post("/admin/accounts", h.addAccount) + body := []byte(`{"mobile":"13800138000","password":"pwd2"}`) + req := httptest.NewRequest(http.MethodPost, "/admin/accounts", bytes.NewReader(body)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + if got := len(h.Store.Accounts()); got != 1 { + t.Fatalf("expected no duplicate insert, got=%d", got) + } +} + +func TestFindAccountByIdentifierSupportsMobile(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[ + {"email":"u@example.com","mobile":"13800138000","password":"pwd"} + ] + }`) + + accByMobile, ok := findAccountByIdentifier(h.Store, "13800138000") + if !ok { + t.Fatal("expected find by mobile") + } + if accByMobile.Email != "u@example.com" { + t.Fatalf("unexpected account by mobile: %#v", accByMobile) + } + accByMobileWithCountryCode, ok := findAccountByIdentifier(h.Store, "+8613800138000") + if !ok { + t.Fatal("expected find by +86 mobile") + } + if accByMobileWithCountryCode.Email != "u@example.com" { + t.Fatalf("unexpected account by +86 mobile: %#v", accByMobileWithCountryCode) + } + +} diff --git a/internal/httpapi/admin/accounts/handler_accounts_queue.go b/internal/httpapi/admin/accounts/handler_accounts_queue.go new file mode 100644 index 0000000000000000000000000000000000000000..48b68e8b40cb3fef09da146b2cfee8d75c0c99db --- /dev/null +++ b/internal/httpapi/admin/accounts/handler_accounts_queue.go @@ -0,0 +1,7 @@ +package accounts + +import "net/http" + +func (h *Handler) queueStatus(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, http.StatusOK, h.Pool.Status()) +} diff --git a/internal/httpapi/admin/accounts/handler_accounts_testing.go b/internal/httpapi/admin/accounts/handler_accounts_testing.go new file mode 100644 index 0000000000000000000000000000000000000000..d92c1dcf47fa91c550868b199d5ffff0463d61db --- /dev/null +++ b/internal/httpapi/admin/accounts/handler_accounts_testing.go @@ -0,0 +1,299 @@ +package accounts + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "strings" + "sync" + "time" + + authn "ds2api/internal/auth" + "ds2api/internal/config" + "ds2api/internal/prompt" + "ds2api/internal/promptcompat" + "ds2api/internal/sse" +) + +type modelAliasSnapshotReader struct { + aliases map[string]string +} + +func (m modelAliasSnapshotReader) ModelAliases() map[string]string { + return m.aliases +} + +func (h *Handler) testSingleAccount(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + identifier, _ := req["identifier"].(string) + if strings.TrimSpace(identifier) == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "需要账号标识(identifier / email / mobile)"}) + return + } + acc, ok := findAccountByIdentifier(h.Store, identifier) + if !ok { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": "账号不存在"}) + return + } + model, _ := req["model"].(string) + if model == "" { + model = "deepseek-v4-flash" + } + message, _ := req["message"].(string) + result := h.testAccount(r.Context(), acc, model, message) + writeJSON(w, http.StatusOK, result) +} + +func (h *Handler) testAllAccounts(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + model, _ := req["model"].(string) + if model == "" { + model = "deepseek-v4-flash" + } + accounts := h.Store.Snapshot().Accounts + if len(accounts) == 0 { + writeJSON(w, http.StatusOK, map[string]any{"total": 0, "success": 0, "failed": 0, "results": []any{}}) + return + } + + // Concurrent testing with a semaphore to limit parallelism. + const maxConcurrency = 5 + results := runAccountTestsConcurrently(accounts, maxConcurrency, func(_ int, account config.Account) map[string]any { + return h.testAccount(r.Context(), account, model, "") + }) + + success := 0 + for _, res := range results { + if ok, _ := res["success"].(bool); ok { + success++ + } + } + writeJSON(w, http.StatusOK, map[string]any{"total": len(accounts), "success": success, "failed": len(accounts) - success, "results": results}) +} + +func runAccountTestsConcurrently(accounts []config.Account, maxConcurrency int, testFn func(int, config.Account) map[string]any) []map[string]any { + if maxConcurrency <= 0 { + maxConcurrency = 1 + } + sem := make(chan struct{}, maxConcurrency) + results := make([]map[string]any, len(accounts)) + var wg sync.WaitGroup + for i, acc := range accounts { + wg.Add(1) + go func(idx int, account config.Account) { + defer wg.Done() + sem <- struct{}{} // acquire + defer func() { <-sem }() // release + results[idx] = testFn(idx, account) + }(i, acc) + } + wg.Wait() + return results +} + +func (h *Handler) testAccount(ctx context.Context, acc config.Account, model, message string) map[string]any { + start := time.Now() + identifier := acc.Identifier() + result := map[string]any{ + "account": identifier, + "success": false, + "response_time": 0, + "message": "", + "model": model, + "session_count": 0, + "config_writable": !h.Store.IsEnvBacked(), + "config_warning": "", + } + defer func() { + status := "failed" + if ok, _ := result["success"].(bool); ok { + status = "ok" + } + _ = h.Store.UpdateAccountTestStatus(identifier, status) + }() + token, err := h.DS.Login(ctx, acc) + if err != nil { + result["message"] = "登录失败: " + err.Error() + return result + } + if err := h.Store.UpdateAccountToken(acc.Identifier(), token); err != nil { + result["config_warning"] = "登录成功,但 token 持久化失败(仅保存在内存,重启后会丢失): " + err.Error() + } + authCtx := &authn.RequestAuth{UseConfigToken: false, DeepSeekToken: token, AccountID: identifier, Account: acc} + proxyCtx := authn.WithAuth(ctx, authCtx) + sessionID, err := h.DS.CreateSession(proxyCtx, authCtx, 1) + if err != nil { + newToken, loginErr := h.DS.Login(proxyCtx, acc) + if loginErr != nil { + result["message"] = "创建会话失败: " + err.Error() + return result + } + token = newToken + authCtx.DeepSeekToken = token + if err := h.Store.UpdateAccountToken(acc.Identifier(), token); err != nil { + result["config_warning"] = "刷新 token 成功,但 token 持久化失败(仅保存在内存,重启后会丢失): " + err.Error() + } + sessionID, err = h.DS.CreateSession(proxyCtx, authCtx, 1) + if err != nil { + result["message"] = "创建会话失败: " + err.Error() + return result + } + } + + // 获取会话数量 + sessionStats, sessionErr := h.DS.GetSessionCountForToken(proxyCtx, token) + if sessionErr == nil && sessionStats != nil { + result["session_count"] = sessionStats.FirstPageCount + } + + if strings.TrimSpace(message) == "" { + result["success"] = true + result["message"] = "Token 刷新成功(登录与会话创建成功)" + if warning, _ := result["config_warning"].(string); strings.TrimSpace(warning) != "" { + result["message"] = result["message"].(string) + ";" + warning + } + result["response_time"] = int(time.Since(start).Milliseconds()) + return result + } + thinking, search, ok := config.GetModelConfig(model) + resolvedModel, resolved := config.ResolveModel(modelAliasSnapshotReader{ + aliases: h.Store.Snapshot().ModelAliases, + }, model) + if resolved { + model = resolvedModel + thinking, search, ok = config.GetModelConfig(model) + } + if !ok { + thinking, search = false, false + } + pow, err := h.DS.GetPow(proxyCtx, authCtx, 1) + if err != nil { + result["message"] = "获取 PoW 失败: " + err.Error() + return result + } + payload := promptcompat.StandardRequest{ + ResolvedModel: model, + FinalPrompt: prompt.MessagesPrepare([]map[string]any{{"role": "user", "content": message}}), + Thinking: thinking, + Search: search, + }.CompletionPayload(sessionID) + resp, err := h.DS.CallCompletion(proxyCtx, authCtx, payload, pow, 1) + if err != nil { + result["message"] = "请求失败: " + err.Error() + return result + } + if resp.StatusCode != http.StatusOK { + defer func() { _ = resp.Body.Close() }() + result["message"] = fmt.Sprintf("请求失败: HTTP %d", resp.StatusCode) + return result + } + collected := sse.CollectStream(resp, thinking, true) + result["success"] = true + result["response_time"] = int(time.Since(start).Milliseconds()) + if collected.Text != "" { + result["message"] = collected.Text + } else { + result["message"] = "(无回复内容)" + } + if collected.Thinking != "" { + result["thinking"] = collected.Thinking + } + return result +} + +func (h *Handler) testAPI(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + model, _ := req["model"].(string) + message, _ := req["message"].(string) + apiKey, _ := req["api_key"].(string) + if model == "" { + model = "deepseek-v4-flash" + } + if message == "" { + message = "你好" + } + if apiKey == "" { + keys := h.Store.Snapshot().Keys + if len(keys) == 0 { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "没有可用的 API Key"}) + return + } + apiKey = keys[0] + } + host := r.Host + scheme := "http" + if strings.Contains(strings.ToLower(host), "vercel") || strings.Contains(strings.ToLower(r.Header.Get("X-Forwarded-Proto")), "https") { + scheme = "https" + } + payload := map[string]any{"model": model, "messages": []map[string]any{{"role": "user", "content": message}}, "stream": false} + b, _ := json.Marshal(payload) + request, _ := http.NewRequestWithContext(r.Context(), http.MethodPost, fmt.Sprintf("%s://%s/v1/chat/completions", scheme, host), bytes.NewReader(b)) + request.Header.Set("Authorization", "Bearer "+apiKey) + request.Header.Set("Content-Type", "application/json") + resp, err := (&http.Client{Timeout: 60 * time.Second}).Do(request) + if err != nil { + writeJSON(w, http.StatusOK, map[string]any{"success": false, "error": err.Error()}) + return + } + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + if resp.StatusCode == http.StatusOK { + var parsed any + _ = json.Unmarshal(body, &parsed) + writeJSON(w, http.StatusOK, map[string]any{"success": true, "status_code": resp.StatusCode, "response": parsed}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": false, "status_code": resp.StatusCode, "response": string(body)}) +} + +func (h *Handler) deleteAllSessions(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + identifier, _ := req["identifier"].(string) + if strings.TrimSpace(identifier) == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "需要账号标识(identifier / email / mobile)"}) + return + } + acc, ok := findAccountByIdentifier(h.Store, identifier) + if !ok { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": "账号不存在"}) + return + } + + // 每次先登录刷新一次 token,避免使用过期 token。 + authCtx := &authn.RequestAuth{UseConfigToken: false, AccountID: acc.Identifier(), Account: acc} + proxyCtx := authn.WithAuth(r.Context(), authCtx) + token, err := h.DS.Login(proxyCtx, acc) + if err != nil { + writeJSON(w, http.StatusOK, map[string]any{"success": false, "message": "登录失败: " + err.Error()}) + return + } + _ = h.Store.UpdateAccountToken(acc.Identifier(), token) + authCtx.DeepSeekToken = token + + // 删除所有会话 + err = h.DS.DeleteAllSessionsForToken(proxyCtx, token) + if err != nil { + // token 可能过期,尝试重新登录并重试一次 + newToken, loginErr := h.DS.Login(proxyCtx, acc) + if loginErr != nil { + writeJSON(w, http.StatusOK, map[string]any{"success": false, "message": "删除失败: " + err.Error()}) + return + } + token = newToken + _ = h.Store.UpdateAccountToken(acc.Identifier(), token) + authCtx.DeepSeekToken = token + if retryErr := h.DS.DeleteAllSessionsForToken(proxyCtx, token); retryErr != nil { + writeJSON(w, http.StatusOK, map[string]any{"success": false, "message": "删除失败: " + retryErr.Error()}) + return + } + } + + writeJSON(w, http.StatusOK, map[string]any{"success": true, "message": "删除成功"}) +} diff --git a/internal/httpapi/admin/accounts/handler_accounts_testing_test.go b/internal/httpapi/admin/accounts/handler_accounts_testing_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d8f6ece6028943351c6a2079d11ca797f87d3983 --- /dev/null +++ b/internal/httpapi/admin/accounts/handler_accounts_testing_test.go @@ -0,0 +1,211 @@ +package accounts + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" +) + +type testingDSMock struct { + loginCalls int + createSessionCalls int + getPowCalls int + callCompletionCalls int + deleteAllSessionsCalls int + deleteAllSessionsError error + deleteAllSessionsErrorOnce bool +} + +func (m *testingDSMock) Login(_ context.Context, _ config.Account) (string, error) { + m.loginCalls++ + return "new-token", nil +} + +func (m *testingDSMock) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + m.createSessionCalls++ + return "session-id", nil +} + +func (m *testingDSMock) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + m.getPowCalls++ + return "", errors.New("should not call GetPow in this test") +} + +func (m *testingDSMock) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + m.callCompletionCalls++ + return nil, errors.New("should not call CallCompletion in this test") +} + +func (m *testingDSMock) DeleteAllSessionsForToken(_ context.Context, _ string) error { + m.deleteAllSessionsCalls++ + if m.deleteAllSessionsError != nil { + err := m.deleteAllSessionsError + if m.deleteAllSessionsErrorOnce { + m.deleteAllSessionsError = nil + } + return err + } + return nil +} + +func (m *testingDSMock) GetSessionCountForToken(_ context.Context, _ string) (*dsclient.SessionStats, error) { + return &dsclient.SessionStats{Success: true}, nil +} + +func TestTestAccount_BatchModeOnlyCreatesSession(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"accounts":[{"email":"batch@example.com","password":"pwd","token":""}]}`) + store := config.LoadStore() + ds := &testingDSMock{} + h := &Handler{Store: store, DS: ds} + acc, ok := store.FindAccount("batch@example.com") + if !ok { + t.Fatal("expected test account") + } + + result := h.testAccount(context.Background(), acc, "deepseek-v4-flash", "") + + if ok, _ := result["success"].(bool); !ok { + t.Fatalf("expected success=true, got %#v", result) + } + msg, _ := result["message"].(string) + if !strings.Contains(msg, "Token 刷新成功") { + t.Fatalf("expected session-only success message, got %q", msg) + } + if ds.loginCalls != 1 || ds.createSessionCalls != 1 { + t.Fatalf("unexpected Login/CreateSession calls: login=%d createSession=%d", ds.loginCalls, ds.createSessionCalls) + } + if ds.getPowCalls != 0 || ds.callCompletionCalls != 0 { + t.Fatalf("expected no completion flow calls, got getPow=%d callCompletion=%d", ds.getPowCalls, ds.callCompletionCalls) + } + updated, ok := store.FindAccount("batch@example.com") + if !ok { + t.Fatal("expected updated account") + } + if updated.Token != "new-token" { + t.Fatalf("expected refreshed token to be persisted, got %q", updated.Token) + } + testStatus, ok := store.AccountTestStatus("batch@example.com") + if !ok || testStatus != "ok" { + t.Fatalf("expected runtime test status ok, got %q (ok=%v)", testStatus, ok) + } +} + +func TestDeleteAllSessions_RetryWithReloginOnDeleteFailure(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"accounts":[{"email":"batch@example.com","password":"pwd","token":"expired-token"}]}`) + store := config.LoadStore() + ds := &testingDSMock{deleteAllSessionsError: errors.New("token expired"), deleteAllSessionsErrorOnce: true} + h := &Handler{Store: store, DS: ds} + + req := httptest.NewRequest(http.MethodPost, "/delete-all", bytes.NewBufferString(`{"identifier":"batch@example.com"}`)) + rec := httptest.NewRecorder() + h.deleteAllSessions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected status 200, got %d", rec.Code) + } + var resp map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &resp); err != nil { + t.Fatalf("unmarshal response: %v", err) + } + if ok, _ := resp["success"].(bool); !ok { + t.Fatalf("expected success response, got %#v", resp) + } + if ds.loginCalls != 2 { + t.Fatalf("expected initial login plus relogin, got %d", ds.loginCalls) + } + if ds.deleteAllSessionsCalls != 2 { + t.Fatalf("expected delete called twice, got %d", ds.deleteAllSessionsCalls) + } + updated, ok := store.FindAccount("batch@example.com") + if !ok { + t.Fatal("expected account") + } + if updated.Token != "new-token" { + t.Fatalf("expected refreshed token persisted, got %q", updated.Token) + } +} + +type completionPayloadDSMock struct { + payload map[string]any +} + +func (m *completionPayloadDSMock) Login(_ context.Context, _ config.Account) (string, error) { + return "new-token", nil +} + +func (m *completionPayloadDSMock) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +func (m *completionPayloadDSMock) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow-ok", nil +} + +func (m *completionPayloadDSMock) CallCompletion(_ context.Context, _ *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + m.payload = payload + return &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader("data: {\"v\":\"ok\"}\n\ndata: [DONE]\n\n")), + }, nil +} + +func (m *completionPayloadDSMock) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +func (m *completionPayloadDSMock) GetSessionCountForToken(_ context.Context, _ string) (*dsclient.SessionStats, error) { + return &dsclient.SessionStats{Success: true}, nil +} + +func TestTestAccount_MessageModeUsesExpertModelTypeForExpertModel(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"accounts":[{"email":"batch@example.com","password":"pwd","token":"seed-token"}]}`) + store := config.LoadStore() + ds := &completionPayloadDSMock{} + h := &Handler{Store: store, DS: ds} + acc, ok := store.FindAccount("batch@example.com") + if !ok { + t.Fatal("expected test account") + } + + result := h.testAccount(context.Background(), acc, "deepseek-v4-pro", "hello") + + if ok, _ := result["success"].(bool); !ok { + t.Fatalf("expected success=true, got %#v", result) + } + if got := ds.payload["model_type"]; got != "expert" { + t.Fatalf("expected model_type expert, got %#v", got) + } + if got := ds.payload["chat_session_id"]; got != "session-id" { + t.Fatalf("unexpected chat_session_id: %#v", got) + } +} + +func TestTestAccount_MessageModeUsesVisionModelTypeForVisionModel(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"accounts":[{"email":"batch@example.com","password":"pwd","token":"seed-token"}]}`) + store := config.LoadStore() + ds := &completionPayloadDSMock{} + h := &Handler{Store: store, DS: ds} + acc, ok := store.FindAccount("batch@example.com") + if !ok { + t.Fatal("expected test account") + } + + result := h.testAccount(context.Background(), acc, "deepseek-v4-vision", "hello") + + if ok, _ := result["success"].(bool); !ok { + t.Fatalf("expected success=true, got %#v", result) + } + if got := ds.payload["model_type"]; got != "vision" { + t.Fatalf("expected model_type vision, got %#v", got) + } +} diff --git a/internal/httpapi/admin/accounts/routes.go b/internal/httpapi/admin/accounts/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..13491c17f0137cf19712a6d1eec831b26ab2ede2 --- /dev/null +++ b/internal/httpapi/admin/accounts/routes.go @@ -0,0 +1,38 @@ +package accounts + +import ( + "context" + "net/http" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/config" +) + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/accounts", h.listAccounts) + r.Post("/accounts", h.addAccount) + r.Put("/accounts/{identifier}", h.updateAccount) + r.Delete("/accounts/{identifier}", h.deleteAccount) + r.Get("/queue/status", h.queueStatus) + r.Post("/accounts/test", h.testSingleAccount) + r.Post("/accounts/test-all", h.testAllAccounts) + r.Post("/accounts/sessions/delete-all", h.deleteAllSessions) + r.Post("/test", h.testAPI) +} + +func RunAccountTestsConcurrently(accounts []config.Account, maxConcurrency int, testFn func(int, config.Account) map[string]any) []map[string]any { + return runAccountTestsConcurrently(accounts, maxConcurrency, testFn) +} + +func (h *Handler) TestAccount(ctx context.Context, acc config.Account, model, message string) map[string]any { + return h.testAccount(ctx, acc, model, message) +} + +func (h *Handler) ListAccounts(w http.ResponseWriter, r *http.Request) { h.listAccounts(w, r) } +func (h *Handler) AddAccount(w http.ResponseWriter, r *http.Request) { h.addAccount(w, r) } +func (h *Handler) UpdateAccount(w http.ResponseWriter, r *http.Request) { h.updateAccount(w, r) } +func (h *Handler) DeleteAccount(w http.ResponseWriter, r *http.Request) { h.deleteAccount(w, r) } +func (h *Handler) DeleteAllSessions(w http.ResponseWriter, r *http.Request) { + h.deleteAllSessions(w, r) +} diff --git a/internal/httpapi/admin/accounts/test_http_helpers_test.go b/internal/httpapi/admin/accounts/test_http_helpers_test.go new file mode 100644 index 0000000000000000000000000000000000000000..4a4f736e822ddd7075a02fd7e960dc37931b35bb --- /dev/null +++ b/internal/httpapi/admin/accounts/test_http_helpers_test.go @@ -0,0 +1,35 @@ +package accounts + +import ( + "bytes" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/account" + "ds2api/internal/config" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +func newHTTPAdminHarness(t *testing.T, rawConfig string, ds adminshared.DeepSeekCaller) http.Handler { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", rawConfig) + store := config.LoadStore() + h := &Handler{ + Store: store, + Pool: account.NewPool(store), + DS: ds, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + return r +} + +func adminReq(method, path string, body []byte) *http.Request { + req := httptest.NewRequest(method, path, bytes.NewReader(body)) + req.Header.Set("Authorization", "Bearer admin") + req.Header.Set("Content-Type", "application/json") + return req +} diff --git a/internal/httpapi/admin/auth/deps.go b/internal/httpapi/admin/auth/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..4f8b9c38623a101b1f6e960cccf305cc022e025d --- /dev/null +++ b/internal/httpapi/admin/auth/deps.go @@ -0,0 +1,20 @@ +package auth + +import ( + "ds2api/internal/chathistory" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON +var intFrom = adminshared.IntFrom +var maskSecretPreview = adminshared.MaskSecretPreview + +func nilIfEmpty(s string) any { return adminshared.NilIfEmpty(s) } diff --git a/internal/httpapi/admin/auth/handler_auth.go b/internal/httpapi/admin/auth/handler_auth.go new file mode 100644 index 0000000000000000000000000000000000000000..d7a04d5a87cba1fa04b02b408704565dfb277290 --- /dev/null +++ b/internal/httpapi/admin/auth/handler_auth.go @@ -0,0 +1,94 @@ +package auth + +import ( + "encoding/json" + "net/http" + "os" + "strings" + "time" + + authn "ds2api/internal/auth" +) + +func (h *Handler) requireAdmin(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if err := authn.VerifyAdminRequestWithStore(r, h.Store); err != nil { + writeJSON(w, http.StatusUnauthorized, map[string]any{"detail": err.Error()}) + return + } + next.ServeHTTP(w, r) + }) +} + +func (h *Handler) login(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + adminKey, _ := req["admin_key"].(string) + expireHours := intFrom(req["expire_hours"]) + if !authn.VerifyAdminCredential(adminKey, h.Store) { + writeJSON(w, http.StatusUnauthorized, map[string]any{"detail": "Invalid admin key"}) + return + } + token, err := authn.CreateJWTWithStore(expireHours, h.Store) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + if expireHours <= 0 { + expireHours = h.Store.AdminJWTExpireHours() + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "token": token, "expires_in": expireHours * 3600}) +} + +func (h *Handler) verify(w http.ResponseWriter, r *http.Request) { + header := strings.TrimSpace(r.Header.Get("Authorization")) + if !strings.HasPrefix(strings.ToLower(header), "bearer ") { + writeJSON(w, http.StatusUnauthorized, map[string]any{"detail": "No credentials provided"}) + return + } + token := strings.TrimSpace(header[7:]) + payload, err := authn.VerifyJWTWithStore(token, h.Store) + if err != nil { + writeJSON(w, http.StatusUnauthorized, map[string]any{"detail": err.Error()}) + return + } + exp, _ := payload["exp"].(float64) + remaining := int64(exp) - time.Now().Unix() + if remaining < 0 { + remaining = 0 + } + writeJSON(w, http.StatusOK, map[string]any{"valid": true, "expires_at": int64(exp), "remaining_seconds": remaining}) +} + +func (h *Handler) getVercelConfig(w http.ResponseWriter, _ *http.Request) { + saved := h.Store.Snapshot().Vercel + token, tokenSource := firstConfiguredValue( + [2]string{"env", os.Getenv("VERCEL_TOKEN")}, + [2]string{"config", saved.Token}, + ) + projectID, _ := firstConfiguredValue( + [2]string{"env", os.Getenv("VERCEL_PROJECT_ID")}, + [2]string{"config", saved.ProjectID}, + ) + teamID, _ := firstConfiguredValue( + [2]string{"env", os.Getenv("VERCEL_TEAM_ID")}, + [2]string{"config", saved.TeamID}, + ) + writeJSON(w, http.StatusOK, map[string]any{ + "has_token": token != "", + "token_preview": maskSecretPreview(token), + "token_source": nilIfEmpty(tokenSource), + "project_id": projectID, + "team_id": nilIfEmpty(teamID), + }) +} + +func firstConfiguredValue(values ...[2]string) (string, string) { + for _, pair := range values { + value := strings.TrimSpace(pair[1]) + if value != "" { + return value, strings.TrimSpace(pair[0]) + } + } + return "", "" +} diff --git a/internal/httpapi/admin/auth/handler_auth_test.go b/internal/httpapi/admin/auth/handler_auth_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e3db5b40d8d1873611dba041f196b66dd67fee07 --- /dev/null +++ b/internal/httpapi/admin/auth/handler_auth_test.go @@ -0,0 +1,38 @@ +package auth + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "ds2api/internal/config" +) + +func TestGetVercelConfigFallsBackToSavedConfig(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"vercel":{"token":"saved-token","project_id":"saved-project","team_id":"saved-team"}}`) + t.Setenv("VERCEL_TOKEN", "") + t.Setenv("VERCEL_PROJECT_ID", "") + t.Setenv("VERCEL_TEAM_ID", "") + h := &Handler{Store: config.LoadStore()} + + rec := httptest.NewRecorder() + h.getVercelConfig(rec, httptest.NewRequest(http.MethodGet, "/admin/vercel/config", nil)) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d: %s", rec.Code, rec.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response: %v", err) + } + if payload["has_token"] != true { + t.Fatalf("expected saved token to be detected: %#v", payload) + } + if payload["token_source"] != "config" || payload["project_id"] != "saved-project" || payload["team_id"] != "saved-team" { + t.Fatalf("unexpected preconfig payload: %#v", payload) + } + if payload["token_preview"] == "saved-token" { + t.Fatal("token preview leaked the full token") + } +} diff --git a/internal/httpapi/admin/auth/routes.go b/internal/httpapi/admin/auth/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..91ec102b60f7ac0bc1a4840b067b57b47f8dab9d --- /dev/null +++ b/internal/httpapi/admin/auth/routes.go @@ -0,0 +1,20 @@ +package auth + +import ( + "net/http" + + "github.com/go-chi/chi/v5" +) + +func (h *Handler) RequireAdmin(next http.Handler) http.Handler { + return h.requireAdmin(next) +} + +func RegisterPublicRoutes(r chi.Router, h *Handler) { + r.Post("/login", h.login) + r.Get("/verify", h.verify) +} + +func RegisterProtectedRoutes(r chi.Router, h *Handler) { + r.Get("/vercel/config", h.getVercelConfig) +} diff --git a/internal/httpapi/admin/configmgmt/deps.go b/internal/httpapi/admin/configmgmt/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..8b9a1cc09a3aa22e708d40717535311fc5353ab3 --- /dev/null +++ b/internal/httpapi/admin/configmgmt/deps.go @@ -0,0 +1,50 @@ +package configmgmt + +import ( + "ds2api/internal/chathistory" + "ds2api/internal/config" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON + +func maskSecretPreview(secret string) string { + return adminshared.MaskSecretPreview(secret) +} +func toStringSlice(v any) ([]string, bool) { return adminshared.ToStringSlice(v) } +func toAccount(m map[string]any) config.Account { + return adminshared.ToAccount(m) +} +func toAPIKeys(v any) ([]config.APIKey, bool) { return adminshared.ToAPIKeys(v) } +func mergeAPIKeysPreferStructured(existing, incoming []config.APIKey) ([]config.APIKey, int) { + return adminshared.MergeAPIKeysPreferStructured(existing, incoming) +} +func fieldString(m map[string]any, key string) string { + return adminshared.FieldString(m, key) +} +func fieldStringOptional(m map[string]any, key string) (string, bool) { + return adminshared.FieldStringOptional(m, key) +} +func normalizeAccountForStorage(acc config.Account) config.Account { + return adminshared.NormalizeAccountForStorage(acc) +} +func accountDedupeKey(acc config.Account) string { return adminshared.AccountDedupeKey(acc) } +func normalizeAndDedupeAccounts(accounts []config.Account) []config.Account { + return adminshared.NormalizeAndDedupeAccounts(accounts) +} +func newRequestError(detail string) error { return adminshared.NewRequestError(detail) } +func requestErrorDetail(err error) (string, bool) { + return adminshared.RequestErrorDetail(err) +} +func normalizeSettingsConfig(c *config.Config) { adminshared.NormalizeSettingsConfig(c) } +func validateSettingsConfig(c config.Config) error { + return adminshared.ValidateSettingsConfig(c) +} diff --git a/internal/httpapi/admin/configmgmt/handler_config_import.go b/internal/httpapi/admin/configmgmt/handler_config_import.go new file mode 100644 index 0000000000000000000000000000000000000000..0060591f65a31b474f7abeda9418a6720389e46b --- /dev/null +++ b/internal/httpapi/admin/configmgmt/handler_config_import.go @@ -0,0 +1,149 @@ +package configmgmt + +import ( + "encoding/json" + "net/http" + "strings" + + "ds2api/internal/config" +) + +func (h *Handler) configImport(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + + mode := strings.TrimSpace(strings.ToLower(r.URL.Query().Get("mode"))) + if mode == "" { + mode = strings.TrimSpace(strings.ToLower(fieldString(req, "mode"))) + } + if mode == "" { + mode = "merge" + } + if mode != "merge" && mode != "replace" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "mode must be merge or replace"}) + return + } + + payload := req + if raw, ok := req["config"].(map[string]any); ok && len(raw) > 0 { + payload = raw + } + rawJSON, err := json.Marshal(payload) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid config payload"}) + return + } + var incoming config.Config + if err := json.Unmarshal(rawJSON, &incoming); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + incoming.ClearAccountTokens() + + importedKeys, importedAccounts := 0, 0 + err = h.Store.Update(func(c *config.Config) error { + next := c.Clone() + if mode == "replace" { + next = incoming.Clone() + next.Accounts = normalizeAndDedupeAccounts(next.Accounts) + next.VercelSyncHash = c.VercelSyncHash + next.VercelSyncTime = c.VercelSyncTime + importedKeys = len(next.APIKeys) + importedAccounts = len(next.Accounts) + } else { + var changed int + next.APIKeys, changed = mergeAPIKeysPreferStructured(next.APIKeys, incoming.APIKeys) + importedKeys += changed + + existingAccounts := map[string]struct{}{} + for _, acc := range next.Accounts { + acc = normalizeAccountForStorage(acc) + key := accountDedupeKey(acc) + if key != "" { + existingAccounts[key] = struct{}{} + } + } + for _, acc := range incoming.Accounts { + acc = normalizeAccountForStorage(acc) + key := accountDedupeKey(acc) + if key == "" { + continue + } + if _, ok := existingAccounts[key]; ok { + continue + } + existingAccounts[key] = struct{}{} + next.Accounts = append(next.Accounts, acc) + importedAccounts++ + } + + if len(incoming.ModelAliases) > 0 { + if next.ModelAliases == nil { + next.ModelAliases = map[string]string{} + } + for k, v := range incoming.ModelAliases { + next.ModelAliases[k] = v + } + } + if incoming.Responses.StoreTTLSeconds > 0 { + next.Responses.StoreTTLSeconds = incoming.Responses.StoreTTLSeconds + } + if strings.TrimSpace(incoming.Embeddings.Provider) != "" { + next.Embeddings.Provider = incoming.Embeddings.Provider + } + incomingVercel := config.NormalizeVercelConfig(incoming.Vercel) + if strings.TrimSpace(incomingVercel.Token) != "" || strings.TrimSpace(incomingVercel.ProjectID) != "" || strings.TrimSpace(incomingVercel.TeamID) != "" { + next.Vercel = incomingVercel + } + if strings.TrimSpace(incoming.Admin.PasswordHash) != "" { + next.Admin.PasswordHash = incoming.Admin.PasswordHash + } + if incoming.Admin.JWTExpireHours > 0 { + next.Admin.JWTExpireHours = incoming.Admin.JWTExpireHours + } + if incoming.Admin.JWTValidAfterUnix > 0 { + next.Admin.JWTValidAfterUnix = incoming.Admin.JWTValidAfterUnix + } + if incoming.Runtime.AccountMaxInflight > 0 { + next.Runtime.AccountMaxInflight = incoming.Runtime.AccountMaxInflight + } + if incoming.Runtime.AccountMaxQueue > 0 { + next.Runtime.AccountMaxQueue = incoming.Runtime.AccountMaxQueue + } + if incoming.Runtime.GlobalMaxInflight > 0 { + next.Runtime.GlobalMaxInflight = incoming.Runtime.GlobalMaxInflight + } + if incoming.Runtime.TokenRefreshIntervalHours > 0 { + next.Runtime.TokenRefreshIntervalHours = incoming.Runtime.TokenRefreshIntervalHours + } + } + + normalizeSettingsConfig(&next) + if err := validateSettingsConfig(next); err != nil { + return newRequestError(err.Error()) + } + + *c = next + return nil + }) + if err != nil { + if detail, ok := requestErrorDetail(err); ok { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": detail}) + return + } + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + + h.Pool.Reset() + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "mode": mode, + "imported_keys": importedKeys, + "imported_accounts": importedAccounts, + "message": "config imported", + }) +} diff --git a/internal/httpapi/admin/configmgmt/handler_config_read.go b/internal/httpapi/admin/configmgmt/handler_config_read.go new file mode 100644 index 0000000000000000000000000000000000000000..74157f9ca8f7307ef3921d6e11e1c9fb1fd14293 --- /dev/null +++ b/internal/httpapi/admin/configmgmt/handler_config_read.go @@ -0,0 +1,79 @@ +package configmgmt + +import ( + "net/http" + "strings" + + "ds2api/internal/config" +) + +func (h *Handler) getConfig(w http.ResponseWriter, _ *http.Request) { + snap := h.Store.Snapshot() + safe := map[string]any{ + "keys": snap.Keys, + "api_keys": snap.APIKeys, + "accounts": []map[string]any{}, + "proxies": []map[string]any{}, + "env_backed": h.Store.IsEnvBacked(), + "env_source_present": h.Store.HasEnvConfigSource(), + "env_writeback_enabled": h.Store.IsEnvWritebackEnabled(), + "config_path": h.Store.ConfigPath(), + "model_aliases": snap.ModelAliases, + "vercel": map[string]any{ + "has_token": strings.TrimSpace(snap.Vercel.Token) != "", + "token_preview": maskSecretPreview(snap.Vercel.Token), + "project_id": snap.Vercel.ProjectID, + "team_id": snap.Vercel.TeamID, + }, + } + accounts := make([]map[string]any, 0, len(snap.Accounts)) + for _, acc := range snap.Accounts { + token := strings.TrimSpace(acc.Token) + accounts = append(accounts, map[string]any{ + "identifier": acc.Identifier(), + "name": acc.Name, + "remark": acc.Remark, + "email": acc.Email, + "mobile": acc.Mobile, + "proxy_id": acc.ProxyID, + "has_password": strings.TrimSpace(acc.Password) != "", + "has_token": token != "", + "token_preview": maskSecretPreview(token), + }) + } + safe["accounts"] = accounts + proxies := make([]map[string]any, 0, len(snap.Proxies)) + for _, proxy := range snap.Proxies { + proxy = config.NormalizeProxy(proxy) + proxies = append(proxies, map[string]any{ + "id": proxy.ID, + "name": proxy.Name, + "type": proxy.Type, + "host": proxy.Host, + "port": proxy.Port, + "username": proxy.Username, + "has_password": strings.TrimSpace(proxy.Password) != "", + }) + } + safe["proxies"] = proxies + writeJSON(w, http.StatusOK, safe) +} + +func (h *Handler) exportConfig(w http.ResponseWriter, _ *http.Request) { + h.configExport(w, nil) +} + +func (h *Handler) configExport(w http.ResponseWriter, _ *http.Request) { + snap := h.Store.Snapshot() + jsonStr, b64, err := h.Store.ExportJSONAndBase64() + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "config": snap, + "json": jsonStr, + "base64": b64, + }) +} diff --git a/internal/httpapi/admin/configmgmt/handler_config_write.go b/internal/httpapi/admin/configmgmt/handler_config_write.go new file mode 100644 index 0000000000000000000000000000000000000000..8b1aa88d801b182b7c7ce63d3650c90cb9a165f7 --- /dev/null +++ b/internal/httpapi/admin/configmgmt/handler_config_write.go @@ -0,0 +1,227 @@ +package configmgmt + +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/config" +) + +func (h *Handler) updateConfig(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + old := h.Store.Snapshot() + err := h.Store.Update(func(c *config.Config) error { + if apiKeys, ok := toAPIKeys(req["api_keys"]); ok { + c.APIKeys = apiKeys + } else if keys, ok := toStringSlice(req["keys"]); ok { + c.Keys = keys + } + if accountsRaw, ok := req["accounts"].([]any); ok { + existing := map[string]config.Account{} + for _, a := range old.Accounts { + a = normalizeAccountForStorage(a) + key := accountDedupeKey(a) + if key != "" { + existing[key] = a + } + } + seen := map[string]struct{}{} + accounts := make([]config.Account, 0, len(accountsRaw)) + for _, item := range accountsRaw { + m, ok := item.(map[string]any) + if !ok { + continue + } + acc := normalizeAccountForStorage(toAccount(m)) + key := accountDedupeKey(acc) + if key == "" { + continue + } + if _, ok := seen[key]; ok { + continue + } + if prev, ok := existing[key]; ok { + if strings.TrimSpace(acc.Password) == "" { + acc.Password = prev.Password + } + } + seen[key] = struct{}{} + accounts = append(accounts, acc) + } + c.Accounts = accounts + } + if m, ok := req["model_aliases"].(map[string]any); ok { + aliases := make(map[string]string, len(m)) + for k, v := range m { + aliases[k] = fmt.Sprintf("%v", v) + } + c.ModelAliases = aliases + } + return nil + }) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + h.Pool.Reset() + writeJSON(w, http.StatusOK, map[string]any{"success": true, "message": "配置已更新"}) +} + +func (h *Handler) addKey(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + key, _ := req["key"].(string) + key = strings.TrimSpace(key) + name := fieldString(req, "name") + remark := fieldString(req, "remark") + if key == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "Key 不能为空"}) + return + } + err := h.Store.Update(func(c *config.Config) error { + for _, item := range c.APIKeys { + if item.Key == key { + return fmt.Errorf("key 已存在") + } + } + c.APIKeys = append(c.APIKeys, config.APIKey{Key: key, Name: name, Remark: remark}) + return nil + }) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "total_keys": len(h.Store.Snapshot().Keys)}) +} + +func (h *Handler) updateKey(w http.ResponseWriter, r *http.Request) { + key := strings.TrimSpace(chi.URLParam(r, "key")) + if key == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "key 不能为空"}) + return + } + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + name, nameOK := fieldStringOptional(req, "name") + remark, remarkOK := fieldStringOptional(req, "remark") + + err := h.Store.Update(func(c *config.Config) error { + idx := -1 + for i, item := range c.APIKeys { + if item.Key == key { + idx = i + break + } + } + if idx < 0 { + return fmt.Errorf("key 不存在") + } + if nameOK { + c.APIKeys[idx].Name = name + } + if remarkOK { + c.APIKeys[idx].Remark = remark + } + return nil + }) + if err != nil { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "total_keys": len(h.Store.Snapshot().Keys)}) +} + +func (h *Handler) deleteKey(w http.ResponseWriter, r *http.Request) { + key := chi.URLParam(r, "key") + err := h.Store.Update(func(c *config.Config) error { + idx := -1 + for i, item := range c.APIKeys { + if item.Key == key { + idx = i + break + } + } + if idx < 0 { + return fmt.Errorf("key 不存在") + } + c.APIKeys = append(c.APIKeys[:idx], c.APIKeys[idx+1:]...) + return nil + }) + if err != nil { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "total_keys": len(h.Store.Snapshot().Keys)}) +} + +func (h *Handler) batchImport(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "无效的 JSON 格式"}) + return + } + importedKeys, importedAccounts := 0, 0 + err := h.Store.Update(func(c *config.Config) error { + if apiKeys, ok := toAPIKeys(req["api_keys"]); ok { + var changed int + c.APIKeys, changed = mergeAPIKeysPreferStructured(c.APIKeys, apiKeys) + importedKeys += changed + } + if keys, ok := req["keys"].([]any); ok { + legacy := make([]config.APIKey, 0, len(keys)) + for _, k := range keys { + key := strings.TrimSpace(fmt.Sprintf("%v", k)) + if key == "" { + continue + } + legacy = append(legacy, config.APIKey{Key: key}) + } + var changed int + c.APIKeys, changed = mergeAPIKeysPreferStructured(c.APIKeys, legacy) + importedKeys += changed + } + if accounts, ok := req["accounts"].([]any); ok { + existing := map[string]bool{} + for _, a := range c.Accounts { + a = normalizeAccountForStorage(a) + key := accountDedupeKey(a) + if key != "" { + existing[key] = true + } + } + for _, item := range accounts { + m, ok := item.(map[string]any) + if !ok { + continue + } + acc := normalizeAccountForStorage(toAccount(m)) + key := accountDedupeKey(acc) + if key == "" || existing[key] { + continue + } + c.Accounts = append(c.Accounts, acc) + existing[key] = true + importedAccounts++ + } + } + return nil + }) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + h.Pool.Reset() + writeJSON(w, http.StatusOK, map[string]any{"success": true, "imported_keys": importedKeys, "imported_accounts": importedAccounts}) +} diff --git a/internal/httpapi/admin/configmgmt/handler_keys_test.go b/internal/httpapi/admin/configmgmt/handler_keys_test.go new file mode 100644 index 0000000000000000000000000000000000000000..9c2c80ca32460bc760e306164b7d9a9e91f078e7 --- /dev/null +++ b/internal/httpapi/admin/configmgmt/handler_keys_test.go @@ -0,0 +1,76 @@ +package configmgmt + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" +) + +func TestKeyEndpointsPreserveStructuredMetadata(t *testing.T) { + h := newAdminTestHandler(t, `{ + "api_keys":[{"key":"k1","name":"primary","remark":"prod"}] + }`) + + r := chi.NewRouter() + r.Post("/admin/keys", h.addKey) + r.Put("/admin/keys/{key}", h.updateKey) + r.Delete("/admin/keys/{key}", h.deleteKey) + + addBody := []byte(`{"key":"k2","name":"secondary","remark":"staging"}`) + addReq := httptest.NewRequest(http.MethodPost, "/admin/keys", bytes.NewReader(addBody)) + addRec := httptest.NewRecorder() + r.ServeHTTP(addRec, addReq) + if addRec.Code != http.StatusOK { + t.Fatalf("add status=%d body=%s", addRec.Code, addRec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after add: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary" || snap.APIKeys[0].Remark != "prod" { + t.Fatalf("existing metadata was lost after add: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Name != "secondary" || snap.APIKeys[1].Remark != "staging" { + t.Fatalf("new metadata was lost after add: %#v", snap.APIKeys[1]) + } + + updateBody := map[string]any{ + "name": "primary-updated", + "remark": "prod-updated", + } + updateBytes, _ := json.Marshal(updateBody) + updateReq := httptest.NewRequest(http.MethodPut, "/admin/keys/k1", bytes.NewReader(updateBytes)) + updateRec := httptest.NewRecorder() + r.ServeHTTP(updateRec, updateReq) + if updateRec.Code != http.StatusOK { + t.Fatalf("update status=%d body=%s", updateRec.Code, updateRec.Body.String()) + } + + snap = h.Store.Snapshot() + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after update: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Key != "k1" || snap.APIKeys[0].Name != "primary-updated" || snap.APIKeys[0].Remark != "prod-updated" { + t.Fatalf("metadata update did not persist: %#v", snap.APIKeys[0]) + } + + deleteReq := httptest.NewRequest(http.MethodDelete, "/admin/keys/k1", nil) + deleteRec := httptest.NewRecorder() + r.ServeHTTP(deleteRec, deleteReq) + if deleteRec.Code != http.StatusOK { + t.Fatalf("delete status=%d body=%s", deleteRec.Code, deleteRec.Body.String()) + } + + snap = h.Store.Snapshot() + if len(snap.APIKeys) != 1 || snap.APIKeys[0].Key != "k2" { + t.Fatalf("unexpected api keys after delete: %#v", snap.APIKeys) + } + if len(snap.Keys) != 1 || snap.Keys[0] != "k2" { + t.Fatalf("unexpected legacy keys after delete: %#v", snap.Keys) + } +} diff --git a/internal/httpapi/admin/configmgmt/routes.go b/internal/httpapi/admin/configmgmt/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..a3ece47b7042a0c970514a0dc8abdf92806f0cda --- /dev/null +++ b/internal/httpapi/admin/configmgmt/routes.go @@ -0,0 +1,27 @@ +package configmgmt + +import ( + "net/http" + + "github.com/go-chi/chi/v5" +) + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/config", h.getConfig) + r.Post("/config", h.updateConfig) + r.Post("/config/import", h.configImport) + r.Get("/config/export", h.configExport) + r.Get("/export", h.exportConfig) + r.Post("/keys", h.addKey) + r.Put("/keys/{key}", h.updateKey) + r.Delete("/keys/{key}", h.deleteKey) + r.Post("/import", h.batchImport) +} + +func (h *Handler) GetConfig(w http.ResponseWriter, r *http.Request) { h.getConfig(w, r) } +func (h *Handler) UpdateConfig(w http.ResponseWriter, r *http.Request) { h.updateConfig(w, r) } +func (h *Handler) ConfigImport(w http.ResponseWriter, r *http.Request) { h.configImport(w, r) } +func (h *Handler) BatchImport(w http.ResponseWriter, r *http.Request) { h.batchImport(w, r) } +func (h *Handler) AddKey(w http.ResponseWriter, r *http.Request) { h.addKey(w, r) } +func (h *Handler) UpdateKey(w http.ResponseWriter, r *http.Request) { h.updateKey(w, r) } +func (h *Handler) DeleteKey(w http.ResponseWriter, r *http.Request) { h.deleteKey(w, r) } diff --git a/internal/httpapi/admin/configmgmt/test_helpers_test.go b/internal/httpapi/admin/configmgmt/test_helpers_test.go new file mode 100644 index 0000000000000000000000000000000000000000..1d2f96ceca4d84e183715577a94a7fe5ab12c1d5 --- /dev/null +++ b/internal/httpapi/admin/configmgmt/test_helpers_test.go @@ -0,0 +1,18 @@ +package configmgmt + +import ( + "testing" + + "ds2api/internal/account" + "ds2api/internal/config" +) + +func newAdminTestHandler(t *testing.T, raw string) *Handler { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", raw) + store := config.LoadStore() + return &Handler{ + Store: store, + Pool: account.NewPool(store), + } +} diff --git a/internal/httpapi/admin/devcapture/deps.go b/internal/httpapi/admin/devcapture/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..5eaa7cdda8f5fd2b4c438d6dff8a6cc453e87d20 --- /dev/null +++ b/internal/httpapi/admin/devcapture/deps.go @@ -0,0 +1,16 @@ +package devcapture + +import ( + "ds2api/internal/chathistory" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON diff --git a/internal/httpapi/admin/devcapture/handler_dev_capture.go b/internal/httpapi/admin/devcapture/handler_dev_capture.go new file mode 100644 index 0000000000000000000000000000000000000000..b1f4ced38c1dbd60445921f72f676df631236939 --- /dev/null +++ b/internal/httpapi/admin/devcapture/handler_dev_capture.go @@ -0,0 +1,26 @@ +package devcapture + +import ( + "net/http" + + "ds2api/internal/devcapture" +) + +func (h *Handler) getDevCaptures(w http.ResponseWriter, _ *http.Request) { + store := devcapture.Global() + writeJSON(w, http.StatusOK, map[string]any{ + "enabled": store.Enabled(), + "limit": store.Limit(), + "max_body_bytes": store.MaxBodyBytes(), + "items": store.Snapshot(), + }) +} + +func (h *Handler) clearDevCaptures(w http.ResponseWriter, _ *http.Request) { + store := devcapture.Global() + store.Clear() + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "detail": "capture logs cleared", + }) +} diff --git a/internal/httpapi/admin/devcapture/handler_dev_capture_test.go b/internal/httpapi/admin/devcapture/handler_dev_capture_test.go new file mode 100644 index 0000000000000000000000000000000000000000..a588ccabd27a4203b454bd4df717b9e5fed1edcc --- /dev/null +++ b/internal/httpapi/admin/devcapture/handler_dev_capture_test.go @@ -0,0 +1,45 @@ +package devcapture + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestGetDevCapturesShape(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/admin/dev/captures", nil) + h.getDevCaptures(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode failed: %v", err) + } + if _, ok := out["enabled"]; !ok { + t.Fatalf("expected enabled field, got %#v", out) + } + if _, ok := out["items"]; !ok { + t.Fatalf("expected items field, got %#v", out) + } +} + +func TestClearDevCapturesShape(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodDelete, "/admin/dev/captures", nil) + h.clearDevCaptures(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode failed: %v", err) + } + if out["success"] != true { + t.Fatalf("expected success=true, got %#v", out) + } +} diff --git a/internal/httpapi/admin/devcapture/routes.go b/internal/httpapi/admin/devcapture/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..34e826ad6aaac3f4b8510fa4a14bcb752faf9d91 --- /dev/null +++ b/internal/httpapi/admin/devcapture/routes.go @@ -0,0 +1,8 @@ +package devcapture + +import "github.com/go-chi/chi/v5" + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/dev/captures", h.getDevCaptures) + r.Delete("/dev/captures", h.clearDevCaptures) +} diff --git a/internal/httpapi/admin/handler.go b/internal/httpapi/admin/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..f6f0ee7b849cf96973316ba6fd97371f83d9ad68 --- /dev/null +++ b/internal/httpapi/admin/handler.go @@ -0,0 +1,72 @@ +package admin + +import ( + "github.com/go-chi/chi/v5" + + "ds2api/internal/chathistory" + adminaccounts "ds2api/internal/httpapi/admin/accounts" + adminauth "ds2api/internal/httpapi/admin/auth" + adminconfig "ds2api/internal/httpapi/admin/configmgmt" + admindevcapture "ds2api/internal/httpapi/admin/devcapture" + adminhistory "ds2api/internal/httpapi/admin/history" + adminproxies "ds2api/internal/httpapi/admin/proxies" + adminrawsamples "ds2api/internal/httpapi/admin/rawsamples" + adminsettings "ds2api/internal/httpapi/admin/settings" + adminshared "ds2api/internal/httpapi/admin/shared" + adminvercel "ds2api/internal/httpapi/admin/vercel" + adminversion "ds2api/internal/httpapi/admin/version" + "ds2api/internal/proxyhealth" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store + Checker *proxyhealth.Checker +} + +func RegisterRoutes(r chi.Router, h *Handler) { + deps := adminsharedDeps(h) + authHandler := &adminauth.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + accountsHandler := &adminaccounts.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + configHandler := &adminconfig.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + settingsHandler := &adminsettings.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + proxiesHandler := &adminproxies.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory, Checker: h.Checker} + rawSamplesHandler := &adminrawsamples.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + vercelHandler := &adminvercel.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + historyHandler := &adminhistory.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + devCaptureHandler := &admindevcapture.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + versionHandler := &adminversion.Handler{Store: deps.Store, Pool: deps.Pool, DS: deps.DS, OpenAI: deps.OpenAI, ChatHistory: deps.ChatHistory} + + adminauth.RegisterPublicRoutes(r, authHandler) + r.Group(func(pr chi.Router) { + pr.Use(authHandler.RequireAdmin) + adminauth.RegisterProtectedRoutes(pr, authHandler) + adminconfig.RegisterRoutes(pr, configHandler) + adminsettings.RegisterRoutes(pr, settingsHandler) + adminproxies.RegisterRoutes(pr, proxiesHandler) + adminaccounts.RegisterRoutes(pr, accountsHandler) + adminrawsamples.RegisterRoutes(pr, rawSamplesHandler) + adminvercel.RegisterRoutes(pr, vercelHandler) + admindevcapture.RegisterRoutes(pr, devCaptureHandler) + adminhistory.RegisterRoutes(pr, historyHandler) + adminversion.RegisterRoutes(pr, versionHandler) + }) +} + +func adminsharedDeps(h *Handler) adminsharedDepsValue { + if h == nil { + return adminsharedDepsValue{} + } + return adminsharedDepsValue{Store: h.Store, Pool: h.Pool, DS: h.DS, OpenAI: h.OpenAI, ChatHistory: h.ChatHistory} +} + +type adminsharedDepsValue struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} diff --git a/internal/httpapi/admin/handler_settings_test.go b/internal/httpapi/admin/handler_settings_test.go new file mode 100644 index 0000000000000000000000000000000000000000..44376421f323f9812d917f7ed55fd5b4869f3e2b --- /dev/null +++ b/internal/httpapi/admin/handler_settings_test.go @@ -0,0 +1,857 @@ +package admin + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + authn "ds2api/internal/auth" +) + +func TestGetSettingsDefaultPasswordWarning(t *testing.T) { + t.Setenv("DS2API_ADMIN_KEY", "") + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + req := httptest.NewRequest(http.MethodGet, "/admin/settings", nil) + rec := httptest.NewRecorder() + h.getSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + var body map[string]any + _ = json.Unmarshal(rec.Body.Bytes(), &body) + admin, _ := body["admin"].(map[string]any) + warn, _ := admin["default_password_warning"].(bool) + if !warn { + t.Fatalf("expected default password warning true, body=%v", body) + } +} + +func TestGetSettingsIncludesTokenRefreshInterval(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "runtime":{"token_refresh_interval_hours":9} + }`) + req := httptest.NewRequest(http.MethodGet, "/admin/settings", nil) + rec := httptest.NewRecorder() + h.getSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + var body map[string]any + _ = json.Unmarshal(rec.Body.Bytes(), &body) + runtime, _ := body["runtime"].(map[string]any) + if got := intFrom(runtime["token_refresh_interval_hours"]); got != 9 { + t.Fatalf("expected token_refresh_interval_hours=9, got %d body=%v", got, body) + } +} + +func TestGetSettingsIncludesCurrentInputFileDefaults(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + req := httptest.NewRequest(http.MethodGet, "/admin/settings", nil) + rec := httptest.NewRecorder() + h.getSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + var body map[string]any + _ = json.Unmarshal(rec.Body.Bytes(), &body) + currentInputFile, _ := body["current_input_file"].(map[string]any) + if got := boolFrom(currentInputFile["enabled"]); !got { + t.Fatalf("expected current_input_file.enabled=true, body=%v", body) + } + if got := intFrom(currentInputFile["min_chars"]); got != 0 { + t.Fatalf("expected current_input_file.min_chars=0, got %d body=%v", got, body) + } + thinkingInjection, _ := body["thinking_injection"].(map[string]any) + if got := boolFrom(thinkingInjection["enabled"]); !got { + t.Fatalf("expected thinking_injection.enabled=true, body=%v", body) + } + if got, _ := thinkingInjection["prompt"].(string); got != "" { + t.Fatalf("expected empty custom thinking prompt, got %q body=%v", got, body) + } + if got, _ := thinkingInjection["default_prompt"].(string); got == "" { + t.Fatalf("expected default thinking prompt, body=%v", body) + } +} + +func TestUpdateSettingsValidation(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + payload := map[string]any{ + "runtime": map[string]any{ + "account_max_inflight": 0, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } +} + +func TestUpdateSettingsValidationRejectsTokenRefreshInterval(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + payload := map[string]any{ + "runtime": map[string]any{ + "token_refresh_interval_hours": 0, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } + if !bytes.Contains(rec.Body.Bytes(), []byte("runtime.token_refresh_interval_hours")) { + t.Fatalf("expected token refresh validation detail, got %s", rec.Body.String()) + } +} + +func TestUpdateSettingsAllowsEmptyEmbeddingsProvider(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + payload := map[string]any{ + "responses": map[string]any{ + "store_ttl_seconds": 600, + }, + "embeddings": map[string]any{ + "provider": "", + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if got := h.Store.Snapshot().Responses.StoreTTLSeconds; got != 600 { + t.Fatalf("store_ttl_seconds=%d want=600", got) + } +} + +func TestUpdateSettingsValidationWithMergedRuntimeSnapshot(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "runtime":{ + "account_max_inflight":8, + "global_max_inflight":8 + } + }`) + payload := map[string]any{ + "runtime": map[string]any{ + "account_max_inflight": 16, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } + if !bytes.Contains(rec.Body.Bytes(), []byte("runtime.global_max_inflight")) { + t.Fatalf("expected merged runtime validation detail, got %s", rec.Body.String()) + } +} + +func TestUpdateSettingsWithoutRuntimeSkipsMergedRuntimeValidation(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "runtime":{ + "account_max_inflight":8, + "global_max_inflight":4 + } + }`) + payload := map[string]any{ + "responses": map[string]any{ + "store_ttl_seconds": 600, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if got := h.Store.Snapshot().Responses.StoreTTLSeconds; got != 600 { + t.Fatalf("store_ttl_seconds=%d want=600", got) + } +} + +func TestUpdateSettingsCurrentInputFile(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"],"history_split":{"enabled":true,"trigger_after_turns":2}}`) + payload := map[string]any{ + "current_input_file": map[string]any{ + "enabled": true, + "min_chars": 12345, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.CurrentInputFile.Enabled == nil || !*snap.CurrentInputFile.Enabled { + t.Fatalf("expected current_input_file.enabled=true, got %#v", snap.CurrentInputFile) + } + if snap.CurrentInputFile.MinChars != 12345 { + t.Fatalf("expected current_input_file.min_chars=12345, got %#v", snap.CurrentInputFile) + } + if !h.Store.CurrentInputFileEnabled() { + t.Fatal("expected current input file accessor to stay enabled") + } +} + +func TestUpdateSettingsCurrentInputFilePartialUpdatePreservesEnabled(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"],"current_input_file":{"enabled":false,"min_chars":777}}`) + payload := map[string]any{ + "current_input_file": map[string]any{ + "min_chars": 5000, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.CurrentInputFile.Enabled == nil || *snap.CurrentInputFile.Enabled { + t.Fatalf("expected current_input_file.enabled to remain false, got %#v", snap.CurrentInputFile.Enabled) + } + if snap.CurrentInputFile.MinChars != 5000 { + t.Fatalf("expected current_input_file.min_chars=5000, got %#v", snap.CurrentInputFile) + } +} + +func TestUpdateSettingsCurrentInputFilePartialUpdatePreservesMinChars(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"],"current_input_file":{"enabled":false,"min_chars":777}}`) + payload := map[string]any{ + "current_input_file": map[string]any{ + "enabled": true, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.CurrentInputFile.Enabled == nil || !*snap.CurrentInputFile.Enabled { + t.Fatalf("expected current_input_file.enabled=true, got %#v", snap.CurrentInputFile.Enabled) + } + if snap.CurrentInputFile.MinChars != 777 { + t.Fatalf("expected current_input_file.min_chars to remain 777, got %#v", snap.CurrentInputFile) + } +} + +func TestUpdateSettingsIgnoresHistorySplitPayload(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + payload := map[string]any{ + "history_split": map[string]any{ + "enabled": true, + "trigger_after_turns": 3, + }, + "current_input_file": map[string]any{ + "enabled": true, + "min_chars": 0, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.CurrentInputFile.Enabled == nil || !*snap.CurrentInputFile.Enabled { + t.Fatalf("expected current_input_file to remain enabled, got %#v", snap.CurrentInputFile.Enabled) + } +} + +func TestUpdateSettingsThinkingInjection(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + payload := map[string]any{ + "thinking_injection": map[string]any{ + "enabled": false, + "prompt": " custom thinking prompt ", + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.ThinkingInjection.Enabled == nil || *snap.ThinkingInjection.Enabled { + t.Fatalf("expected thinking_injection.enabled=false, got %#v", snap.ThinkingInjection.Enabled) + } + if h.Store.ThinkingInjectionEnabled() { + t.Fatal("expected thinking injection accessor to reflect disabled config") + } + if got := h.Store.ThinkingInjectionPrompt(); got != "custom thinking prompt" { + t.Fatalf("expected custom thinking prompt, got %q", got) + } +} + +func TestUpdateSettingsThinkingInjectionPartialPromptPreservesEnabled(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"],"thinking_injection":{"enabled":false,"prompt":"original prompt"}}`) + payload := map[string]any{ + "thinking_injection": map[string]any{ + "prompt": " updated prompt ", + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.ThinkingInjection.Enabled == nil || *snap.ThinkingInjection.Enabled { + t.Fatalf("expected thinking_injection.enabled to remain false, got %#v", snap.ThinkingInjection.Enabled) + } + if got := h.Store.ThinkingInjectionPrompt(); got != "updated prompt" { + t.Fatalf("expected updated prompt, got %q", got) + } +} + +func TestUpdateSettingsThinkingInjectionPartialEnabledPreservesPrompt(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"],"thinking_injection":{"enabled":false,"prompt":"original prompt"}}`) + payload := map[string]any{ + "thinking_injection": map[string]any{ + "enabled": true, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.ThinkingInjection.Enabled == nil || !*snap.ThinkingInjection.Enabled { + t.Fatalf("expected thinking_injection.enabled=true, got %#v", snap.ThinkingInjection.Enabled) + } + if got := h.Store.ThinkingInjectionPrompt(); got != "original prompt" { + t.Fatalf("expected original prompt to be preserved, got %q", got) + } +} + +func TestUpdateSettingsAutoDeleteMode(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"],"auto_delete":{"sessions":true}}`) + + payload := map[string]any{ + "auto_delete": map[string]any{ + "mode": "single", + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + + snap := h.Store.Snapshot() + if got := snap.AutoDelete.Mode; got != "single" { + t.Fatalf("auto_delete.mode=%q want=single", got) + } + if got := h.Store.AutoDeleteMode(); got != "single" { + t.Fatalf("AutoDeleteMode()=%q want=single", got) + } +} + +func TestUpdateSettingsHotReloadRuntime(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "accounts":[{"email":"a@test.com","token":"t1"},{"email":"b@test.com","token":"t2"}] + }`) + + payload := map[string]any{ + "runtime": map[string]any{ + "account_max_inflight": 3, + "account_max_queue": 20, + "global_max_inflight": 5, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + status := h.Pool.Status() + if got := intFrom(status["max_inflight_per_account"]); got != 3 { + t.Fatalf("max_inflight_per_account=%d want=3", got) + } + if got := intFrom(status["max_queue_size"]); got != 20 { + t.Fatalf("max_queue_size=%d want=20", got) + } + if got := intFrom(status["global_max_inflight"]); got != 5 { + t.Fatalf("global_max_inflight=%d want=5", got) + } +} + +func TestUpdateSettingsHotReloadTokenRefreshInterval(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "runtime":{"token_refresh_interval_hours":6} + }`) + + payload := map[string]any{ + "runtime": map[string]any{ + "token_refresh_interval_hours": 12, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPut, "/admin/settings", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettings(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + if got := h.Store.RuntimeTokenRefreshIntervalHours(); got != 12 { + t.Fatalf("token_refresh_interval_hours=%d want=12", got) + } +} + +func TestUpdateConfigPreservesStructuredAPIKeysWhenBothFieldsPresent(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["legacy"], + "api_keys":[{"key":"legacy","name":"primary","remark":"prod"}], + "accounts":[] + }`) + + payload := map[string]any{ + "keys": []any{"legacy", "new-key"}, + "api_keys": []any{ + map[string]any{"key": "legacy", "name": "primary-updated", "remark": "prod-updated"}, + map[string]any{"key": "new-key", "name": "secondary", "remark": "staging"}, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPost, "/admin/config", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateConfig(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.Keys) != 2 || snap.Keys[0] != "legacy" || snap.Keys[1] != "new-key" { + t.Fatalf("unexpected keys after config update: %#v", snap.Keys) + } + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after config update: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary-updated" || snap.APIKeys[0].Remark != "prod-updated" { + t.Fatalf("structured metadata for existing key was not preserved: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Name != "secondary" || snap.APIKeys[1].Remark != "staging" { + t.Fatalf("structured metadata for new key was not preserved: %#v", snap.APIKeys[1]) + } +} + +func TestUpdateConfigLegacyKeysPreserveStructuredMetadata(t *testing.T) { + h := newAdminTestHandler(t, `{ + "api_keys":[{"key":"legacy","name":"primary","remark":"prod"}], + "accounts":[] + }`) + + payload := map[string]any{ + "keys": []any{"legacy", "new-key"}, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPost, "/admin/config", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateConfig(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.Keys) != 2 || snap.Keys[0] != "legacy" || snap.Keys[1] != "new-key" { + t.Fatalf("unexpected keys after legacy config update: %#v", snap.Keys) + } + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after legacy config update: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary" || snap.APIKeys[0].Remark != "prod" { + t.Fatalf("existing structured metadata was lost: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Key != "new-key" || snap.APIKeys[1].Name != "" || snap.APIKeys[1].Remark != "" { + t.Fatalf("new legacy key should remain metadata-free: %#v", snap.APIKeys[1]) + } +} + +func TestUpdateConfigReplacesModelAliases(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "model_aliases":{"claude-sonnet-4-6":"deepseek-v4-flash"} + }`) + + payload := map[string]any{ + "model_aliases": map[string]any{ + "gpt-5.5": "deepseek-v4-pro", + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPost, "/admin/config", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateConfig(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.ModelAliases) != 1 { + t.Fatalf("expected aliases to be replaced, got %#v", snap.ModelAliases) + } + if snap.ModelAliases["gpt-5.5"] != "deepseek-v4-pro" { + t.Fatalf("expected updated alias, got %#v", snap.ModelAliases) + } +} + +func TestUpdateSettingsPasswordInvalidatesOldJWT(t *testing.T) { + hash := authn.HashAdminPassword("old-password") + h := newAdminTestHandler(t, `{"admin":{"password_hash":"`+hash+`"}}`) + + token, err := authn.CreateJWTWithStore(1, h.Store) + if err != nil { + t.Fatalf("create jwt failed: %v", err) + } + if _, err := authn.VerifyJWTWithStore(token, h.Store); err != nil { + t.Fatalf("verify before update failed: %v", err) + } + + body := map[string]any{"new_password": "new-password"} + b, _ := json.Marshal(body) + req := httptest.NewRequest(http.MethodPost, "/admin/settings/password", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateSettingsPassword(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + + if _, err := authn.VerifyJWTWithStore(token, h.Store); err == nil { + t.Fatal("expected old token to be invalid after password update") + } + if !authn.VerifyAdminCredential("new-password", h.Store) { + t.Fatal("expected new password credential to be accepted") + } +} + +func TestConfigImportMergeAndReplace(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "accounts":[{"email":"a@test.com","password":"p1"}] + }`) + + merge := map[string]any{ + "mode": "merge", + "config": map[string]any{ + "keys": []any{"k1", "k2"}, + "accounts": []any{ + map[string]any{"email": "a@test.com", "password": "p1"}, + map[string]any{"email": "b@test.com", "password": "p2"}, + }, + }, + } + mergeBytes, _ := json.Marshal(merge) + mergeReq := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=merge", bytes.NewReader(mergeBytes)) + mergeRec := httptest.NewRecorder() + h.configImport(mergeRec, mergeReq) + if mergeRec.Code != http.StatusOK { + t.Fatalf("merge status=%d body=%s", mergeRec.Code, mergeRec.Body.String()) + } + if got := len(h.Store.Keys()); got != 2 { + t.Fatalf("keys after merge=%d want=2", got) + } + if got := len(h.Store.Accounts()); got != 2 { + t.Fatalf("accounts after merge=%d want=2", got) + } + + replace := map[string]any{ + "mode": "replace", + "config": map[string]any{ + "keys": []any{"k9"}, + }, + } + replaceBytes, _ := json.Marshal(replace) + replaceReq := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=replace", bytes.NewReader(replaceBytes)) + replaceRec := httptest.NewRecorder() + h.configImport(replaceRec, replaceReq) + if replaceRec.Code != http.StatusOK { + t.Fatalf("replace status=%d body=%s", replaceRec.Code, replaceRec.Body.String()) + } + keys := h.Store.Keys() + if len(keys) != 1 || keys[0] != "k9" { + t.Fatalf("unexpected keys after replace: %#v", keys) + } + if got := len(h.Store.Accounts()); got != 0 { + t.Fatalf("accounts after replace=%d want=0", got) + } +} + +func TestConfigImportMergePreservesStructuredAPIKeys(t *testing.T) { + h := newAdminTestHandler(t, `{ + "api_keys":[{"key":"k1","name":"primary","remark":"prod"}] + }`) + + merge := map[string]any{ + "mode": "merge", + "config": map[string]any{ + "api_keys": []any{ + map[string]any{"key": "k1", "name": "should-not-overwrite", "remark": "ignored"}, + map[string]any{"key": "k2", "name": "secondary", "remark": "staging"}, + }, + }, + } + mergeBytes, _ := json.Marshal(merge) + mergeReq := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=merge", bytes.NewReader(mergeBytes)) + mergeRec := httptest.NewRecorder() + h.configImport(mergeRec, mergeReq) + if mergeRec.Code != http.StatusOK { + t.Fatalf("merge status=%d body=%s", mergeRec.Code, mergeRec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after structured merge: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary" || snap.APIKeys[0].Remark != "prod" { + t.Fatalf("existing structured metadata was overwritten: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Name != "secondary" || snap.APIKeys[1].Remark != "staging" { + t.Fatalf("new structured metadata was lost: %#v", snap.APIKeys[1]) + } +} + +func TestConfigImportMergeUpgradesLegacyAPIKeys(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["legacy"], + "accounts":[] + }`) + + merge := map[string]any{ + "mode": "merge", + "config": map[string]any{ + "api_keys": []any{ + map[string]any{"key": "legacy", "name": "primary", "remark": "prod"}, + map[string]any{"key": "new-key", "name": "secondary", "remark": "staging"}, + }, + }, + } + mergeBytes, _ := json.Marshal(merge) + mergeReq := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=merge", bytes.NewReader(mergeBytes)) + mergeRec := httptest.NewRecorder() + h.configImport(mergeRec, mergeReq) + if mergeRec.Code != http.StatusOK { + t.Fatalf("merge status=%d body=%s", mergeRec.Code, mergeRec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.Keys) != 2 || snap.Keys[0] != "legacy" || snap.Keys[1] != "new-key" { + t.Fatalf("unexpected keys after legacy import merge: %#v", snap.Keys) + } + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after legacy import merge: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary" || snap.APIKeys[0].Remark != "prod" { + t.Fatalf("legacy key metadata was not upgraded: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Name != "secondary" || snap.APIKeys[1].Remark != "staging" { + t.Fatalf("new structured metadata was not preserved: %#v", snap.APIKeys[1]) + } +} + +func TestBatchImportUpgradesLegacyAPIKeys(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["legacy"], + "accounts":[] + }`) + + payload := map[string]any{ + "keys": []any{"legacy", "new-key"}, + "api_keys": []any{ + map[string]any{"key": "legacy", "name": "primary", "remark": "prod"}, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPost, "/admin/import", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.batchImport(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + + snap := h.Store.Snapshot() + if len(snap.Keys) != 2 || snap.Keys[0] != "legacy" || snap.Keys[1] != "new-key" { + t.Fatalf("unexpected keys after batch import: %#v", snap.Keys) + } + if len(snap.APIKeys) != 2 { + t.Fatalf("unexpected api keys after batch import: %#v", snap.APIKeys) + } + if snap.APIKeys[0].Name != "primary" || snap.APIKeys[0].Remark != "prod" { + t.Fatalf("legacy key metadata was not upgraded: %#v", snap.APIKeys[0]) + } + if snap.APIKeys[1].Name != "" || snap.APIKeys[1].Remark != "" { + t.Fatalf("new batch-imported key should stay metadata-free: %#v", snap.APIKeys[1]) + } +} + +func TestConfigImportAppliesTokenRefreshInterval(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + + replace := map[string]any{ + "mode": "replace", + "config": map[string]any{ + "keys": []any{"k9"}, + "runtime": map[string]any{ + "token_refresh_interval_hours": 11, + }, + }, + } + replaceBytes, _ := json.Marshal(replace) + replaceReq := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=replace", bytes.NewReader(replaceBytes)) + replaceRec := httptest.NewRecorder() + h.configImport(replaceRec, replaceReq) + if replaceRec.Code != http.StatusOK { + t.Fatalf("replace status=%d body=%s", replaceRec.Code, replaceRec.Body.String()) + } + if got := h.Store.RuntimeTokenRefreshIntervalHours(); got != 11 { + t.Fatalf("token_refresh_interval_hours=%d want=11", got) + } +} + +func TestConfigImportRejectsInvalidRuntimeBounds(t *testing.T) { + h := newAdminTestHandler(t, `{"keys":["k1"]}`) + payload := map[string]any{ + "mode": "replace", + "config": map[string]any{ + "keys": []any{"k2"}, + "runtime": map[string]any{ + "account_max_inflight": 300, + }, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=replace", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.configImport(rec, req) + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } + if !bytes.Contains(rec.Body.Bytes(), []byte("runtime.account_max_inflight")) { + t.Fatalf("expected runtime bound detail, got %s", rec.Body.String()) + } + keys := h.Store.Keys() + if len(keys) != 1 || keys[0] != "k1" { + t.Fatalf("store should remain unchanged, keys=%v", keys) + } +} + +func TestConfigImportRejectsMergedRuntimeConflict(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "runtime":{ + "account_max_inflight":8, + "global_max_inflight":8 + } + }`) + payload := map[string]any{ + "mode": "merge", + "config": map[string]any{ + "runtime": map[string]any{ + "account_max_inflight": 16, + }, + }, + } + b, _ := json.Marshal(payload) + req := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=merge", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.configImport(rec, req) + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } + if !bytes.Contains(rec.Body.Bytes(), []byte("runtime.global_max_inflight")) { + t.Fatalf("expected merged runtime validation detail, got %s", rec.Body.String()) + } + snap := h.Store.Snapshot() + if snap.Runtime.AccountMaxInflight != 8 || snap.Runtime.GlobalMaxInflight != 8 { + t.Fatalf("runtime should remain unchanged, runtime=%+v", snap.Runtime) + } +} + +func TestConfigImportMergeDedupesMobileAliases(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "accounts":[{"mobile":"+8613800138000","password":"p1"}] + }`) + + merge := map[string]any{ + "mode": "merge", + "config": map[string]any{ + "accounts": []any{ + map[string]any{"mobile": "13800138000", "password": "p2"}, + }, + }, + } + b, _ := json.Marshal(merge) + req := httptest.NewRequest(http.MethodPost, "/admin/config/import?mode=merge", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.configImport(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + if got := len(h.Store.Accounts()); got != 1 { + t.Fatalf("expected merge dedupe by canonical mobile, got=%d", got) + } +} + +func TestUpdateConfigDedupesMobileAliases(t *testing.T) { + h := newAdminTestHandler(t, `{ + "keys":["k1"], + "accounts":[{"mobile":"+8613800138000","password":"old"}] + }`) + + reqBody := map[string]any{ + "accounts": []any{ + map[string]any{"mobile": "+8613800138000"}, + map[string]any{"mobile": "13800138000"}, + }, + } + b, _ := json.Marshal(reqBody) + req := httptest.NewRequest(http.MethodPost, "/admin/config", bytes.NewReader(b)) + rec := httptest.NewRecorder() + h.updateConfig(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + accounts := h.Store.Accounts() + if len(accounts) != 1 { + t.Fatalf("expected update dedupe by canonical mobile, got=%d", len(accounts)) + } + if accounts[0].Identifier() != "+8613800138000" { + t.Fatalf("unexpected identifier: %q", accounts[0].Identifier()) + } +} diff --git a/internal/httpapi/admin/handler_test.go b/internal/httpapi/admin/handler_test.go new file mode 100644 index 0000000000000000000000000000000000000000..aa2db2420124977dfdb6f4a8b2d13fba24cab218 --- /dev/null +++ b/internal/httpapi/admin/handler_test.go @@ -0,0 +1,143 @@ +package admin + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "sync/atomic" + "testing" + "time" + + "ds2api/internal/config" +) + +func TestToAccountMissingFieldsRemainEmpty(t *testing.T) { + acc := toAccount(map[string]any{ + "email": "user@example.com", + "password": "secret", + }) + if acc.Email != "user@example.com" { + t.Fatalf("unexpected email: %q", acc.Email) + } + if acc.Mobile != "" { + t.Fatalf("expected empty mobile, got %q", acc.Mobile) + } + if acc.Token != "" { + t.Fatalf("expected empty token, got %q", acc.Token) + } +} + +func TestFieldStringNilToEmpty(t *testing.T) { + if got := fieldString(map[string]any{"token": nil}, "token"); got != "" { + t.Fatalf("expected empty string for nil field, got %q", got) + } + if got := fieldString(map[string]any{}, "token"); got != "" { + t.Fatalf("expected empty string for missing field, got %q", got) + } +} + +func TestMaskSecretPreviewKeepsOnlyFirstAndLastTwoChars(t *testing.T) { + cases := map[string]string{ + "": "", + "a": "*", + "ab": "**", + "abcd": "****", + "abcdef": "ab****ef", + "abc12345": "ab****45", + } + + for input, want := range cases { + if got := maskSecretPreview(input); got != want { + t.Fatalf("maskSecretPreview(%q)=%q want %q", input, got, want) + } + } +} + +func TestGetConfigMasksAccountTokenPreview(t *testing.T) { + h := newAdminTestHandler(t, `{ + "accounts":[{"email":"u@example.com","password":"pwd"}] + }`) + if err := h.Store.UpdateAccountToken("u@example.com", "abcdefgh"); err != nil { + t.Fatalf("seed runtime token: %v", err) + } + + req := httptest.NewRequest(http.MethodGet, "/admin/config", nil) + rec := httptest.NewRecorder() + h.getConfig(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response failed: %v", err) + } + accounts, _ := payload["accounts"].([]any) + if len(accounts) != 1 { + t.Fatalf("expected 1 account, got %d", len(accounts)) + } + first, _ := accounts[0].(map[string]any) + if got, _ := first["token_preview"].(string); got != "ab****gh" { + t.Fatalf("expected masked token preview, got %q", got) + } +} + +func TestRunAccountTestsConcurrentlyKeepsInputOrder(t *testing.T) { + accounts := []config.Account{ + {Email: "a@example.com"}, + {Email: "b@example.com"}, + {Email: "c@example.com"}, + } + results := runAccountTestsConcurrently(accounts, 2, func(idx int, acc config.Account) map[string]any { + return map[string]any{ + "idx": idx, + "account": acc.Identifier(), + } + }) + if len(results) != len(accounts) { + t.Fatalf("unexpected result length: got %d want %d", len(results), len(accounts)) + } + for i := range accounts { + gotIdx, _ := results[i]["idx"].(int) + if gotIdx != i { + t.Fatalf("result index mismatch at %d: got %d", i, gotIdx) + } + gotID, _ := results[i]["account"].(string) + if gotID != accounts[i].Identifier() { + t.Fatalf("result order mismatch at %d: got %q want %q", i, gotID, accounts[i].Identifier()) + } + } +} + +func TestRunAccountTestsConcurrentlyRespectsLimit(t *testing.T) { + const limit = 3 + accounts := []config.Account{ + {Email: "1@example.com"}, + {Email: "2@example.com"}, + {Email: "3@example.com"}, + {Email: "4@example.com"}, + {Email: "5@example.com"}, + {Email: "6@example.com"}, + } + var current int32 + var maxSeen int32 + _ = runAccountTestsConcurrently(accounts, limit, func(_ int, _ config.Account) map[string]any { + c := atomic.AddInt32(¤t, 1) + for { + m := atomic.LoadInt32(&maxSeen) + if c <= m || atomic.CompareAndSwapInt32(&maxSeen, m, c) { + break + } + } + time.Sleep(20 * time.Millisecond) + atomic.AddInt32(¤t, -1) + return map[string]any{"success": true} + }) + if maxSeen > limit { + t.Fatalf("concurrency exceeded limit: got %d > %d", maxSeen, limit) + } + if maxSeen < 2 { + t.Fatalf("expected concurrent execution, max seen %d", maxSeen) + } +} diff --git a/internal/httpapi/admin/history/deps.go b/internal/httpapi/admin/history/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..755259646613360236bd0667d0df075abe4dfa3a --- /dev/null +++ b/internal/httpapi/admin/history/deps.go @@ -0,0 +1,16 @@ +package history + +import ( + "ds2api/internal/chathistory" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON diff --git a/internal/httpapi/admin/history/handler_chat_history.go b/internal/httpapi/admin/history/handler_chat_history.go new file mode 100644 index 0000000000000000000000000000000000000000..8072a2a489eb9f27e11968182e3789ed4b8c649f --- /dev/null +++ b/internal/httpapi/admin/history/handler_chat_history.go @@ -0,0 +1,171 @@ +package history + +import ( + "encoding/json" + "net/http" + "strings" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/chathistory" +) + +func (h *Handler) getChatHistory(w http.ResponseWriter, r *http.Request) { + store := h.ChatHistory + if store == nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{"detail": "chat history store is not configured"}) + return + } + ifNoneMatch := strings.TrimSpace(r.Header.Get("If-None-Match")) + if ifNoneMatch != "" { + revision, err := store.Revision() + if err != nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{ + "detail": err.Error(), + "path": store.Path(), + }) + return + } + etag := chathistory.ListETag(revision) + w.Header().Set("ETag", etag) + w.Header().Set("Cache-Control", "no-cache") + if ifNoneMatch == etag { + w.WriteHeader(http.StatusNotModified) + return + } + } + snapshot, err := store.Snapshot() + if err != nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{ + "detail": err.Error(), + "path": store.Path(), + }) + return + } + etag := chathistory.ListETag(snapshot.Revision) + w.Header().Set("ETag", etag) + w.Header().Set("Cache-Control", "no-cache") + if ifNoneMatch == etag { + w.WriteHeader(http.StatusNotModified) + return + } + writeJSON(w, http.StatusOK, map[string]any{ + "version": snapshot.Version, + "limit": snapshot.Limit, + "revision": snapshot.Revision, + "items": snapshot.Items, + "path": store.Path(), + }) +} + +func (h *Handler) getChatHistoryItem(w http.ResponseWriter, r *http.Request) { + store := h.ChatHistory + if store == nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{"detail": "chat history store is not configured"}) + return + } + id := strings.TrimSpace(chi.URLParam(r, "id")) + if id == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "history id is required"}) + return + } + ifNoneMatch := strings.TrimSpace(r.Header.Get("If-None-Match")) + if ifNoneMatch != "" { + revision, err := store.DetailRevision(id) + if err != nil { + status := http.StatusInternalServerError + if strings.Contains(strings.ToLower(err.Error()), "not found") { + status = http.StatusNotFound + } + writeJSON(w, status, map[string]any{"detail": err.Error()}) + return + } + etag := chathistory.DetailETag(id, revision) + w.Header().Set("ETag", etag) + w.Header().Set("Cache-Control", "no-cache") + if ifNoneMatch == etag { + w.WriteHeader(http.StatusNotModified) + return + } + } + item, err := store.Get(id) + if err != nil { + status := http.StatusInternalServerError + if strings.Contains(strings.ToLower(err.Error()), "not found") { + status = http.StatusNotFound + } + writeJSON(w, status, map[string]any{"detail": err.Error()}) + return + } + etag := chathistory.DetailETag(item.ID, item.Revision) + w.Header().Set("ETag", etag) + w.Header().Set("Cache-Control", "no-cache") + if ifNoneMatch == etag { + w.WriteHeader(http.StatusNotModified) + return + } + writeJSON(w, http.StatusOK, map[string]any{ + "item": item, + }) +} + +func (h *Handler) clearChatHistory(w http.ResponseWriter, _ *http.Request) { + store := h.ChatHistory + if store == nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{"detail": "chat history store is not configured"}) + return + } + if err := store.Clear(); err != nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{"detail": err.Error(), "path": store.Path()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true}) +} + +func (h *Handler) deleteChatHistoryItem(w http.ResponseWriter, r *http.Request) { + store := h.ChatHistory + if store == nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{"detail": "chat history store is not configured"}) + return + } + id := strings.TrimSpace(chi.URLParam(r, "id")) + if id == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "history id is required"}) + return + } + if err := store.Delete(id); err != nil { + status := http.StatusInternalServerError + if strings.Contains(strings.ToLower(err.Error()), "not found") { + status = http.StatusNotFound + } + writeJSON(w, status, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true}) +} + +func (h *Handler) updateChatHistorySettings(w http.ResponseWriter, r *http.Request) { + store := h.ChatHistory + if store == nil { + writeJSON(w, http.StatusServiceUnavailable, map[string]any{"detail": "chat history store is not configured"}) + return + } + var body struct { + Limit int `json:"limit"` + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + snapshot, err := store.SetLimit(body.Limit) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "limit": snapshot.Limit, + "revision": snapshot.Revision, + "items": snapshot.Items, + }) +} diff --git a/internal/httpapi/admin/history/handler_chat_history_test.go b/internal/httpapi/admin/history/handler_chat_history_test.go new file mode 100644 index 0000000000000000000000000000000000000000..4d3e32fe83a168be29bca53cda6d40affd19c81f --- /dev/null +++ b/internal/httpapi/admin/history/handler_chat_history_test.go @@ -0,0 +1,185 @@ +package history + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/chathistory" + "ds2api/internal/config" +) + +func newChatHistoryAdminHarness(t *testing.T) (*Handler, *chathistory.Store) { + t.Helper() + dir := t.TempDir() + configPath := filepath.Join(dir, "config.json") + if err := os.WriteFile(configPath, []byte(`{}`), 0o644); err != nil { + t.Fatalf("write config failed: %v", err) + } + t.Setenv("DS2API_CONFIG_PATH", configPath) + t.Setenv("DS2API_ADMIN_KEY", "admin") + t.Setenv("DS2API_CONFIG_JSON", "") + store, err := config.LoadStoreWithError() + if err != nil { + t.Fatalf("load config store failed: %v", err) + } + historyStore := chathistory.New(filepath.Join(dir, "chat_history.json")) + return &Handler{Store: store, ChatHistory: historyStore}, historyStore +} + +func TestGetChatHistoryAndUpdateSettings(t *testing.T) { + h, historyStore := newChatHistoryAdminHarness(t) + entry, err := historyStore.Start(chathistory.StartParams{ + CallerID: "caller:test", + AccountID: "user@example.com", + Model: "deepseek-v4-flash", + UserInput: "hello", + }) + if err != nil { + t.Fatalf("start history failed: %v", err) + } + if _, err := historyStore.Update(entry.ID, chathistory.UpdateParams{ + Status: "success", + Content: "world", + Completed: true, + }); err != nil { + t.Fatalf("update history failed: %v", err) + } + + r := chi.NewRouter() + RegisterRoutes(r, h) + + req := httptest.NewRequest(http.MethodGet, "/chat-history", nil) + req.Header.Set("Authorization", "Bearer admin") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode payload failed: %v", err) + } + items, _ := payload["items"].([]any) + if len(items) != 1 { + t.Fatalf("expected one history item, got %#v", payload) + } + if rec.Header().Get("ETag") == "" { + t.Fatalf("expected list etag header") + } + + notModifiedReq := httptest.NewRequest(http.MethodGet, "/chat-history", nil) + notModifiedReq.Header.Set("Authorization", "Bearer admin") + notModifiedReq.Header.Set("If-None-Match", rec.Header().Get("ETag")) + notModifiedRec := httptest.NewRecorder() + r.ServeHTTP(notModifiedRec, notModifiedReq) + if notModifiedRec.Code != http.StatusNotModified { + t.Fatalf("expected 304, got %d body=%s", notModifiedRec.Code, notModifiedRec.Body.String()) + } + + itemReq := httptest.NewRequest(http.MethodGet, "/chat-history/"+entry.ID, nil) + itemReq.Header.Set("Authorization", "Bearer admin") + itemRec := httptest.NewRecorder() + r.ServeHTTP(itemRec, itemReq) + if itemRec.Code != http.StatusOK { + t.Fatalf("expected item 200, got %d body=%s", itemRec.Code, itemRec.Body.String()) + } + if itemRec.Header().Get("ETag") == "" { + t.Fatalf("expected detail etag header") + } + + notModifiedItemReq := httptest.NewRequest(http.MethodGet, "/chat-history/"+entry.ID, nil) + notModifiedItemReq.Header.Set("Authorization", "Bearer admin") + notModifiedItemReq.Header.Set("If-None-Match", itemRec.Header().Get("ETag")) + notModifiedItemRec := httptest.NewRecorder() + r.ServeHTTP(notModifiedItemRec, notModifiedItemReq) + if notModifiedItemRec.Code != http.StatusNotModified { + t.Fatalf("expected detail 304, got %d body=%s", notModifiedItemRec.Code, notModifiedItemRec.Body.String()) + } + + updateReq := httptest.NewRequest(http.MethodPut, "/chat-history/settings", bytes.NewReader([]byte(`{"limit":10}`))) + updateReq.Header.Set("Authorization", "Bearer admin") + updateRec := httptest.NewRecorder() + r.ServeHTTP(updateRec, updateReq) + if updateRec.Code != http.StatusOK { + t.Fatalf("expected 200 from settings update, got %d body=%s", updateRec.Code, updateRec.Body.String()) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if snapshot.Limit != 10 { + t.Fatalf("expected limit=10, got %d", snapshot.Limit) + } + + disableReq := httptest.NewRequest(http.MethodPut, "/chat-history/settings", bytes.NewReader([]byte(`{"limit":0}`))) + disableReq.Header.Set("Authorization", "Bearer admin") + disableRec := httptest.NewRecorder() + r.ServeHTTP(disableRec, disableReq) + if disableRec.Code != http.StatusOK { + t.Fatalf("expected 200 from disable update, got %d body=%s", disableRec.Code, disableRec.Body.String()) + } + snapshot, err = historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot after disable failed: %v", err) + } + if snapshot.Limit != chathistory.DisabledLimit { + t.Fatalf("expected limit=0, got %d", snapshot.Limit) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected history preserved when disabled, got %d", len(snapshot.Items)) + } +} + +func TestDeleteAndClearChatHistory(t *testing.T) { + h, historyStore := newChatHistoryAdminHarness(t) + entryA, err := historyStore.Start(chathistory.StartParams{UserInput: "a"}) + if err != nil { + t.Fatalf("start A failed: %v", err) + } + if _, err := historyStore.Start(chathistory.StartParams{UserInput: "b"}); err != nil { + t.Fatalf("start B failed: %v", err) + } + + r := chi.NewRouter() + RegisterRoutes(r, h) + + deleteReq := httptest.NewRequest(http.MethodDelete, "/chat-history/"+entryA.ID, nil) + deleteReq.Header.Set("Authorization", "Bearer admin") + deleteRec := httptest.NewRecorder() + r.ServeHTTP(deleteRec, deleteReq) + if deleteRec.Code != http.StatusOK { + t.Fatalf("expected delete 200, got %d body=%s", deleteRec.Code, deleteRec.Body.String()) + } + + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one item after delete, got %d", len(snapshot.Items)) + } + + clearReq := httptest.NewRequest(http.MethodDelete, "/chat-history", nil) + clearReq.Header.Set("Authorization", "Bearer admin") + clearRec := httptest.NewRecorder() + r.ServeHTTP(clearRec, clearReq) + if clearRec.Code != http.StatusOK { + t.Fatalf("expected clear 200, got %d body=%s", clearRec.Code, clearRec.Body.String()) + } + + snapshot, err = historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 0 { + t.Fatalf("expected empty items after clear, got %d", len(snapshot.Items)) + } +} diff --git a/internal/httpapi/admin/history/routes.go b/internal/httpapi/admin/history/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..c6f1f43bac9fe85237b5742cbf75c5410155aec6 --- /dev/null +++ b/internal/httpapi/admin/history/routes.go @@ -0,0 +1,11 @@ +package history + +import "github.com/go-chi/chi/v5" + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/chat-history", h.getChatHistory) + r.Get("/chat-history/{id}", h.getChatHistoryItem) + r.Delete("/chat-history", h.clearChatHistory) + r.Delete("/chat-history/{id}", h.deleteChatHistoryItem) + r.Put("/chat-history/settings", h.updateChatHistorySettings) +} diff --git a/internal/httpapi/admin/proxies/deps.go b/internal/httpapi/admin/proxies/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..ac3435ae4aea9b368119040b286594c52ed136f9 --- /dev/null +++ b/internal/httpapi/admin/proxies/deps.go @@ -0,0 +1,34 @@ +package proxies + +import ( + "ds2api/internal/chathistory" + "ds2api/internal/config" + adminshared "ds2api/internal/httpapi/admin/shared" + "ds2api/internal/proxyhealth" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store + Checker *proxyhealth.Checker +} + +var writeJSON = adminshared.WriteJSON + +func fieldString(m map[string]any, key string) string { + return adminshared.FieldString(m, key) +} +func accountMatchesIdentifier(acc config.Account, identifier string) bool { + return adminshared.AccountMatchesIdentifier(acc, identifier) +} +func toProxy(m map[string]any) config.Proxy { return adminshared.ToProxy(m) } +func findProxyByID(c config.Config, proxyID string) (config.Proxy, bool) { + return adminshared.FindProxyByID(c, proxyID) +} +func newRequestError(detail string) error { return adminshared.NewRequestError(detail) } +func requestErrorDetail(err error) (string, bool) { + return adminshared.RequestErrorDetail(err) +} diff --git a/internal/httpapi/admin/proxies/handler_proxies.go b/internal/httpapi/admin/proxies/handler_proxies.go new file mode 100644 index 0000000000000000000000000000000000000000..a5d2d8beb7d7b3b53a938ae15de526ec4e559aca --- /dev/null +++ b/internal/httpapi/admin/proxies/handler_proxies.go @@ -0,0 +1,296 @@ +package proxies + +import ( + "context" + "encoding/json" + "net/http" + "net/url" + "strings" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/proxyhealth" +) + +var proxyConnectivityTester = func(ctx context.Context, proxy config.Proxy) map[string]any { + return dsclient.TestProxyConnectivity(ctx, proxy) +} + +func validateProxyMutation(cfg *config.Config) error { + if cfg == nil { + return nil + } + if err := config.ValidateProxyConfig(cfg.Proxies); err != nil { + return err + } + return config.ValidateAccountProxyReferences(cfg.Accounts, cfg.Proxies) +} + +func proxyResponse(proxy config.Proxy) map[string]any { + proxy = config.NormalizeProxy(proxy) + return map[string]any{ + "id": proxy.ID, + "name": proxy.Name, + "type": proxy.Type, + "host": proxy.Host, + "port": proxy.Port, + "username": proxy.Username, + "has_password": strings.TrimSpace(proxy.Password) != "", + "disabled": proxy.Disabled, + } +} + +func (h *Handler) listProxies(w http.ResponseWriter, _ *http.Request) { + proxies := h.Store.Snapshot().Proxies + healthResults := h.checkerResults() + items := make([]map[string]any, 0, len(proxies)) + for _, proxy := range proxies { + proxy = config.NormalizeProxy(proxy) + item := map[string]any{ + "id": proxy.ID, + "name": proxy.Name, + "type": proxy.Type, + "host": proxy.Host, + "port": proxy.Port, + "username": proxy.Username, + "has_password": strings.TrimSpace(proxy.Password) != "", + "disabled": proxy.Disabled, + } + if hr, ok := healthResults[proxy.ID]; ok { + item["health"] = map[string]any{ + "healthy": hr.Healthy, + "disabled": hr.Disabled, + "message": hr.Message, + "response_time": hr.ResponseTime, + "checked_at": hr.CheckedAt, + "retries_used": hr.RetriesUsed, + } + } + items = append(items, item) + } + writeJSON(w, http.StatusOK, map[string]any{"items": items, "total": len(items)}) +} + +func (h *Handler) addProxy(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + proxy := toProxy(req) + err := h.Store.Update(func(c *config.Config) error { + c.Proxies = append(c.Proxies, proxy) + return validateProxyMutation(c) + }) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "proxy": proxyResponse(proxy)}) +} + +func (h *Handler) updateProxy(w http.ResponseWriter, r *http.Request) { + proxyID := chi.URLParam(r, "proxyID") + if decoded, err := url.PathUnescape(proxyID); err == nil { + proxyID = decoded + } + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + proxy := toProxy(req) + proxy.ID = strings.TrimSpace(proxyID) + + err := h.Store.Update(func(c *config.Config) error { + for i, existing := range c.Proxies { + existing = config.NormalizeProxy(existing) + if existing.ID != proxy.ID { + continue + } + if proxy.Password == "" { + proxy.Password = existing.Password + } + c.Proxies[i] = proxy + return validateProxyMutation(c) + } + return newRequestError("代理不存在") + }) + if err != nil { + if detail, ok := requestErrorDetail(err); ok { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": detail}) + return + } + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "proxy": proxyResponse(proxy)}) +} + +func (h *Handler) deleteProxy(w http.ResponseWriter, r *http.Request) { + proxyID := chi.URLParam(r, "proxyID") + if decoded, err := url.PathUnescape(proxyID); err == nil { + proxyID = decoded + } + err := h.Store.Update(func(c *config.Config) error { + idx := -1 + for i, existing := range c.Proxies { + existing = config.NormalizeProxy(existing) + if existing.ID == strings.TrimSpace(proxyID) { + idx = i + break + } + } + if idx < 0 { + return newRequestError("代理不存在") + } + c.Proxies = append(c.Proxies[:idx], c.Proxies[idx+1:]...) + for i := range c.Accounts { + if strings.TrimSpace(c.Accounts[i].ProxyID) == strings.TrimSpace(proxyID) { + c.Accounts[i].ProxyID = "" + } + } + return validateProxyMutation(c) + }) + if err != nil { + if detail, ok := requestErrorDetail(err); ok { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": detail}) + return + } + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true}) +} + +func (h *Handler) testProxy(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + proxyID := fieldString(req, "proxy_id") + + var proxy config.Proxy + if proxyID != "" { + var ok bool + proxy, ok = findProxyByID(h.Store.Snapshot(), proxyID) + if !ok { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": "代理不存在"}) + return + } + } else { + proxy = toProxy(req) + } + + result := proxyConnectivityTester(r.Context(), proxy) + writeJSON(w, http.StatusOK, result) +} + +func (h *Handler) updateAccountProxy(w http.ResponseWriter, r *http.Request) { + identifier := chi.URLParam(r, "identifier") + if decoded, err := url.PathUnescape(identifier); err == nil { + identifier = decoded + } + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + proxyID := fieldString(req, "proxy_id") + + err := h.Store.Update(func(c *config.Config) error { + if proxyID != "" { + if _, ok := findProxyByID(*c, proxyID); !ok { + return newRequestError("代理不存在") + } + } + for i, acc := range c.Accounts { + if !accountMatchesIdentifier(acc, identifier) { + continue + } + c.Accounts[i].ProxyID = proxyID + return validateProxyMutation(c) + } + return newRequestError("账号不存在") + }) + if err != nil { + if detail, ok := requestErrorDetail(err); ok { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": detail}) + return + } + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + h.Pool.Reset() + writeJSON(w, http.StatusOK, map[string]any{"success": true, "proxy_id": proxyID}) +} + +func (h *Handler) unbanProxy(w http.ResponseWriter, r *http.Request) { + proxyID := chi.URLParam(r, "proxyID") + if decoded, err := url.PathUnescape(proxyID); err == nil { + proxyID = decoded + } + proxyID = strings.TrimSpace(proxyID) + if proxyID == "" { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "proxy_id 不能为空"}) + return + } + + if h.Checker == nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": "代理健康检查器未启用"}) + return + } + + if err := h.Checker.UnbanProxy(proxyID); err != nil { + writeJSON(w, http.StatusNotFound, map[string]any{"detail": err.Error()}) + return + } + writeJSON(w, http.StatusOK, map[string]any{"success": true, "proxy_id": proxyID}) +} + +func (h *Handler) proxyHealth(w http.ResponseWriter, _ *http.Request) { + if h.Checker == nil { + writeJSON(w, http.StatusOK, map[string]any{"items": []any{}, "total": 0}) + return + } + results := h.Checker.Results() + items := make([]map[string]any, 0, len(results)) + for _, r := range results { + items = append(items, map[string]any{ + "proxy_id": r.ProxyID, + "healthy": r.Healthy, + "disabled": r.Disabled, + "message": r.Message, + "response_time": r.ResponseTime, + "checked_at": r.CheckedAt, + "retries_used": r.RetriesUsed, + }) + } + writeJSON(w, http.StatusOK, map[string]any{"items": items, "total": len(items)}) +} + +func (h *Handler) checkerResults() map[string]*proxyhealth.CheckResult { + if h.Checker == nil { + return nil + } + return h.Checker.Results() +} + +func (h *Handler) checkAllProxies(w http.ResponseWriter, r *http.Request) { + if h.Checker == nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": "代理健康检查器未启用"}) + return + } + + results := h.Checker.CheckAll(r.Context()) + items := make([]map[string]any, 0, len(results)) + for _, r := range results { + items = append(items, map[string]any{ + "proxy_id": r.ProxyID, + "healthy": r.Healthy, + "disabled": r.Disabled, + "message": r.Message, + "response_time": r.ResponseTime, + "checked_at": r.CheckedAt, + "retries_used": r.RetriesUsed, + }) + } + + // Refresh pool in case accounts were migrated during auto-ban. + if h.Pool != nil { + h.Pool.Reset() + } + + writeJSON(w, http.StatusOK, map[string]any{"items": items, "total": len(items)}) +} diff --git a/internal/httpapi/admin/proxies/handler_proxies_test.go b/internal/httpapi/admin/proxies/handler_proxies_test.go new file mode 100644 index 0000000000000000000000000000000000000000..2c6a81cc508048ec6893b04327953d5877f9526e --- /dev/null +++ b/internal/httpapi/admin/proxies/handler_proxies_test.go @@ -0,0 +1,227 @@ +package proxies + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/account" + "ds2api/internal/config" +) + +func newAdminProxyTestHandler(t *testing.T, raw string) *Handler { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", raw) + store := config.LoadStore() + return &Handler{ + Store: store, + Pool: account.NewPool(store), + } +} + +func TestAddProxyPersistsNormalizedProxy(t *testing.T) { + h := newAdminProxyTestHandler(t, `{"accounts":[]}`) + + r := chi.NewRouter() + r.Post("/admin/proxies", h.addProxy) + + req := httptest.NewRequest(http.MethodPost, "/admin/proxies", bytes.NewBufferString(`{ + "name":" HK Exit ", + "type":" SOCKS5H ", + "host":" 127.0.0.1 ", + "port":1081, + "username":" user ", + "password":" pass " + }`)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + proxies := h.Store.Snapshot().Proxies + if len(proxies) != 1 { + t.Fatalf("expected 1 proxy, got %d", len(proxies)) + } + if proxies[0].Name != "HK Exit" { + t.Fatalf("unexpected proxy name: %#v", proxies[0]) + } + if proxies[0].Type != "socks5h" { + t.Fatalf("unexpected proxy type: %#v", proxies[0]) + } + if proxies[0].Username != "user" || proxies[0].Password != "pass" { + t.Fatalf("expected trimmed credentials, got %#v", proxies[0]) + } + if proxies[0].ID == "" { + t.Fatalf("expected generated proxy id, got %#v", proxies[0]) + } +} + +func TestAddProxyDoesNotFailOnUnrelatedInvalidRuntimeConfig(t *testing.T) { + router := newHTTPAdminHarness(t, `{ + "keys":["k1"], + "runtime":{ + "account_max_inflight":8, + "global_max_inflight":4 + } + }`, &testingDSMock{}) + + rec := httptest.NewRecorder() + router.ServeHTTP(rec, adminReq(http.MethodPost, "/proxies", []byte(`{ + "name":"HK Exit", + "type":"socks5h", + "host":"127.0.0.1", + "port":1080 + }`))) + + if rec.Code != http.StatusOK { + t.Fatalf("expected add proxy success despite unrelated runtime issue, got %d body=%s", rec.Code, rec.Body.String()) + } + + readRec := httptest.NewRecorder() + router.ServeHTTP(readRec, adminReq(http.MethodGet, "/config", nil)) + if readRec.Code != http.StatusOK { + t.Fatalf("config read status=%d body=%s", readRec.Code, readRec.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(readRec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode config response: %v", err) + } + proxies, _ := payload["proxies"].([]any) + if len(proxies) != 1 { + t.Fatalf("expected proxy to be persisted, got %#v", payload["proxies"]) + } +} + +func TestDeleteProxyClearsAssignedAccountProxyID(t *testing.T) { + h := newAdminProxyTestHandler(t, `{ + "proxies":[{"id":"proxy-1","name":"Node 1","type":"socks5","host":"127.0.0.1","port":1080}], + "accounts":[{"email":"u@example.com","password":"pwd","proxy_id":"proxy-1"}] + }`) + + r := chi.NewRouter() + r.Delete("/admin/proxies/{proxyID}", h.deleteProxy) + + req := httptest.NewRequest(http.MethodDelete, "/admin/proxies/proxy-1", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + snap := h.Store.Snapshot() + if len(snap.Proxies) != 0 { + t.Fatalf("expected proxy removed, got %#v", snap.Proxies) + } + if len(snap.Accounts) != 1 { + t.Fatalf("expected account kept, got %#v", snap.Accounts) + } + if snap.Accounts[0].ProxyID != "" { + t.Fatalf("expected proxy assignment cleared, got %#v", snap.Accounts[0]) + } +} + +func TestUpdateProxyResponseDoesNotExposeStoredPassword(t *testing.T) { + h := newAdminProxyTestHandler(t, `{ + "proxies":[{"id":"proxy-1","name":"Node 1","type":"socks5h","host":"127.0.0.1","port":1080,"username":"u","password":"secret"}] + }`) + + r := chi.NewRouter() + r.Put("/admin/proxies/{proxyID}", h.updateProxy) + + req := httptest.NewRequest(http.MethodPut, "/admin/proxies/proxy-1", bytes.NewBufferString(`{ + "name":"Node 1", + "type":"socks5h", + "host":"127.0.0.2", + "port":1081, + "username":"u2" + }`)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response: %v", err) + } + proxy, _ := payload["proxy"].(map[string]any) + if _, exists := proxy["password"]; exists { + t.Fatalf("response should not expose password, got %#v", proxy) + } + if hasPassword, _ := proxy["has_password"].(bool); !hasPassword { + t.Fatalf("expected has_password=true, got %#v", proxy) + } +} + +func TestUpdateAccountProxyAssignsProxyID(t *testing.T) { + h := newAdminProxyTestHandler(t, `{ + "proxies":[{"id":"proxy-1","name":"Node 1","type":"socks5h","host":"127.0.0.1","port":1080}], + "accounts":[{"email":"u@example.com","password":"pwd"}] + }`) + + r := chi.NewRouter() + r.Put("/admin/accounts/{identifier}/proxy", h.updateAccountProxy) + + req := httptest.NewRequest(http.MethodPut, "/admin/accounts/u@example.com/proxy", bytes.NewBufferString(`{"proxy_id":"proxy-1"}`)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + acc, ok := h.Store.FindAccount("u@example.com") + if !ok { + t.Fatal("expected account") + } + if acc.ProxyID != "proxy-1" { + t.Fatalf("expected proxy assigned, got %#v", acc) + } +} + +func TestTestProxyUsesStoredProxy(t *testing.T) { + h := newAdminProxyTestHandler(t, `{ + "proxies":[{"id":"proxy-1","name":"Node 1","type":"socks5h","host":"127.0.0.1","port":1080}] + }`) + + original := proxyConnectivityTester + defer func() { proxyConnectivityTester = original }() + + var got config.Proxy + proxyConnectivityTester = func(_ context.Context, proxy config.Proxy) map[string]any { + got = proxy + return map[string]any{ + "success": true, + "proxy_id": proxy.ID, + "proxy_type": proxy.Type, + "response_time": 12, + } + } + + r := chi.NewRouter() + r.Post("/admin/proxies/test", h.testProxy) + + req := httptest.NewRequest(http.MethodPost, "/admin/proxies/test", bytes.NewBufferString(`{"proxy_id":"proxy-1"}`)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + if got.ID != "proxy-1" || got.Type != "socks5h" { + t.Fatalf("expected stored proxy passed to tester, got %#v", got) + } + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("decode response: %v", err) + } + if ok, _ := payload["success"].(bool); !ok { + t.Fatalf("expected success payload, got %#v", payload) + } +} diff --git a/internal/httpapi/admin/proxies/routes.go b/internal/httpapi/admin/proxies/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..a07a9961d370f68196a92a4137357593faad22d1 --- /dev/null +++ b/internal/httpapi/admin/proxies/routes.go @@ -0,0 +1,27 @@ +package proxies + +import ( + "net/http" + + "github.com/go-chi/chi/v5" +) + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/proxies", h.listProxies) + r.Post("/proxies", h.addProxy) + r.Put("/proxies/{proxyID}", h.updateProxy) + r.Delete("/proxies/{proxyID}", h.deleteProxy) + r.Post("/proxies/test", h.testProxy) + r.Post("/proxies/check-all", h.checkAllProxies) + r.Post("/proxies/{proxyID}/unban", h.unbanProxy) + r.Get("/proxies/health", h.proxyHealth) + r.Put("/accounts/{identifier}/proxy", h.updateAccountProxy) +} + +func (h *Handler) AddProxy(w http.ResponseWriter, r *http.Request) { h.addProxy(w, r) } +func (h *Handler) UpdateProxy(w http.ResponseWriter, r *http.Request) { h.updateProxy(w, r) } +func (h *Handler) DeleteProxy(w http.ResponseWriter, r *http.Request) { h.deleteProxy(w, r) } +func (h *Handler) TestProxy(w http.ResponseWriter, r *http.Request) { h.testProxy(w, r) } +func (h *Handler) UpdateAccountProxy(w http.ResponseWriter, r *http.Request) { + h.updateAccountProxy(w, r) +} diff --git a/internal/httpapi/admin/proxies/test_http_helpers_test.go b/internal/httpapi/admin/proxies/test_http_helpers_test.go new file mode 100644 index 0000000000000000000000000000000000000000..96c609e4ca3e7f4dbbbbfe39cab8de1454c39fe4 --- /dev/null +++ b/internal/httpapi/admin/proxies/test_http_helpers_test.go @@ -0,0 +1,57 @@ +package proxies + +import ( + "bytes" + "context" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + adminconfig "ds2api/internal/httpapi/admin/configmgmt" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type testingDSMock struct{} + +func (m *testingDSMock) Login(_ context.Context, _ config.Account) (string, error) { + return "token", nil +} +func (m *testingDSMock) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} +func (m *testingDSMock) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} +func (m *testingDSMock) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil +} +func (m *testingDSMock) DeleteAllSessionsForToken(_ context.Context, _ string) error { return nil } +func (m *testingDSMock) GetSessionCountForToken(_ context.Context, _ string) (*dsclient.SessionStats, error) { + return &dsclient.SessionStats{}, nil +} + +func newHTTPAdminHarness(t *testing.T, rawConfig string, ds adminshared.DeepSeekCaller) http.Handler { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", rawConfig) + store := config.LoadStore() + pool := account.NewPool(store) + h := &Handler{Store: store, Pool: pool, DS: ds} + configHandler := &adminconfig.Handler{Store: store, Pool: pool, DS: ds} + r := chi.NewRouter() + RegisterRoutes(r, h) + r.Get("/config", configHandler.GetConfig) + return r +} + +func adminReq(method, path string, body []byte) *http.Request { + req := httptest.NewRequest(method, path, bytes.NewReader(body)) + req.Header.Set("Authorization", "Bearer admin") + req.Header.Set("Content-Type", "application/json") + return req +} diff --git a/internal/httpapi/admin/rawsamples/deps.go b/internal/httpapi/admin/rawsamples/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..618d0d18b5e314f0421b27528f0e9fd87ee16f4d --- /dev/null +++ b/internal/httpapi/admin/rawsamples/deps.go @@ -0,0 +1,27 @@ +package rawsamples + +import ( + "net/http" + + "ds2api/internal/chathistory" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON + +func intFromQuery(r *http.Request, key string, d int) int { + return adminshared.IntFromQuery(r, key, d) +} +func nilIfEmpty(s string) any { return adminshared.NilIfEmpty(s) } +func toStringSlice(v any) ([]string, bool) { return adminshared.ToStringSlice(v) } +func fieldString(m map[string]any, key string) string { + return adminshared.FieldString(m, key) +} diff --git a/internal/httpapi/admin/rawsamples/handler_raw_samples.go b/internal/httpapi/admin/rawsamples/handler_raw_samples.go new file mode 100644 index 0000000000000000000000000000000000000000..df86077d5c6ce0a206c044b1a6bc824c12d7f60c --- /dev/null +++ b/internal/httpapi/admin/rawsamples/handler_raw_samples.go @@ -0,0 +1,553 @@ +package rawsamples + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "net/http" + "net/http/httptest" + "net/url" + "sort" + "strings" + + "ds2api/internal/config" + "ds2api/internal/devcapture" + adminshared "ds2api/internal/httpapi/admin/shared" + "ds2api/internal/rawsample" + "ds2api/internal/util" +) + +type captureChain struct { + Key string + Entries []devcapture.Entry +} + +func (h *Handler) captureRawSample(w http.ResponseWriter, r *http.Request) { + if h.OpenAI == nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": "OpenAI handler is not configured"}) + return + } + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + + payload, sampleID, apiKey, err := prepareRawSampleCaptureRequest(h.Store, req) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + + body, err := json.Marshal(payload) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": "failed to encode capture request"}) + return + } + + traceID := rawsample.NormalizeSampleID(sampleID) + if traceID == "" { + traceID = rawsample.DefaultSampleID("capture") + } + + before := devcapture.Global().Snapshot() + rec := httptest.NewRecorder() + captureReq := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__trace_id="+url.QueryEscape(traceID), bytes.NewReader(body)) + captureReq.Header.Set("Authorization", "Bearer "+apiKey) + captureReq.Header.Set("Content-Type", "application/json") + h.OpenAI.ChatCompletions(rec, captureReq) + after := devcapture.Global().Snapshot() + + if rec.Code >= http.StatusBadRequest { + copyHeader(w.Header(), rec.Header()) + w.WriteHeader(rec.Code) + _, _ = io.Copy(w, bytes.NewReader(rec.Body.Bytes())) + return + } + + captureEntries, err := collectNewCaptureEntries(before, after) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + + saved, err := rawsample.Persist(rawsample.PersistOptions{ + RootDir: config.RawStreamSampleRoot(), + SampleID: sampleID, + Source: "admin/dev/raw-samples/capture", + Request: payload, + Capture: captureSummaryFromEntries(captureEntries), + UpstreamBody: combineCaptureBodies(captureEntries), + }) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + + copyHeader(w.Header(), rec.Header()) + w.Header().Set("X-Ds2-Sample-Id", saved.SampleID) + w.Header().Set("X-Ds2-Sample-Dir", saved.Dir) + w.Header().Set("X-Ds2-Sample-Meta", saved.MetaPath) + w.Header().Set("X-Ds2-Sample-Upstream", saved.UpstreamPath) + w.WriteHeader(rec.Code) + _, _ = io.Copy(w, bytes.NewReader(rec.Body.Bytes())) +} + +func prepareRawSampleCaptureRequest(store adminshared.ConfigStore, req map[string]any) (map[string]any, string, string, error) { + payload := cloneMap(req) + sampleID := strings.TrimSpace(fieldString(payload, "sample_id")) + apiKey := strings.TrimSpace(fieldString(payload, "api_key")) + + for _, k := range []string{"sample_id", "api_key", "promote_default", "persist", "source"} { + delete(payload, k) + } + + if apiKey == "" { + if store == nil { + return nil, "", "", fmt.Errorf("no api key provided") + } + keys := store.Keys() + if len(keys) == 0 { + return nil, "", "", fmt.Errorf("no api key available") + } + apiKey = strings.TrimSpace(keys[0]) + } + + if model := strings.TrimSpace(fieldString(payload, "model")); model == "" { + payload["model"] = "deepseek-v4-flash" + } + if _, ok := payload["stream"]; !ok { + payload["stream"] = true + } + + if messagesRaw, ok := payload["messages"].([]any); !ok || len(messagesRaw) == 0 { + message := strings.TrimSpace(fieldString(payload, "message")) + if message == "" { + message = "你好" + } + payload["messages"] = []map[string]any{{"role": "user", "content": message}} + } + delete(payload, "message") + + if sampleID == "" { + model := strings.TrimSpace(fieldString(payload, "model")) + if model == "" { + model = "capture" + } + sampleID = rawsample.DefaultSampleID(model) + } + + return payload, sampleID, apiKey, nil +} + +func collectNewCaptureEntries(before, after []devcapture.Entry) ([]devcapture.Entry, error) { + beforeIDs := make(map[string]struct{}, len(before)) + for _, entry := range before { + beforeIDs[entry.ID] = struct{}{} + } + + entries := make([]devcapture.Entry, 0, len(after)) + for _, entry := range after { + if _, ok := beforeIDs[entry.ID]; ok { + continue + } + if strings.TrimSpace(entry.ResponseBody) == "" { + continue + } + entries = append(entries, entry) + } + if len(entries) == 0 { + return nil, fmt.Errorf("no upstream capture was recorded") + } + + // Snapshot order is newest-first; reverse to preserve the actual request order. + for i, j := 0, len(entries)-1; i < j; i, j = i+1, j-1 { + entries[i], entries[j] = entries[j], entries[i] + } + return entries, nil +} + +func captureSummaryFromEntries(entries []devcapture.Entry) rawsample.CaptureSummary { + if len(entries) == 0 { + return rawsample.CaptureSummary{} + } + + // Primary metadata comes from the first (initial) capture. + summary := rawsample.CaptureSummary{ + Label: strings.TrimSpace(entries[0].Label), + URL: strings.TrimSpace(entries[0].URL), + StatusCode: entries[0].StatusCode, + } + + // Record every round (initial + continuations) so replay/debug + // can reconstruct the full multi-round interaction. + totalBytes := 0 + rounds := make([]rawsample.CaptureRound, 0, len(entries)) + for _, entry := range entries { + n := len(entry.ResponseBody) + totalBytes += n + rounds = append(rounds, rawsample.CaptureRound{ + Label: strings.TrimSpace(entry.Label), + URL: strings.TrimSpace(entry.URL), + StatusCode: entry.StatusCode, + ResponseBytes: n, + }) + } + summary.ResponseBytes = totalBytes + if len(rounds) > 1 { + summary.Rounds = rounds + } + return summary +} + +func combineCaptureBodies(entries []devcapture.Entry) []byte { + if len(entries) == 0 { + return nil + } + + var buf bytes.Buffer + for _, entry := range entries { + if buf.Len() > 0 { + last := buf.Bytes()[buf.Len()-1] + if last != '\n' { + buf.WriteByte('\n') + } + } + buf.WriteString(entry.ResponseBody) + } + return buf.Bytes() +} + +func copyHeader(dst, src http.Header) { + for k, vv := range src { + dst.Del(k) + for _, v := range vv { + dst.Add(k, v) + } + } +} + +func cloneMap(in map[string]any) map[string]any { + if len(in) == 0 { + return map[string]any{} + } + out := make(map[string]any, len(in)) + for k, v := range in { + out[k] = v + } + return out +} + +func (h *Handler) queryRawSampleCaptures(w http.ResponseWriter, r *http.Request) { + query := strings.TrimSpace(r.URL.Query().Get("q")) + limit := intFromQuery(r, "limit", 20) + if limit <= 0 { + limit = 20 + } + if limit > 50 { + limit = 50 + } + + chains := buildCaptureChains(devcapture.Global().Snapshot()) + items := make([]map[string]any, 0, len(chains)) + for _, chain := range chains { + if query != "" && !captureChainMatchesQuery(chain, query) { + continue + } + items = append(items, buildCaptureChainQueryItem(chain, query)) + if len(items) >= limit { + break + } + } + + writeJSON(w, http.StatusOK, map[string]any{ + "query": query, + "limit": limit, + "count": len(items), + "items": items, + }) +} + +func (h *Handler) saveRawSampleFromCaptures(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + + snapshot := devcapture.Global().Snapshot() + if len(snapshot) == 0 { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "no capture logs available"}) + return + } + + chain, err := resolveCaptureChainSelection(snapshot, req) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + + sampleID := strings.TrimSpace(fieldString(req, "sample_id")) + source := strings.TrimSpace(fieldString(req, "source")) + if source == "" { + source = "admin/dev/raw-samples/save" + } + requestPayload := captureChainRequestPayload(chain) + + saved, err := rawsample.Persist(rawsample.PersistOptions{ + RootDir: config.RawStreamSampleRoot(), + SampleID: sampleID, + Source: source, + Request: requestPayload, + Capture: captureSummaryFromEntries(chain.Entries), + UpstreamBody: combineCaptureBodies(chain.Entries), + }) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "sample_id": saved.SampleID, + "sample_dir": saved.Dir, + "meta_path": saved.MetaPath, + "upstream_path": saved.UpstreamPath, + "chain_key": chain.Key, + "capture_ids": captureChainIDs(chain), + "round_count": len(chain.Entries), + }) +} + +func buildCaptureChains(snapshot []devcapture.Entry) []captureChain { + if len(snapshot) == 0 { + return nil + } + ordered := make([]devcapture.Entry, len(snapshot)) + // devcapture snapshots are newest-first because the store prepends entries. + // Reverse once so equal-second timestamps can preserve the actual capture + // order (completion before continue) under the stable CreatedAt sort below. + for i := range snapshot { + ordered[len(snapshot)-1-i] = snapshot[i] + } + sort.SliceStable(ordered, func(i, j int) bool { + return ordered[i].CreatedAt < ordered[j].CreatedAt + }) + + byKey := make(map[string]*captureChain, len(ordered)) + keys := make([]string, 0, len(ordered)) + for _, entry := range ordered { + key := captureChainKey(entry) + if key == "" { + key = "capture:" + entry.ID + } + if _, ok := byKey[key]; !ok { + byKey[key] = &captureChain{Key: key} + keys = append(keys, key) + } + byKey[key].Entries = append(byKey[key].Entries, entry) + } + + chains := make([]captureChain, 0, len(keys)) + for _, key := range keys { + chains = append(chains, *byKey[key]) + } + sort.SliceStable(chains, func(i, j int) bool { + return latestCreatedAt(chains[i]) > latestCreatedAt(chains[j]) + }) + return chains +} + +func captureChainKey(entry devcapture.Entry) string { + req := parseCaptureRequestBody(entry.RequestBody) + if sessionID := strings.TrimSpace(fieldString(req, "chat_session_id")); sessionID != "" { + return "session:" + sessionID + } + return "capture:" + entry.ID +} + +func parseCaptureRequestBody(raw string) map[string]any { + raw = strings.TrimSpace(raw) + if raw == "" { + return nil + } + var out map[string]any + if err := json.Unmarshal([]byte(raw), &out); err != nil { + return nil + } + return out +} + +func latestCreatedAt(chain captureChain) int64 { + var latest int64 + for _, entry := range chain.Entries { + if entry.CreatedAt > latest { + latest = entry.CreatedAt + } + } + return latest +} + +func captureChainMatchesQuery(chain captureChain, query string) bool { + query = strings.ToLower(strings.TrimSpace(query)) + if query == "" { + return true + } + for _, entry := range chain.Entries { + hay := strings.ToLower(strings.Join([]string{ + entry.Label, + entry.URL, + entry.AccountID, + entry.RequestBody, + entry.ResponseBody, + }, "\n")) + if strings.Contains(hay, query) { + return true + } + } + return false +} + +func buildCaptureChainQueryItem(chain captureChain, query string) map[string]any { + first := chain.Entries[0] + last := chain.Entries[len(chain.Entries)-1] + requestPreview := previewCaptureChainRequest(chain) + responsePreview := previewCaptureChainResponse(chain) + + return map[string]any{ + "chain_key": chain.Key, + "capture_ids": captureChainIDs(chain), + "created_at": latestCreatedAt(chain), + "round_count": len(chain.Entries), + "account_id": nilIfEmpty(strings.TrimSpace(first.AccountID)), + "initial_label": first.Label, + "initial_url": first.URL, + "latest_label": last.Label, + "latest_url": last.URL, + "request_preview": requestPreview, + "response_preview": responsePreview, + "query": query, + "response_truncated": captureChainHasTruncatedResponse(chain), + } +} + +func captureChainIDs(chain captureChain) []string { + out := make([]string, 0, len(chain.Entries)) + for _, entry := range chain.Entries { + out = append(out, entry.ID) + } + return out +} + +func previewCaptureChainRequest(chain captureChain) string { + for _, entry := range chain.Entries { + req := parseCaptureRequestBody(entry.RequestBody) + if prompt := strings.TrimSpace(fieldString(req, "prompt")); prompt != "" { + return previewText(prompt, 280) + } + if messages, ok := req["messages"].([]any); ok { + var parts []string + for _, item := range messages { + m, _ := item.(map[string]any) + content := strings.TrimSpace(fieldString(m, "content")) + if content != "" { + parts = append(parts, content) + } + } + if len(parts) > 0 { + return previewText(strings.Join(parts, "\n"), 280) + } + } + } + return previewText(strings.TrimSpace(chain.Entries[0].RequestBody), 280) +} + +func previewCaptureChainResponse(chain captureChain) string { + var b strings.Builder + for _, entry := range chain.Entries { + if b.Len() > 0 { + b.WriteByte('\n') + } + b.WriteString(strings.TrimSpace(entry.ResponseBody)) + if b.Len() >= 280 { + break + } + } + return previewText(b.String(), 280) +} + +func previewText(text string, limit int) string { + text = strings.TrimSpace(text) + if limit <= 0 { + return text + } + if truncated, ok := util.TruncateRunes(text, limit); ok { + return truncated + "..." + } + return text +} + +func captureChainHasTruncatedResponse(chain captureChain) bool { + for _, entry := range chain.Entries { + if entry.ResponseTruncated { + return true + } + } + return false +} + +func resolveCaptureChainSelection(snapshot []devcapture.Entry, req map[string]any) (captureChain, error) { + chains := buildCaptureChains(snapshot) + if len(chains) == 0 { + return captureChain{}, fmt.Errorf("no capture logs available") + } + + if chainKey := strings.TrimSpace(fieldString(req, "chain_key")); chainKey != "" { + for _, chain := range chains { + if chain.Key == chainKey { + return chain, nil + } + } + return captureChain{}, fmt.Errorf("capture chain not found") + } + + captureID := strings.TrimSpace(fieldString(req, "capture_id")) + if captureID == "" { + if ids, ok := toStringSlice(req["capture_ids"]); ok && len(ids) > 0 { + captureID = strings.TrimSpace(ids[0]) + } + } + if captureID != "" { + for _, chain := range chains { + for _, entry := range chain.Entries { + if entry.ID == captureID { + return chain, nil + } + } + } + return captureChain{}, fmt.Errorf("capture id not found") + } + + query := strings.TrimSpace(fieldString(req, "query")) + if query != "" { + for _, chain := range chains { + if captureChainMatchesQuery(chain, query) { + return chain, nil + } + } + return captureChain{}, fmt.Errorf("no capture chain matched query") + } + + return captureChain{}, fmt.Errorf("capture_id, chain_key, or query is required") +} + +func captureChainRequestPayload(chain captureChain) any { + for _, entry := range chain.Entries { + if req := parseCaptureRequestBody(entry.RequestBody); req != nil { + return req + } + } + return strings.TrimSpace(chain.Entries[0].RequestBody) +} diff --git a/internal/httpapi/admin/rawsamples/handler_raw_samples_test.go b/internal/httpapi/admin/rawsamples/handler_raw_samples_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c4756d12d646b317b91d24c2de241007bda1d23d --- /dev/null +++ b/internal/httpapi/admin/rawsamples/handler_raw_samples_test.go @@ -0,0 +1,400 @@ +package rawsamples + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" + "unicode/utf8" + + "ds2api/internal/devcapture" +) + +type stubOpenAIChatCaller struct{} + +func (stubOpenAIChatCaller) ChatCompletions(w http.ResponseWriter, _ *http.Request) { + store := devcapture.Global() + session := store.Start("deepseek_completion", "https://chat.deepseek.com/api/v0/chat/completion", "acct-test", map[string]any{"model": "deepseek-v4-flash"}) + raw := io.NopCloser(strings.NewReader( + "data: {\"v\":\"hello [reference:1]\"}\n\n" + + "data: {\"v\":\"FINISHED\",\"p\":\"response/status\"}\n\n", + )) + if session != nil { + raw = session.WrapBody(raw, http.StatusOK) + } + _, _ = io.ReadAll(raw) + _ = raw.Close() + + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(http.StatusOK) + _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"hello\"},\"index\":0}],\"created\":1,\"id\":\"id\",\"model\":\"m\",\"object\":\"chat.completion.chunk\"}\n\n") +} + +type stubOpenAIChatCallerWithContinuations struct{} + +func (stubOpenAIChatCallerWithContinuations) ChatCompletions(w http.ResponseWriter, _ *http.Request) { + recordCapturedResponse("deepseek_completion", "https://chat.deepseek.com/api/v0/chat/completion", http.StatusOK, map[string]any{"model": "deepseek-v4-flash"}, "data: {\"v\":\"hello [reference:1]\"}\n\n"+"data: [DONE]\n\n") + recordCapturedResponse("deepseek_continue", "https://chat.deepseek.com/api/v0/chat/continue", http.StatusOK, map[string]any{"chat_session_id": "session-1", "message_id": 2}, "data: {\"v\":\"continued\"}\n\n"+"data: [DONE]\n\n") + + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(http.StatusOK) + _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"hello continued\"},\"index\":0}],\"created\":1,\"id\":\"id\",\"model\":\"m\",\"object\":\"chat.completion.chunk\"}\n\n") +} + +type stubOpenAIChatCallerWithoutCapture struct{} + +func (stubOpenAIChatCallerWithoutCapture) ChatCompletions(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(http.StatusOK) + _, _ = io.WriteString(w, "data: {\"choices\":[{\"delta\":{\"content\":\"hello\"},\"index\":0}],\"created\":1,\"id\":\"id\",\"model\":\"m\",\"object\":\"chat.completion.chunk\"}\n\n") +} + +func recordCapturedResponse(label, rawURL string, statusCode int, request any, body string) { + store := devcapture.Global() + session := store.Start(label, rawURL, "acct-test", request) + raw := io.NopCloser(strings.NewReader(body)) + if session != nil { + raw = session.WrapBody(raw, statusCode) + } + _, _ = io.ReadAll(raw) + _ = raw.Close() +} + +func TestCaptureRawSampleWritesPersistentSample(t *testing.T) { + t.Setenv("DS2API_RAW_STREAM_SAMPLE_ROOT", t.TempDir()) + devcapture.Global().Clear() + defer devcapture.Global().Clear() + + h := &Handler{OpenAI: stubOpenAIChatCaller{}} + reqBody := `{ + "sample_id":"My Sample 01", + "api_key":"local-key", + "model":"deepseek-v4-flash", + "message":"广州天气", + "stream":true + }` + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/admin/dev/raw-samples/capture", strings.NewReader(reqBody)) + h.captureRawSample(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if got := rec.Header().Get("X-Ds2-Sample-Id"); got != "my-sample-01" { + t.Fatalf("expected sample id header my-sample-01, got %q", got) + } + if got := rec.Header().Get("X-Ds2-Sample-Upstream"); got != filepath.Join(os.Getenv("DS2API_RAW_STREAM_SAMPLE_ROOT"), "my-sample-01", "upstream.stream.sse") { + t.Fatalf("unexpected sample upstream header: %q", got) + } + if !strings.Contains(rec.Body.String(), `"content":"hello"`) { + t.Fatalf("expected proxied openai output, got %s", rec.Body.String()) + } + + sampleDir := filepath.Join(os.Getenv("DS2API_RAW_STREAM_SAMPLE_ROOT"), "my-sample-01") + if _, err := os.Stat(sampleDir); err != nil { + t.Fatalf("sample dir missing: %v", err) + } + metaBytes, err := os.ReadFile(filepath.Join(sampleDir, "meta.json")) + if err != nil { + t.Fatalf("read meta: %v", err) + } + var meta map[string]any + if err := json.Unmarshal(metaBytes, &meta); err != nil { + t.Fatalf("decode meta: %v", err) + } + if meta["sample_id"] != "my-sample-01" { + t.Fatalf("unexpected meta sample_id: %#v", meta["sample_id"]) + } + capture, _ := meta["capture"].(map[string]any) + if capture == nil { + t.Fatalf("missing capture meta: %#v", meta) + } + if got := int(capture["response_bytes"].(float64)); got == 0 { + t.Fatalf("expected capture bytes to be recorded, got %#v", capture) + } + if _, ok := meta["processed"]; ok { + t.Fatalf("unexpected processed meta: %#v", meta["processed"]) + } +} + +func TestCaptureRawSampleCombinesContinuationCaptures(t *testing.T) { + t.Setenv("DS2API_RAW_STREAM_SAMPLE_ROOT", t.TempDir()) + devcapture.Global().Clear() + defer devcapture.Global().Clear() + + h := &Handler{OpenAI: stubOpenAIChatCallerWithContinuations{}} + reqBody := `{ + "sample_id":"My Sample 02", + "api_key":"local-key", + "model":"deepseek-v4-flash", + "message":"广州天气", + "stream":true + }` + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/admin/dev/raw-samples/capture", strings.NewReader(reqBody)) + h.captureRawSample(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + + sampleDir := filepath.Join(os.Getenv("DS2API_RAW_STREAM_SAMPLE_ROOT"), "my-sample-02") + upstreamBytes, err := os.ReadFile(filepath.Join(sampleDir, "upstream.stream.sse")) + if err != nil { + t.Fatalf("read upstream: %v", err) + } + upstream := string(upstreamBytes) + if !strings.Contains(upstream, "hello [reference:1]") { + t.Fatalf("expected initial capture in combined upstream, got %s", upstream) + } + if !strings.Contains(upstream, "continued") { + t.Fatalf("expected continuation capture in combined upstream, got %s", upstream) + } + if strings.Index(upstream, "hello [reference:1]") > strings.Index(upstream, "continued") { + t.Fatalf("expected initial capture before continuation, got %s", upstream) + } + + metaBytes, err := os.ReadFile(filepath.Join(sampleDir, "meta.json")) + if err != nil { + t.Fatalf("read meta: %v", err) + } + var meta map[string]any + if err := json.Unmarshal(metaBytes, &meta); err != nil { + t.Fatalf("decode meta: %v", err) + } + capture, _ := meta["capture"].(map[string]any) + if capture == nil { + t.Fatalf("missing capture meta: %#v", meta) + } + if got := int(capture["response_bytes"].(float64)); got != len(upstreamBytes) { + t.Fatalf("expected combined response_bytes %d, got %#v", len(upstreamBytes), capture["response_bytes"]) + } + + rounds, _ := capture["rounds"].([]any) + if len(rounds) != 2 { + t.Fatalf("expected 2 capture rounds, got %d: %#v", len(rounds), capture) + } + r0, _ := rounds[0].(map[string]any) + r1, _ := rounds[1].(map[string]any) + if r0["label"] != "deepseek_completion" { + t.Fatalf("expected first round label deepseek_completion, got %v", r0["label"]) + } + if r1["label"] != "deepseek_continue" { + t.Fatalf("expected second round label deepseek_continue, got %v", r1["label"]) + } +} + +func TestCaptureRawSampleReturnsErrorWhenNoNewCaptureRecorded(t *testing.T) { + root := t.TempDir() + t.Setenv("DS2API_RAW_STREAM_SAMPLE_ROOT", root) + devcapture.Global().Clear() + defer devcapture.Global().Clear() + + recordCapturedResponse("preexisting", "https://chat.deepseek.com/api/v0/chat/completion", http.StatusOK, map[string]any{"model": "deepseek-v4-flash"}, "data: {\"v\":\"old\"}\n\n") + + h := &Handler{OpenAI: stubOpenAIChatCallerWithoutCapture{}} + reqBody := `{ + "sample_id":"My Sample 03", + "api_key":"local-key", + "model":"deepseek-v4-flash", + "message":"广州天气", + "stream":true + }` + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/admin/dev/raw-samples/capture", strings.NewReader(reqBody)) + h.captureRawSample(rec, req) + + if rec.Code != http.StatusInternalServerError { + t.Fatalf("expected 500, got %d body=%s", rec.Code, rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "no upstream capture was recorded") { + t.Fatalf("expected no-capture error, got %s", rec.Body.String()) + } + + if _, err := os.Stat(filepath.Join(root, "my-sample-03")); !os.IsNotExist(err) { + t.Fatalf("expected no sample dir to be created, stat err=%v", err) + } +} + +func TestCombineCaptureBodiesPreservesOrderAndSeparators(t *testing.T) { + entries := []devcapture.Entry{ + {ResponseBody: "first"}, + {ResponseBody: "second"}, + } + got := combineCaptureBodies(entries) + if !bytes.Equal(got, []byte("first\nsecond")) { + t.Fatalf("unexpected combined body: %q", string(got)) + } +} + +func TestPreviewTextPreservesUTF8MB4Characters(t *testing.T) { + preview := previewText(strings.Repeat("😀", 281), 280) + if !utf8.ValidString(preview) { + t.Fatalf("expected valid utf-8 preview, got %q", preview) + } + if preview != strings.Repeat("😀", 280)+"..." { + t.Fatalf("unexpected preview: %q", preview) + } +} + +func TestQueryRawSampleCapturesGroupsBySessionAndMatchesQuestion(t *testing.T) { + devcapture.Global().Clear() + defer devcapture.Global().Clear() + + recordCapturedResponse( + "deepseek_completion", + "https://chat.deepseek.com/api/v0/chat/completion", + http.StatusOK, + map[string]any{ + "chat_session_id": "session-query-1", + "prompt": "用户问题:广州天气怎么样?", + }, + "data: {\"v\":\"先看天气\"}\n\n", + ) + recordCapturedResponse( + "deepseek_continue", + "https://chat.deepseek.com/api/v0/chat/continue", + http.StatusOK, + map[string]any{ + "chat_session_id": "session-query-1", + "message_id": 2, + }, + "data: {\"v\":\"再补充一点\"}\n\n", + ) + + h := &Handler{} + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/admin/dev/raw-samples/query?q=广州天气", nil) + h.queryRawSampleCaptures(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode failed: %v", err) + } + items, _ := out["items"].([]any) + if len(items) != 1 { + t.Fatalf("expected 1 item, got %d body=%s", len(items), rec.Body.String()) + } + item, _ := items[0].(map[string]any) + if item["chain_key"] != "session:session-query-1" { + t.Fatalf("unexpected chain key: %#v", item["chain_key"]) + } + if int(item["round_count"].(float64)) != 2 { + t.Fatalf("expected 2 rounds, got %#v", item["round_count"]) + } + reqPreview, _ := item["request_preview"].(string) + if !strings.Contains(reqPreview, "广州天气") { + t.Fatalf("expected request preview to contain query, got %q", reqPreview) + } +} + +func TestBuildCaptureChainsPreservesCaptureOrderWhenTimestampsCollide(t *testing.T) { + snapshot := []devcapture.Entry{ + { + ID: "cap_continue", + CreatedAt: 1712365200, + Label: "deepseek_continue", + RequestBody: `{"chat_session_id":"session-collision","message_id":2}`, + ResponseBody: "data: {\"v\":\"第二段\"}\n\n", + }, + { + ID: "cap_completion", + CreatedAt: 1712365200, + Label: "deepseek_completion", + RequestBody: `{"chat_session_id":"session-collision","prompt":"题目"}`, + ResponseBody: "data: {\"v\":\"第一段\"}\n\n", + }, + } + + chains := buildCaptureChains(snapshot) + if len(chains) != 1 { + t.Fatalf("expected 1 chain, got %d", len(chains)) + } + if len(chains[0].Entries) != 2 { + t.Fatalf("expected 2 entries, got %d", len(chains[0].Entries)) + } + if chains[0].Entries[0].Label != "deepseek_completion" { + t.Fatalf("expected completion first, got %#v", chains[0].Entries) + } + if chains[0].Entries[1].Label != "deepseek_continue" { + t.Fatalf("expected continue second, got %#v", chains[0].Entries) + } +} + +func TestSaveRawSampleFromCapturesPersistsSelectedChain(t *testing.T) { + root := t.TempDir() + t.Setenv("DS2API_RAW_STREAM_SAMPLE_ROOT", root) + devcapture.Global().Clear() + defer devcapture.Global().Clear() + + recordCapturedResponse( + "deepseek_completion", + "https://chat.deepseek.com/api/v0/chat/completion", + http.StatusOK, + map[string]any{ + "chat_session_id": "session-save-1", + "prompt": "请回答深圳天气", + }, + "data: {\"v\":\"第一段\"}\n\n", + ) + recordCapturedResponse( + "deepseek_continue", + "https://chat.deepseek.com/api/v0/chat/continue", + http.StatusOK, + map[string]any{ + "chat_session_id": "session-save-1", + "message_id": 2, + }, + "data: {\"v\":\"第二段\"}\n\n", + ) + + h := &Handler{} + rec := httptest.NewRecorder() + reqBody := `{"query":"深圳天气","sample_id":"saved-from-memory"}` + req := httptest.NewRequest(http.MethodPost, "/admin/dev/raw-samples/save", strings.NewReader(reqBody)) + h.saveRawSampleFromCaptures(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode failed: %v", err) + } + if out["sample_id"] != "saved-from-memory" { + t.Fatalf("unexpected sample id: %#v", out["sample_id"]) + } + if int(out["round_count"].(float64)) != 2 { + t.Fatalf("expected round_count=2, got %#v", out["round_count"]) + } + + sampleDir := filepath.Join(root, "saved-from-memory") + upstreamBytes, err := os.ReadFile(filepath.Join(sampleDir, "upstream.stream.sse")) + if err != nil { + t.Fatalf("read upstream: %v", err) + } + upstream := string(upstreamBytes) + if !strings.Contains(upstream, "第一段") || !strings.Contains(upstream, "第二段") { + t.Fatalf("expected combined upstream, got %q", upstream) + } + metaBytes, err := os.ReadFile(filepath.Join(sampleDir, "meta.json")) + if err != nil { + t.Fatalf("read meta: %v", err) + } + var meta map[string]any + if err := json.Unmarshal(metaBytes, &meta); err != nil { + t.Fatalf("decode meta: %v", err) + } + reqMeta, _ := meta["request"].(map[string]any) + if fieldString(reqMeta, "chat_session_id") != "session-save-1" { + t.Fatalf("expected request to come from selected chain, got %#v", meta["request"]) + } +} diff --git a/internal/httpapi/admin/rawsamples/routes.go b/internal/httpapi/admin/rawsamples/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..9eb2109be851550e173235d30662f85d399f0de4 --- /dev/null +++ b/internal/httpapi/admin/rawsamples/routes.go @@ -0,0 +1,9 @@ +package rawsamples + +import "github.com/go-chi/chi/v5" + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Post("/dev/raw-samples/capture", h.captureRawSample) + r.Get("/dev/raw-samples/query", h.queryRawSampleCaptures) + r.Post("/dev/raw-samples/save", h.saveRawSampleFromCaptures) +} diff --git a/internal/httpapi/admin/settings/deps.go b/internal/httpapi/admin/settings/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..6df91f4f832163e7f7c1e9f2195186244cc0a041 --- /dev/null +++ b/internal/httpapi/admin/settings/deps.go @@ -0,0 +1,29 @@ +package settings + +import ( + "ds2api/internal/chathistory" + "ds2api/internal/config" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON +var intFrom = adminshared.IntFrom + +func fieldString(m map[string]any, key string) string { + return adminshared.FieldString(m, key) +} +func validateRuntimeSettings(runtime config.RuntimeConfig) error { + return adminshared.ValidateRuntimeSettings(runtime) +} + +func (h *Handler) computeSyncHash() string { + return adminshared.ComputeSyncHash(h.Store) +} diff --git a/internal/httpapi/admin/settings/handler_settings_parse.go b/internal/httpapi/admin/settings/handler_settings_parse.go new file mode 100644 index 0000000000000000000000000000000000000000..f507287ff722863271e95760ff54123aad1ed040 --- /dev/null +++ b/internal/httpapi/admin/settings/handler_settings_parse.go @@ -0,0 +1,172 @@ +package settings + +import ( + "fmt" + "strings" + + "ds2api/internal/config" +) + +func boolFrom(v any) bool { + if v == nil { + return false + } + switch x := v.(type) { + case bool: + return x + case string: + return strings.ToLower(strings.TrimSpace(x)) == "true" + default: + return false + } +} + +func parseSettingsUpdateRequest(req map[string]any) (*config.AdminConfig, *config.RuntimeConfig, *config.ResponsesConfig, *config.EmbeddingsConfig, *config.AutoDeleteConfig, *config.CurrentInputFileConfig, *config.ThinkingInjectionConfig, map[string]string, error) { + var ( + adminCfg *config.AdminConfig + runtimeCfg *config.RuntimeConfig + respCfg *config.ResponsesConfig + embCfg *config.EmbeddingsConfig + autoDeleteCfg *config.AutoDeleteConfig + currentInputCfg *config.CurrentInputFileConfig + thinkingInjCfg *config.ThinkingInjectionConfig + aliasMap map[string]string + ) + + if raw, ok := req["admin"].(map[string]any); ok { + cfg := &config.AdminConfig{} + if v, exists := raw["jwt_expire_hours"]; exists { + n := intFrom(v) + if err := config.ValidateIntRange("admin.jwt_expire_hours", n, 1, 720, true); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.JWTExpireHours = n + } + adminCfg = cfg + } + + if raw, ok := req["runtime"].(map[string]any); ok { + cfg := &config.RuntimeConfig{} + if v, exists := raw["account_max_inflight"]; exists { + n := intFrom(v) + if err := config.ValidateIntRange("runtime.account_max_inflight", n, 1, 256, true); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.AccountMaxInflight = n + } + if v, exists := raw["account_max_queue"]; exists { + n := intFrom(v) + if err := config.ValidateIntRange("runtime.account_max_queue", n, 1, 200000, true); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.AccountMaxQueue = n + } + if v, exists := raw["global_max_inflight"]; exists { + n := intFrom(v) + if err := config.ValidateIntRange("runtime.global_max_inflight", n, 1, 200000, true); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.GlobalMaxInflight = n + } + if v, exists := raw["token_refresh_interval_hours"]; exists { + n := intFrom(v) + if err := config.ValidateIntRange("runtime.token_refresh_interval_hours", n, 1, 720, true); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.TokenRefreshIntervalHours = n + } + if cfg.AccountMaxInflight > 0 && cfg.GlobalMaxInflight > 0 && cfg.GlobalMaxInflight < cfg.AccountMaxInflight { + return nil, nil, nil, nil, nil, nil, nil, nil, fmt.Errorf("runtime.global_max_inflight must be >= runtime.account_max_inflight") + } + runtimeCfg = cfg + } + + if raw, ok := req["responses"].(map[string]any); ok { + cfg := &config.ResponsesConfig{} + if v, exists := raw["store_ttl_seconds"]; exists { + n := intFrom(v) + if err := config.ValidateIntRange("responses.store_ttl_seconds", n, 30, 86400, true); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.StoreTTLSeconds = n + } + respCfg = cfg + } + + if raw, ok := req["embeddings"].(map[string]any); ok { + cfg := &config.EmbeddingsConfig{} + if v, exists := raw["provider"]; exists { + p := strings.TrimSpace(fmt.Sprintf("%v", v)) + if err := config.ValidateTrimmedString("embeddings.provider", p, false); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.Provider = p + } + embCfg = cfg + } + + if raw, ok := req["model_aliases"].(map[string]any); ok { + if aliasMap == nil { + aliasMap = map[string]string{} + } + for k, v := range raw { + key := strings.TrimSpace(k) + val := strings.TrimSpace(fmt.Sprintf("%v", v)) + if key == "" || val == "" { + continue + } + aliasMap[key] = val + } + } + + if raw, ok := req["auto_delete"].(map[string]any); ok { + cfg := &config.AutoDeleteConfig{} + if v, exists := raw["mode"]; exists { + mode := strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", v))) + if err := config.ValidateAutoDeleteMode(mode); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + if mode == "" { + mode = "none" + } + cfg.Mode = mode + } + if v, exists := raw["sessions"]; exists { + cfg.Sessions = boolFrom(v) + } + autoDeleteCfg = cfg + } + + if raw, ok := req["current_input_file"].(map[string]any); ok { + cfg := &config.CurrentInputFileConfig{} + if v, exists := raw["enabled"]; exists { + enabled := boolFrom(v) + cfg.Enabled = &enabled + } + if v, exists := raw["min_chars"]; exists { + n := intFrom(v) + if err := config.ValidateIntRange("current_input_file.min_chars", n, 0, 100000000, true); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + cfg.MinChars = n + } + if err := config.ValidateCurrentInputFileConfig(*cfg); err != nil { + return nil, nil, nil, nil, nil, nil, nil, nil, err + } + currentInputCfg = cfg + } + + if raw, ok := req["thinking_injection"].(map[string]any); ok { + cfg := &config.ThinkingInjectionConfig{} + if v, exists := raw["enabled"]; exists { + b := boolFrom(v) + cfg.Enabled = &b + } + if v, exists := raw["prompt"]; exists { + cfg.Prompt = strings.TrimSpace(fmt.Sprintf("%v", v)) + } + thinkingInjCfg = cfg + } + + return adminCfg, runtimeCfg, respCfg, embCfg, autoDeleteCfg, currentInputCfg, thinkingInjCfg, aliasMap, nil +} diff --git a/internal/httpapi/admin/settings/handler_settings_read.go b/internal/httpapi/admin/settings/handler_settings_read.go new file mode 100644 index 0000000000000000000000000000000000000000..695338b8d0cb5faaa30e8a61cfe7097b9579d1e6 --- /dev/null +++ b/internal/httpapi/admin/settings/handler_settings_read.go @@ -0,0 +1,69 @@ +package settings + +import ( + "net/http" + "strings" + + authn "ds2api/internal/auth" + "ds2api/internal/config" + "ds2api/internal/promptcompat" +) + +func (h *Handler) getSettings(w http.ResponseWriter, _ *http.Request) { + snap := h.Store.Snapshot() + recommended := defaultRuntimeRecommended(len(snap.Accounts), h.Store.RuntimeAccountMaxInflight()) + needsSync := config.IsVercel() && snap.VercelSyncHash != "" && snap.VercelSyncHash != h.computeSyncHash() + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "config_snapshot": map[string]any{ + "admin": map[string]any{ + "jwt_expire_hours": snap.Admin.JWTExpireHours, + }, + "runtime": map[string]any{ + "account_max_inflight": snap.Runtime.AccountMaxInflight, + "account_max_queue": snap.Runtime.AccountMaxQueue, + "global_max_inflight": snap.Runtime.GlobalMaxInflight, + "token_refresh_interval_hours": snap.Runtime.TokenRefreshIntervalHours, + }, + "responses": snap.Responses, + "embeddings": snap.Embeddings, + "auto_delete": snap.AutoDelete, + "current_input_file": map[string]any{ + "enabled": snap.CurrentInputFile.Enabled, + "min_chars": snap.CurrentInputFile.MinChars, + }, + "thinking_injection": map[string]any{ + "enabled": snap.ThinkingInjection.Enabled, + "prompt": snap.ThinkingInjection.Prompt, + }, + "model_aliases": snap.ModelAliases, + }, + "admin": map[string]any{ + "has_password_hash": strings.TrimSpace(snap.Admin.PasswordHash) != "", + "jwt_expire_hours": h.Store.AdminJWTExpireHours(), + "jwt_valid_after_unix": snap.Admin.JWTValidAfterUnix, + "default_password_warning": authn.UsingDefaultAdminKey(h.Store), + }, + "runtime": map[string]any{ + "account_max_inflight": h.Store.RuntimeAccountMaxInflight(), + "account_max_queue": h.Store.RuntimeAccountMaxQueue(recommended), + "global_max_inflight": h.Store.RuntimeGlobalMaxInflight(recommended), + "token_refresh_interval_hours": h.Store.RuntimeTokenRefreshIntervalHours(), + }, + "responses": snap.Responses, + "embeddings": snap.Embeddings, + "auto_delete": snap.AutoDelete, + "current_input_file": map[string]any{ + "enabled": h.Store.CurrentInputFileEnabled(), + "min_chars": h.Store.CurrentInputFileMinChars(), + }, + "thinking_injection": map[string]any{ + "enabled": h.Store.ThinkingInjectionEnabled(), + "prompt": h.Store.ThinkingInjectionPrompt(), + "default_prompt": promptcompat.DefaultThinkingInjectionPrompt, + }, + "model_aliases": snap.ModelAliases, + "env_backed": h.Store.IsEnvBacked(), + "needs_vercel_sync": needsSync, + }) +} diff --git a/internal/httpapi/admin/settings/handler_settings_runtime.go b/internal/httpapi/admin/settings/handler_settings_runtime.go new file mode 100644 index 0000000000000000000000000000000000000000..eee3c6e68042eee35eb19df2168cf3ad322263e8 --- /dev/null +++ b/internal/httpapi/admin/settings/handler_settings_runtime.go @@ -0,0 +1,44 @@ +package settings + +import "ds2api/internal/config" + +func validateMergedRuntimeSettings(current config.RuntimeConfig, incoming *config.RuntimeConfig) error { + merged := current + if incoming != nil { + if incoming.AccountMaxInflight > 0 { + merged.AccountMaxInflight = incoming.AccountMaxInflight + } + if incoming.AccountMaxQueue > 0 { + merged.AccountMaxQueue = incoming.AccountMaxQueue + } + if incoming.GlobalMaxInflight > 0 { + merged.GlobalMaxInflight = incoming.GlobalMaxInflight + } + if incoming.TokenRefreshIntervalHours > 0 { + merged.TokenRefreshIntervalHours = incoming.TokenRefreshIntervalHours + } + } + return validateRuntimeSettings(merged) +} + +func (h *Handler) applyRuntimeSettings() { + if h == nil || h.Store == nil || h.Pool == nil { + return + } + accountCount := len(h.Store.Accounts()) + maxPer := h.Store.RuntimeAccountMaxInflight() + recommended := defaultRuntimeRecommended(accountCount, maxPer) + maxQueue := h.Store.RuntimeAccountMaxQueue(recommended) + global := h.Store.RuntimeGlobalMaxInflight(recommended) + h.Pool.ApplyRuntimeLimits(maxPer, maxQueue, global) +} + +func defaultRuntimeRecommended(accountCount, maxPer int) int { + if maxPer <= 0 { + maxPer = 1 + } + if accountCount <= 0 { + return maxPer + } + return accountCount * maxPer +} diff --git a/internal/httpapi/admin/settings/handler_settings_write.go b/internal/httpapi/admin/settings/handler_settings_write.go new file mode 100644 index 0000000000000000000000000000000000000000..3c8d143220184f9d98971fea6d8d353c0e479a67 --- /dev/null +++ b/internal/httpapi/admin/settings/handler_settings_write.go @@ -0,0 +1,143 @@ +package settings + +import ( + "encoding/json" + "net/http" + "strings" + "time" + + authn "ds2api/internal/auth" + "ds2api/internal/config" +) + +func (h *Handler) updateSettings(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + + adminCfg, runtimeCfg, responsesCfg, embeddingsCfg, autoDeleteCfg, currentInputCfg, thinkingInjCfg, aliasMap, err := parseSettingsUpdateRequest(req) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + if runtimeCfg != nil { + if err := validateMergedRuntimeSettings(h.Store.Snapshot().Runtime, runtimeCfg); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + } + currentInputEnabledSet := hasNestedSettingsKey(req, "current_input_file", "enabled") + currentInputMinCharsSet := hasNestedSettingsKey(req, "current_input_file", "min_chars") + thinkingInjectionEnabledSet := hasNestedSettingsKey(req, "thinking_injection", "enabled") + thinkingInjectionPromptSet := hasNestedSettingsKey(req, "thinking_injection", "prompt") + + if err := h.Store.Update(func(c *config.Config) error { + if adminCfg != nil { + if adminCfg.JWTExpireHours > 0 { + c.Admin.JWTExpireHours = adminCfg.JWTExpireHours + } + } + if runtimeCfg != nil { + if runtimeCfg.AccountMaxInflight > 0 { + c.Runtime.AccountMaxInflight = runtimeCfg.AccountMaxInflight + } + if runtimeCfg.AccountMaxQueue > 0 { + c.Runtime.AccountMaxQueue = runtimeCfg.AccountMaxQueue + } + if runtimeCfg.GlobalMaxInflight > 0 { + c.Runtime.GlobalMaxInflight = runtimeCfg.GlobalMaxInflight + } + if runtimeCfg.TokenRefreshIntervalHours > 0 { + c.Runtime.TokenRefreshIntervalHours = runtimeCfg.TokenRefreshIntervalHours + } + } + if responsesCfg != nil && responsesCfg.StoreTTLSeconds > 0 { + c.Responses.StoreTTLSeconds = responsesCfg.StoreTTLSeconds + } + if embeddingsCfg != nil && strings.TrimSpace(embeddingsCfg.Provider) != "" { + c.Embeddings.Provider = strings.TrimSpace(embeddingsCfg.Provider) + } + if autoDeleteCfg != nil { + c.AutoDelete.Mode = autoDeleteCfg.Mode + c.AutoDelete.Sessions = autoDeleteCfg.Sessions + } + if currentInputCfg != nil { + if currentInputEnabledSet { + c.CurrentInputFile.Enabled = currentInputCfg.Enabled + } + if currentInputMinCharsSet { + c.CurrentInputFile.MinChars = currentInputCfg.MinChars + } + } + if thinkingInjCfg != nil { + if thinkingInjectionEnabledSet { + c.ThinkingInjection.Enabled = thinkingInjCfg.Enabled + } + if thinkingInjectionPromptSet { + c.ThinkingInjection.Prompt = thinkingInjCfg.Prompt + } + } + if aliasMap != nil { + c.ModelAliases = aliasMap + } + return nil + }); err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + + h.applyRuntimeSettings() + needsSync := config.IsVercel() || h.Store.IsEnvBacked() + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "message": "settings updated and hot reloaded", + "env_backed": h.Store.IsEnvBacked(), + "needs_vercel_sync": needsSync, + "manual_sync_message": "配置已保存。Vercel 部署请在 Vercel Sync 页面手动同步。", + }) +} + +func (h *Handler) updateSettingsPassword(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + newPassword := strings.TrimSpace(fieldString(req, "new_password")) + if newPassword == "" { + newPassword = strings.TrimSpace(fieldString(req, "password")) + } + if len(newPassword) < 4 { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "new password must be at least 4 characters"}) + return + } + + now := time.Now().Unix() + hash := authn.HashAdminPassword(newPassword) + if err := h.Store.Update(func(c *config.Config) error { + c.Admin.PasswordHash = hash + c.Admin.JWTValidAfterUnix = now + return nil + }); err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + + writeJSON(w, http.StatusOK, map[string]any{ + "success": true, + "message": "password updated", + "force_relogin": true, + "jwt_valid_after_unix": now, + }) +} + +func hasNestedSettingsKey(req map[string]any, section, key string) bool { + raw, ok := req[section].(map[string]any) + if !ok { + return false + } + _, exists := raw[key] + return exists +} diff --git a/internal/httpapi/admin/settings/routes.go b/internal/httpapi/admin/settings/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..0d445848fa9c49207c8a1cf7f24dbba5c6c1e66d --- /dev/null +++ b/internal/httpapi/admin/settings/routes.go @@ -0,0 +1,20 @@ +package settings + +import ( + "net/http" + + "github.com/go-chi/chi/v5" +) + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/settings", h.getSettings) + r.Put("/settings", h.updateSettings) + r.Post("/settings/password", h.updateSettingsPassword) +} + +func (h *Handler) GetSettings(w http.ResponseWriter, r *http.Request) { h.getSettings(w, r) } +func (h *Handler) UpdateSettings(w http.ResponseWriter, r *http.Request) { h.updateSettings(w, r) } +func (h *Handler) UpdateSettingsPassword(w http.ResponseWriter, r *http.Request) { + h.updateSettingsPassword(w, r) +} +func BoolFrom(v any) bool { return boolFrom(v) } diff --git a/internal/httpapi/admin/shared/deps.go b/internal/httpapi/admin/shared/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..e063ae1c74716082b2253da71ddbf342697df79f --- /dev/null +++ b/internal/httpapi/admin/shared/deps.go @@ -0,0 +1,64 @@ +package shared + +import ( + "context" + "net/http" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" +) + +type ConfigStore interface { + Snapshot() config.Config + Keys() []string + Accounts() []config.Account + FindAccount(identifier string) (config.Account, bool) + UpdateAccountToken(identifier, token string) error + UpdateAccountTestStatus(identifier, status string) error + AccountTestStatus(identifier string) (string, bool) + Update(mutator func(*config.Config) error) error + ExportJSONAndBase64() (string, string, error) + IsEnvBacked() bool + IsEnvWritebackEnabled() bool + HasEnvConfigSource() bool + ConfigPath() string + SetVercelSync(hash string, ts int64) error + AdminPasswordHash() string + AdminJWTExpireHours() int + AdminJWTValidAfterUnix() int64 + RuntimeAccountMaxInflight() int + RuntimeAccountMaxQueue(defaultSize int) int + RuntimeGlobalMaxInflight(defaultSize int) int + RuntimeTokenRefreshIntervalHours() int + AutoDeleteMode() string + CurrentInputFileEnabled() bool + CurrentInputFileMinChars() int + ThinkingInjectionEnabled() bool + ThinkingInjectionPrompt() string + AutoDeleteSessions() bool +} + +type PoolController interface { + Reset() + Status() map[string]any + ApplyRuntimeLimits(maxInflightPerAccount, maxQueueSize, globalMaxInflight int) +} + +type OpenAIChatCaller interface { + ChatCompletions(w http.ResponseWriter, r *http.Request) +} + +type DeepSeekCaller interface { + Login(ctx context.Context, acc config.Account) (string, error) + CreateSession(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + GetPow(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + CallCompletion(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, maxAttempts int) (*http.Response, error) + GetSessionCountForToken(ctx context.Context, token string) (*dsclient.SessionStats, error) + DeleteAllSessionsForToken(ctx context.Context, token string) error +} + +var _ ConfigStore = (*config.Store)(nil) +var _ PoolController = (*account.Pool)(nil) +var _ DeepSeekCaller = (*dsclient.Client)(nil) diff --git a/internal/httpapi/admin/shared/helpers.go b/internal/httpapi/admin/shared/helpers.go new file mode 100644 index 0000000000000000000000000000000000000000..bc78abb5e684b8f9b68446597afc82abe526393d --- /dev/null +++ b/internal/httpapi/admin/shared/helpers.go @@ -0,0 +1,405 @@ +package shared + +import ( + "crypto/md5" + "encoding/json" + "fmt" + "net/http" + "strconv" + "strings" + + "ds2api/internal/config" + "ds2api/internal/util" +) + +var intFrom = util.IntFrom + +var WriteJSON = util.WriteJSON +var IntFrom = util.IntFrom + +func ReverseAccounts(a []config.Account) { reverseAccounts(a) } +func IntFromQuery(r *http.Request, key string, d int) int { + return intFromQuery(r, key, d) +} +func NilIfEmpty(s string) any { return nilIfEmpty(s) } +func NilIfZero(v int64) any { return nilIfZero(v) } +func MaskSecretPreview(secret string) string { + return maskSecretPreview(secret) +} +func ToStringSlice(v any) ([]string, bool) { return toStringSlice(v) } +func ToAccount(m map[string]any) config.Account { + return toAccount(m) +} +func ToAPIKeys(v any) ([]config.APIKey, bool) { + return toAPIKeys(v) +} +func NormalizeAPIKeyForStorage(item config.APIKey) config.APIKey { + return normalizeAPIKeyForStorage(item) +} +func APIKeyHasMetadata(item config.APIKey) bool { + return apiKeyHasMetadata(item) +} +func MergeAPIKeysPreferStructured(existing, incoming []config.APIKey) ([]config.APIKey, int) { + return mergeAPIKeysPreferStructured(existing, incoming) +} +func MergeAPIKeyRecord(existing, incoming config.APIKey) config.APIKey { + return mergeAPIKeyRecord(existing, incoming) +} +func FieldString(m map[string]any, key string) string { + return fieldString(m, key) +} +func FieldStringOptional(m map[string]any, key string) (string, bool) { + return fieldStringOptional(m, key) +} +func StatusOr(v int, d int) int { return statusOr(v, d) } +func AccountMatchesIdentifier(acc config.Account, identifier string) bool { + return accountMatchesIdentifier(acc, identifier) +} +func NormalizeAccountForStorage(acc config.Account) config.Account { + return normalizeAccountForStorage(acc) +} +func ToProxy(m map[string]any) config.Proxy { + return toProxy(m) +} +func FindProxyByID(c config.Config, proxyID string) (config.Proxy, bool) { + return findProxyByID(c, proxyID) +} +func AccountDedupeKey(acc config.Account) string { return accountDedupeKey(acc) } +func NormalizeAndDedupeAccounts(accounts []config.Account) []config.Account { + return normalizeAndDedupeAccounts(accounts) +} +func FindAccountByIdentifier(store ConfigStore, identifier string) (config.Account, bool) { + return findAccountByIdentifier(store, identifier) +} + +func ComputeSyncHash(store ConfigStore) string { + if store == nil { + return "" + } + snap := store.Snapshot().Clone() + snap.ClearAccountTokens() + snap.ClearVercelCredentials() + snap.VercelSyncHash = "" + snap.VercelSyncTime = 0 + b, _ := json.Marshal(snap) + sum := md5.Sum(b) + return fmt.Sprintf("%x", sum) +} + +func SyncHashForJSON(s string) string { + var cfg config.Config + if err := json.Unmarshal([]byte(s), &cfg); err != nil { + return "" + } + cfg.VercelSyncHash = "" + cfg.VercelSyncTime = 0 + cfg.ClearAccountTokens() + cfg.ClearVercelCredentials() + b, err := json.Marshal(cfg) + if err != nil { + return "" + } + sum := md5.Sum(b) + return fmt.Sprintf("%x", sum) +} + +func reverseAccounts(a []config.Account) { + for i, j := 0, len(a)-1; i < j; i, j = i+1, j-1 { + a[i], a[j] = a[j], a[i] + } +} + +func intFromQuery(r *http.Request, key string, d int) int { + v := r.URL.Query().Get(key) + if v == "" { + return d + } + n, err := strconv.Atoi(v) + if err != nil { + return d + } + return n +} + +func nilIfEmpty(s string) any { + if s == "" { + return nil + } + return s +} + +func nilIfZero(v int64) any { + if v == 0 { + return nil + } + return v +} + +func maskSecretPreview(secret string) string { + secret = strings.TrimSpace(secret) + if secret == "" { + return "" + } + if len(secret) <= 4 { + return strings.Repeat("*", len(secret)) + } + return secret[:2] + "****" + secret[len(secret)-2:] +} + +func toStringSlice(v any) ([]string, bool) { + arr, ok := v.([]any) + if !ok { + return nil, false + } + out := make([]string, 0, len(arr)) + for _, item := range arr { + out = append(out, strings.TrimSpace(fmt.Sprintf("%v", item))) + } + return out, true +} + +func toAccount(m map[string]any) config.Account { + email := fieldString(m, "email") + mobile := config.NormalizeMobileForStorage(fieldString(m, "mobile")) + return config.Account{ + Name: fieldString(m, "name"), + Remark: fieldString(m, "remark"), + Email: email, + Mobile: mobile, + Password: fieldString(m, "password"), + ProxyID: fieldString(m, "proxy_id"), + } +} + +func toAPIKeys(v any) ([]config.APIKey, bool) { + arr, ok := v.([]any) + if !ok { + return nil, false + } + out := make([]config.APIKey, 0, len(arr)) + seen := map[string]struct{}{} + for _, item := range arr { + switch x := item.(type) { + case map[string]any: + key := fieldString(x, "key") + if key == "" { + continue + } + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + out = append(out, config.APIKey{ + Key: key, + Name: fieldString(x, "name"), + Remark: fieldString(x, "remark"), + }) + default: + key := strings.TrimSpace(fmt.Sprintf("%v", item)) + if key == "" { + continue + } + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + out = append(out, config.APIKey{Key: key}) + } + } + return out, true +} + +func normalizeAPIKeyForStorage(item config.APIKey) config.APIKey { + return config.APIKey{ + Key: strings.TrimSpace(item.Key), + Name: strings.TrimSpace(item.Name), + Remark: strings.TrimSpace(item.Remark), + } +} + +func apiKeyHasMetadata(item config.APIKey) bool { + return strings.TrimSpace(item.Name) != "" || strings.TrimSpace(item.Remark) != "" +} + +func mergeAPIKeysPreferStructured(existing, incoming []config.APIKey) ([]config.APIKey, int) { + if len(existing) == 0 && len(incoming) == 0 { + return nil, 0 + } + + merged := make([]config.APIKey, 0, len(existing)+len(incoming)) + index := make(map[string]int, len(existing)+len(incoming)) + for _, item := range existing { + item = normalizeAPIKeyForStorage(item) + if item.Key == "" { + continue + } + if _, ok := index[item.Key]; ok { + continue + } + index[item.Key] = len(merged) + merged = append(merged, item) + } + + imported := 0 + for _, item := range incoming { + item = normalizeAPIKeyForStorage(item) + if item.Key == "" { + continue + } + if idx, ok := index[item.Key]; ok { + keep := merged[idx] + next := mergeAPIKeyRecord(keep, item) + if next != keep { + merged[idx] = next + imported++ + } + continue + } + index[item.Key] = len(merged) + merged = append(merged, item) + imported++ + } + + if len(merged) == 0 { + return nil, imported + } + return merged, imported +} + +func mergeAPIKeyRecord(existing, incoming config.APIKey) config.APIKey { + existing = normalizeAPIKeyForStorage(existing) + incoming = normalizeAPIKeyForStorage(incoming) + if existing.Key == "" { + return incoming + } + if apiKeyHasMetadata(existing) { + return existing + } + if apiKeyHasMetadata(incoming) { + return incoming + } + return existing +} + +func fieldString(m map[string]any, key string) string { + v, ok := m[key] + if !ok || v == nil { + return "" + } + return strings.TrimSpace(fmt.Sprintf("%v", v)) +} + +func fieldStringOptional(m map[string]any, key string) (string, bool) { + v, ok := m[key] + if !ok || v == nil { + return "", false + } + return strings.TrimSpace(fmt.Sprintf("%v", v)), true +} + +func statusOr(v int, d int) int { + if v == 0 { + return d + } + return v +} + +func accountMatchesIdentifier(acc config.Account, identifier string) bool { + id := strings.TrimSpace(identifier) + if id == "" { + return false + } + if strings.TrimSpace(acc.Email) == id { + return true + } + if mobileKey := config.CanonicalMobileKey(id); mobileKey != "" && mobileKey == config.CanonicalMobileKey(acc.Mobile) { + return true + } + return acc.Identifier() == id +} + +func normalizeAccountForStorage(acc config.Account) config.Account { + acc.Name = strings.TrimSpace(acc.Name) + acc.Remark = strings.TrimSpace(acc.Remark) + acc.Email = strings.TrimSpace(acc.Email) + acc.Mobile = config.NormalizeMobileForStorage(acc.Mobile) + acc.ProxyID = strings.TrimSpace(acc.ProxyID) + return acc +} + +func toProxy(m map[string]any) config.Proxy { + return config.NormalizeProxy(config.Proxy{ + ID: fieldString(m, "id"), + Name: fieldString(m, "name"), + Type: fieldString(m, "type"), + Host: fieldString(m, "host"), + Port: intFrom(m["port"]), + Username: fieldString(m, "username"), + Password: fieldString(m, "password"), + }) +} + +func findProxyByID(c config.Config, proxyID string) (config.Proxy, bool) { + id := strings.TrimSpace(proxyID) + if id == "" { + return config.Proxy{}, false + } + for _, proxy := range c.Proxies { + proxy = config.NormalizeProxy(proxy) + if proxy.ID == id { + return proxy, true + } + } + return config.Proxy{}, false +} + +func accountDedupeKey(acc config.Account) string { + if email := strings.TrimSpace(acc.Email); email != "" { + return "email:" + email + } + if mobile := config.CanonicalMobileKey(acc.Mobile); mobile != "" { + return "mobile:" + mobile + } + if id := strings.TrimSpace(acc.Identifier()); id != "" { + return "id:" + id + } + return "" +} + +func normalizeAndDedupeAccounts(accounts []config.Account) []config.Account { + if len(accounts) == 0 { + return nil + } + out := make([]config.Account, 0, len(accounts)) + seen := make(map[string]struct{}, len(accounts)) + for _, acc := range accounts { + acc = normalizeAccountForStorage(acc) + key := accountDedupeKey(acc) + if key == "" { + continue + } + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + out = append(out, acc) + } + return out +} + +func findAccountByIdentifier(store ConfigStore, identifier string) (config.Account, bool) { + id := strings.TrimSpace(identifier) + if id == "" { + return config.Account{}, false + } + if acc, ok := store.FindAccount(id); ok { + return acc, true + } + accounts := store.Snapshot().Accounts + for _, acc := range accounts { + if accountMatchesIdentifier(acc, id) { + return acc, true + } + } + return config.Account{}, false +} diff --git a/internal/httpapi/admin/shared/helpers_edge_test.go b/internal/httpapi/admin/shared/helpers_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..516300594a939b162200b99b64d62be90b10f534 --- /dev/null +++ b/internal/httpapi/admin/shared/helpers_edge_test.go @@ -0,0 +1,240 @@ +package shared + +import ( + "net/http" + "net/http/httptest" + "testing" + + "ds2api/internal/config" +) + +// ─── reverseAccounts ───────────────────────────────────────────────── + +func TestReverseAccountsEmpty(t *testing.T) { + a := []config.Account{} + reverseAccounts(a) + if len(a) != 0 { + t.Fatal("expected empty") + } +} + +func TestReverseAccountsTwoElements(t *testing.T) { + a := []config.Account{ + {Email: "a@test.com"}, + {Email: "b@test.com"}, + } + reverseAccounts(a) + if a[0].Email != "b@test.com" || a[1].Email != "a@test.com" { + t.Fatalf("unexpected order after reverse: %v", a) + } +} + +func TestReverseAccountsThreeElements(t *testing.T) { + a := []config.Account{ + {Email: "1@test.com"}, + {Email: "2@test.com"}, + {Email: "3@test.com"}, + } + reverseAccounts(a) + if a[0].Email != "3@test.com" || a[1].Email != "2@test.com" || a[2].Email != "1@test.com" { + t.Fatalf("unexpected order: %v", a) + } +} + +// ─── intFromQuery edge cases ───────────────────────────────────────── + +func TestIntFromQueryPresent(t *testing.T) { + req := httptest.NewRequest("GET", "/?limit=5", nil) + if got := intFromQuery(req, "limit", 10); got != 5 { + t.Fatalf("expected 5, got %d", got) + } +} + +func TestIntFromQueryMissing(t *testing.T) { + req := httptest.NewRequest("GET", "/", nil) + if got := intFromQuery(req, "limit", 10); got != 10 { + t.Fatalf("expected default 10, got %d", got) + } +} + +func TestIntFromQueryInvalid(t *testing.T) { + req := httptest.NewRequest("GET", "/?limit=abc", nil) + if got := intFromQuery(req, "limit", 10); got != 10 { + t.Fatalf("expected default 10 for invalid, got %d", got) + } +} + +func TestIntFromQueryNegative(t *testing.T) { + req := httptest.NewRequest("GET", "/?limit=-3", nil) + if got := intFromQuery(req, "limit", 10); got != -3 { + t.Fatalf("expected -3, got %d", got) + } +} + +func TestIntFromQueryZero(t *testing.T) { + req := httptest.NewRequest("GET", "/?limit=0", nil) + if got := intFromQuery(req, "limit", 10); got != 0 { + t.Fatalf("expected 0, got %d", got) + } +} + +// ─── nilIfEmpty ────────────────────────────────────────────────────── + +func TestNilIfEmptyEmpty(t *testing.T) { + if nilIfEmpty("") != nil { + t.Fatal("expected nil for empty string") + } +} + +func TestNilIfEmptyNonEmpty(t *testing.T) { + if nilIfEmpty("hello") != "hello" { + t.Fatal("expected 'hello'") + } +} + +// ─── nilIfZero ─────────────────────────────────────────────────────── + +func TestNilIfZeroZero(t *testing.T) { + if nilIfZero(0) != nil { + t.Fatal("expected nil for zero") + } +} + +func TestNilIfZeroNonZero(t *testing.T) { + if nilIfZero(42) != int64(42) { + t.Fatal("expected 42") + } +} + +func TestNilIfZeroNegative(t *testing.T) { + if nilIfZero(-1) != int64(-1) { + t.Fatal("expected -1") + } +} + +// ─── toStringSlice ─────────────────────────────────────────────────── + +func TestToStringSliceFromAnySlice(t *testing.T) { + input := []any{"a", "b", "c"} + got, ok := toStringSlice(input) + if !ok || len(got) != 3 { + t.Fatalf("expected 3 strings, got %#v ok=%v", got, ok) + } + if got[0] != "a" || got[1] != "b" || got[2] != "c" { + t.Fatalf("unexpected values: %#v", got) + } +} + +func TestToStringSliceFromMixed(t *testing.T) { + input := []any{"hello", 42, true} + got, ok := toStringSlice(input) + if !ok { + t.Fatal("expected ok for mixed types") + } + if got[0] != "hello" || got[1] != "42" || got[2] != "true" { + t.Fatalf("unexpected values: %#v", got) + } +} + +func TestToStringSliceFromNonSlice(t *testing.T) { + _, ok := toStringSlice("not a slice") + if ok { + t.Fatal("expected not ok for string input") + } +} + +func TestToStringSliceFromNil(t *testing.T) { + _, ok := toStringSlice(nil) + if ok { + t.Fatal("expected not ok for nil input") + } +} + +func TestToStringSliceEmpty(t *testing.T) { + got, ok := toStringSlice([]any{}) + if !ok { + t.Fatal("expected ok for empty slice") + } + if len(got) != 0 { + t.Fatalf("expected empty result, got %#v", got) + } +} + +func TestToStringSliceTrimsWhitespace(t *testing.T) { + got, ok := toStringSlice([]any{" hello ", " world "}) + if !ok { + t.Fatal("expected ok") + } + if got[0] != "hello" || got[1] != "world" { + t.Fatalf("expected trimmed values, got %#v", got) + } +} + +// ─── toAccount edge cases ──────────────────────────────────────────── + +func TestToAccountAllFields(t *testing.T) { + acc := toAccount(map[string]any{ + "email": "user@test.com", + "mobile": "13800138000", + "password": "secret", + "token": "tok123", + }) + if acc.Email != "user@test.com" { + t.Fatalf("unexpected email: %q", acc.Email) + } + if acc.Mobile != "+8613800138000" { + t.Fatalf("unexpected mobile: %q", acc.Mobile) + } + if acc.Password != "secret" { + t.Fatalf("unexpected password: %q", acc.Password) + } + if acc.Token != "" { + t.Fatalf("expected token to be ignored, got %q", acc.Token) + } +} + +func TestToAccountNumericValues(t *testing.T) { + acc := toAccount(map[string]any{ + "email": 12345, + }) + if acc.Email != "12345" { + t.Fatalf("expected numeric converted to string, got %q", acc.Email) + } +} + +// ─── fieldString edge cases ────────────────────────────────────────── + +func TestFieldStringNonString(t *testing.T) { + got := fieldString(map[string]any{"key": 42}, "key") + if got != "42" { + t.Fatalf("expected '42' for int, got %q", got) + } +} + +func TestFieldStringBool(t *testing.T) { + got := fieldString(map[string]any{"key": true}, "key") + if got != "true" { + t.Fatalf("expected 'true', got %q", got) + } +} + +func TestFieldStringWhitespace(t *testing.T) { + got := fieldString(map[string]any{"key": " hello "}, "key") + if got != "hello" { + t.Fatalf("expected trimmed 'hello', got %q", got) + } +} + +// ─── statusOr ──────────────────────────────────────────────────────── + +func TestStatusOrZeroReturnsDefault(t *testing.T) { + if got := statusOr(0, http.StatusOK); got != http.StatusOK { + t.Fatalf("expected %d, got %d", http.StatusOK, got) + } +} + +func TestStatusOrNonZeroReturnsValue(t *testing.T) { + if got := statusOr(http.StatusBadRequest, http.StatusOK); got != http.StatusBadRequest { + t.Fatalf("expected %d, got %d", http.StatusBadRequest, got) + } +} diff --git a/internal/httpapi/admin/shared/request_error.go b/internal/httpapi/admin/shared/request_error.go new file mode 100644 index 0000000000000000000000000000000000000000..e17433ea2e576fb9a61b78521d0cbfa44715473a --- /dev/null +++ b/internal/httpapi/admin/shared/request_error.go @@ -0,0 +1,31 @@ +package shared + +import "errors" + +type requestError struct { + detail string +} + +func (e *requestError) Error() string { + return e.detail +} + +func newRequestError(detail string) error { + return &requestError{detail: detail} +} + +func NewRequestError(detail string) error { + return newRequestError(detail) +} + +func requestErrorDetail(err error) (string, bool) { + var reqErr *requestError + if errors.As(err, &reqErr) { + return reqErr.detail, true + } + return "", false +} + +func RequestErrorDetail(err error) (string, bool) { + return requestErrorDetail(err) +} diff --git a/internal/httpapi/admin/shared/settings_validation.go b/internal/httpapi/admin/shared/settings_validation.go new file mode 100644 index 0000000000000000000000000000000000000000..981e19ed6b8231d1f19f7611c27b4b5957c6140c --- /dev/null +++ b/internal/httpapi/admin/shared/settings_validation.go @@ -0,0 +1,35 @@ +package shared + +import ( + "strings" + + "ds2api/internal/config" +) + +func normalizeSettingsConfig(c *config.Config) { + if c == nil { + return + } + c.Admin.PasswordHash = strings.TrimSpace(c.Admin.PasswordHash) + c.Embeddings.Provider = strings.TrimSpace(c.Embeddings.Provider) +} + +func NormalizeSettingsConfig(c *config.Config) { + normalizeSettingsConfig(c) +} + +func validateSettingsConfig(c config.Config) error { + return config.ValidateConfig(c) +} + +func ValidateSettingsConfig(c config.Config) error { + return validateSettingsConfig(c) +} + +func validateRuntimeSettings(runtime config.RuntimeConfig) error { + return config.ValidateRuntimeConfig(runtime) +} + +func ValidateRuntimeSettings(runtime config.RuntimeConfig) error { + return validateRuntimeSettings(runtime) +} diff --git a/internal/httpapi/admin/test_bridge_test.go b/internal/httpapi/admin/test_bridge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5d523b159e61a7e87506482037358aa3b5786ea8 --- /dev/null +++ b/internal/httpapi/admin/test_bridge_test.go @@ -0,0 +1,123 @@ +package admin + +import ( + "context" + "net/http" + "testing" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + adminaccounts "ds2api/internal/httpapi/admin/accounts" + adminconfig "ds2api/internal/httpapi/admin/configmgmt" + adminsettings "ds2api/internal/httpapi/admin/settings" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +var intFrom = adminshared.IntFrom + +func toAccount(m map[string]any) config.Account { return adminshared.ToAccount(m) } +func fieldString(m map[string]any, key string) string { + return adminshared.FieldString(m, key) +} +func maskSecretPreview(secret string) string { return adminshared.MaskSecretPreview(secret) } +func boolFrom(v any) bool { return adminsettings.BoolFrom(v) } + +func newAdminTestHandler(t *testing.T, raw string) *Handler { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", raw) + store := config.LoadStore() + return &Handler{ + Store: store, + Pool: account.NewPool(store), + } +} + +type testingDSMock struct { + loginToken string + deleteAllSessionsError error + deleteAllSessionsErrorOnce bool + sessionCount *dsclient.SessionStats + loginCalls int + deleteAllCalls int +} + +func (m *testingDSMock) Login(_ context.Context, _ config.Account) (string, error) { + m.loginCalls++ + if m.loginToken == "" { + return "token", nil + } + return m.loginToken, nil +} + +func (m *testingDSMock) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +func (m *testingDSMock) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m *testingDSMock) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil +} + +func (m *testingDSMock) DeleteAllSessionsForToken(_ context.Context, _ string) error { + m.deleteAllCalls++ + if m.deleteAllSessionsError != nil { + err := m.deleteAllSessionsError + if m.deleteAllSessionsErrorOnce { + m.deleteAllSessionsError = nil + } + return err + } + return nil +} + +func (m *testingDSMock) GetSessionCountForToken(_ context.Context, _ string) (*dsclient.SessionStats, error) { + if m.sessionCount != nil { + return m.sessionCount, nil + } + return &dsclient.SessionStats{}, nil +} + +func (h *Handler) configHandler() *adminconfig.Handler { + return &adminconfig.Handler{Store: h.Store, Pool: h.Pool, DS: h.DS, OpenAI: h.OpenAI, ChatHistory: h.ChatHistory} +} + +func (h *Handler) settingsHandler() *adminsettings.Handler { + return &adminsettings.Handler{Store: h.Store, Pool: h.Pool, DS: h.DS, OpenAI: h.OpenAI, ChatHistory: h.ChatHistory} +} + +func (h *Handler) getConfig(w http.ResponseWriter, r *http.Request) { + h.configHandler().GetConfig(w, r) +} + +func (h *Handler) updateConfig(w http.ResponseWriter, r *http.Request) { + h.configHandler().UpdateConfig(w, r) +} + +func (h *Handler) configImport(w http.ResponseWriter, r *http.Request) { + h.configHandler().ConfigImport(w, r) +} + +func (h *Handler) batchImport(w http.ResponseWriter, r *http.Request) { + h.configHandler().BatchImport(w, r) +} + +func (h *Handler) getSettings(w http.ResponseWriter, r *http.Request) { + h.settingsHandler().GetSettings(w, r) +} + +func (h *Handler) updateSettings(w http.ResponseWriter, r *http.Request) { + h.settingsHandler().UpdateSettings(w, r) +} + +func (h *Handler) updateSettingsPassword(w http.ResponseWriter, r *http.Request) { + h.settingsHandler().UpdateSettingsPassword(w, r) +} + +func runAccountTestsConcurrently(accounts []config.Account, maxConcurrency int, testFn func(int, config.Account) map[string]any) []map[string]any { + return adminaccounts.RunAccountTestsConcurrently(accounts, maxConcurrency, testFn) +} diff --git a/internal/httpapi/admin/token_runtime_http_test.go b/internal/httpapi/admin/token_runtime_http_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0933fb738721ee09dd49bb20c4302fcc2aa65293 --- /dev/null +++ b/internal/httpapi/admin/token_runtime_http_test.go @@ -0,0 +1,109 @@ +package admin + +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/account" + "ds2api/internal/config" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +func newHTTPAdminHarness(t *testing.T, rawConfig string, ds adminshared.DeepSeekCaller) http.Handler { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", rawConfig) + store := config.LoadStore() + h := &Handler{ + Store: store, + Pool: account.NewPool(store), + DS: ds, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + return r +} + +func adminReq(method, path string, body []byte) *http.Request { + req := httptest.NewRequest(method, path, bytes.NewReader(body)) + req.Header.Set("Authorization", "Bearer admin") + req.Header.Set("Content-Type", "application/json") + return req +} + +func TestConfigImportIgnoresTokenFieldInPayload(t *testing.T) { + ds := &testingDSMock{} + router := newHTTPAdminHarness(t, `{"accounts":[]}`, ds) + + payload := []byte(`{ + "mode":"replace", + "config":{ + "accounts":[{"email":"u@example.com","password":"pwd","token":"expired-token"}] + } + }`) + rec := httptest.NewRecorder() + router.ServeHTTP(rec, adminReq(http.MethodPost, "/config/import", payload)) + if rec.Code != http.StatusOK { + t.Fatalf("import status=%d body=%s", rec.Code, rec.Body.String()) + } + + readRec := httptest.NewRecorder() + router.ServeHTTP(readRec, adminReq(http.MethodGet, "/config", nil)) + if readRec.Code != http.StatusOK { + t.Fatalf("get config status=%d body=%s", readRec.Code, readRec.Body.String()) + } + var data map[string]any + if err := json.Unmarshal(readRec.Body.Bytes(), &data); err != nil { + t.Fatalf("decode config response: %v", err) + } + accounts, _ := data["accounts"].([]any) + if len(accounts) != 1 { + t.Fatalf("expected one account, got %d", len(accounts)) + } + accountMap, _ := accounts[0].(map[string]any) + if hasToken, _ := accountMap["has_token"].(bool); hasToken { + t.Fatalf("expected imported token to be ignored, account=%#v", accountMap) + } +} + +func TestAccountTestRefreshesRuntimeTokenButExportOmitsToken(t *testing.T) { + ds := &testingDSMock{} + router := newHTTPAdminHarness(t, `{ + "accounts":[{"email":"batch@example.com","password":"pwd","token":"stale-token"}] + }`, ds) + + rec := httptest.NewRecorder() + router.ServeHTTP(rec, adminReq(http.MethodPost, "/accounts/test", []byte(`{"identifier":"batch@example.com"}`))) + if rec.Code != http.StatusOK { + t.Fatalf("test account status=%d body=%s", rec.Code, rec.Body.String()) + } + var testResp map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &testResp); err != nil { + t.Fatalf("decode test response: %v", err) + } + if ok, _ := testResp["success"].(bool); !ok { + t.Fatalf("expected test success, got %#v", testResp) + } + if ds.loginCalls < 1 { + t.Fatalf("expected login to be called at least once, got %d", ds.loginCalls) + } + + exportRec := httptest.NewRecorder() + router.ServeHTTP(exportRec, adminReq(http.MethodGet, "/config/export", nil)) + if exportRec.Code != http.StatusOK { + t.Fatalf("export status=%d body=%s", exportRec.Code, exportRec.Body.String()) + } + var exportResp map[string]any + if err := json.Unmarshal(exportRec.Body.Bytes(), &exportResp); err != nil { + t.Fatalf("decode export response: %v", err) + } + exportJSON, _ := exportResp["json"].(string) + if strings.Contains(exportJSON, `"token"`) { + t.Fatalf("expected export json to omit tokens, got %s", exportJSON) + } +} diff --git a/internal/httpapi/admin/vercel/deps.go b/internal/httpapi/admin/vercel/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..c719edc7f4bbb28dde0eb0a1b28c4343e8d05201 --- /dev/null +++ b/internal/httpapi/admin/vercel/deps.go @@ -0,0 +1,24 @@ +package vercel + +import ( + "ds2api/internal/chathistory" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON +var intFrom = adminshared.IntFrom + +func nilIfZero(v int64) any { return adminshared.NilIfZero(v) } +func statusOr(v int, d int) int { return adminshared.StatusOr(v, d) } + +func (h *Handler) computeSyncHash() string { + return adminshared.ComputeSyncHash(h.Store) +} diff --git a/internal/httpapi/admin/vercel/handler_vercel.go b/internal/httpapi/admin/vercel/handler_vercel.go new file mode 100644 index 0000000000000000000000000000000000000000..4b56df4facfb172e78dcb3cb2381497830acad5d --- /dev/null +++ b/internal/httpapi/admin/vercel/handler_vercel.go @@ -0,0 +1,369 @@ +package vercel + +import ( + "bytes" + "context" + "crypto/md5" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "net/http" + "net/url" + "os" + "strings" + "time" + + "ds2api/internal/config" +) + +func (h *Handler) syncVercel(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": "invalid json"}) + return + } + opts, err := parseVercelSyncOptions(req, h.Store.Snapshot().Vercel) + if err != nil { + writeJSON(w, http.StatusBadRequest, map[string]any{"detail": err.Error()}) + return + } + validated, failed := h.validateAccountsForVercelSync(r.Context(), opts.AutoValidate) + cfgJSON, cfgB64, err := h.exportSyncConfig(req) + if err != nil { + writeJSON(w, http.StatusInternalServerError, map[string]any{"detail": err.Error()}) + return + } + client := &http.Client{Timeout: 30 * time.Second} + params := buildVercelParams(opts.TeamID) + headers := map[string]string{"Authorization": "Bearer " + opts.VercelToken} + + envResp, status, err := vercelRequest(r.Context(), client, http.MethodGet, "https://api.vercel.com/v9/projects/"+opts.ProjectID+"/env", params, headers, nil) + if err != nil || status != http.StatusOK { + writeJSON(w, statusOr(status, http.StatusInternalServerError), map[string]any{"detail": "获取环境变量失败"}) + return + } + envs, _ := envResp["envs"].([]any) + status, err = upsertVercelEnv(r.Context(), client, opts.ProjectID, params, headers, envs, "DS2API_CONFIG_JSON", cfgB64) + if err != nil || (status != http.StatusOK && status != http.StatusCreated) { + writeJSON(w, statusOr(status, http.StatusInternalServerError), map[string]any{"detail": "更新环境变量失败"}) + return + } + savedCreds := h.saveVercelProjectCredentials(r.Context(), client, opts, params, headers, envs) + credentialsWarning := "" + if saved, err := h.saveLocalVercelCredentials(opts); err == nil && saved { + savedCreds = append(savedCreds, "config.vercel") + } else if err != nil { + credentialsWarning = "保存 Vercel 凭据到本地配置失败: " + err.Error() + } + manual, deployURL := triggerVercelDeployment(r.Context(), client, opts.ProjectID, params, headers) + _ = h.Store.SetVercelSync(syncHashForJSON(cfgJSON), time.Now().Unix()) + result := map[string]any{"success": true, "validated_accounts": validated} + if manual { + result["message"] = "配置已同步到 Vercel,请手动触发重新部署" + result["manual_deploy_required"] = true + } else { + result["message"] = "配置已同步,正在重新部署..." + result["deployment_url"] = deployURL + } + if len(failed) > 0 { + result["failed_accounts"] = failed + } + if len(savedCreds) > 0 { + result["saved_credentials"] = savedCreds + } + if credentialsWarning != "" { + result["credentials_warning"] = credentialsWarning + } + writeJSON(w, http.StatusOK, result) +} + +type vercelSyncOptions struct { + VercelToken string + ProjectID string + TeamID string + AutoValidate bool + SaveCreds bool + UsePreconfig bool +} + +func parseVercelSyncOptions(req map[string]any, saved config.VercelConfig) (vercelSyncOptions, error) { + vercelToken, _ := req["vercel_token"].(string) + projectID, _ := req["project_id"].(string) + teamID, _ := req["team_id"].(string) + autoValidate := true + if v, ok := req["auto_validate"].(bool); ok { + autoValidate = v + } + saveCreds := true + if v, ok := req["save_credentials"].(bool); ok { + saveCreds = v + } + usePreconfig := vercelToken == "__USE_PRECONFIG__" || strings.TrimSpace(vercelToken) == "" + if usePreconfig { + vercelToken = firstNonEmpty(os.Getenv("VERCEL_TOKEN"), saved.Token) + } + if strings.TrimSpace(projectID) == "" { + projectID = firstNonEmpty(os.Getenv("VERCEL_PROJECT_ID"), saved.ProjectID) + } + if strings.TrimSpace(teamID) == "" { + teamID = firstNonEmpty(os.Getenv("VERCEL_TEAM_ID"), saved.TeamID) + } + vercelToken = strings.TrimSpace(vercelToken) + projectID = strings.TrimSpace(projectID) + teamID = strings.TrimSpace(teamID) + if vercelToken == "" || projectID == "" { + return vercelSyncOptions{}, fmt.Errorf("需要 Vercel Token 和 Project ID") + } + return vercelSyncOptions{ + VercelToken: vercelToken, + ProjectID: projectID, + TeamID: teamID, + AutoValidate: autoValidate, + SaveCreds: saveCreds, + UsePreconfig: usePreconfig, + }, nil +} + +func firstNonEmpty(values ...string) string { + for _, value := range values { + if trimmed := strings.TrimSpace(value); trimmed != "" { + return trimmed + } + } + return "" +} + +func buildVercelParams(teamID string) url.Values { + params := url.Values{} + if strings.TrimSpace(teamID) != "" { + params.Set("teamId", strings.TrimSpace(teamID)) + } + return params +} + +func (h *Handler) validateAccountsForVercelSync(ctx context.Context, enabled bool) (int, []string) { + if !enabled { + return 0, nil + } + validated, failed := 0, []string{} + for _, acc := range h.Store.Snapshot().Accounts { + if strings.TrimSpace(acc.Token) != "" { + continue + } + token, err := h.DS.Login(ctx, acc) + if err != nil { + failed = append(failed, acc.Identifier()) + } else { + validated++ + _ = h.Store.UpdateAccountToken(acc.Identifier(), token) + } + time.Sleep(500 * time.Millisecond) + } + return validated, failed +} + +func upsertVercelEnv(ctx context.Context, client *http.Client, projectID string, params url.Values, headers map[string]string, envs []any, key, value string) (int, error) { + existingID := findEnvID(envs, key) + if existingID != "" { + _, status, err := vercelRequest(ctx, client, http.MethodPatch, "https://api.vercel.com/v9/projects/"+projectID+"/env/"+existingID, params, headers, map[string]any{"value": value}) + return status, err + } + _, status, err := vercelRequest(ctx, client, http.MethodPost, "https://api.vercel.com/v10/projects/"+projectID+"/env", params, headers, map[string]any{ + "key": key, + "value": value, + "type": "encrypted", + "target": []string{"production", "preview"}, + }) + return status, err +} + +func (h *Handler) saveVercelProjectCredentials(ctx context.Context, client *http.Client, opts vercelSyncOptions, params url.Values, headers map[string]string, envs []any) []string { + if !opts.SaveCreds || opts.UsePreconfig { + return nil + } + saved := []string{} + creds := [][2]string{{"VERCEL_TOKEN", opts.VercelToken}, {"VERCEL_PROJECT_ID", opts.ProjectID}} + if opts.TeamID != "" { + creds = append(creds, [2]string{"VERCEL_TEAM_ID", opts.TeamID}) + } + for _, kv := range creds { + status, _ := upsertVercelEnv(ctx, client, opts.ProjectID, params, headers, envs, kv[0], kv[1]) + if status == http.StatusOK || status == http.StatusCreated { + saved = append(saved, kv[0]) + } + } + return saved +} + +func (h *Handler) saveLocalVercelCredentials(opts vercelSyncOptions) (bool, error) { + if !opts.SaveCreds { + return false, nil + } + err := h.Store.Update(func(c *config.Config) error { + token := opts.VercelToken + if opts.UsePreconfig { + token = c.Vercel.Token + } + c.Vercel = config.NormalizeVercelConfig(config.VercelConfig{ + Token: token, + ProjectID: opts.ProjectID, + TeamID: opts.TeamID, + }) + return nil + }) + return err == nil, err +} + +func triggerVercelDeployment(ctx context.Context, client *http.Client, projectID string, params url.Values, headers map[string]string) (bool, string) { + projectResp, status, _ := vercelRequest(ctx, client, http.MethodGet, "https://api.vercel.com/v9/projects/"+projectID, params, headers, nil) + if status != http.StatusOK { + return true, "" + } + link, ok := projectResp["link"].(map[string]any) + if !ok { + return true, "" + } + linkType, _ := link["type"].(string) + if linkType != "github" { + return true, "" + } + repoID := intFrom(link["repoId"]) + ref, _ := link["productionBranch"].(string) + if ref == "" { + ref = "main" + } + depResp, depStatus, _ := vercelRequest(ctx, client, http.MethodPost, "https://api.vercel.com/v13/deployments", params, headers, map[string]any{ + "name": projectID, + "project": projectID, + "target": "production", + "gitSource": map[string]any{ + "type": "github", + "repoId": repoID, + "ref": ref, + }, + }) + if depStatus != http.StatusOK && depStatus != http.StatusCreated { + return true, "" + } + deployURL, _ := depResp["url"].(string) + return false, deployURL +} + +func (h *Handler) vercelStatus(w http.ResponseWriter, r *http.Request) { + snap := h.Store.Snapshot() + current := h.computeSyncHash() + synced := snap.VercelSyncHash != "" && snap.VercelSyncHash == current + draftHash := "" + draftDiffers := false + if r != nil && r.Method == http.MethodPost && r.Body != nil { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err == nil { + if cfgJSON, _, err := h.exportSyncConfig(req); err == nil { + draftHash = syncHashForJSON(cfgJSON) + draftDiffers = draftHash != "" && draftHash != current + } + } + } + writeJSON(w, http.StatusOK, map[string]any{ + "synced": synced, + "last_sync_time": nilIfZero(snap.VercelSyncTime), + "has_synced_before": snap.VercelSyncHash != "", + "env_backed": h.Store.IsEnvBacked(), + "config_hash": current, + "last_synced_hash": snap.VercelSyncHash, + "draft_hash": draftHash, + "draft_differs": draftDiffers, + }) +} + +func (h *Handler) exportSyncConfig(req map[string]any) (string, string, error) { + override, ok := req["config_override"] + if !ok || override == nil { + return encodeVercelSyncConfig(h.Store.Snapshot()) + } + raw, err := json.Marshal(override) + if err != nil { + return "", "", err + } + var cfg config.Config + if err := json.Unmarshal(raw, &cfg); err != nil { + return "", "", err + } + return encodeVercelSyncConfig(cfg) +} + +func encodeVercelSyncConfig(cfg config.Config) (string, string, error) { + cfg.DropInvalidAccounts() + cfg.ClearAccountTokens() + cfg.ClearVercelCredentials() + cfg.VercelSyncHash = "" + cfg.VercelSyncTime = 0 + b, err := json.Marshal(cfg) + if err != nil { + return "", "", err + } + return string(b), base64.StdEncoding.EncodeToString(b), nil +} + +func syncHashForJSON(s string) string { + var cfg config.Config + if err := json.Unmarshal([]byte(s), &cfg); err != nil { + return "" + } + cfg.VercelSyncHash = "" + cfg.VercelSyncTime = 0 + cfg.ClearAccountTokens() + cfg.ClearVercelCredentials() + b, err := json.Marshal(cfg) + if err != nil { + return "" + } + sum := md5.Sum(b) + return fmt.Sprintf("%x", sum) +} + +func vercelRequest(ctx context.Context, client *http.Client, method, endpoint string, params url.Values, headers map[string]string, body any) (map[string]any, int, error) { + if len(params) > 0 { + endpoint += "?" + params.Encode() + } + var reader io.Reader + if body != nil { + b, _ := json.Marshal(body) + reader = bytes.NewReader(b) + } + req, err := http.NewRequestWithContext(ctx, method, endpoint, reader) + if err != nil { + return nil, 0, err + } + for k, v := range headers { + req.Header.Set(k, v) + } + req.Header.Set("Content-Type", "application/json") + resp, err := client.Do(req) + if err != nil { + return nil, 0, err + } + defer func() { _ = resp.Body.Close() }() + b, _ := io.ReadAll(resp.Body) + parsed := map[string]any{} + _ = json.Unmarshal(b, &parsed) + if len(parsed) == 0 { + parsed["raw"] = string(b) + } + return parsed, resp.StatusCode, nil +} + +func findEnvID(envs []any, key string) string { + for _, item := range envs { + m, ok := item.(map[string]any) + if !ok { + continue + } + if k, _ := m["key"].(string); k == key { + id, _ := m["id"].(string) + return id + } + } + return "" +} diff --git a/internal/httpapi/admin/vercel/handler_vercel_test.go b/internal/httpapi/admin/vercel/handler_vercel_test.go new file mode 100644 index 0000000000000000000000000000000000000000..66aa618860f912de8b85d32604726ec23f287d67 --- /dev/null +++ b/internal/httpapi/admin/vercel/handler_vercel_test.go @@ -0,0 +1,100 @@ +package vercel + +import ( + "encoding/json" + "strings" + "testing" + + "ds2api/internal/config" +) + +func TestParseVercelSyncOptionsFallsBackToSavedConfig(t *testing.T) { + t.Setenv("VERCEL_TOKEN", "") + t.Setenv("VERCEL_PROJECT_ID", "") + t.Setenv("VERCEL_TEAM_ID", "") + + opts, err := parseVercelSyncOptions(map[string]any{ + "vercel_token": "__USE_PRECONFIG__", + }, config.VercelConfig{ + Token: " saved-token ", + ProjectID: " saved-project ", + TeamID: " saved-team ", + }) + if err != nil { + t.Fatalf("parse options error: %v", err) + } + if opts.VercelToken != "saved-token" || opts.ProjectID != "saved-project" || opts.TeamID != "saved-team" { + t.Fatalf("unexpected options: %#v", opts) + } + if !opts.UsePreconfig { + t.Fatal("expected preconfig mode") + } +} + +func TestSaveLocalVercelCredentialsStoresExplicitInput(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"]}`) + store := config.LoadStore() + h := &Handler{Store: store} + + saved, err := h.saveLocalVercelCredentials(vercelSyncOptions{ + VercelToken: " token ", + ProjectID: " project ", + TeamID: " team ", + SaveCreds: true, + }) + if err != nil { + t.Fatalf("save local credentials error: %v", err) + } + if !saved { + t.Fatal("expected credentials to be saved") + } + got := store.Snapshot().Vercel + if got.Token != "token" || got.ProjectID != "project" || got.TeamID != "team" { + t.Fatalf("unexpected saved credentials: %#v", got) + } +} + +func TestSaveLocalVercelCredentialsPreservesPreconfiguredTokenAndUpdatesProject(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"vercel":{"token":"saved-token","project_id":"old-project","team_id":"old-team"}}`) + store := config.LoadStore() + h := &Handler{Store: store} + + saved, err := h.saveLocalVercelCredentials(vercelSyncOptions{ + VercelToken: "resolved-token", + ProjectID: "new-project", + TeamID: "new-team", + SaveCreds: true, + UsePreconfig: true, + }) + if err != nil { + t.Fatalf("save local credentials error: %v", err) + } + if !saved { + t.Fatal("expected project/team updates to be saved") + } + got := store.Snapshot().Vercel + if got.Token != "saved-token" || got.ProjectID != "new-project" || got.TeamID != "new-team" { + t.Fatalf("unexpected saved credentials: %#v", got) + } +} + +func TestExportSyncConfigStripsSavedVercelCredentials(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"vercel":{"token":"secret-token","project_id":"project","team_id":"team"}}`) + store := config.LoadStore() + h := &Handler{Store: store} + + jsonStr, _, err := h.exportSyncConfig(map[string]any{}) + if err != nil { + t.Fatalf("export sync config error: %v", err) + } + if strings.Contains(jsonStr, "secret-token") || strings.Contains(jsonStr, `"vercel"`) { + t.Fatalf("expected sync export to strip Vercel credentials, got %s", jsonStr) + } + var exported config.Config + if err := json.Unmarshal([]byte(jsonStr), &exported); err != nil { + t.Fatalf("exported config is invalid JSON: %v", err) + } + if len(exported.Keys) != 1 || exported.Keys[0] != "k1" { + t.Fatalf("unexpected exported config: %#v", exported) + } +} diff --git a/internal/httpapi/admin/vercel/routes.go b/internal/httpapi/admin/vercel/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..dec4d1b31147011bad1a8e23e74c1ed2db86c956 --- /dev/null +++ b/internal/httpapi/admin/vercel/routes.go @@ -0,0 +1,9 @@ +package vercel + +import "github.com/go-chi/chi/v5" + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Post("/vercel/sync", h.syncVercel) + r.Get("/vercel/status", h.vercelStatus) + r.Post("/vercel/status", h.vercelStatus) +} diff --git a/internal/httpapi/admin/version/deps.go b/internal/httpapi/admin/version/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..cf181cae40b40106de280179ac2ece585abb3990 --- /dev/null +++ b/internal/httpapi/admin/version/deps.go @@ -0,0 +1,16 @@ +package version + +import ( + "ds2api/internal/chathistory" + adminshared "ds2api/internal/httpapi/admin/shared" +) + +type Handler struct { + Store adminshared.ConfigStore + Pool adminshared.PoolController + DS adminshared.DeepSeekCaller + OpenAI adminshared.OpenAIChatCaller + ChatHistory *chathistory.Store +} + +var writeJSON = adminshared.WriteJSON diff --git a/internal/httpapi/admin/version/handler_version.go b/internal/httpapi/admin/version/handler_version.go new file mode 100644 index 0000000000000000000000000000000000000000..fb6271e8652b7b8c1d1aeb2ee539656d241098ee --- /dev/null +++ b/internal/httpapi/admin/version/handler_version.go @@ -0,0 +1,75 @@ +package version + +import ( + "encoding/json" + "net/http" + "strings" + "time" + + "ds2api/internal/version" +) + +const latestReleaseAPI = "https://api.github.com/repos/CJackHwang/ds2api/releases/latest" + +type latestReleasePayload struct { + TagName string `json:"tag_name"` + HTMLURL string `json:"html_url"` + PublishedAt string `json:"published_at"` +} + +func (h *Handler) getVersion(w http.ResponseWriter, _ *http.Request) { + current, source := version.Current() + resp := map[string]any{ + "success": true, + "current_version": current, + "current_tag": version.Tag(current), + "source": source, + "checked_at": time.Now().UTC().Format(time.RFC3339), + } + + req, err := http.NewRequest(http.MethodGet, latestReleaseAPI, nil) + if err != nil { + resp["check_error"] = err.Error() + writeJSON(w, http.StatusOK, resp) + return + } + req.Header.Set("Accept", "application/vnd.github+json") + req.Header.Set("User-Agent", "ds2api-version-check") + + client := &http.Client{Timeout: 4 * time.Second} + r, err := client.Do(req) + if err != nil { + resp["check_error"] = err.Error() + writeJSON(w, http.StatusOK, resp) + return + } + defer func() { _ = r.Body.Close() }() + if r.StatusCode < 200 || r.StatusCode >= 300 { + resp["check_error"] = "github api status: " + r.Status + writeJSON(w, http.StatusOK, resp) + return + } + + var data latestReleasePayload + if err := json.NewDecoder(r.Body).Decode(&data); err != nil { + resp["check_error"] = err.Error() + writeJSON(w, http.StatusOK, resp) + return + } + + latest := strings.TrimSpace(data.TagName) + if latest == "" { + resp["check_error"] = "missing latest tag" + writeJSON(w, http.StatusOK, resp) + return + } + latestVersion := strings.TrimPrefix(latest, "v") + + resp["latest_tag"] = latest + resp["latest_version"] = latestVersion + resp["release_url"] = data.HTMLURL + resp["published_at"] = data.PublishedAt + resp["has_update"] = version.Compare(current, latestVersion) < 0 + + writeJSON(w, http.StatusOK, resp) +} diff --git a/internal/httpapi/admin/version/routes.go b/internal/httpapi/admin/version/routes.go new file mode 100644 index 0000000000000000000000000000000000000000..31368b0c1572eb40a42598aede91823d2b6f27b3 --- /dev/null +++ b/internal/httpapi/admin/version/routes.go @@ -0,0 +1,7 @@ +package version + +import "github.com/go-chi/chi/v5" + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/version", h.getVersion) +} diff --git a/internal/httpapi/claude/convert.go b/internal/httpapi/claude/convert.go new file mode 100644 index 0000000000000000000000000000000000000000..2233a65a101e87ac6ebff1148c5f3ee03d06eb66 --- /dev/null +++ b/internal/httpapi/claude/convert.go @@ -0,0 +1,11 @@ +package claude + +import ( + "ds2api/internal/claudeconv" +) + +const defaultClaudeModel = "claude-sonnet-4-6" + +func convertClaudeToDeepSeek(claudeReq map[string]any, store ConfigReader) map[string]any { + return claudeconv.ConvertClaudeToDeepSeek(claudeReq, store, defaultClaudeModel) +} diff --git a/internal/httpapi/claude/current_input_file_test.go b/internal/httpapi/claude/current_input_file_test.go new file mode 100644 index 0000000000000000000000000000000000000000..dfa89b9fe572d5e609ca2210b7b164efde7d645c --- /dev/null +++ b/internal/httpapi/claude/current_input_file_test.go @@ -0,0 +1,206 @@ +package claude + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + dsclient "ds2api/internal/deepseek/client" +) + +type claudeCurrentInputAuth struct{} + +type claudeHistoryConfig struct { + aliases map[string]string +} + +func (m claudeHistoryConfig) ModelAliases() map[string]string { return m.aliases } +func (claudeHistoryConfig) CurrentInputFileEnabled() bool { return false } +func (claudeHistoryConfig) CurrentInputFileMinChars() int { return 0 } + +func (claudeCurrentInputAuth) Determine(*http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + DeepSeekToken: "direct-token", + CallerID: "caller:test", + TriedAccounts: map[string]bool{}, + }, nil +} + +func TestClaudeDirectRecordsResponseHistory(t *testing.T) { + ds := &claudeCurrentInputDS{} + historyStore := chathistory.New(filepath.Join(t.TempDir(), "history.json")) + h := &Handler{ + Store: claudeHistoryConfig{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}}, + Auth: claudeCurrentInputAuth{}, + DS: ds, + ChatHistory: historyStore, + } + reqBody := `{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hello from claude"}],"max_tokens":1024}` + req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot history: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + item, err := historyStore.Get(snapshot.Items[0].ID) + if err != nil { + t.Fatalf("get history item: %v", err) + } + if item.Surface != "claude.messages" { + t.Fatalf("unexpected surface: %q", item.Surface) + } + if item.Model != "claude-sonnet-4-6" { + t.Fatalf("unexpected model: %q", item.Model) + } + if item.UserInput != "hello from claude" { + t.Fatalf("unexpected user input: %q", item.UserInput) + } + if item.Content != "ok" { + t.Fatalf("expected raw upstream content, got %q", item.Content) + } +} + +func (claudeCurrentInputAuth) Release(*auth.RequestAuth) {} + +type claudeCurrentInputDS struct { + uploads []dsclient.UploadFileRequest + payload map[string]any +} + +func (d *claudeCurrentInputDS) CreateSession(context.Context, *auth.RequestAuth, int) (string, error) { + return "session-id", nil +} + +func (d *claudeCurrentInputDS) GetPow(context.Context, *auth.RequestAuth, int) (string, error) { + return "pow", nil +} + +func (d *claudeCurrentInputDS) UploadFile(_ context.Context, _ *auth.RequestAuth, req dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + d.uploads = append(d.uploads, req) + id := "file-claude-history" + if len(d.uploads) > 1 { + id = "file-claude-tools" + } + return &dsclient.UploadFileResult{ID: id}, nil +} + +func (d *claudeCurrentInputDS) CallCompletion(_ context.Context, _ *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + d.payload = payload + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader("data: {\"p\":\"response/content\",\"v\":\"ok\"}\n")), + }, nil +} + +func TestClaudeDirectAppliesCurrentInputFile(t *testing.T) { + ds := &claudeCurrentInputDS{} + historyStore := chathistory.New(filepath.Join(t.TempDir(), "history.json")) + h := &Handler{ + Store: mockClaudeConfig{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}}, + Auth: claudeCurrentInputAuth{}, + DS: ds, + ChatHistory: historyStore, + } + reqBody := `{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hello from claude"}],"max_tokens":1024}` + req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploads) != 1 { + t.Fatalf("expected one current input upload, got %d", len(ds.uploads)) + } + if strings.Contains(strings.ToLower(ds.uploads[0].Filename), "history") || !strings.HasSuffix(ds.uploads[0].Filename, ".txt") { + t.Fatalf("unexpected upload filename: %q", ds.uploads[0].Filename) + } + refIDs, _ := ds.payload["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-claude-history" { + t.Fatalf("expected uploaded history ref id, got %#v", ds.payload["ref_file_ids"]) + } + prompt, _ := ds.payload["prompt"].(string) + if !strings.Contains(prompt, ds.uploads[0].Filename) { + t.Fatalf("expected continuation prompt, got %q", prompt) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot history: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + full, err := historyStore.Get(snapshot.Items[0].ID) + if err != nil { + t.Fatalf("get history item: %v", err) + } + if full.HistoryText != string(ds.uploads[0].Data) { + t.Fatalf("expected uploaded current input file to be persisted in history text") + } + if len(full.Messages) != 1 || !strings.Contains(full.Messages[0].Content, ".txt") { + t.Fatalf("expected persisted message to match upstream continuation prompt, got %#v", full.Messages) + } +} + +func TestClaudeCurrentInputFileUploadsToolsSeparately(t *testing.T) { + ds := &claudeCurrentInputDS{} + h := &Handler{ + Store: mockClaudeConfig{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}}, + Auth: claudeCurrentInputAuth{}, + DS: ds, + } + reqBody := `{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hello from claude"}],"tools":[{"name":"search","description":"Search docs","input_schema":{"type":"object"}}],"max_tokens":1024}` + req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploads) != 2 { + t.Fatalf("expected history and tools uploads, got %d", len(ds.uploads)) + } + if strings.Contains(strings.ToLower(ds.uploads[0].Filename), "history") || !strings.HasSuffix(ds.uploads[0].Filename, ".txt") || ds.uploads[1].Filename != "context_tools.txt" { + t.Fatalf("unexpected upload filenames: %#v", ds.uploads) + } + historyText := string(ds.uploads[0].Data) + if strings.Contains(historyText, "You have access to these tools") || strings.Contains(historyText, "Description: Search docs") { + t.Fatalf("history transcript should not embed tool descriptions, got %q", historyText) + } + toolsText := string(ds.uploads[1].Data) + if !strings.Contains(toolsText, "# context_tools.txt") || !strings.Contains(toolsText, "Tool: search") || !strings.Contains(toolsText, "Description: Search docs") { + t.Fatalf("expected tools transcript to include tool schema, got %q", toolsText) + } + refIDs, _ := ds.payload["ref_file_ids"].([]any) + if len(refIDs) < 2 || refIDs[0] != "file-claude-history" || refIDs[1] != "file-claude-tools" { + t.Fatalf("expected history and tools ref ids first, got %#v", ds.payload["ref_file_ids"]) + } + prompt, _ := ds.payload["prompt"].(string) + if !strings.Contains(prompt, "context_tools.txt") || !strings.Contains(prompt, "TOOL CALL SCHEME") { + t.Fatalf("expected live prompt to reference tools file and retain format instructions, got %q", prompt) + } + if strings.Contains(prompt, "Description: Search docs") { + t.Fatalf("live prompt should not inline tool descriptions, got %q", prompt) + } +} diff --git a/internal/httpapi/claude/deps.go b/internal/httpapi/claude/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..70e56c3403c8bb63027002167f2111a75539c34d --- /dev/null +++ b/internal/httpapi/claude/deps.go @@ -0,0 +1,36 @@ +package claude + +import ( + "context" + "net/http" + + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" +) + +type AuthResolver interface { + Determine(req *http.Request) (*auth.RequestAuth, error) + Release(a *auth.RequestAuth) +} + +type DeepSeekCaller interface { + CreateSession(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + GetPow(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + UploadFile(ctx context.Context, a *auth.RequestAuth, req dsclient.UploadFileRequest, maxAttempts int) (*dsclient.UploadFileResult, error) + CallCompletion(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, maxAttempts int) (*http.Response, error) +} + +type ConfigReader interface { + ModelAliases() map[string]string + CurrentInputFileEnabled() bool + CurrentInputFileMinChars() int +} + +type OpenAIChatRunner interface { + ChatCompletions(w http.ResponseWriter, r *http.Request) +} + +var _ AuthResolver = (*auth.Resolver)(nil) +var _ DeepSeekCaller = (*dsclient.Client)(nil) +var _ ConfigReader = (*config.Store)(nil) diff --git a/internal/httpapi/claude/deps_injection_test.go b/internal/httpapi/claude/deps_injection_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5fee82f1e8cf938ae8ac18d3e66e4f789efc9681 --- /dev/null +++ b/internal/httpapi/claude/deps_injection_test.go @@ -0,0 +1,116 @@ +package claude + +import "testing" + +type mockClaudeConfig struct { + aliases map[string]string +} + +func (m mockClaudeConfig) ModelAliases() map[string]string { return m.aliases } +func (mockClaudeConfig) CurrentInputFileEnabled() bool { return true } +func (mockClaudeConfig) CurrentInputFileMinChars() int { return 0 } + +func TestNormalizeClaudeRequestUsesGlobalAliasMapping(t *testing.T) { + req := map[string]any{ + "model": "claude-opus-4-6", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + } + out, err := normalizeClaudeRequest(mockClaudeConfig{ + aliases: map[string]string{ + "claude-opus-4-6": "deepseek-v4-pro-search", + }, + }, req) + if err != nil { + t.Fatalf("normalizeClaudeRequest error: %v", err) + } + if out.Standard.ResolvedModel != "deepseek-v4-pro-search" { + t.Fatalf("resolved model mismatch: got=%q", out.Standard.ResolvedModel) + } + if !out.Standard.Thinking || !out.Standard.Search { + t.Fatalf("unexpected flags: thinking=%v search=%v", out.Standard.Thinking, out.Standard.Search) + } +} + +func TestNormalizeClaudeRequestDisablesThinkingWhenRequested(t *testing.T) { + req := map[string]any{ + "model": "claude-opus-4-6", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + "thinking": map[string]any{"type": "disabled"}, + } + out, err := normalizeClaudeRequest(mockClaudeConfig{ + aliases: map[string]string{ + "claude-opus-4-6": "deepseek-v4-pro", + }, + }, req) + if err != nil { + t.Fatalf("normalizeClaudeRequest error: %v", err) + } + if out.Standard.Thinking { + t.Fatalf("expected explicit Claude thinking disable to win") + } +} + +func TestNormalizeClaudeRequestEnablesThinkingWhenRequested(t *testing.T) { + req := map[string]any{ + "model": "claude-opus-4-6", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + "thinking": map[string]any{"type": "enabled", "budget_tokens": 1024}, + } + out, err := normalizeClaudeRequest(mockClaudeConfig{ + aliases: map[string]string{ + "claude-opus-4-6": "deepseek-v4-pro", + }, + }, req) + if err != nil { + t.Fatalf("normalizeClaudeRequest error: %v", err) + } + if !out.Standard.Thinking { + t.Fatalf("expected explicit Claude thinking request to enable downstream thinking") + } +} + +func TestNormalizeClaudeRequestNoThinkingAliasForcesThinkingOff(t *testing.T) { + req := map[string]any{ + "model": "claude-opus-4-6-nothinking", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + "thinking": map[string]any{"type": "enabled", "budget_tokens": 1024}, + } + out, err := normalizeClaudeRequest(mockClaudeConfig{}, req) + if err != nil { + t.Fatalf("normalizeClaudeRequest error: %v", err) + } + if out.Standard.ResolvedModel != "deepseek-v4-pro-nothinking" { + t.Fatalf("resolved model mismatch: got=%q", out.Standard.ResolvedModel) + } + if out.Standard.Thinking { + t.Fatalf("expected nothinking alias to force downstream thinking off") + } +} + +func TestNormalizeClaudeRequestPrefersGlobalAliasMapping(t *testing.T) { + req := map[string]any{ + "model": "claude-sonnet-4-6", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + } + out, err := normalizeClaudeRequest(mockClaudeConfig{ + aliases: map[string]string{ + "claude-sonnet-4-6": "deepseek-v4-flash", + }, + }, req) + if err != nil { + t.Fatalf("normalizeClaudeRequest error: %v", err) + } + if out.Standard.ResolvedModel != "deepseek-v4-flash" { + t.Fatalf("expected global alias to win for explicit model, got=%q", out.Standard.ResolvedModel) + } +} diff --git a/internal/httpapi/claude/error_shape_test.go b/internal/httpapi/claude/error_shape_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b9dc46983dcfc674f5ff90571edc36dc8c2136ad --- /dev/null +++ b/internal/httpapi/claude/error_shape_test.go @@ -0,0 +1,34 @@ +package claude + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestWriteClaudeErrorIncludesUnifiedFields(t *testing.T) { + rec := httptest.NewRecorder() + writeClaudeError(rec, http.StatusUnauthorized, "bad token") + if rec.Code != http.StatusUnauthorized { + t.Fatalf("expected 401, got %d", rec.Code) + } + + var body map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatalf("decode body: %v", err) + } + errObj, _ := body["error"].(map[string]any) + if errObj["message"] != "bad token" { + t.Fatalf("unexpected message: %v", errObj["message"]) + } + if errObj["type"] != "invalid_request_error" { + t.Fatalf("unexpected type: %v", errObj["type"]) + } + if errObj["code"] != "authentication_failed" { + t.Fatalf("unexpected code: %v", errObj["code"]) + } + if _, ok := errObj["param"]; !ok { + t.Fatal("expected param field") + } +} diff --git a/internal/httpapi/claude/handler_errors.go b/internal/httpapi/claude/handler_errors.go new file mode 100644 index 0000000000000000000000000000000000000000..f1188d6268e5b8ebb65a022c113ca72edfc1250c --- /dev/null +++ b/internal/httpapi/claude/handler_errors.go @@ -0,0 +1,25 @@ +package claude + +import "net/http" + +func writeClaudeError(w http.ResponseWriter, status int, message string) { + code := "invalid_request" + switch status { + case http.StatusUnauthorized: + code = "authentication_failed" + case http.StatusTooManyRequests: + code = "rate_limit_exceeded" + case http.StatusNotFound: + code = "not_found" + case http.StatusInternalServerError: + code = "internal_error" + } + writeJSON(w, status, map[string]any{ + "error": map[string]any{ + "type": "invalid_request_error", + "message": message, + "code": code, + "param": nil, + }, + }) +} diff --git a/internal/httpapi/claude/handler_helpers_misc.go b/internal/httpapi/claude/handler_helpers_misc.go new file mode 100644 index 0000000000000000000000000000000000000000..6062dc64782ce13bb339e4007f99479375c7c975 --- /dev/null +++ b/internal/httpapi/claude/handler_helpers_misc.go @@ -0,0 +1,77 @@ +package claude + +import ( + "ds2api/internal/toolcall" + "fmt" + "strings" +) + +func hasSystemMessage(messages []any) bool { + for _, m := range messages { + msg, ok := m.(map[string]any) + if ok && msg["role"] == "system" { + return true + } + } + return false +} + +func extractClaudeToolNames(tools []any) []string { + out := make([]string, 0, len(tools)) + for _, t := range tools { + m, ok := t.(map[string]any) + if !ok { + continue + } + name, _, _ := extractClaudeToolMeta(m) + if name != "" { + out = append(out, name) + } + } + return out +} + +func extractClaudeToolMeta(m map[string]any) (string, string, any) { + name, desc, schemaObj := toolcall.ExtractToolMeta(m) + if strings.TrimSpace(desc) == "" { + desc = "No description available" + } + return strings.TrimSpace(name), strings.TrimSpace(desc), schemaObj +} + +func toMessageMaps(v any) []map[string]any { + arr, ok := v.([]any) + if !ok { + return nil + } + out := make([]map[string]any, 0, len(arr)) + for _, item := range arr { + if m, ok := item.(map[string]any); ok { + out = append(out, m) + } + } + return out +} + +func extractMessageContent(v any) string { + switch x := v.(type) { + case string: + return x + case []any: + parts := make([]string, 0, len(x)) + for _, it := range x { + parts = append(parts, fmt.Sprintf("%v", it)) + } + return strings.Join(parts, "\n") + default: + return fmt.Sprintf("%v", x) + } +} + +func cloneMap(in map[string]any) map[string]any { + out := make(map[string]any, len(in)) + for k, v := range in { + out[k] = v + } + return out +} diff --git a/internal/httpapi/claude/handler_messages.go b/internal/httpapi/claude/handler_messages.go new file mode 100644 index 0000000000000000000000000000000000000000..a89ed8da10bd7ea392b88f7dc56d5f6674215e35 --- /dev/null +++ b/internal/httpapi/claude/handler_messages.go @@ -0,0 +1,473 @@ +package claude + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "io" + "net/http" + "net/http/httptest" + "strings" + "time" + + "ds2api/internal/auth" + "ds2api/internal/completionruntime" + "ds2api/internal/config" + claudefmt "ds2api/internal/format/claude" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/httpapi/requestbody" + "ds2api/internal/promptcompat" + "ds2api/internal/responsehistory" + streamengine "ds2api/internal/stream" + "ds2api/internal/translatorcliproxy" + "ds2api/internal/util" + + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" +) + +func (h *Handler) Messages(w http.ResponseWriter, r *http.Request) { + if strings.TrimSpace(r.Header.Get("anthropic-version")) == "" { + r.Header.Set("anthropic-version", "2023-06-01") + } + if isClaudeVercelProxyRequest(r) && h.proxyViaOpenAI(w, r, h.Store) { + return + } + if h.Auth == nil || h.DS == nil { + if h.OpenAI != nil && h.proxyViaOpenAI(w, r, h.Store) { + return + } + writeClaudeError(w, http.StatusInternalServerError, "Claude runtime backend unavailable.") + return + } + if h.handleClaudeDirect(w, r) { + return + } + writeClaudeError(w, http.StatusBadGateway, "Failed to handle Claude request.") +} + +func isClaudeVercelProxyRequest(r *http.Request) bool { + if r == nil || r.URL == nil { + return false + } + return strings.TrimSpace(r.URL.Query().Get("__stream_prepare")) == "1" || + strings.TrimSpace(r.URL.Query().Get("__stream_release")) == "1" +} + +func (h *Handler) handleClaudeDirect(w http.ResponseWriter, r *http.Request) bool { + raw, err := io.ReadAll(r.Body) + if err != nil { + if errors.Is(err, requestbody.ErrInvalidUTF8Body) { + writeClaudeError(w, http.StatusBadRequest, "invalid json") + } else { + writeClaudeError(w, http.StatusBadRequest, "invalid body") + } + return true + } + var req map[string]any + if err := json.Unmarshal(raw, &req); err != nil { + writeClaudeError(w, http.StatusBadRequest, "invalid json") + return true + } + norm, err := normalizeClaudeRequest(h.Store, req) + if err != nil { + writeClaudeError(w, http.StatusBadRequest, err.Error()) + return true + } + exposeThinking := norm.Standard.Thinking + a, err := h.Auth.Determine(r) + if err != nil { + writeClaudeError(w, http.StatusUnauthorized, err.Error()) + return true + } + defer h.Auth.Release(a) + stdReq, err := h.applyCurrentInputFile(r.Context(), a, norm.Standard) + if err != nil { + status, message := mapCurrentInputFileError(err) + writeClaudeError(w, status, message) + return true + } + historySession := responsehistory.Start(responsehistory.StartParams{ + Store: h.ChatHistory, + Request: r, + Auth: a, + Surface: "claude.messages", + Standard: stdReq, + }) + if stdReq.Stream { + h.handleClaudeDirectStream(w, r, a, stdReq, historySession) + return true + } + result, outErr := completionruntime.ExecuteNonStreamWithRetry(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + RetryEnabled: true, + CurrentInputFile: h.Store, + }) + if outErr != nil { + if historySession != nil { + historySession.ErrorTurn(outErr.Status, outErr.Message, outErr.Code, result.Turn) + } + writeClaudeError(w, outErr.Status, outErr.Message) + return true + } + if historySession != nil { + historySession.SuccessTurn(http.StatusOK, result.Turn, responsehistory.GenericUsage(result.Turn)) + } + writeJSON(w, http.StatusOK, claudefmt.BuildMessageResponseFromTurn( + fmt.Sprintf("msg_%d", time.Now().UnixNano()), + stdReq.ResponseModel, + result.Turn, + exposeThinking, + )) + return true +} + +func (h *Handler) applyCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { + if h == nil { + return stdReq, nil + } + return (history.Service{Store: h.Store, DS: h.DS}).ApplyCurrentInputFile(ctx, a, stdReq) +} + +func mapCurrentInputFileError(err error) (int, string) { + return history.MapError(err) +} + +func (h *Handler) handleClaudeDirectStream(w http.ResponseWriter, r *http.Request, a *auth.RequestAuth, stdReq promptcompat.StandardRequest, historySession *responsehistory.Session) { + start, outErr := completionruntime.StartCompletion(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + CurrentInputFile: h.Store, + }) + if outErr != nil { + if historySession != nil { + historySession.Error(outErr.Status, outErr.Message, outErr.Code, "", "") + } + writeClaudeError(w, outErr.Status, outErr.Message) + return + } + streamReq := start.Request + h.handleClaudeStreamRealtimeWithRetry(w, r, a, start.Response, start.Payload, start.Pow, streamReq, streamReq.ResponseModel, streamReq.Messages, streamReq.Thinking, streamReq.Search, streamReq.ToolNames, streamReq.ToolsRaw, streamReq.PromptTokenText, historySession) +} + +func (h *Handler) proxyViaOpenAI(w http.ResponseWriter, r *http.Request, store ConfigReader) bool { + raw, err := io.ReadAll(r.Body) + if err != nil { + if errors.Is(err, requestbody.ErrInvalidUTF8Body) { + writeClaudeError(w, http.StatusBadRequest, "invalid json") + } else { + writeClaudeError(w, http.StatusBadRequest, "invalid body") + } + return true + } + var req map[string]any + if err := json.Unmarshal(raw, &req); err != nil { + writeClaudeError(w, http.StatusBadRequest, "invalid json") + return true + } + model, _ := req["model"].(string) + stream := util.ToBool(req["stream"]) + + // Use the shared global model resolver so Claude/OpenAI/Gemini stay consistent. + translateModel := model + if store != nil { + if norm, normErr := normalizeClaudeRequest(store, cloneMap(req)); normErr == nil && strings.TrimSpace(norm.Standard.ResolvedModel) != "" { + translateModel = strings.TrimSpace(norm.Standard.ResolvedModel) + } + } + translatedReq := translatorcliproxy.ToOpenAI(sdktranslator.FormatClaude, translateModel, raw, stream) + translatedReq, exposeThinking := applyClaudeThinkingPolicyToOpenAIRequest(translatedReq, req) + + isVercelPrepare := strings.TrimSpace(r.URL.Query().Get("__stream_prepare")) == "1" + isVercelRelease := strings.TrimSpace(r.URL.Query().Get("__stream_release")) == "1" + + if isVercelRelease { + proxyReq := r.Clone(r.Context()) + proxyReq.URL.Path = "/v1/chat/completions" + proxyReq.Body = io.NopCloser(bytes.NewReader(raw)) + proxyReq.ContentLength = int64(len(raw)) + rec := httptest.NewRecorder() + h.OpenAI.ChatCompletions(rec, proxyReq) + res := rec.Result() + defer func() { _ = res.Body.Close() }() + body, _ := io.ReadAll(res.Body) + for k, vv := range res.Header { + for _, v := range vv { + w.Header().Add(k, v) + } + } + w.WriteHeader(res.StatusCode) + _, _ = w.Write(body) + return true + } + + proxyReq := r.Clone(r.Context()) + proxyReq.URL.Path = "/v1/chat/completions" + proxyReq.Body = io.NopCloser(bytes.NewReader(translatedReq)) + proxyReq.ContentLength = int64(len(translatedReq)) + + if stream && !isVercelPrepare { + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + streamWriter := translatorcliproxy.NewOpenAIStreamTranslatorWriter(w, sdktranslator.FormatClaude, model, raw, translatedReq) + h.OpenAI.ChatCompletions(streamWriter, proxyReq) + return true + } + + rec := httptest.NewRecorder() + h.OpenAI.ChatCompletions(rec, proxyReq) + res := rec.Result() + defer func() { _ = res.Body.Close() }() + body, _ := io.ReadAll(res.Body) + if res.StatusCode < 200 || res.StatusCode >= 300 { + for k, vv := range res.Header { + for _, v := range vv { + w.Header().Add(k, v) + } + } + w.WriteHeader(res.StatusCode) + _, _ = w.Write(body) + return true + } + if isVercelPrepare { + for k, vv := range res.Header { + for _, v := range vv { + w.Header().Add(k, v) + } + } + w.WriteHeader(res.StatusCode) + _, _ = w.Write(body) + return true + } + converted := translatorcliproxy.FromOpenAINonStream(sdktranslator.FormatClaude, model, raw, translatedReq, body) + if !exposeThinking { + converted = stripClaudeThinkingBlocks(converted) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(converted) + return true +} + +func applyClaudeThinkingPolicyToOpenAIRequest(translated []byte, original map[string]any) ([]byte, bool) { + req := map[string]any{} + if err := json.Unmarshal(translated, &req); err != nil { + return translated, false + } + enabled, ok := util.ResolveThinkingOverride(original) + if !ok { + if _, translatedHasOverride := util.ResolveThinkingOverride(req); translatedHasOverride { + return translated, false + } + enabled = true + } + typ := "disabled" + if enabled { + typ = "enabled" + } + req["thinking"] = map[string]any{"type": typ} + out, err := json.Marshal(req) + if err != nil { + return translated, enabled + } + return out, enabled +} + +func stripClaudeThinkingBlocks(raw []byte) []byte { + var payload map[string]any + if err := json.Unmarshal(raw, &payload); err != nil { + return raw + } + content, _ := payload["content"].([]any) + if len(content) == 0 { + return raw + } + filtered := make([]any, 0, len(content)) + for _, item := range content { + block, _ := item.(map[string]any) + blockType, _ := block["type"].(string) + if strings.TrimSpace(blockType) == "thinking" { + continue + } + filtered = append(filtered, item) + } + payload["content"] = filtered + out, err := json.Marshal(payload) + if err != nil { + return raw + } + return out +} + +func (h *Handler) handleClaudeStreamRealtime(w http.ResponseWriter, r *http.Request, resp *http.Response, model string, messages []any, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, historySessions ...*responsehistory.Session) { + var historySession *responsehistory.Session + if len(historySessions) > 0 { + historySession = historySessions[0] + } + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.Error(resp.StatusCode, strings.TrimSpace(string(body)), "error", "", "") + } + writeClaudeError(w, http.StatusInternalServerError, string(body)) + return + } + + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + if !canFlush { + config.Logger.Warn("[claude_stream] response writer does not support flush; streaming may be buffered") + } + + streamRuntime := newClaudeStreamRuntime( + w, + rc, + canFlush, + model, + messages, + thinkingEnabled, + searchEnabled, + stripReferenceMarkersEnabled(), + toolNames, + toolsRaw, + buildClaudePromptTokenText(messages, thinkingEnabled), + historySession, + ) + streamRuntime.sendMessageStart() + + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: r.Context(), + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: claudeStreamPingInterval, + IdleTimeout: claudeStreamIdleTimeout, + MaxKeepAliveNoInput: claudeStreamMaxKeepaliveCnt, + }, streamengine.ConsumeHooks{ + OnKeepAlive: func() { + streamRuntime.sendPing() + }, + OnParsed: streamRuntime.onParsed, + OnFinalize: streamRuntime.onFinalize, + }) +} + +func (h *Handler) handleClaudeStreamRealtimeWithRetry(w http.ResponseWriter, r *http.Request, a *auth.RequestAuth, resp *http.Response, payload map[string]any, pow string, stdReq promptcompat.StandardRequest, model string, messages []any, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, promptTokenText string, historySession *responsehistory.Session) { + if resp.StatusCode != http.StatusOK { + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.Error(resp.StatusCode, strings.TrimSpace(string(body)), "error", "", "") + } + writeClaudeError(w, http.StatusInternalServerError, string(body)) + return + } + + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + if !canFlush { + config.Logger.Warn("[claude_stream] response writer does not support flush; streaming may be buffered") + } + + streamRuntime := newClaudeStreamRuntime( + w, + rc, + canFlush, + model, + messages, + thinkingEnabled, + searchEnabled, + stripReferenceMarkersEnabled(), + toolNames, + toolsRaw, + promptTokenText, + historySession, + ) + streamRuntime.sendMessageStart() + + completionruntime.ExecuteStreamWithRetry(r.Context(), h.DS, a, resp, payload, pow, completionruntime.StreamRetryOptions{ + Surface: "claude.messages", + Stream: true, + RetryEnabled: true, + MaxAttempts: 3, + UsagePrompt: promptTokenText, + Request: stdReq, + CurrentInputFile: h.Store, + }, completionruntime.StreamRetryHooks{ + ConsumeAttempt: func(currentResp *http.Response, allowDeferEmpty bool) (bool, bool) { + return h.consumeClaudeStreamAttempt(r, currentResp, streamRuntime, thinkingEnabled, allowDeferEmpty) + }, + Finalize: func(_ int) { + streamRuntime.finalize("end_turn", false) + }, + ParentMessageID: func() int { + return streamRuntime.responseMessageID + }, + OnRetryPrompt: func(prompt string) { + streamRuntime.promptTokenText = prompt + }, + OnRetryFailure: func(status int, message, code string) { + streamRuntime.sendErrorWithCode(status, strings.TrimSpace(message), code) + }, + }) +} + +func (h *Handler) consumeClaudeStreamAttempt(r *http.Request, resp *http.Response, streamRuntime *claudeStreamRuntime, thinkingEnabled bool, allowDeferEmpty bool) (bool, bool) { + defer func() { _ = resp.Body.Close() }() + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + finalReason := streamengine.StopReason("") + var scannerErr error + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: r.Context(), + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: claudeStreamPingInterval, + IdleTimeout: claudeStreamIdleTimeout, + MaxKeepAliveNoInput: claudeStreamMaxKeepaliveCnt, + }, streamengine.ConsumeHooks{ + OnKeepAlive: func() { + streamRuntime.sendPing() + }, + OnParsed: streamRuntime.onParsed, + OnFinalize: func(reason streamengine.StopReason, err error) { + finalReason = reason + scannerErr = err + }, + }) + if string(finalReason) == "upstream_error" { + if streamRuntime.history != nil { + streamRuntime.history.Error(500, streamRuntime.upstreamErr, "upstream_error", responsehistory.ThinkingForArchive(streamRuntime.rawThinking.String(), streamRuntime.toolDetectionThinking.String(), streamRuntime.thinking.String()), responsehistory.TextForArchive(streamRuntime.rawText.String(), streamRuntime.text.String())) + } + streamRuntime.sendError(streamRuntime.upstreamErr) + return true, false + } + if scannerErr != nil { + if streamRuntime.history != nil { + streamRuntime.history.Error(500, scannerErr.Error(), "error", responsehistory.ThinkingForArchive(streamRuntime.rawThinking.String(), streamRuntime.toolDetectionThinking.String(), streamRuntime.thinking.String()), responsehistory.TextForArchive(streamRuntime.rawText.String(), streamRuntime.text.String())) + } + streamRuntime.sendError(scannerErr.Error()) + return true, false + } + terminalWritten := streamRuntime.finalize("end_turn", allowDeferEmpty) + if terminalWritten { + return true, false + } + return false, true +} diff --git a/internal/httpapi/claude/handler_routes.go b/internal/httpapi/claude/handler_routes.go new file mode 100644 index 0000000000000000000000000000000000000000..257fc080dc0c229e6dec8a84e37a13623f16f5ff --- /dev/null +++ b/internal/httpapi/claude/handler_routes.go @@ -0,0 +1,49 @@ +package claude + +import ( + "net/http" + "time" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/chathistory" + "ds2api/internal/config" + dsprotocol "ds2api/internal/deepseek/protocol" + "ds2api/internal/textclean" + "ds2api/internal/util" +) + +// writeJSON is a package-internal alias to avoid mass-renaming all call-sites. +var writeJSON = util.WriteJSON + +type Handler struct { + Store ConfigReader + Auth AuthResolver + DS DeepSeekCaller + OpenAI OpenAIChatRunner + ChatHistory *chathistory.Store +} + +func stripReferenceMarkersEnabled() bool { + return textclean.StripReferenceMarkersEnabled() +} + +var ( + claudeStreamPingInterval = time.Duration(dsprotocol.KeepAliveTimeout) * time.Second + claudeStreamIdleTimeout = time.Duration(dsprotocol.StreamIdleTimeout) * time.Second + claudeStreamMaxKeepaliveCnt = dsprotocol.MaxKeepaliveCount +) + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/anthropic/v1/models", h.ListModels) + r.Post("/anthropic/v1/messages", h.Messages) + r.Post("/anthropic/v1/messages/count_tokens", h.CountTokens) + r.Post("/v1/messages", h.Messages) + r.Post("/messages", h.Messages) + r.Post("/v1/messages/count_tokens", h.CountTokens) + r.Post("/messages/count_tokens", h.CountTokens) +} + +func (h *Handler) ListModels(w http.ResponseWriter, _ *http.Request) { + writeJSON(w, http.StatusOK, config.ClaudeModelsResponse()) +} diff --git a/internal/httpapi/claude/handler_stream_test.go b/internal/httpapi/claude/handler_stream_test.go new file mode 100644 index 0000000000000000000000000000000000000000..7cd68a7269c46fbcac722435021f8f34d690a99e --- /dev/null +++ b/internal/httpapi/claude/handler_stream_test.go @@ -0,0 +1,481 @@ +package claude + +import ( + "ds2api/internal/sse" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" +) + +type claudeFrame struct { + Event string + Payload map[string]any +} + +func makeClaudeSSEHTTPResponse(lines ...string) *http.Response { + body := strings.Join(lines, "\n") + if !strings.HasSuffix(body, "\n") { + body += "\n" + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +func makeClaudeContentLine(t *testing.T, text string) string { + t.Helper() + line, err := json.Marshal(map[string]any{ + "p": "response/content", + "v": text, + }) + if err != nil { + t.Fatalf("marshal content line failed: %v", err) + } + return "data: " + string(line) +} + +func parseClaudeFrames(t *testing.T, body string) []claudeFrame { + t.Helper() + chunks := strings.Split(body, "\n\n") + frames := make([]claudeFrame, 0, len(chunks)) + for _, chunk := range chunks { + chunk = strings.TrimSpace(chunk) + if chunk == "" { + continue + } + lines := strings.Split(chunk, "\n") + eventName := "" + dataPayload := "" + for _, line := range lines { + line = strings.TrimSpace(line) + switch { + case strings.HasPrefix(line, "event:"): + eventName = strings.TrimSpace(strings.TrimPrefix(line, "event:")) + case strings.HasPrefix(line, "data:"): + dataPayload = strings.TrimSpace(strings.TrimPrefix(line, "data:")) + } + } + if eventName == "" || dataPayload == "" { + continue + } + var payload map[string]any + if err := json.Unmarshal([]byte(dataPayload), &payload); err != nil { + t.Fatalf("decode frame failed: %v, payload=%s", err, dataPayload) + } + frames = append(frames, claudeFrame{Event: eventName, Payload: payload}) + } + return frames +} + +func findClaudeFrames(frames []claudeFrame, event string) []claudeFrame { + out := make([]claudeFrame, 0) + for _, f := range frames { + if f.Event == event { + out = append(out, f) + } + } + return out +} + +func collectClaudeTextDeltas(frames []claudeFrame) string { + var combined strings.Builder + for _, f := range findClaudeFrames(frames, "content_block_delta") { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["type"] == "text_delta" { + combined.WriteString(asString(delta["text"])) + } + } + return combined.String() +} + +func TestHandleClaudeStreamRealtimeTextIncrementsWithEventHeaders(t *testing.T) { + h := &Handler{} + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/content","v":"Hel"}`, + `data: {"p":"response/content","v":"lo"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "hi"}}, false, false, nil, nil) + + body := rec.Body.String() + if !strings.Contains(body, "event: message_start") { + t.Fatalf("missing event header: message_start, body=%s", body) + } + if !strings.Contains(body, "event: content_block_delta") { + t.Fatalf("missing event header: content_block_delta, body=%s", body) + } + if !strings.Contains(body, "event: message_stop") { + t.Fatalf("missing event header: message_stop, body=%s", body) + } + + frames := parseClaudeFrames(t, body) + deltas := findClaudeFrames(frames, "content_block_delta") + if len(deltas) < 1 { + t.Fatalf("expected at least 1 text delta, got=%d body=%s", len(deltas), body) + } + combined := strings.Builder{} + for _, f := range deltas { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["type"] == "text_delta" { + combined.WriteString(asString(delta["text"])) + } + } + if combined.String() != "Hello" { + t.Fatalf("unexpected combined text: %q body=%s", combined.String(), body) + } +} + +func TestHandleClaudeStreamRealtimeToolBufferedPlainTextDoesNotRepeatFinalText(t *testing.T) { + h := &Handler{} + want := "明白\n\nBash\nIN\npwd\nOUT\nok" + resp := makeClaudeSSEHTTPResponse( + makeClaudeContentLine(t, "明"), + makeClaudeContentLine(t, "白\n\nBash\nIN\npwd\n"), + makeClaudeContentLine(t, "OUT\nok"), + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "use tool"}}, false, false, []string{"Bash"}, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + if got := collectClaudeTextDeltas(frames); got != want { + t.Fatalf("unexpected combined text: got %q want %q body=%s", got, want, rec.Body.String()) + } +} + +func TestHandleClaudeStreamRealtimeTrimsContinuationReplay(t *testing.T) { + h := &Handler{} + prefix := strings.Repeat("A", 40) + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/content","v":"`+prefix+`"}`, + `data: {"p":"response/content","v":"`+prefix+` tail"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "hi"}}, false, false, nil, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + combined := strings.Builder{} + for _, f := range findClaudeFrames(frames, "content_block_delta") { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["type"] == "text_delta" { + combined.WriteString(asString(delta["text"])) + } + } + if got, want := combined.String(), prefix+" tail"; got != want { + t.Fatalf("unexpected combined text: got %q want %q body=%s", got, want, rec.Body.String()) + } +} + +func TestHandleClaudeStreamRealtimeThinkingDelta(t *testing.T) { + h := &Handler{} + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"思"}`, + `data: {"p":"response/thinking_content","v":"考"}`, + `data: {"p":"response/content","v":"ok"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "hi"}}, true, false, nil, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + foundThinkingDelta := false + for _, f := range findClaudeFrames(frames, "content_block_delta") { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["type"] == "thinking_delta" { + foundThinkingDelta = true + break + } + } + if !foundThinkingDelta { + t.Fatalf("expected thinking_delta event, body=%s", rec.Body.String()) + } +} + +func TestHandleClaudeStreamRealtimeSkipsThinkingFallbackWhenFinalTextExists(t *testing.T) { + h := &Handler{} + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"{\"tool_calls\":[{\"name\":\"search\""}`, + `data: {"p":"response/thinking_content","v":",\"input\":{\"q\":\"go\"}}]}"}`, + `data: {"p":"response/content","v":"normal answer"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "use tool"}}, true, false, []string{"search"}, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + for _, f := range findClaudeFrames(frames, "content_block_start") { + contentBlock, _ := f.Payload["content_block"].(map[string]any) + if contentBlock["type"] == "tool_use" { + t.Fatalf("unexpected tool_use block when final text exists, body=%s", rec.Body.String()) + } + } + + foundEndTurn := false + for _, f := range findClaudeFrames(frames, "message_delta") { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["stop_reason"] == "end_turn" { + foundEndTurn = true + break + } + } + if !foundEndTurn { + t.Fatalf("expected stop_reason=end_turn, body=%s", rec.Body.String()) + } +} + +func TestHandleClaudeStreamRealtimeUpstreamErrorEvent(t *testing.T) { + h := &Handler{} + resp := makeClaudeSSEHTTPResponse( + `data: {"error":{"message":"boom"}}`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "hi"}}, false, false, nil, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + errFrames := findClaudeFrames(frames, "error") + if len(errFrames) == 0 { + t.Fatalf("expected error event frame, body=%s", rec.Body.String()) + } + if errFrames[0].Payload["type"] != "error" { + t.Fatalf("expected error payload type, body=%s", rec.Body.String()) + } +} + +func TestHandleClaudeStreamRealtimePingEvent(t *testing.T) { + h := &Handler{} + oldPing := claudeStreamPingInterval + oldIdle := claudeStreamIdleTimeout + oldKeepalive := claudeStreamMaxKeepaliveCnt + claudeStreamPingInterval = 10 * time.Millisecond + claudeStreamIdleTimeout = 300 * time.Millisecond + claudeStreamMaxKeepaliveCnt = 50 + defer func() { + claudeStreamPingInterval = oldPing + claudeStreamIdleTimeout = oldIdle + claudeStreamMaxKeepaliveCnt = oldKeepalive + }() + + pr, pw := io.Pipe() + resp := &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: pr} + go func() { + time.Sleep(40 * time.Millisecond) + _, _ = io.WriteString(pw, "data: {\"p\":\"response/content\",\"v\":\"hi\"}\n") + _, _ = io.WriteString(pw, "data: [DONE]\n") + _ = pw.Close() + }() + + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "hi"}}, false, false, nil, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + if len(findClaudeFrames(frames, "ping")) == 0 { + t.Fatalf("expected ping event in stream, body=%s", rec.Body.String()) + } +} + +func TestCollectDeepSeekRegression(t *testing.T) { + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"想"}`, + `data: {"p":"response/content","v":"答"}`, + `data: [DONE]`, + ) + result := sse.CollectStream(resp, true, true) + if result.Thinking != "想" { + t.Fatalf("unexpected thinking: %q", result.Thinking) + } + if result.Text != "答" { + t.Fatalf("unexpected text: %q", result.Text) + } +} + +func asString(v any) string { + s, _ := v.(string) + return s +} + +func TestHandleClaudeStreamRealtimeToolSafetyAcrossStructuredFormats(t *testing.T) { + tests := []struct { + name string + payload string + wantToolUse bool + }{ + {name: "invoke_parameter_wrapper", payload: `pwd`, wantToolUse: true}, + {name: "legacy_single_tool_root", payload: `Bashpwd`, wantToolUse: false}, + {name: "legacy_tool_call_json", payload: `{"tool":"Bash","params":{"command":"pwd"}}`, wantToolUse: false}, + {name: "legacy_nested_tool_tag_style", payload: `pwd`, wantToolUse: false}, + {name: "legacy_function_tag_style", payload: `Bashpwd`, wantToolUse: false}, + {name: "legacy_antml_argument_style", payload: `pwd`, wantToolUse: false}, + {name: "legacy_antml_function_attr_parameters", payload: `{"command":"pwd"}`, wantToolUse: false}, + {name: "legacy_function_calls_wrapper", payload: `pwd`, wantToolUse: false}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + h := &Handler{} + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/content","v":"`+strings.ReplaceAll(tc.payload, `"`, `\"`)+`"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "use tool"}}, false, false, []string{"Bash"}, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + foundToolUse := false + for _, f := range findClaudeFrames(frames, "content_block_start") { + contentBlock, _ := f.Payload["content_block"].(map[string]any) + if contentBlock["type"] == "tool_use" { + foundToolUse = true + break + } + } + if foundToolUse != tc.wantToolUse { + t.Fatalf("unexpected tool_use=%v for format %s, body=%s", foundToolUse, tc.name, rec.Body.String()) + } + }) + } +} + +func TestHandleClaudeStreamRealtimeDetectsToolUseWithLeadingProse(t *testing.T) { + h := &Handler{} + payload := "I'll call a tool now.\\n/tmp/a.txtabc" + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/content","v":"`+payload+`"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "use tool"}}, false, false, []string{"write_file"}, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + foundToolUse := false + for _, f := range findClaudeFrames(frames, "content_block_start") { + contentBlock, _ := f.Payload["content_block"].(map[string]any) + if contentBlock["type"] == "tool_use" && contentBlock["name"] == "write_file" { + foundToolUse = true + break + } + } + if !foundToolUse { + t.Fatalf("expected tool_use block with leading prose payload, body=%s", rec.Body.String()) + } + + for _, f := range findClaudeFrames(frames, "message_delta") { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["stop_reason"] == "tool_use" { + return + } + } + t.Fatalf("expected stop_reason=tool_use, body=%s", rec.Body.String()) +} + +func TestHandleClaudeStreamRealtimeIgnoresUnclosedFencedToolExample(t *testing.T) { + h := &Handler{} + resp := makeClaudeSSEHTTPResponse( + "data: {\"p\":\"response/content\",\"v\":\"Here is an example:\\n```json\\n{\\\"tool_calls\\\":[{\\\"name\\\":\\\"Bash\\\",\\\"input\\\":{\\\"command\\\":\\\"pwd\\\"}}]}\"}", + "data: {\"p\":\"response/content\",\"v\":\"\\n```\\nDo not execute it.\"}", + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "show example only"}}, false, false, []string{"Bash"}, nil) + + frames := parseClaudeFrames(t, rec.Body.String()) + foundToolUse := false + for _, f := range findClaudeFrames(frames, "content_block_start") { + contentBlock, _ := f.Payload["content_block"].(map[string]any) + if contentBlock["type"] == "tool_use" { + foundToolUse = true + break + } + } + if foundToolUse { + t.Fatalf("expected no tool_use for fenced example, body=%s", rec.Body.String()) + } + + foundToolStop := false + for _, f := range findClaudeFrames(frames, "message_delta") { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["stop_reason"] == "tool_use" { + foundToolStop = true + break + } + } + if foundToolStop { + t.Fatalf("expected stop_reason to remain content-only, body=%s", rec.Body.String()) + } +} + +// Backward-compatible alias for historical test name used in CI logs. +func TestHandleClaudeStreamRealtimePromotesUnclosedFencedToolExample(t *testing.T) { + TestHandleClaudeStreamRealtimeIgnoresUnclosedFencedToolExample(t) +} + +func TestHandleClaudeStreamRealtimeNormalizesToolInputBySchema(t *testing.T) { + h := &Handler{} + resp := makeClaudeSSEHTTPResponse( + `data: {"p":"response/content","v":"{\"input\":{\"content\":{\"message\":\"hi\"},\"taskId\":1}}"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil) + toolsRaw := []any{ + map[string]any{ + "name": "Write", + "inputSchema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + "taskId": map[string]any{"type": "string"}, + }, + }, + }, + } + + h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "write"}}, false, false, []string{"Write"}, toolsRaw) + + frames := parseClaudeFrames(t, rec.Body.String()) + for _, f := range findClaudeFrames(frames, "content_block_delta") { + delta, _ := f.Payload["delta"].(map[string]any) + if delta["type"] != "input_json_delta" { + continue + } + partial := asString(delta["partial_json"]) + var args map[string]any + if err := json.Unmarshal([]byte(partial), &args); err != nil { + t.Fatalf("decode partial_json failed: %v payload=%s", err, partial) + } + if args["content"] != `{"message":"hi"}` { + t.Fatalf("expected content normalized to string, got %#v", args["content"]) + } + if args["taskId"] != "1" { + t.Fatalf("expected taskId normalized to string, got %#v", args["taskId"]) + } + return + } + t.Fatalf("expected input_json_delta frame, body=%s", rec.Body.String()) +} diff --git a/internal/httpapi/claude/handler_tokens.go b/internal/httpapi/claude/handler_tokens.go new file mode 100644 index 0000000000000000000000000000000000000000..d122b0ff3a9335e4dfb3809f027791b5fa274946 --- /dev/null +++ b/internal/httpapi/claude/handler_tokens.go @@ -0,0 +1,34 @@ +package claude + +import ( + "encoding/json" + "net/http" +) + +func (h *Handler) CountTokens(w http.ResponseWriter, r *http.Request) { + a, err := h.Auth.Determine(r) + if err != nil { + writeClaudeError(w, http.StatusUnauthorized, err.Error()) + return + } + defer h.Auth.Release(a) + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeClaudeError(w, http.StatusBadRequest, "invalid json") + return + } + model, _ := req["model"].(string) + messages, _ := req["messages"].([]any) + if model == "" || len(messages) == 0 { + writeClaudeError(w, http.StatusBadRequest, "Request must include 'model' and 'messages'.") + return + } + normalized, err := normalizeClaudeRequest(h.Store, req) + if err != nil { + writeClaudeError(w, http.StatusBadRequest, err.Error()) + return + } + inputTokens := countClaudeInputTokens(normalized.Standard) + writeJSON(w, http.StatusOK, map[string]any{"input_tokens": inputTokens}) +} diff --git a/internal/httpapi/claude/handler_util_test.go b/internal/httpapi/claude/handler_util_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b076207f506d7311d2159c1726ba84108eb97446 --- /dev/null +++ b/internal/httpapi/claude/handler_util_test.go @@ -0,0 +1,597 @@ +package claude + +import ( + "strings" + "testing" +) + +// ─── normalizeClaudeMessages ───────────────────────────────────────── + +func TestNormalizeClaudeMessagesSimpleString(t *testing.T) { + msgs := []any{ + map[string]any{"role": "user", "content": "Hello"}, + } + got := normalizeClaudeMessages(msgs) + if len(got) != 1 { + t.Fatalf("expected 1 message, got %d", len(got)) + } + m := got[0].(map[string]any) + if m["content"] != "Hello" { + t.Fatalf("expected 'Hello', got %v", m["content"]) + } +} + +func TestNormalizeClaudeMessagesArrayContent(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{"type": "text", "text": "line1"}, + map[string]any{"type": "text", "text": "line2"}, + }, + }, + } + got := normalizeClaudeMessages(msgs) + m := got[0].(map[string]any) + if m["content"] != "line1\nline2" { + t.Fatalf("expected joined text, got %q", m["content"]) + } +} + +func TestNormalizeClaudeMessagesToolResult(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{"type": "tool_result", "content": "tool output"}, + }, + }, + } + got := normalizeClaudeMessages(msgs) + if len(got) != 1 { + t.Fatalf("expected one normalized message, got %d", len(got)) + } + m := got[0].(map[string]any) + if m["role"] != "tool" { + t.Fatalf("expected tool role preserved, got %#v", m["role"]) + } + content, _ := m["content"].(string) + if content != "tool output" { + t.Fatalf("expected raw tool output content preserved, got %q", content) + } +} + +func TestNormalizeClaudeMessagesToolUseToAssistantToolCalls(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "assistant", + "content": []any{ + map[string]any{ + "type": "tool_use", + "id": "call_1", + "name": "search_web", + "input": map[string]any{"query": "latest"}, + }, + }, + }, + } + + got := normalizeClaudeMessages(msgs) + if len(got) != 1 { + t.Fatalf("expected one normalized tool-call message, got %d", len(got)) + } + m := got[0].(map[string]any) + if m["role"] != "assistant" { + t.Fatalf("expected assistant role, got %#v", m["role"]) + } + tc, _ := m["tool_calls"].([]any) + if len(tc) != 1 { + t.Fatalf("expected one tool call, got %#v", m["tool_calls"]) + } + call, _ := tc[0].(map[string]any) + if call["id"] != "call_1" { + t.Fatalf("expected call id preserved, got %#v", call) + } + content, _ := m["content"].(string) + if !containsStr(content, "<|DSML|tool_calls>") || !containsStr(content, `<|DSML|invoke name="search_web">`) { + t.Fatalf("expected assistant content to include DSML tool call history, got %q", content) + } + if !containsStr(content, `<|DSML|parameter name="query">`) { + t.Fatalf("expected assistant content to include serialized parameters, got %q", content) + } +} + +func TestNormalizeClaudeMessagesPreservesThinkingOnToolUseHistory(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "assistant", + "content": []any{ + map[string]any{"type": "thinking", "thinking": "need live search before answering"}, + map[string]any{ + "type": "tool_use", + "id": "call_1", + "name": "search_web", + "input": map[string]any{"query": "latest"}, + }, + }, + }, + } + + got := normalizeClaudeMessages(msgs) + if len(got) != 1 { + t.Fatalf("expected one normalized tool-call message, got %#v", got) + } + m := got[0].(map[string]any) + if m["reasoning_content"] != "need live search before answering" { + t.Fatalf("expected thinking preserved as reasoning_content, got %#v", m) + } + tc, _ := m["tool_calls"].([]any) + if len(tc) != 1 { + t.Fatalf("expected one tool call, got %#v", m["tool_calls"]) + } + prompt := buildClaudePromptTokenText(got, true) + if !containsStr(prompt, "[reasoning_content]\nneed live search before answering\n[/reasoning_content]") { + t.Fatalf("expected thinking in prompt history, got %q", prompt) + } + if !containsStr(prompt, `<|DSML|invoke name="search_web">`) { + t.Fatalf("expected tool call in prompt history, got %q", prompt) + } +} + +func TestNormalizeClaudeMessagesDoesNotPromoteUserToolUse(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{ + "type": "tool_use", + "id": "call_unsafe", + "name": "dangerous_tool", + "input": map[string]any{"value": "x"}, + }, + }, + }, + } + + got := normalizeClaudeMessages(msgs) + if len(got) != 1 { + t.Fatalf("expected one normalized message, got %d", len(got)) + } + m := got[0].(map[string]any) + if m["role"] != "user" { + t.Fatalf("expected user role preserved, got %#v", m["role"]) + } + if _, ok := m["tool_calls"]; ok { + t.Fatalf("expected no tool_calls promotion for user message, got %#v", m["tool_calls"]) + } + content, _ := m["content"].(string) + if !containsStr(content, `"type":"tool_use"`) || !containsStr(content, "dangerous_tool") { + t.Fatalf("expected raw tool_use block preserved in user content, got %q", content) + } +} + +func TestNormalizeClaudeMessagesSkipsNonMap(t *testing.T) { + msgs := []any{"not a map", 42} + got := normalizeClaudeMessages(msgs) + if len(got) != 0 { + t.Fatalf("expected 0 messages for non-map items, got %d", len(got)) + } +} + +func TestNormalizeClaudeMessagesEmpty(t *testing.T) { + got := normalizeClaudeMessages(nil) + if len(got) != 0 { + t.Fatalf("expected 0, got %d", len(got)) + } +} + +func TestNormalizeClaudeMessagesPreservesRole(t *testing.T) { + msgs := []any{ + map[string]any{"role": "assistant", "content": "response"}, + } + got := normalizeClaudeMessages(msgs) + m := got[0].(map[string]any) + if m["role"] != "assistant" { + t.Fatalf("expected 'assistant', got %q", m["role"]) + } +} + +func TestNormalizeClaudeMessagesMixedContentBlocks(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{"type": "text", "text": "Hello"}, + map[string]any{"type": "image", "source": map[string]any{"type": "base64", "data": strings.Repeat("A", 2048)}}, + map[string]any{"type": "text", "text": "World"}, + }, + }, + } + got := normalizeClaudeMessages(msgs) + m := got[0].(map[string]any) + content, _ := m["content"].(string) + if !containsStr(content, "Hello") || !containsStr(content, "World") || !containsStr(content, `"type":"image"`) { + t.Fatalf("expected text plus non-text block marker preserved, got %q", content) + } + if !containsStr(content, omittedBinaryMarker) { + t.Fatalf("expected binary payload omitted marker, got %q", content) + } + if containsStr(content, strings.Repeat("A", 100)) { + t.Fatalf("expected raw base64 payload not to be included, got %q", content) + } +} + +func TestNormalizeClaudeMessagesToolResultNonTextPayloadStringified(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{ + "type": "tool_result", + "tool_use_id": "call_image_1", + "name": "vision_tool", + "content": []any{ + map[string]any{"type": "text", "text": "image analysis"}, + map[string]any{ + "type": "image", + "source": map[string]any{"type": "base64", "media_type": "image/png", "data": strings.Repeat("B", 2048)}, + }, + }, + }, + }, + }, + } + + got := normalizeClaudeMessages(msgs) + if len(got) != 1 { + t.Fatalf("expected one normalized message, got %d", len(got)) + } + m := got[0].(map[string]any) + if m["role"] != "tool" { + t.Fatalf("expected tool role, got %#v", m["role"]) + } + content, _ := m["content"].(string) + if !containsStr(content, `"type":"tool_result"`) || !containsStr(content, `"type":"image"`) { + t.Fatalf("expected non-text tool_result payload to be JSON stringified, got %q", content) + } + if !containsStr(content, omittedBinaryMarker) { + t.Fatalf("expected binary data to be sanitized with omitted marker, got %q", content) + } + if containsStr(content, strings.Repeat("B", 100)) { + t.Fatalf("expected raw base64 payload not to be included, got %q", content) + } +} + +func TestNormalizeClaudeMessagesBackfillsToolResultCallIDByName(t *testing.T) { + msgs := []any{ + map[string]any{ + "role": "assistant", + "content": []any{ + map[string]any{ + "type": "tool_use", + "name": "search_web", + "input": map[string]any{"query": "latest"}, + }, + }, + }, + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{ + "type": "tool_result", + "name": "search_web", + "content": "ok", + }, + }, + }, + } + + got := normalizeClaudeMessages(msgs) + if len(got) != 2 { + t.Fatalf("expected 2 messages, got %#v", got) + } + assistant, _ := got[0].(map[string]any) + tc, _ := assistant["tool_calls"].([]any) + call, _ := tc[0].(map[string]any) + callID, _ := call["id"].(string) + if !strings.HasPrefix(callID, "call_claude_") { + t.Fatalf("expected generated call id, got %#v", call) + } + toolMsg, _ := got[1].(map[string]any) + if toolMsg["tool_call_id"] != callID { + t.Fatalf("expected tool_result to reuse generated id, got %#v", toolMsg) + } +} + +// ─── buildClaudeToolPrompt ─────────────────────────────────────────── + +func TestBuildClaudeToolPromptSingleTool(t *testing.T) { + tools := []any{ + map[string]any{ + "name": "search", + "description": "Search the web", + "input_schema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "query": map[string]any{"type": "string"}, + }, + }, + }, + } + prompt := buildClaudeToolPrompt(tools) + if prompt == "" { + t.Fatal("expected non-empty prompt") + } + // Should contain tool name and description + if !containsStr(prompt, "search") { + t.Fatalf("expected 'search' in prompt") + } + if !containsStr(prompt, "Search the web") { + t.Fatalf("expected description in prompt") + } + if !containsStr(prompt, "<|DSML|tool_calls>") { + t.Fatalf("expected DSML tool_calls format in prompt") + } + if !containsStr(prompt, "TOOL CALL SCHEME") { + t.Fatalf("expected tool call scheme header in prompt") + } +} + +func TestBuildClaudeToolPromptMultipleTools(t *testing.T) { + tools := []any{ + map[string]any{"name": "tool1", "description": "desc1"}, + map[string]any{"name": "tool2", "description": "desc2"}, + } + prompt := buildClaudeToolPrompt(tools) + if !containsStr(prompt, "tool1") || !containsStr(prompt, "tool2") { + t.Fatalf("expected both tools in prompt") + } +} + +func TestBuildClaudeToolPromptSupportsOpenAIStyleFunctionTool(t *testing.T) { + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "Search via function tool", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "q": map[string]any{"type": "string"}, + }, + }, + }, + }, + } + prompt := buildClaudeToolPrompt(tools) + if !containsStr(prompt, "Tool: search") { + t.Fatalf("expected OpenAI-style function tool name in prompt, got: %q", prompt) + } + if !containsStr(prompt, "Search via function tool") { + t.Fatalf("expected OpenAI-style function tool description in prompt, got: %q", prompt) + } + if !containsStr(prompt, "\"q\"") { + t.Fatalf("expected parameters schema serialized in prompt, got: %q", prompt) + } +} + +func TestBuildClaudeToolPromptSkipsNonMap(t *testing.T) { + tools := []any{"not a map"} + prompt := buildClaudeToolPrompt(tools) + // No valid tools → empty prompt + if prompt != "" { + t.Fatalf("expected empty prompt for non-map tools, got: %q", prompt) + } +} + +// ─── hasSystemMessage ──────────────────────────────────────────────── + +func TestHasSystemMessageTrue(t *testing.T) { + msgs := []any{ + map[string]any{"role": "system", "content": "You are a helper"}, + map[string]any{"role": "user", "content": "Hi"}, + } + if !hasSystemMessage(msgs) { + t.Fatal("expected true") + } +} + +func TestHasSystemMessageFalse(t *testing.T) { + msgs := []any{ + map[string]any{"role": "user", "content": "Hi"}, + map[string]any{"role": "assistant", "content": "Hello"}, + } + if hasSystemMessage(msgs) { + t.Fatal("expected false") + } +} + +func TestHasSystemMessageEmpty(t *testing.T) { + if hasSystemMessage(nil) { + t.Fatal("expected false for nil") + } +} + +func TestHasSystemMessageNonMap(t *testing.T) { + msgs := []any{"not a map"} + if hasSystemMessage(msgs) { + t.Fatal("expected false for non-map") + } +} + +// ─── extractClaudeToolNames ────────────────────────────────────────── + +func TestExtractClaudeToolNamesSingle(t *testing.T) { + tools := []any{ + map[string]any{"name": "search"}, + } + names := extractClaudeToolNames(tools) + if len(names) != 1 || names[0] != "search" { + t.Fatalf("expected [search], got %v", names) + } +} + +func TestExtractClaudeToolNamesMultiple(t *testing.T) { + tools := []any{ + map[string]any{"name": "search"}, + map[string]any{"name": "calculate"}, + } + names := extractClaudeToolNames(tools) + if len(names) != 2 { + t.Fatalf("expected 2 names, got %v", names) + } +} + +func TestExtractClaudeToolNamesSkipsEmptyName(t *testing.T) { + tools := []any{ + map[string]any{"name": ""}, + map[string]any{"name": "valid"}, + } + names := extractClaudeToolNames(tools) + if len(names) != 1 || names[0] != "valid" { + t.Fatalf("expected [valid], got %v", names) + } +} + +func TestExtractClaudeToolNamesSkipsNonMap(t *testing.T) { + tools := []any{"not a map", 42} + names := extractClaudeToolNames(tools) + if len(names) != 0 { + t.Fatalf("expected 0, got %v", names) + } +} + +func TestExtractClaudeToolNamesNil(t *testing.T) { + names := extractClaudeToolNames(nil) + if len(names) != 0 { + t.Fatalf("expected 0, got %v", names) + } +} + +func TestExtractClaudeToolNamesSupportsOpenAIStyleFunctionTool(t *testing.T) { + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + }, + }, + } + names := extractClaudeToolNames(tools) + if len(names) != 1 || names[0] != "search" { + t.Fatalf("expected [search], got %v", names) + } +} + +// ─── toMessageMaps ─────────────────────────────────────────────────── + +func TestToMessageMapsNormal(t *testing.T) { + input := []any{ + map[string]any{"role": "user", "content": "Hello"}, + } + got := toMessageMaps(input) + if len(got) != 1 { + t.Fatalf("expected 1, got %d", len(got)) + } +} + +func TestToMessageMapsNonSlice(t *testing.T) { + got := toMessageMaps("not a slice") + if got != nil { + t.Fatalf("expected nil, got %v", got) + } +} + +func TestToMessageMapsSkipsNonMap(t *testing.T) { + input := []any{"string", map[string]any{"role": "user"}, 42} + got := toMessageMaps(input) + if len(got) != 1 { + t.Fatalf("expected 1 map, got %d", len(got)) + } +} + +func TestToMessageMapsNil(t *testing.T) { + got := toMessageMaps(nil) + if got != nil { + t.Fatalf("expected nil, got %v", got) + } +} + +// ─── extractMessageContent ────────────────────────────────────────── + +func TestExtractMessageContentString(t *testing.T) { + if got := extractMessageContent("hello"); got != "hello" { + t.Fatalf("expected 'hello', got %q", got) + } +} + +func TestExtractMessageContentArray(t *testing.T) { + input := []any{"part1", "part2"} + got := extractMessageContent(input) + if got != "part1\npart2" { + t.Fatalf("expected joined, got %q", got) + } +} + +func TestExtractMessageContentOther(t *testing.T) { + got := extractMessageContent(42) + if got != "42" { + t.Fatalf("expected '42', got %q", got) + } +} + +func TestExtractMessageContentNil(t *testing.T) { + got := extractMessageContent(nil) + if got != "" { + t.Fatalf("expected '', got %q", got) + } +} + +// ─── cloneMap ──────────────────────────────────────────────────────── + +func TestCloneMapBasic(t *testing.T) { + original := map[string]any{"a": 1, "b": "hello"} + clone := cloneMap(original) + original["a"] = 999 + if clone["a"] != 1 { + t.Fatalf("expected 1, got %v", clone["a"]) + } + if clone["b"] != "hello" { + t.Fatalf("expected 'hello', got %v", clone["b"]) + } +} + +func TestCloneMapEmpty(t *testing.T) { + clone := cloneMap(map[string]any{}) + if len(clone) != 0 { + t.Fatalf("expected empty, got %v", clone) + } +} + +func TestCloneMapNested(t *testing.T) { + // cloneMap is shallow, so nested maps share references + inner := map[string]any{"key": "value"} + original := map[string]any{"nested": inner} + clone := cloneMap(original) + // Shallow clone means inner is shared + inner["key"] = "modified" + cloneNested := clone["nested"].(map[string]any) + if cloneNested["key"] != "modified" { + t.Fatal("expected shallow clone to share nested references") + } +} + +// helper +func containsStr(s, sub string) bool { + return len(s) >= len(sub) && (s == sub || len(s) > 0 && findSubstring(s, sub)) +} + +func findSubstring(s, sub string) bool { + for i := 0; i <= len(s)-len(sub); i++ { + if s[i:i+len(sub)] == sub { + return true + } + } + return false +} diff --git a/internal/httpapi/claude/handler_utils.go b/internal/httpapi/claude/handler_utils.go new file mode 100644 index 0000000000000000000000000000000000000000..e3537b4cc92a67487e5c4212c9199082b9c5404b --- /dev/null +++ b/internal/httpapi/claude/handler_utils.go @@ -0,0 +1,292 @@ +package claude + +import ( + "ds2api/internal/toolcall" + "encoding/json" + "fmt" + "strings" + + "ds2api/internal/prompt" +) + +func normalizeClaudeMessages(messages []any) []any { + out := make([]any, 0, len(messages)) + state := &claudeToolCallState{ + nameByID: map[string]string{}, + lastIDByName: map[string]string{}, + callIDSequence: 0, + } + for _, m := range messages { + msg, ok := m.(map[string]any) + if !ok { + continue + } + role := strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", msg["role"]))) + switch content := msg["content"].(type) { + case []any: + textParts := make([]string, 0, len(content)) + pendingThinking := "" + flushText := func() { + if len(textParts) == 0 { + return + } + message := map[string]any{ + "role": role, + "content": strings.Join(textParts, "\n"), + } + if role == "assistant" && strings.TrimSpace(pendingThinking) != "" { + message["reasoning_content"] = pendingThinking + message["content"] = prependClaudeReasoningForPrompt(pendingThinking, safeStringValue(message["content"])) + pendingThinking = "" + } + out = append(out, message) + textParts = textParts[:0] + } + for _, block := range content { + b, ok := block.(map[string]any) + if !ok { + continue + } + typeStr := strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", b["type"]))) + switch typeStr { + case "text": + if t, ok := b["text"].(string); ok { + textParts = append(textParts, t) + } + case "thinking": + if role == "assistant" { + if thinking := extractClaudeThinkingBlockText(b); thinking != "" { + if pendingThinking == "" { + pendingThinking = thinking + } else { + pendingThinking += "\n" + thinking + } + } + continue + } + if raw := strings.TrimSpace(formatClaudeUnknownBlockForPrompt(b)); raw != "" { + textParts = append(textParts, raw) + } + case "tool_use": + if role == "assistant" { + flushText() + if toolMsg := normalizeClaudeToolUseToAssistant(b, state); toolMsg != nil { + if strings.TrimSpace(pendingThinking) != "" { + toolMsg["reasoning_content"] = pendingThinking + toolMsg["content"] = prependClaudeReasoningForPrompt(pendingThinking, safeStringValue(toolMsg["content"])) + pendingThinking = "" + } + out = append(out, toolMsg) + } + continue + } + if raw := strings.TrimSpace(formatClaudeUnknownBlockForPrompt(b)); raw != "" { + textParts = append(textParts, raw) + } + case "tool_result": + flushText() + if toolMsg := normalizeClaudeToolResultToToolMessage(b, state); toolMsg != nil { + out = append(out, toolMsg) + } + default: + if raw := strings.TrimSpace(formatClaudeUnknownBlockForPrompt(b)); raw != "" { + textParts = append(textParts, raw) + } + } + } + flushText() + if role == "assistant" && strings.TrimSpace(pendingThinking) != "" { + out = append(out, map[string]any{ + "role": "assistant", + "reasoning_content": pendingThinking, + "content": formatClaudeReasoningForPrompt(pendingThinking), + }) + } + default: + copied := cloneMap(msg) + out = append(out, copied) + } + } + return out +} + +func prependClaudeReasoningForPrompt(reasoning, content string) string { + reasoning = strings.TrimSpace(reasoning) + content = strings.TrimSpace(content) + if reasoning == "" { + return content + } + block := formatClaudeReasoningForPrompt(reasoning) + if content == "" { + return block + } + return block + "\n\n" + content +} + +func formatClaudeReasoningForPrompt(reasoning string) string { + reasoning = strings.TrimSpace(reasoning) + if reasoning == "" { + return "" + } + return "[reasoning_content]\n" + reasoning + "\n[/reasoning_content]" +} + +func extractClaudeThinkingBlockText(block map[string]any) string { + if block == nil { + return "" + } + for _, key := range []string{"thinking", "text", "content"} { + if text := strings.TrimSpace(safeStringValue(block[key])); text != "" { + return text + } + } + return "" +} + +func buildClaudeToolPrompt(tools []any) string { + toolSchemas := make([]string, 0, len(tools)) + names := make([]string, 0, len(tools)) + for _, t := range tools { + m, ok := t.(map[string]any) + if !ok { + continue + } + name, desc, schemaObj := extractClaudeToolMeta(m) + if name == "" { + continue + } + names = append(names, name) + schema, _ := json.Marshal(schemaObj) + toolSchemas = append(toolSchemas, fmt.Sprintf("Tool: %s\nDescription: %s\nParameters: %s", name, desc, schema)) + } + if len(toolSchemas) == 0 { + return "" + } + return "You have access to these tools:\n\n" + + strings.Join(toolSchemas, "\n\n") + "\n\n" + + toolcall.BuildToolCallInstructions(names) +} + +//nolint:unused // retained for compatibility with pending Claude tool-result prompt flow. +func formatClaudeToolResultForPrompt(block map[string]any) string { + if block == nil { + return "" + } + payload := map[string]any{ + "type": "tool_result", + "content": block["content"], + } + if toolCallID := strings.TrimSpace(fmt.Sprintf("%v", block["tool_use_id"])); toolCallID != "" { + payload["tool_call_id"] = toolCallID + } else if toolCallID := strings.TrimSpace(fmt.Sprintf("%v", block["tool_call_id"])); toolCallID != "" { + payload["tool_call_id"] = toolCallID + } + if name := strings.TrimSpace(fmt.Sprintf("%v", block["name"])); name != "" { + payload["name"] = name + } + b, err := json.Marshal(payload) + if err != nil { + return strings.TrimSpace(fmt.Sprintf("%v", payload)) + } + return string(b) +} + +func normalizeClaudeToolUseToAssistant(block map[string]any, state *claudeToolCallState) map[string]any { + if block == nil { + return nil + } + name := strings.TrimSpace(fmt.Sprintf("%v", block["name"])) + if name == "" { + return nil + } + callID := safeStringValue(block["id"]) + if callID == "" { + callID = safeStringValue(block["tool_use_id"]) + } + if callID == "" { + callID = state.nextID() + } + state.nameByID[callID] = name + state.lastIDByName[strings.ToLower(name)] = callID + arguments := block["input"] + if arguments == nil { + arguments = map[string]any{} + } + argsJSON, err := json.Marshal(arguments) + if err != nil || len(argsJSON) == 0 { + argsJSON = []byte("{}") + } + toolCalls := []any{ + map[string]any{ + "id": callID, + "type": "function", + "function": map[string]any{ + "name": name, + "arguments": string(argsJSON), + }, + }, + } + return map[string]any{ + "role": "assistant", + "content": prompt.FormatToolCallsForPrompt(toolCalls), + "tool_calls": toolCalls, + } +} + +func normalizeClaudeToolResultToToolMessage(block map[string]any, state *claudeToolCallState) map[string]any { + if block == nil { + return nil + } + name := safeStringValue(block["name"]) + toolCallID := safeStringValue(block["tool_use_id"]) + if toolCallID == "" { + toolCallID = safeStringValue(block["tool_call_id"]) + } + if toolCallID == "" { + if name != "" { + toolCallID = strings.TrimSpace(state.lastIDByName[strings.ToLower(name)]) + } + } + if toolCallID == "" { + toolCallID = state.nextID() + } + out := map[string]any{ + "role": "tool", + "tool_call_id": toolCallID, + "content": normalizeClaudeToolResultContent(block["content"]), + } + if name != "" { + out["name"] = name + state.nameByID[toolCallID] = name + state.lastIDByName[strings.ToLower(name)] = toolCallID + } else if inferred := strings.TrimSpace(state.nameByID[toolCallID]); inferred != "" { + out["name"] = inferred + } + return out +} + +func normalizeClaudeToolResultContent(content any) any { + if text, ok := content.(string); ok { + return text + } + payload := map[string]any{ + "type": "tool_result", + "content": content, + } + b, err := json.Marshal(sanitizeClaudeBlockForPrompt(payload)) + if err != nil { + return strings.TrimSpace(fmt.Sprintf("%v", content)) + } + return string(b) +} + +func formatClaudeBlockRaw(block map[string]any) string { + if block == nil { + return "" + } + b, err := json.Marshal(block) + if err != nil { + return strings.TrimSpace(fmt.Sprintf("%v", block)) + } + return string(b) +} diff --git a/internal/httpapi/claude/handler_utils_sanitize.go b/internal/httpapi/claude/handler_utils_sanitize.go new file mode 100644 index 0000000000000000000000000000000000000000..95f20655f676ca7bf4fc7c6830b79c43698a4006 --- /dev/null +++ b/internal/httpapi/claude/handler_utils_sanitize.go @@ -0,0 +1,106 @@ +package claude + +import ( + "encoding/json" + "fmt" + "strings" +) + +const ( + maxClaudeRawPromptChars = 1024 + omittedBinaryMarker = "[omitted_binary_payload]" +) + +func formatClaudeUnknownBlockForPrompt(block map[string]any) string { + if block == nil { + return "" + } + safe := sanitizeClaudeBlockForPrompt(block) + raw := strings.TrimSpace(formatClaudeBlockRaw(safe)) + if raw == "" { + return "" + } + if len(raw) > maxClaudeRawPromptChars { + return raw[:maxClaudeRawPromptChars] + "...(truncated)" + } + return raw +} + +func sanitizeClaudeBlockForPrompt(block map[string]any) map[string]any { + out := cloneMap(block) + for k, v := range out { + if looksLikeBinaryFieldName(k) { + out[k] = omittedBinaryMarker + continue + } + switch inner := v.(type) { + case map[string]any: + out[k] = sanitizeClaudeBlockForPrompt(inner) + case []any: + out[k] = sanitizeClaudeArrayForPrompt(inner) + case string: + out[k] = sanitizeClaudeStringForPrompt(k, inner) + } + } + return out +} + +func sanitizeClaudeArrayForPrompt(items []any) []any { + out := make([]any, 0, len(items)) + for _, item := range items { + switch v := item.(type) { + case map[string]any: + out = append(out, sanitizeClaudeBlockForPrompt(v)) + case []any: + out = append(out, sanitizeClaudeArrayForPrompt(v)) + default: + out = append(out, v) + } + } + return out +} + +func sanitizeClaudeStringForPrompt(key, value string) string { + trimmed := strings.TrimSpace(value) + if trimmed == "" { + return "" + } + if looksLikeBinaryFieldName(key) || looksLikeBase64Payload(trimmed) { + return omittedBinaryMarker + } + if len(trimmed) > maxClaudeRawPromptChars { + return trimmed[:maxClaudeRawPromptChars] + "...(truncated)" + } + return trimmed +} + +func looksLikeBinaryFieldName(name string) bool { + n := strings.ToLower(strings.TrimSpace(name)) + return n == "data" || n == "bytes" || n == "base64" || n == "inline_data" || n == "inlinedata" +} + +func looksLikeBase64Payload(v string) bool { + if len(v) < 512 { + return false + } + compact := strings.TrimRight(v, "=") + if compact == "" { + return false + } + for _, ch := range compact { + if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '+' || ch == '/' || ch == '-' || ch == '_' { + continue + } + return false + } + return true +} + +//nolint:unused // helper kept for compatibility with upcoming sanitize pipeline. +func marshalCompactJSON(v any) string { + b, err := json.Marshal(v) + if err != nil { + return strings.TrimSpace(fmt.Sprintf("%v", v)) + } + return string(b) +} diff --git a/internal/httpapi/claude/output_clean.go b/internal/httpapi/claude/output_clean.go new file mode 100644 index 0000000000000000000000000000000000000000..60217f2b46b48ed79f3c8a20b9bf80cced2fceea --- /dev/null +++ b/internal/httpapi/claude/output_clean.go @@ -0,0 +1,13 @@ +package claude + +import textclean "ds2api/internal/textclean" + +func cleanVisibleOutput(text string, stripReferenceMarkers bool) string { + if text == "" { + return text + } + if stripReferenceMarkers { + text = textclean.StripReferenceMarkers(text) + } + return text +} diff --git a/internal/httpapi/claude/prompt_token_text.go b/internal/httpapi/claude/prompt_token_text.go new file mode 100644 index 0000000000000000000000000000000000000000..f70641cb4c7eb909684e62198f73c40cc5c5bee0 --- /dev/null +++ b/internal/httpapi/claude/prompt_token_text.go @@ -0,0 +1,7 @@ +package claude + +import "ds2api/internal/prompt" + +func buildClaudePromptTokenText(messages []any, thinkingEnabled bool) string { + return prompt.MessagesPrepareWithThinking(toMessageMaps(messages), thinkingEnabled) +} diff --git a/internal/httpapi/claude/proxy_vercel_test.go b/internal/httpapi/claude/proxy_vercel_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3a879df4061aac6eb8686f2ab9b43f00e24bcfbd --- /dev/null +++ b/internal/httpapi/claude/proxy_vercel_test.go @@ -0,0 +1,252 @@ +package claude + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +type claudeProxyStoreStub struct { + aliases map[string]string +} + +func (s claudeProxyStoreStub) ModelAliases() map[string]string { return s.aliases } + +func (claudeProxyStoreStub) CurrentInputFileEnabled() bool { return true } +func (claudeProxyStoreStub) CurrentInputFileMinChars() int { return 0 } + +type openAIProxyStub struct { + status int + body string +} + +func TestClaudeProxyViaOpenAIPrefersGlobalAliasMapping(t *testing.T) { + openAI := &openAIProxyCaptureStub{} + h := &Handler{ + Store: claudeProxyStoreStub{ + aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}, + }, + OpenAI: openAI, + } + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hi"}],"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + if got := strings.TrimSpace(openAI.seenModel); got != "deepseek-v4-flash" { + t.Fatalf("expected global alias mapped proxy model deepseek-v4-flash, got %q", got) + } +} + +func (s openAIProxyStub) ChatCompletions(w http.ResponseWriter, _ *http.Request) { + if s.status == 0 { + s.status = http.StatusOK + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(s.status) + _, _ = w.Write([]byte(s.body)) +} + +type openAIProxyCaptureStub struct { + seenModel string + seenReq map[string]any +} + +func (s *openAIProxyCaptureStub) ChatCompletions(w http.ResponseWriter, r *http.Request) { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + s.seenReq = req + if m, ok := req["model"].(string); ok { + s.seenModel = m + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"id":"ok","choices":[{"message":{"role":"assistant","content":"ok"}}]}`)) +} + +func TestClaudeProxyViaOpenAIVercelPreparePassthrough(t *testing.T) { + h := &Handler{OpenAI: openAIProxyStub{status: 200, body: `{"lease_id":"lease_123","payload":{"a":1}}`}} + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages?__stream_prepare=1", strings.NewReader(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":true}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("expected json response, got err=%v body=%s", err, rec.Body.String()) + } + if _, ok := out["lease_id"]; !ok { + t.Fatalf("expected lease_id in prepare passthrough, got=%v", out) + } +} + +func TestClaudeProxyViaOpenAIUsesGlobalAliasMapping(t *testing.T) { + openAI := &openAIProxyCaptureStub{} + h := &Handler{ + Store: claudeProxyStoreStub{aliases: map[string]string{"claude-3-opus": "deepseek-v4-pro"}}, + OpenAI: openAI, + } + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-3-opus","messages":[{"role":"user","content":"hi"}],"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + if got := strings.TrimSpace(openAI.seenModel); got != "deepseek-v4-pro" { + t.Fatalf("expected mapped proxy model deepseek-v4-pro, got %q", got) + } +} + +func TestClaudeProxyViaOpenAIPreservesThinkingOverride(t *testing.T) { + openAI := &openAIProxyCaptureStub{} + h := &Handler{ + Store: claudeProxyStoreStub{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}}, + OpenAI: openAI, + } + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hi"}],"thinking":{"type":"disabled"},"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + thinking, _ := openAI.seenReq["thinking"].(map[string]any) + if thinking["type"] != "disabled" { + t.Fatalf("expected translated OpenAI request to preserve disabled thinking, got %#v", openAI.seenReq) + } +} + +func TestClaudeProxyViaOpenAIEnablesThinkingInternallyByDefaultForNonStream(t *testing.T) { + openAI := &openAIProxyCaptureStub{} + h := &Handler{ + Store: claudeProxyStoreStub{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}}, + OpenAI: openAI, + } + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hi"}],"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + thinking, _ := openAI.seenReq["thinking"].(map[string]any) + if thinking["type"] != "enabled" { + t.Fatalf("expected Claude non-stream default to enable downstream thinking internally, got %#v", openAI.seenReq) + } +} + +func TestClaudeProxyViaOpenAIEnablesThinkingWhenRequested(t *testing.T) { + openAI := &openAIProxyCaptureStub{} + h := &Handler{ + Store: claudeProxyStoreStub{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}}, + OpenAI: openAI, + } + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hi"}],"thinking":{"type":"enabled","budget_tokens":1024},"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + thinking, _ := openAI.seenReq["thinking"].(map[string]any) + if thinking["type"] != "enabled" { + t.Fatalf("expected Claude explicit thinking to enable downstream thinking, got %#v", openAI.seenReq) + } +} + +func TestClaudeProxyViaOpenAIEnablesStreamThinkingByDefault(t *testing.T) { + openAI := &openAIProxyCaptureStub{} + h := &Handler{ + Store: claudeProxyStoreStub{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}}, + OpenAI: openAI, + } + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hi"}],"stream":true}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + thinking, _ := openAI.seenReq["thinking"].(map[string]any) + if thinking["type"] != "enabled" { + t.Fatalf("expected Claude stream default to enable downstream thinking, got %#v", openAI.seenReq) + } +} + +func TestClaudeProxyViaOpenAIExposesThinkingBlocksByDefault(t *testing.T) { + body := `{"id":"chatcmpl_1","object":"chat.completion","created":1,"model":"claude-sonnet-4-5","choices":[{"index":0,"message":{"role":"assistant","content":null,"reasoning_content":"internal reasoning","tool_calls":[{"id":"call_1","type":"function","function":{"name":"search","arguments":"{\"q\":\"x\"}"}}]},"finish_reason":"tool_calls"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}` + h := &Handler{OpenAI: openAIProxyStub{status: 200, body: body}} + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + got := rec.Body.String() + if !strings.Contains(got, `"type":"thinking"`) { + t.Fatalf("expected converted Claude response to expose thinking block, got %s", got) + } + if !strings.Contains(got, `"tool_use"`) { + t.Fatalf("expected converted Claude response to preserve tool_use, got %s", got) + } +} + +func TestClaudeProxyViaOpenAIStripsThinkingBlocksWhenDisabled(t *testing.T) { + body := `{"id":"chatcmpl_1","object":"chat.completion","created":1,"model":"claude-sonnet-4-5","choices":[{"index":0,"message":{"role":"assistant","content":"ok","reasoning_content":"internal reasoning"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}` + h := &Handler{OpenAI: openAIProxyStub{status: 200, body: body}} + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"thinking":{"type":"disabled"},"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + got := rec.Body.String() + if strings.Contains(got, `"type":"thinking"`) { + t.Fatalf("expected disabled thinking to strip thinking block, got %s", got) + } +} + +func TestClaudeProxyTranslatesInlineImageToOpenAIDataURL(t *testing.T) { + openAI := &openAIProxyCaptureStub{} + h := &Handler{OpenAI: openAI} + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":[{"type":"text","text":"hello"},{"type":"image","source":{"type":"base64","media_type":"image/png","data":"QUJDRA=="}}]}],"stream":false}`)) + rec := httptest.NewRecorder() + + h.Messages(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + messages, _ := openAI.seenReq["messages"].([]any) + if len(messages) != 1 { + t.Fatalf("expected one translated message, got %#v", openAI.seenReq) + } + msg, _ := messages[0].(map[string]any) + content, _ := msg["content"].([]any) + if len(content) != 2 { + t.Fatalf("expected translated content blocks, got %#v", msg) + } + imageBlock, _ := content[1].(map[string]any) + if strings.TrimSpace(asString(imageBlock["type"])) != "image_url" { + t.Fatalf("expected image_url block, got %#v", imageBlock) + } + imageURL, _ := imageBlock["image_url"].(map[string]any) + if !strings.HasPrefix(strings.TrimSpace(asString(imageURL["url"])), "data:image/png;base64,") { + t.Fatalf("expected translated data url, got %#v", imageBlock) + } +} diff --git a/internal/httpapi/claude/route_alias_test.go b/internal/httpapi/claude/route_alias_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f01e5e3d80f8e199710e2e4f60d3aab425f4f8a5 --- /dev/null +++ b/internal/httpapi/claude/route_alias_test.go @@ -0,0 +1,44 @@ +package claude + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" +) + +type routeAliasAuthStub struct{} + +func (routeAliasAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return nil, auth.ErrUnauthorized +} + +func (routeAliasAuthStub) Release(_ *auth.RequestAuth) {} + +func TestClaudeRouteAliasesDoNot404(t *testing.T) { + h := &Handler{ + Auth: routeAliasAuthStub{}, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + + paths := []string{ + "/anthropic/v1/messages", + "/v1/messages", + "/messages", + "/anthropic/v1/messages/count_tokens", + "/v1/messages/count_tokens", + "/messages/count_tokens", + } + for _, path := range paths { + req := httptest.NewRequest(http.MethodPost, path, nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code == http.StatusNotFound { + t.Fatalf("expected route %s to be registered, got 404", path) + } + } +} diff --git a/internal/httpapi/claude/standard_request.go b/internal/httpapi/claude/standard_request.go new file mode 100644 index 0000000000000000000000000000000000000000..4998eb9492b24a9b91d80862b715232863ac7f96 --- /dev/null +++ b/internal/httpapi/claude/standard_request.go @@ -0,0 +1,122 @@ +package claude + +import ( + "fmt" + "strings" + + "ds2api/internal/config" + "ds2api/internal/prompt" + "ds2api/internal/promptcompat" + "ds2api/internal/util" +) + +type claudeNormalizedRequest struct { + Standard promptcompat.StandardRequest + NormalizedMessages []any +} + +func normalizeClaudeRequest(store ConfigReader, req map[string]any) (claudeNormalizedRequest, error) { + model, _ := req["model"].(string) + messagesRaw, _ := req["messages"].([]any) + if strings.TrimSpace(model) == "" || len(messagesRaw) == 0 { + return claudeNormalizedRequest{}, fmt.Errorf("request must include 'model' and 'messages'") + } + if _, ok := req["max_tokens"]; !ok { + req["max_tokens"] = 8192 + } + normalizedMessages := normalizeClaudeMessages(messagesRaw) + payload := cloneMap(req) + payload["messages"] = normalizedMessages + toolsRequested, _ := req["tools"].([]any) + payload["messages"] = injectClaudeToolPrompt(payload, normalizedMessages, toolsRequested) + + dsPayload := convertClaudeToDeepSeek(payload, store) + dsModel, _ := dsPayload["model"].(string) + defaultThinkingEnabled, searchEnabled, ok := config.GetModelConfig(dsModel) + if !ok { + searchEnabled = false + } + thinkingEnabled := util.ResolveThinkingEnabled(req, defaultThinkingEnabled) + if config.IsNoThinkingModel(dsModel) { + thinkingEnabled = false + } + finalPrompt := prompt.MessagesPrepareWithThinking(toMessageMaps(dsPayload["messages"]), thinkingEnabled) + toolNames := extractClaudeToolNames(toolsRequested) + if len(toolNames) == 0 && len(toolsRequested) > 0 { + toolNames = []string{"__any_tool__"} + } + + return claudeNormalizedRequest{ + Standard: promptcompat.StandardRequest{ + Surface: "anthropic_messages", + RequestedModel: strings.TrimSpace(model), + ResolvedModel: dsModel, + ResponseModel: strings.TrimSpace(model), + Messages: normalizedMessages, + PromptTokenText: finalPrompt, + ToolsRaw: toolsRequested, + FinalPrompt: finalPrompt, + ToolNames: toolNames, + Stream: util.ToBool(req["stream"]), + Thinking: thinkingEnabled, + Search: searchEnabled, + }, + NormalizedMessages: normalizedMessages, + }, nil +} + +func injectClaudeToolPrompt(payload map[string]any, normalizedMessages []any, tools []any) []any { + if len(tools) == 0 { + return normalizedMessages + } + toolPrompt := strings.TrimSpace(buildClaudeToolPrompt(tools)) + if toolPrompt == "" { + return normalizedMessages + } + + // Prefer top-level Anthropic-style system prompt when available. + if systemText, ok := payload["system"].(string); ok && strings.TrimSpace(systemText) != "" { + payload["system"] = mergeSystemPrompt(systemText, toolPrompt) + return normalizedMessages + } + + messages := cloneAnySlice(normalizedMessages) + for i := range messages { + msg, ok := messages[i].(map[string]any) + if !ok { + continue + } + role, _ := msg["role"].(string) + if !strings.EqualFold(strings.TrimSpace(role), "system") { + continue + } + copied := cloneMap(msg) + copied["content"] = mergeSystemPrompt(strings.TrimSpace(fmt.Sprintf("%v", copied["content"])), toolPrompt) + messages[i] = copied + return messages + } + + return append([]any{map[string]any{"role": "system", "content": toolPrompt}}, messages...) +} + +func mergeSystemPrompt(base, extra string) string { + base = strings.TrimSpace(base) + extra = strings.TrimSpace(extra) + switch { + case base == "": + return extra + case extra == "": + return base + default: + return base + "\n\n" + extra + } +} + +func cloneAnySlice(in []any) []any { + if len(in) == 0 { + return nil + } + out := make([]any, len(in)) + copy(out, in) + return out +} diff --git a/internal/httpapi/claude/standard_request_test.go b/internal/httpapi/claude/standard_request_test.go new file mode 100644 index 0000000000000000000000000000000000000000..244b2ac7831110499a06574febc4e61118c26a27 --- /dev/null +++ b/internal/httpapi/claude/standard_request_test.go @@ -0,0 +1,120 @@ +package claude + +import ( + "testing" + + "ds2api/internal/config" +) + +func TestNormalizeClaudeRequest(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{}`) + store := config.LoadStore() + req := map[string]any{ + "model": "claude-opus-4-6", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + "stream": true, + "tools": []any{ + map[string]any{"name": "search", "description": "Search"}, + }, + } + norm, err := normalizeClaudeRequest(store, req) + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + if norm.Standard.ResolvedModel == "" { + t.Fatalf("expected resolved model") + } + if !norm.Standard.Stream { + t.Fatalf("expected stream=true") + } + if len(norm.Standard.ToolNames) == 0 { + t.Fatalf("expected tool names") + } + if norm.Standard.ToolsRaw == nil { + t.Fatalf("expected ToolsRaw preserved for downstream normalization") + } + if norm.Standard.FinalPrompt == "" { + t.Fatalf("expected non-empty final prompt") + } +} + +func TestNormalizeClaudeRequestSupportsCamelCaseInputSchemaPromptInjection(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{}`) + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + "tools": []any{ + map[string]any{ + "name": "todowrite", + "description": "Write todos", + "inputSchema": map[string]any{"type": "object", "properties": map[string]any{"todos": map[string]any{"type": "array"}}}, + }, + }, + } + norm, err := normalizeClaudeRequest(store, req) + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + if !containsStr(norm.Standard.FinalPrompt, `"type":"array"`) { + t.Fatalf("expected inputSchema to be injected into prompt, got=%q", norm.Standard.FinalPrompt) + } +} + +func TestNormalizeClaudeRequestInjectsToolsIntoExistingSystemMessage(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{}`) + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []any{ + map[string]any{"role": "system", "content": "baseline rule"}, + map[string]any{"role": "user", "content": "hello"}, + }, + "tools": []any{ + map[string]any{"name": "search", "description": "Search"}, + }, + } + + norm, err := normalizeClaudeRequest(store, req) + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + if !containsStr(norm.Standard.FinalPrompt, "You have access to these tools") { + t.Fatalf("expected tool prompt injected into final prompt, got=%q", norm.Standard.FinalPrompt) + } + if !containsStr(norm.Standard.FinalPrompt, "baseline rule") { + t.Fatalf("expected existing system message preserved, got=%q", norm.Standard.FinalPrompt) + } +} + +func TestNormalizeClaudeRequestInjectsToolsIntoTopLevelSystem(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{}`) + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-5", + "system": "top-level system", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + "tools": []any{ + map[string]any{"name": "search", "description": "Search"}, + }, + } + + norm, err := normalizeClaudeRequest(store, req) + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + if !containsStr(norm.Standard.FinalPrompt, "top-level system") { + t.Fatalf("expected top-level system preserved, got=%q", norm.Standard.FinalPrompt) + } + if !containsStr(norm.Standard.FinalPrompt, "You have access to these tools") { + t.Fatalf("expected tool prompt injected, got=%q", norm.Standard.FinalPrompt) + } +} diff --git a/internal/httpapi/claude/stream_runtime_core.go b/internal/httpapi/claude/stream_runtime_core.go new file mode 100644 index 0000000000000000000000000000000000000000..c558601475146d68ad602da95116983ab038d3ac --- /dev/null +++ b/internal/httpapi/claude/stream_runtime_core.go @@ -0,0 +1,250 @@ +package claude + +import ( + "fmt" + "net/http" + "strings" + "time" + + "ds2api/internal/responsehistory" + "ds2api/internal/sse" + streamengine "ds2api/internal/stream" + "ds2api/internal/toolcall" + "ds2api/internal/toolstream" +) + +type claudeStreamRuntime struct { + w http.ResponseWriter + rc *http.ResponseController + canFlush bool + + model string + toolNames []string + messages []any + toolsRaw any + promptTokenText string + + thinkingEnabled bool + searchEnabled bool + bufferToolContent bool + stripReferenceMarkers bool + + messageID string + thinking strings.Builder + text strings.Builder + responseMessageID int + + sieve toolstream.State + rawText strings.Builder + rawThinking strings.Builder + toolDetectionThinking strings.Builder + toolCallsDetected bool + + nextBlockIndex int + thinkingBlockOpen bool + thinkingBlockIndex int + textBlockOpen bool + textBlockIndex int + textEmitted bool + ended bool + upstreamErr string + history *responsehistory.Session +} + +func newClaudeStreamRuntime( + w http.ResponseWriter, + rc *http.ResponseController, + canFlush bool, + model string, + messages []any, + thinkingEnabled bool, + searchEnabled bool, + stripReferenceMarkers bool, + toolNames []string, + toolsRaw any, + promptTokenText string, + history *responsehistory.Session, +) *claudeStreamRuntime { + return &claudeStreamRuntime{ + w: w, + rc: rc, + canFlush: canFlush, + model: model, + messages: messages, + thinkingEnabled: thinkingEnabled, + searchEnabled: searchEnabled, + bufferToolContent: len(toolNames) > 0, + stripReferenceMarkers: stripReferenceMarkers, + toolNames: toolNames, + toolsRaw: toolsRaw, + promptTokenText: promptTokenText, + history: history, + messageID: fmt.Sprintf("msg_%d", time.Now().UnixNano()), + thinkingBlockIndex: -1, + textBlockIndex: -1, + } +} + +func (s *claudeStreamRuntime) onParsed(parsed sse.LineResult) streamengine.ParsedDecision { + if !parsed.Parsed { + return streamengine.ParsedDecision{} + } + if parsed.ErrorMessage != "" { + s.upstreamErr = parsed.ErrorMessage + return streamengine.ParsedDecision{Stop: true, StopReason: streamengine.StopReason("upstream_error")} + } + if parsed.ResponseMessageID > 0 { + s.responseMessageID = parsed.ResponseMessageID + } + if parsed.Stop { + return streamengine.ParsedDecision{Stop: true} + } + + contentSeen := false + for _, p := range parsed.ToolDetectionThinkingParts { + trimmed := sse.TrimContinuationOverlapFromBuilder(&s.toolDetectionThinking, p.Text) + if trimmed != "" { + s.toolDetectionThinking.WriteString(trimmed) + } + } + for _, p := range parsed.Parts { + var rawTrimmed string + if p.Type == "thinking" { + rawTrimmed = sse.TrimContinuationOverlapFromBuilder(&s.rawThinking, p.Text) + } else { + rawTrimmed = sse.TrimContinuationOverlapFromBuilder(&s.rawText, p.Text) + } + if rawTrimmed == "" { + continue + } + if p.Type == "thinking" { + s.rawThinking.WriteString(rawTrimmed) + } else { + s.rawText.WriteString(rawTrimmed) + } + cleanedText := cleanVisibleOutput(rawTrimmed, s.stripReferenceMarkers) + if cleanedText == "" { + continue + } + if p.Type != "thinking" && s.searchEnabled && sse.IsCitation(cleanedText) { + continue + } + contentSeen = true + + if p.Type == "thinking" { + if !s.thinkingEnabled { + continue + } + trimmed := sse.TrimContinuationOverlapFromBuilder(&s.thinking, cleanedText) + if trimmed == "" { + continue + } + s.thinking.WriteString(trimmed) + s.closeTextBlock() + if !s.thinkingBlockOpen { + s.thinkingBlockIndex = s.nextBlockIndex + s.nextBlockIndex++ + s.send("content_block_start", map[string]any{ + "type": "content_block_start", + "index": s.thinkingBlockIndex, + "content_block": map[string]any{ + "type": "thinking", + "thinking": "", + }, + }) + s.thinkingBlockOpen = true + } + s.send("content_block_delta", map[string]any{ + "type": "content_block_delta", + "index": s.thinkingBlockIndex, + "delta": map[string]any{ + "type": "thinking_delta", + "thinking": trimmed, + }, + }) + continue + } + + s.text.WriteString(cleanedText) + + if !s.bufferToolContent { + s.closeThinkingBlock() + if !s.textBlockOpen { + s.textBlockIndex = s.nextBlockIndex + s.nextBlockIndex++ + s.send("content_block_start", map[string]any{ + "type": "content_block_start", + "index": s.textBlockIndex, + "content_block": map[string]any{ + "type": "text", + "text": "", + }, + }) + s.textBlockOpen = true + } + s.send("content_block_delta", map[string]any{ + "type": "content_block_delta", + "index": s.textBlockIndex, + "delta": map[string]any{ + "type": "text_delta", + "text": cleanedText, + }, + }) + s.textEmitted = true + continue + } + + events := toolstream.ProcessChunk(&s.sieve, rawTrimmed, s.toolNames) + for _, evt := range events { + if len(evt.ToolCalls) > 0 { + s.closeTextBlock() + s.toolCallsDetected = true + normalized := toolcall.NormalizeParsedToolCallsForSchemas(evt.ToolCalls, s.toolsRaw) + for _, tc := range normalized { + idx := s.nextBlockIndex + s.nextBlockIndex++ + s.sendToolUseBlock(idx, tc) + } + continue + } + if evt.Content == "" { + continue + } + cleaned := cleanVisibleOutput(evt.Content, s.stripReferenceMarkers) + if cleaned == "" || (s.searchEnabled && sse.IsCitation(cleaned)) { + continue + } + s.closeThinkingBlock() + if !s.textBlockOpen { + s.textBlockIndex = s.nextBlockIndex + s.nextBlockIndex++ + s.send("content_block_start", map[string]any{ + "type": "content_block_start", + "index": s.textBlockIndex, + "content_block": map[string]any{ + "type": "text", + "text": "", + }, + }) + s.textBlockOpen = true + } + s.send("content_block_delta", map[string]any{ + "type": "content_block_delta", + "index": s.textBlockIndex, + "delta": map[string]any{ + "type": "text_delta", + "text": cleaned, + }, + }) + s.textEmitted = true + } + } + + if s.history != nil { + s.history.Progress( + responsehistory.ThinkingForArchive(s.rawThinking.String(), s.toolDetectionThinking.String(), s.thinking.String()), + responsehistory.TextForArchive(s.rawText.String(), s.text.String()), + ) + } + return streamengine.ParsedDecision{ContentSeen: contentSeen} +} diff --git a/internal/httpapi/claude/stream_runtime_emit.go b/internal/httpapi/claude/stream_runtime_emit.go new file mode 100644 index 0000000000000000000000000000000000000000..7425a55ea4719bd50f11f99f2a5b72075d02cf5c --- /dev/null +++ b/internal/httpapi/claude/stream_runtime_emit.go @@ -0,0 +1,73 @@ +package claude + +import ( + "encoding/json" + "fmt" + "strings" + + "ds2api/internal/util" +) + +func (s *claudeStreamRuntime) send(event string, v any) { + b, _ := json.Marshal(v) + _, _ = s.w.Write([]byte("event: ")) + _, _ = s.w.Write([]byte(event)) + _, _ = s.w.Write([]byte("\n")) + _, _ = s.w.Write([]byte("data: ")) + _, _ = s.w.Write(b) + _, _ = s.w.Write([]byte("\n\n")) + if s.canFlush { + _ = s.rc.Flush() + } +} + +func (s *claudeStreamRuntime) sendError(message string) { + s.sendErrorWithCode(500, message, "internal_error") +} + +func (s *claudeStreamRuntime) sendErrorWithCode(status int, message, code string) { + msg := strings.TrimSpace(message) + if msg == "" { + msg = "upstream stream error" + } + if code == "" { + code = "internal_error" + } + errType := "api_error" + if status == 429 { + errType = "rate_limit_error" + } + s.send("error", map[string]any{ + "type": "error", + "error": map[string]any{ + "type": errType, + "message": msg, + "code": code, + "param": nil, + }, + }) +} + +func (s *claudeStreamRuntime) sendPing() { + s.send("ping", map[string]any{"type": "ping"}) +} + +func (s *claudeStreamRuntime) sendMessageStart() { + inputTokens := countClaudeInputTokensFromText(s.promptTokenText, s.model) + if inputTokens == 0 { + inputTokens = util.CountPromptTokens(fmt.Sprintf("%v", s.messages), s.model) + } + s.send("message_start", map[string]any{ + "type": "message_start", + "message": map[string]any{ + "id": s.messageID, + "type": "message", + "role": "assistant", + "model": s.model, + "content": []any{}, + "stop_reason": nil, + "stop_sequence": nil, + "usage": map[string]any{"input_tokens": inputTokens, "output_tokens": 0}, + }, + }) +} diff --git a/internal/httpapi/claude/stream_runtime_finalize.go b/internal/httpapi/claude/stream_runtime_finalize.go new file mode 100644 index 0000000000000000000000000000000000000000..07be629a8c1a7fd0e35e69c8b7b336e35303ad1c --- /dev/null +++ b/internal/httpapi/claude/stream_runtime_finalize.go @@ -0,0 +1,233 @@ +package claude + +import ( + "ds2api/internal/assistantturn" + "ds2api/internal/responsehistory" + "ds2api/internal/sse" + "ds2api/internal/toolcall" + "ds2api/internal/toolstream" + "encoding/json" + "fmt" + "time" + + streamengine "ds2api/internal/stream" +) + +func (s *claudeStreamRuntime) closeThinkingBlock() { + if !s.thinkingBlockOpen { + return + } + s.send("content_block_stop", map[string]any{ + "type": "content_block_stop", + "index": s.thinkingBlockIndex, + }) + s.thinkingBlockOpen = false + s.thinkingBlockIndex = -1 +} + +func (s *claudeStreamRuntime) closeTextBlock() { + if !s.textBlockOpen { + return + } + s.send("content_block_stop", map[string]any{ + "type": "content_block_stop", + "index": s.textBlockIndex, + }) + s.textBlockOpen = false + s.textBlockIndex = -1 +} + +func (s *claudeStreamRuntime) sendToolUseBlock(idx int, tc toolcall.ParsedToolCall) { + s.send("content_block_start", map[string]any{ + "type": "content_block_start", + "index": idx, + "content_block": map[string]any{ + "type": "tool_use", + "id": fmt.Sprintf("toolu_%d_%d", time.Now().Unix(), idx), + "name": tc.Name, + "input": map[string]any{}, + }, + }) + inputBytes, _ := json.Marshal(tc.Input) + s.send("content_block_delta", map[string]any{ + "type": "content_block_delta", + "index": idx, + "delta": map[string]any{ + "type": "input_json_delta", + "partial_json": string(inputBytes), + }, + }) + s.send("content_block_stop", map[string]any{ + "type": "content_block_stop", + "index": idx, + }) +} + +func (s *claudeStreamRuntime) finalize(stopReason string, deferEmptyOutput bool) bool { + if s.ended { + return true + } + + if s.bufferToolContent { + for _, evt := range toolstream.Flush(&s.sieve, s.toolNames) { + if len(evt.ToolCalls) > 0 { + s.closeTextBlock() + s.toolCallsDetected = true + normalized := toolcall.NormalizeParsedToolCallsForSchemas(evt.ToolCalls, s.toolsRaw) + for _, tc := range normalized { + idx := s.nextBlockIndex + s.nextBlockIndex++ + s.sendToolUseBlock(idx, tc) + } + continue + } + if evt.Content != "" { + cleaned := cleanVisibleOutput(evt.Content, s.stripReferenceMarkers) + if cleaned == "" || (s.searchEnabled && sse.IsCitation(cleaned)) { + continue + } + if !s.textBlockOpen { + s.textBlockIndex = s.nextBlockIndex + s.nextBlockIndex++ + s.send("content_block_start", map[string]any{ + "type": "content_block_start", + "index": s.textBlockIndex, + "content_block": map[string]any{ + "type": "text", + "text": "", + }, + }) + s.textBlockOpen = true + } + s.send("content_block_delta", map[string]any{ + "type": "content_block_delta", + "index": s.textBlockIndex, + "delta": map[string]any{ + "type": "text_delta", + "text": cleaned, + }, + }) + s.textEmitted = true + } + } + } + + s.closeTextBlock() + + turn := assistantturn.BuildTurnFromStreamSnapshot(assistantturn.StreamSnapshot{ + RawText: s.rawText.String(), + VisibleText: s.text.String(), + RawThinking: s.rawThinking.String(), + VisibleThinking: s.thinking.String(), + DetectionThinking: s.toolDetectionThinking.String(), + ResponseMessageID: s.responseMessageID, + AlreadyEmittedCalls: s.toolCallsDetected, + AlreadyEmittedToolRaw: s.toolCallsDetected, + }, assistantturn.BuildOptions{ + Model: s.model, + Prompt: s.promptTokenText, + SearchEnabled: s.searchEnabled, + StripReferenceMarkers: s.stripReferenceMarkers, + ToolNames: s.toolNames, + ToolsRaw: s.toolsRaw, + }) + finalText := turn.Text + outcome := assistantturn.FinalizeTurn(turn, assistantturn.FinalizeOptions{ + AlreadyEmittedToolCalls: s.toolCallsDetected, + }) + if outcome.ShouldFail { + if deferEmptyOutput { + return false + } + s.ended = true + s.closeThinkingBlock() + s.closeTextBlock() + if s.history != nil { + s.history.Error(outcome.Error.Status, outcome.Error.Message, outcome.Error.Code, responsehistory.ThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), responsehistory.TextForArchive(turn.RawText, turn.Text)) + } + s.sendErrorWithCode(outcome.Error.Status, outcome.Error.Message, outcome.Error.Code) + return true + } + + s.ended = true + s.closeThinkingBlock() + + if s.bufferToolContent && !s.toolCallsDetected { + if len(turn.ToolCalls) > 0 { + stopReason = "tool_use" + for _, tc := range turn.ToolCalls { + idx := s.nextBlockIndex + s.nextBlockIndex++ + s.sendToolUseBlock(idx, tc) + } + } else if finalText != "" && !s.textEmitted { + idx := s.nextBlockIndex + s.nextBlockIndex++ + s.send("content_block_start", map[string]any{ + "type": "content_block_start", + "index": idx, + "content_block": map[string]any{ + "type": "text", + "text": "", + }, + }) + s.send("content_block_delta", map[string]any{ + "type": "content_block_delta", + "index": idx, + "delta": map[string]any{ + "type": "text_delta", + "text": finalText, + }, + }) + s.textEmitted = true + s.send("content_block_stop", map[string]any{ + "type": "content_block_stop", + "index": idx, + }) + } + } + + if outcome.HasToolCalls { + stopReason = "tool_use" + } + if s.history != nil { + s.history.Success( + 200, + responsehistory.ThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), + responsehistory.TextForArchive(turn.RawText, turn.Text), + stopReason, + responsehistory.GenericUsage(turn), + ) + } + + s.send("message_delta", map[string]any{ + "type": "message_delta", + "delta": map[string]any{ + "stop_reason": stopReason, + "stop_sequence": nil, + }, + "usage": map[string]any{ + "output_tokens": outcome.Usage.OutputTokens, + }, + }) + s.send("message_stop", map[string]any{"type": "message_stop"}) + return true +} + +func (s *claudeStreamRuntime) onFinalize(reason streamengine.StopReason, scannerErr error) { + if string(reason) == "upstream_error" { + if s.history != nil { + s.history.Error(500, s.upstreamErr, "upstream_error", responsehistory.ThinkingForArchive(s.rawThinking.String(), s.toolDetectionThinking.String(), s.thinking.String()), responsehistory.TextForArchive(s.rawText.String(), s.text.String())) + } + s.sendError(s.upstreamErr) + return + } + if scannerErr != nil { + if s.history != nil { + s.history.Error(500, scannerErr.Error(), "error", responsehistory.ThinkingForArchive(s.rawThinking.String(), s.toolDetectionThinking.String(), s.thinking.String()), responsehistory.TextForArchive(s.rawText.String(), s.text.String())) + } + s.sendError(scannerErr.Error()) + return + } + s.finalize("end_turn", false) +} diff --git a/internal/httpapi/claude/stream_status_test.go b/internal/httpapi/claude/stream_status_test.go new file mode 100644 index 0000000000000000000000000000000000000000..a2cabe86f413bf43697c66d177ad9affcc2de3b7 --- /dev/null +++ b/internal/httpapi/claude/stream_status_test.go @@ -0,0 +1,64 @@ +package claude + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + chimw "github.com/go-chi/chi/v5/middleware" +) + +type streamStatusClaudeOpenAIStub struct{} + +func (streamStatusClaudeOpenAIStub) ChatCompletions(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl-1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hello\"},\"finish_reason\":null}]}\n\n")) + _, _ = w.Write([]byte("data: [DONE]\n\n")) +} + +type streamStatusClaudeStoreStub struct{} + +func (streamStatusClaudeStoreStub) ModelAliases() map[string]string { return nil } + +func (streamStatusClaudeStoreStub) CurrentInputFileEnabled() bool { return true } +func (streamStatusClaudeStoreStub) CurrentInputFileMinChars() int { return 0 } + +func captureClaudeStatusMiddleware(statuses *[]int) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ww := chimw.NewWrapResponseWriter(w, r.ProtoMajor) + next.ServeHTTP(ww, r) + *statuses = append(*statuses, ww.Status()) + }) + } +} + +func TestClaudeMessagesStreamStatusCapturedAs200(t *testing.T) { + statuses := make([]int, 0, 1) + h := &Handler{ + Store: streamStatusClaudeStoreStub{}, + OpenAI: streamStatusClaudeOpenAIStub{}, + } + r := chi.NewRouter() + r.Use(captureClaudeStatusMiddleware(&statuses)) + RegisterRoutes(r, h) + + reqBody := `{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":true}` + req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", strings.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(statuses) != 1 { + t.Fatalf("expected one captured status, got %d", len(statuses)) + } + if statuses[0] != http.StatusOK { + t.Fatalf("expected captured status 200 (not 000), got %d", statuses[0]) + } +} diff --git a/internal/httpapi/claude/token_count.go b/internal/httpapi/claude/token_count.go new file mode 100644 index 0000000000000000000000000000000000000000..2a065374a1f17ed9d486a1d51d54154ba319b73c --- /dev/null +++ b/internal/httpapi/claude/token_count.go @@ -0,0 +1,20 @@ +package claude + +import ( + "strings" + + "ds2api/internal/promptcompat" + "ds2api/internal/util" +) + +func countClaudeInputTokens(stdReq promptcompat.StandardRequest) int { + promptText := stdReq.PromptTokenText + if strings.TrimSpace(promptText) == "" { + promptText = stdReq.FinalPrompt + } + return countClaudeInputTokensFromText(promptText, stdReq.ResolvedModel) +} + +func countClaudeInputTokensFromText(promptText, model string) int { + return util.CountPromptTokens(promptText, model) +} diff --git a/internal/httpapi/claude/tool_call_state.go b/internal/httpapi/claude/tool_call_state.go new file mode 100644 index 0000000000000000000000000000000000000000..595d08981a3878bef29cccb493a4f0e39f02e002 --- /dev/null +++ b/internal/httpapi/claude/tool_call_state.go @@ -0,0 +1,25 @@ +package claude + +import ( + "fmt" + "strings" +) + +type claudeToolCallState struct { + nameByID map[string]string + lastIDByName map[string]string + callIDSequence int +} + +func (s *claudeToolCallState) nextID() string { + s.callIDSequence++ + return fmt.Sprintf("call_claude_%d", s.callIDSequence) +} + +func safeStringValue(v any) string { + s, ok := v.(string) + if !ok { + return "" + } + return strings.TrimSpace(s) +} diff --git a/internal/httpapi/gemini/convert_messages.go b/internal/httpapi/gemini/convert_messages.go new file mode 100644 index 0000000000000000000000000000000000000000..6dd8f50085a19b8a85140b4617a5d17bfc44949a --- /dev/null +++ b/internal/httpapi/gemini/convert_messages.go @@ -0,0 +1,297 @@ +package gemini + +import ( + "fmt" + "strings" +) + +const maxGeminiRawPromptChars = 1024 + +func geminiMessagesFromRequest(req map[string]any) []any { + out := make([]any, 0, 8) + toolCallCounter := 0 + nextToolCallID := func() string { + toolCallCounter++ + return fmt.Sprintf("call_gemini_%d", toolCallCounter) + } + lastToolCallIDByName := map[string]string{} + if sys := normalizeGeminiSystemInstruction(req["systemInstruction"]); strings.TrimSpace(sys) != "" { + out = append(out, map[string]any{ + "role": "system", + "content": sys, + }) + } + + contents, _ := req["contents"].([]any) + for _, item := range contents { + content, ok := item.(map[string]any) + if !ok { + continue + } + role := mapGeminiRole(content["role"]) + if role == "" { + role = "user" + } + parts, _ := content["parts"].([]any) + if len(parts) == 0 { + if text := strings.TrimSpace(asString(content["text"])); text != "" { + out = append(out, map[string]any{ + "role": role, + "content": text, + }) + } + continue + } + + textParts := make([]string, 0, len(parts)) + pendingThinking := "" + flushText := func() { + if len(textParts) == 0 { + return + } + msg := map[string]any{ + "role": role, + "content": strings.Join(textParts, "\n"), + } + if role == "assistant" && strings.TrimSpace(pendingThinking) != "" { + msg["reasoning_content"] = pendingThinking + pendingThinking = "" + } + out = append(out, msg) + textParts = textParts[:0] + } + + for _, rawPart := range parts { + part, ok := rawPart.(map[string]any) + if !ok { + continue + } + if text := strings.TrimSpace(asString(part["text"])); text != "" { + if role == "assistant" && isGeminiThoughtPart(part) { + if pendingThinking == "" { + pendingThinking = text + } else { + pendingThinking += "\n" + text + } + continue + } + textParts = append(textParts, text) + continue + } + + if fnCall, ok := part["functionCall"].(map[string]any); ok { + flushText() + if name := strings.TrimSpace(asString(fnCall["name"])); name != "" { + callID := strings.TrimSpace(asString(fnCall["id"])) + if callID == "" { + if callID = strings.TrimSpace(asString(fnCall["call_id"])); callID == "" { + callID = nextToolCallID() + } + } + lastToolCallIDByName[strings.ToLower(name)] = callID + msg := map[string]any{ + "role": "assistant", + "tool_calls": []any{ + map[string]any{ + "id": callID, + "type": "function", + "function": map[string]any{ + "name": name, + "arguments": stringifyJSON(fnCall["args"]), + }, + }, + }, + } + if strings.TrimSpace(pendingThinking) != "" { + msg["reasoning_content"] = pendingThinking + pendingThinking = "" + } + out = append(out, msg) + } + continue + } + + if fnResp, ok := part["functionResponse"].(map[string]any); ok { + flushText() + name := strings.TrimSpace(asString(fnResp["name"])) + callID := strings.TrimSpace(asString(fnResp["id"])) + if callID == "" { + callID = strings.TrimSpace(asString(fnResp["callId"])) + } + if callID == "" { + callID = strings.TrimSpace(asString(fnResp["tool_call_id"])) + } + if callID == "" { + callID = strings.TrimSpace(lastToolCallIDByName[strings.ToLower(name)]) + } + if callID == "" { + callID = nextToolCallID() + } + content := fnResp["response"] + if content == nil { + content = fnResp["output"] + } + if content == nil { + content = "" + } + msg := map[string]any{ + "role": "tool", + "tool_call_id": callID, + "content": content, + } + if name != "" { + msg["name"] = name + } + out = append(out, msg) + continue + } + + if raw := strings.TrimSpace(formatGeminiUnknownPartForPrompt(part)); raw != "" && raw != "null" { + textParts = append(textParts, raw) + } + } + flushText() + if role == "assistant" && strings.TrimSpace(pendingThinking) != "" { + out = append(out, map[string]any{ + "role": "assistant", + "reasoning_content": pendingThinking, + }) + } + } + return out +} + +func isGeminiThoughtPart(part map[string]any) bool { + if part == nil { + return false + } + if v, ok := part["thought"].(bool); ok { + return v + } + if v, ok := part["thoughtSignature"].(string); ok && strings.TrimSpace(v) != "" { + return true + } + return false +} + +func normalizeGeminiSystemInstruction(raw any) string { + switch v := raw.(type) { + case string: + return strings.TrimSpace(v) + case map[string]any: + if parts, ok := v["parts"].([]any); ok { + texts := make([]string, 0, len(parts)) + for _, item := range parts { + part, ok := item.(map[string]any) + if !ok { + continue + } + if text := strings.TrimSpace(asString(part["text"])); text != "" { + texts = append(texts, text) + } + } + return strings.Join(texts, "\n") + } + if text := strings.TrimSpace(asString(v["text"])); text != "" { + return text + } + } + return "" +} + +func mapGeminiRole(v any) string { + switch strings.ToLower(strings.TrimSpace(asString(v))) { + case "user": + return "user" + case "model", "assistant": + return "assistant" + case "system": + return "system" + default: + return "" + } +} + +func formatGeminiUnknownPartForPrompt(part map[string]any) string { + safe := sanitizeGeminiPartForPrompt(part) + raw := strings.TrimSpace(stringifyJSON(safe)) + if raw == "" { + return "" + } + if len(raw) > maxGeminiRawPromptChars { + return raw[:maxGeminiRawPromptChars] + "...(truncated)" + } + return raw +} + +func sanitizeGeminiPartForPrompt(part map[string]any) map[string]any { + out := make(map[string]any, len(part)) + for k, v := range part { + if looksLikeGeminiBinaryField(k) { + out[k] = "[omitted_binary_payload]" + continue + } + switch x := v.(type) { + case map[string]any: + out[k] = sanitizeGeminiPartForPrompt(x) + case []any: + out[k] = sanitizeGeminiArrayForPrompt(x) + case string: + out[k] = sanitizeGeminiStringForPrompt(k, x) + default: + out[k] = v + } + } + return out +} + +func sanitizeGeminiArrayForPrompt(items []any) []any { + out := make([]any, 0, len(items)) + for _, item := range items { + switch x := item.(type) { + case map[string]any: + out = append(out, sanitizeGeminiPartForPrompt(x)) + case []any: + out = append(out, sanitizeGeminiArrayForPrompt(x)) + default: + out = append(out, x) + } + } + return out +} + +func sanitizeGeminiStringForPrompt(key, value string) string { + trimmed := strings.TrimSpace(value) + if trimmed == "" { + return "" + } + if looksLikeGeminiBinaryField(key) || looksLikeGeminiBase64(trimmed) { + return "[omitted_binary_payload]" + } + if len(trimmed) > maxGeminiRawPromptChars { + return trimmed[:maxGeminiRawPromptChars] + "...(truncated)" + } + return trimmed +} + +func looksLikeGeminiBinaryField(name string) bool { + n := strings.ToLower(strings.TrimSpace(name)) + return n == "data" || n == "bytes" || n == "inlinedata" || n == "inline_data" || n == "base64" +} + +func looksLikeGeminiBase64(v string) bool { + if len(v) < 512 { + return false + } + compact := strings.TrimRight(v, "=") + if compact == "" { + return false + } + for _, ch := range compact { + if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') || ch == '+' || ch == '/' || ch == '-' || ch == '_' { + continue + } + return false + } + return true +} diff --git a/internal/httpapi/gemini/convert_messages_test.go b/internal/httpapi/gemini/convert_messages_test.go new file mode 100644 index 0000000000000000000000000000000000000000..6f0890f34e723d0dd5bfedde4038e0d5eb0a9744 --- /dev/null +++ b/internal/httpapi/gemini/convert_messages_test.go @@ -0,0 +1,170 @@ +package gemini + +import ( + "ds2api/internal/promptcompat" + "strings" + "testing" +) + +func TestGeminiMessagesFromRequestPreservesFunctionRoundtrip(t *testing.T) { + req := map[string]any{ + "contents": []any{ + map[string]any{ + "role": "model", + "parts": []any{ + map[string]any{ + "functionCall": map[string]any{ + "id": "call_g1", + "name": "search_web", + "args": map[string]any{"query": "ai"}, + }, + }, + }, + }, + map[string]any{ + "role": "user", + "parts": []any{ + map[string]any{ + "functionResponse": map[string]any{ + "id": "call_g1", + "name": "search_web", + "response": "ok", + }, + }, + }, + }, + }, + } + + got := geminiMessagesFromRequest(req) + if len(got) != 2 { + t.Fatalf("expected two normalized messages, got %#v", got) + } + assistant, _ := got[0].(map[string]any) + if assistant["role"] != "assistant" { + t.Fatalf("expected assistant first, got %#v", assistant) + } + tc, _ := assistant["tool_calls"].([]any) + if len(tc) != 1 { + t.Fatalf("expected one tool call, got %#v", assistant["tool_calls"]) + } + toolMsg, _ := got[1].(map[string]any) + if toolMsg["role"] != "tool" || toolMsg["tool_call_id"] != "call_g1" { + t.Fatalf("expected tool message with call id, got %#v", toolMsg) + } +} + +func TestGeminiMessagesFromRequestPreservesThoughtOnFunctionCallHistory(t *testing.T) { + req := map[string]any{ + "contents": []any{ + map[string]any{ + "role": "model", + "parts": []any{ + map[string]any{"text": "need current state before answering", "thought": true}, + map[string]any{ + "functionCall": map[string]any{ + "id": "call_g1", + "name": "search_web", + "args": map[string]any{"query": "ai"}, + }, + }, + }, + }, + }, + } + + got := geminiMessagesFromRequest(req) + if len(got) != 1 { + t.Fatalf("expected one normalized message, got %#v", got) + } + assistant, _ := got[0].(map[string]any) + if assistant["reasoning_content"] != "need current state before answering" { + t.Fatalf("expected thought preserved as reasoning_content, got %#v", assistant) + } + tc, _ := assistant["tool_calls"].([]any) + if len(tc) != 1 { + t.Fatalf("expected one tool call, got %#v", assistant["tool_calls"]) + } + prompt, _ := promptcompat.BuildOpenAIPromptForAdapter(got, nil, "", true) + if !strings.Contains(prompt, "[reasoning_content]\nneed current state before answering\n[/reasoning_content]") { + t.Fatalf("expected thought in prompt history, got %q", prompt) + } + if !strings.Contains(prompt, `<|DSML|invoke name="search_web">`) { + t.Fatalf("expected tool call in prompt history, got %q", prompt) + } +} + +func TestGeminiMessagesFromRequestPreservesUnknownPartAsRawJSONText(t *testing.T) { + req := map[string]any{ + "contents": []any{ + map[string]any{ + "role": "user", + "parts": []any{ + map[string]any{"text": "hello"}, + map[string]any{"inlineData": map[string]any{"mimeType": "image/png", "data": strings.Repeat("A", 2048)}}, + }, + }, + }, + } + + got := geminiMessagesFromRequest(req) + if len(got) != 1 { + t.Fatalf("expected one normalized message, got %#v", got) + } + msg, _ := got[0].(map[string]any) + content, _ := msg["content"].(string) + if !strings.Contains(content, "hello") || !strings.Contains(content, "inlineData") { + t.Fatalf("expected unknown part preserved as raw json text, got %q", content) + } + if !strings.Contains(content, "[omitted_binary_payload]") { + t.Fatalf("expected inlineData payload to be redacted, got %q", content) + } + if strings.Contains(content, strings.Repeat("A", 100)) { + t.Fatalf("expected raw base64 payload not to be embedded, got %q", content) + } +} + +func TestGeminiMessagesFromRequestBackfillsFunctionResponseCallIDByName(t *testing.T) { + req := map[string]any{ + "contents": []any{ + map[string]any{ + "role": "model", + "parts": []any{ + map[string]any{ + "functionCall": map[string]any{ + "name": "search_web", + "args": map[string]any{"query": "docs"}, + }, + }, + }, + }, + map[string]any{ + "role": "user", + "parts": []any{ + map[string]any{ + "functionResponse": map[string]any{ + "name": "search_web", + "response": map[string]any{"ok": true}, + }, + }, + }, + }, + }, + } + + got := geminiMessagesFromRequest(req) + if len(got) != 2 { + t.Fatalf("expected two normalized messages, got %#v", got) + } + assistant, _ := got[0].(map[string]any) + tc, _ := assistant["tool_calls"].([]any) + call, _ := tc[0].(map[string]any) + callID, _ := call["id"].(string) + if !strings.HasPrefix(callID, "call_gemini_") { + t.Fatalf("expected generated call id prefix, got %#v", call) + } + toolMsg, _ := got[1].(map[string]any) + if toolMsg["tool_call_id"] != callID { + t.Fatalf("expected tool response to inherit generated call id, tool=%#v call=%#v", toolMsg, call) + } +} diff --git a/internal/httpapi/gemini/convert_passthrough.go b/internal/httpapi/gemini/convert_passthrough.go new file mode 100644 index 0000000000000000000000000000000000000000..ba943a9ddc7ee469b6716c2bd8a8d5887f72a58f --- /dev/null +++ b/internal/httpapi/gemini/convert_passthrough.go @@ -0,0 +1,55 @@ +package gemini + +import ( + "encoding/json" + "strings" +) + +//nolint:unused // compatibility hook for native Gemini request normalization path. +func collectGeminiPassThrough(req map[string]any) map[string]any { + cfg, _ := req["generationConfig"].(map[string]any) + if len(cfg) == 0 { + return nil + } + out := map[string]any{} + if v, ok := cfg["temperature"]; ok { + out["temperature"] = v + } + if v, ok := cfg["topP"]; ok { + out["top_p"] = v + } + if v, ok := cfg["maxOutputTokens"]; ok { + out["max_tokens"] = v + } + if v, ok := cfg["stopSequences"]; ok { + out["stop"] = v + } + if len(out) == 0 { + return nil + } + return out +} + +func asString(v any) string { + s, _ := v.(string) + return s +} + +func stringifyJSON(v any) string { + switch x := v.(type) { + case nil: + return "{}" + case string: + s := strings.TrimSpace(x) + if s == "" { + return "{}" + } + return s + default: + b, err := json.Marshal(x) + if err != nil || len(b) == 0 { + return "{}" + } + return string(b) + } +} diff --git a/internal/httpapi/gemini/convert_request.go b/internal/httpapi/gemini/convert_request.go new file mode 100644 index 0000000000000000000000000000000000000000..4ad52387b5f79df80e3dd7425903e2101dc11c1f --- /dev/null +++ b/internal/httpapi/gemini/convert_request.go @@ -0,0 +1,56 @@ +package gemini + +import ( + "fmt" + "strings" + + "ds2api/internal/config" + "ds2api/internal/promptcompat" + "ds2api/internal/util" +) + +//nolint:unused // kept for native Gemini adapter route compatibility. +func normalizeGeminiRequest(store ConfigReader, routeModel string, req map[string]any, stream bool) (promptcompat.StandardRequest, error) { + requestedModel := strings.TrimSpace(routeModel) + if requestedModel == "" { + return promptcompat.StandardRequest{}, fmt.Errorf("model is required in request path") + } + + resolvedModel, ok := config.ResolveModel(store, requestedModel) + if !ok { + return promptcompat.StandardRequest{}, fmt.Errorf("model %q is not available", requestedModel) + } + defaultThinkingEnabled, searchEnabled, _ := config.GetModelConfig(resolvedModel) + thinkingEnabled := util.ResolveThinkingEnabled(req, defaultThinkingEnabled) + if config.IsNoThinkingModel(resolvedModel) { + thinkingEnabled = false + } + + messagesRaw := geminiMessagesFromRequest(req) + if len(messagesRaw) == 0 { + return promptcompat.StandardRequest{}, fmt.Errorf("request must include non-empty contents") + } + + toolsRaw := convertGeminiTools(req["tools"]) + finalPrompt, toolNames := promptcompat.BuildOpenAIPromptForAdapter(messagesRaw, toolsRaw, "", thinkingEnabled) + if len(toolNames) == 0 && len(toolsRaw) > 0 { + toolNames = []string{"__any_tool__"} + } + passThrough := collectGeminiPassThrough(req) + + return promptcompat.StandardRequest{ + Surface: "google_gemini", + RequestedModel: requestedModel, + ResolvedModel: resolvedModel, + ResponseModel: requestedModel, + Messages: messagesRaw, + PromptTokenText: finalPrompt, + ToolsRaw: toolsRaw, + FinalPrompt: finalPrompt, + ToolNames: toolNames, + Stream: stream, + Thinking: thinkingEnabled, + Search: searchEnabled, + PassThrough: passThrough, + }, nil +} diff --git a/internal/httpapi/gemini/convert_request_test.go b/internal/httpapi/gemini/convert_request_test.go new file mode 100644 index 0000000000000000000000000000000000000000..797f83cf13d793b6ba5cab0c2105a826554951bf --- /dev/null +++ b/internal/httpapi/gemini/convert_request_test.go @@ -0,0 +1,28 @@ +package gemini + +import "testing" + +func TestNormalizeGeminiRequestNoThinkingModelForcesThinkingOff(t *testing.T) { + req := map[string]any{ + "contents": []any{ + map[string]any{ + "role": "user", + "parts": []any{map[string]any{"text": "hello"}}, + }, + }, + "reasoning_effort": "high", + } + out, err := normalizeGeminiRequest(testGeminiConfig{}, "gemini-2.5-pro-nothinking", req, false) + if err != nil { + t.Fatalf("normalizeGeminiRequest error: %v", err) + } + if out.ResolvedModel != "deepseek-v4-pro-nothinking" { + t.Fatalf("resolved model mismatch: got=%q", out.ResolvedModel) + } + if out.Thinking { + t.Fatalf("expected nothinking model to force thinking off") + } + if out.Search { + t.Fatalf("expected search=false, got=%v", out.Search) + } +} diff --git a/internal/httpapi/gemini/convert_tools.go b/internal/httpapi/gemini/convert_tools.go new file mode 100644 index 0000000000000000000000000000000000000000..3df3a7bcda17162c5fb0d8c8c3911109538ddd31 --- /dev/null +++ b/internal/httpapi/gemini/convert_tools.go @@ -0,0 +1,72 @@ +package gemini + +import "strings" + +//nolint:unused // kept for native Gemini adapter route compatibility. +func convertGeminiTools(raw any) []any { + tools, _ := raw.([]any) + if len(tools) == 0 { + return nil + } + out := make([]any, 0, len(tools)) + for _, item := range tools { + tool, ok := item.(map[string]any) + if !ok { + continue + } + + if fnDecls, ok := tool["functionDeclarations"].([]any); ok && len(fnDecls) > 0 { + for _, declRaw := range fnDecls { + decl, ok := declRaw.(map[string]any) + if !ok { + continue + } + name := strings.TrimSpace(asString(decl["name"])) + if name == "" { + continue + } + function := map[string]any{ + "name": name, + } + if desc := strings.TrimSpace(asString(decl["description"])); desc != "" { + function["description"] = desc + } + if params, ok := decl["parameters"].(map[string]any); ok { + function["parameters"] = params + } + out = append(out, map[string]any{ + "type": "function", + "function": function, + }) + } + continue + } + + // OpenAI-style passthrough fallback. + if _, ok := tool["function"].(map[string]any); ok { + out = append(out, tool) + continue + } + + // Loose fallback for flattened function schema objects. + name := strings.TrimSpace(asString(tool["name"])) + if name == "" { + continue + } + fn := map[string]any{"name": name} + if desc := strings.TrimSpace(asString(tool["description"])); desc != "" { + fn["description"] = desc + } + if params, ok := tool["parameters"].(map[string]any); ok { + fn["parameters"] = params + } + out = append(out, map[string]any{ + "type": "function", + "function": fn, + }) + } + if len(out) == 0 { + return nil + } + return out +} diff --git a/internal/httpapi/gemini/deps.go b/internal/httpapi/gemini/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..028c194ea402aa2453aca268ca1bf94808425eee --- /dev/null +++ b/internal/httpapi/gemini/deps.go @@ -0,0 +1,36 @@ +package gemini + +import ( + "context" + "net/http" + + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" +) + +type AuthResolver interface { + Determine(req *http.Request) (*auth.RequestAuth, error) + Release(a *auth.RequestAuth) +} + +type DeepSeekCaller interface { + CreateSession(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + GetPow(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + UploadFile(ctx context.Context, a *auth.RequestAuth, req dsclient.UploadFileRequest, maxAttempts int) (*dsclient.UploadFileResult, error) + CallCompletion(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, maxAttempts int) (*http.Response, error) +} + +type ConfigReader interface { + ModelAliases() map[string]string + CurrentInputFileEnabled() bool + CurrentInputFileMinChars() int +} + +type OpenAIChatRunner interface { + ChatCompletions(w http.ResponseWriter, r *http.Request) +} + +var _ AuthResolver = (*auth.Resolver)(nil) +var _ DeepSeekCaller = (*dsclient.Client)(nil) +var _ ConfigReader = (*config.Store)(nil) diff --git a/internal/httpapi/gemini/handler_errors.go b/internal/httpapi/gemini/handler_errors.go new file mode 100644 index 0000000000000000000000000000000000000000..09df09bc677c946f4b400bbd4de75bfb1e18bca5 --- /dev/null +++ b/internal/httpapi/gemini/handler_errors.go @@ -0,0 +1,28 @@ +package gemini + +import "net/http" + +func writeGeminiError(w http.ResponseWriter, status int, message string) { + errorStatus := "INVALID_ARGUMENT" + switch status { + case http.StatusUnauthorized: + errorStatus = "UNAUTHENTICATED" + case http.StatusForbidden: + errorStatus = "PERMISSION_DENIED" + case http.StatusTooManyRequests: + errorStatus = "RESOURCE_EXHAUSTED" + case http.StatusNotFound: + errorStatus = "NOT_FOUND" + default: + if status >= 500 { + errorStatus = "INTERNAL" + } + } + writeJSON(w, status, map[string]any{ + "error": map[string]any{ + "code": status, + "message": message, + "status": errorStatus, + }, + }) +} diff --git a/internal/httpapi/gemini/handler_generate.go b/internal/httpapi/gemini/handler_generate.go new file mode 100644 index 0000000000000000000000000000000000000000..b9a648d42a866baff3d8920586486640592dc152 --- /dev/null +++ b/internal/httpapi/gemini/handler_generate.go @@ -0,0 +1,463 @@ +package gemini + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "io" + "net/http" + "net/http/httptest" + "strings" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/completionruntime" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/httpapi/requestbody" + "ds2api/internal/promptcompat" + "ds2api/internal/responsehistory" + "ds2api/internal/sse" + "ds2api/internal/toolcall" + "ds2api/internal/translatorcliproxy" + "ds2api/internal/util" + + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" +) + +func (h *Handler) handleGenerateContent(w http.ResponseWriter, r *http.Request, stream bool) { + if isGeminiVercelProxyRequest(r) && h.proxyViaOpenAI(w, r, stream) { + return + } + if h.Auth == nil || h.DS == nil { + if h.OpenAI != nil && h.proxyViaOpenAI(w, r, stream) { + return + } + writeGeminiError(w, http.StatusInternalServerError, "Gemini runtime backend unavailable.") + return + } + if h.handleGeminiDirect(w, r, stream) { + return + } + writeGeminiError(w, http.StatusBadGateway, "Failed to handle Gemini request.") +} + +func isGeminiVercelProxyRequest(r *http.Request) bool { + if r == nil || r.URL == nil { + return false + } + return strings.TrimSpace(r.URL.Query().Get("__stream_prepare")) == "1" || + strings.TrimSpace(r.URL.Query().Get("__stream_release")) == "1" +} + +func (h *Handler) handleGeminiDirect(w http.ResponseWriter, r *http.Request, stream bool) bool { + raw, err := io.ReadAll(r.Body) + if err != nil { + if errors.Is(err, requestbody.ErrInvalidUTF8Body) { + writeGeminiError(w, http.StatusBadRequest, "invalid json") + } else { + writeGeminiError(w, http.StatusBadRequest, "invalid body") + } + return true + } + routeModel := strings.TrimSpace(chi.URLParam(r, "model")) + var req map[string]any + if err := json.Unmarshal(raw, &req); err != nil { + writeGeminiError(w, http.StatusBadRequest, "invalid json") + return true + } + stdReq, err := normalizeGeminiRequest(h.Store, routeModel, req, stream) + if err != nil { + writeGeminiError(w, http.StatusBadRequest, err.Error()) + return true + } + a, err := h.Auth.Determine(r) + if err != nil { + writeGeminiError(w, http.StatusUnauthorized, err.Error()) + return true + } + defer h.Auth.Release(a) + stdReq, err = h.applyCurrentInputFile(r.Context(), a, stdReq) + if err != nil { + status, message := mapCurrentInputFileError(err) + writeGeminiError(w, status, message) + return true + } + historySession := responsehistory.Start(responsehistory.StartParams{ + Store: h.ChatHistory, + Request: r, + Auth: a, + Surface: "gemini.generate_content", + Standard: stdReq, + }) + if stream { + h.handleGeminiDirectStream(w, r, a, stdReq, historySession) + return true + } + result, outErr := completionruntime.ExecuteNonStreamWithRetry(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + RetryEnabled: true, + CurrentInputFile: h.Store, + }) + if outErr != nil { + if historySession != nil { + historySession.ErrorTurn(outErr.Status, outErr.Message, outErr.Code, result.Turn) + } + writeGeminiError(w, outErr.Status, outErr.Message) + return true + } + if historySession != nil { + historySession.SuccessTurn(http.StatusOK, result.Turn, responsehistory.GenericUsage(result.Turn)) + } + writeJSON(w, http.StatusOK, buildGeminiGenerateContentResponseFromTurn(result.Turn)) + return true +} + +func (h *Handler) applyCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { + if h == nil { + return stdReq, nil + } + return (history.Service{Store: h.Store, DS: h.DS}).ApplyCurrentInputFile(ctx, a, stdReq) +} + +func mapCurrentInputFileError(err error) (int, string) { + return history.MapError(err) +} + +func (h *Handler) handleGeminiDirectStream(w http.ResponseWriter, r *http.Request, a *auth.RequestAuth, stdReq promptcompat.StandardRequest, historySession *responsehistory.Session) { + start, outErr := completionruntime.StartCompletion(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + CurrentInputFile: h.Store, + }) + if outErr != nil { + if historySession != nil { + historySession.Error(outErr.Status, outErr.Message, outErr.Code, "", "") + } + writeGeminiError(w, outErr.Status, outErr.Message) + return + } + streamReq := start.Request + h.handleStreamGenerateContentWithRetry(w, r, a, start.Response, start.Payload, start.Pow, streamReq, streamReq.ResponseModel, streamReq.PromptTokenText, streamReq.Thinking, streamReq.Search, streamReq.ToolNames, streamReq.ToolsRaw, historySession) +} + +func (h *Handler) proxyViaOpenAI(w http.ResponseWriter, r *http.Request, stream bool) bool { + raw, err := io.ReadAll(r.Body) + if err != nil { + if errors.Is(err, requestbody.ErrInvalidUTF8Body) { + writeGeminiError(w, http.StatusBadRequest, "invalid json") + } else { + writeGeminiError(w, http.StatusBadRequest, "invalid body") + } + return true + } + routeModel := strings.TrimSpace(chi.URLParam(r, "model")) + var req map[string]any + if err := json.Unmarshal(raw, &req); err != nil { + writeGeminiError(w, http.StatusBadRequest, "invalid json") + return true + } + translatedReq := translatorcliproxy.ToOpenAI(sdktranslator.FormatGemini, routeModel, raw, stream) + if !strings.Contains(string(translatedReq), `"stream"`) { + var reqMap map[string]any + if json.Unmarshal(translatedReq, &reqMap) == nil { + reqMap["stream"] = stream + if b, e := json.Marshal(reqMap); e == nil { + translatedReq = b + } + } + } + translatedReq = applyGeminiThinkingPolicyToOpenAIRequest(translatedReq, req) + + isVercelPrepare := strings.TrimSpace(r.URL.Query().Get("__stream_prepare")) == "1" + isVercelRelease := strings.TrimSpace(r.URL.Query().Get("__stream_release")) == "1" + + if isVercelRelease { + proxyReq := r.Clone(r.Context()) + proxyReq.URL.Path = "/v1/chat/completions" + proxyReq.Body = io.NopCloser(bytes.NewReader(raw)) + proxyReq.ContentLength = int64(len(raw)) + rec := httptest.NewRecorder() + h.OpenAI.ChatCompletions(rec, proxyReq) + res := rec.Result() + defer func() { _ = res.Body.Close() }() + body, _ := io.ReadAll(res.Body) + for k, vv := range res.Header { + for _, v := range vv { + w.Header().Add(k, v) + } + } + w.WriteHeader(res.StatusCode) + _, _ = w.Write(body) + return true + } + + proxyReq := r.Clone(r.Context()) + proxyReq.URL.Path = "/v1/chat/completions" + proxyReq.Body = io.NopCloser(bytes.NewReader(translatedReq)) + proxyReq.ContentLength = int64(len(translatedReq)) + + if stream && !isVercelPrepare { + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + streamWriter := translatorcliproxy.NewOpenAIStreamTranslatorWriter(w, sdktranslator.FormatGemini, routeModel, raw, translatedReq) + h.OpenAI.ChatCompletions(streamWriter, proxyReq) + return true + } + + rec := httptest.NewRecorder() + h.OpenAI.ChatCompletions(rec, proxyReq) + res := rec.Result() + defer func() { _ = res.Body.Close() }() + body, _ := io.ReadAll(res.Body) + if res.StatusCode < 200 || res.StatusCode >= 300 { + for k, vv := range res.Header { + for _, v := range vv { + w.Header().Add(k, v) + } + } + writeGeminiErrorFromOpenAI(w, res.StatusCode, body) + return true + } + if isVercelPrepare { + for k, vv := range res.Header { + for _, v := range vv { + w.Header().Add(k, v) + } + } + w.WriteHeader(res.StatusCode) + _, _ = w.Write(body) + return true + } + converted := translatorcliproxy.FromOpenAINonStream(sdktranslator.FormatGemini, routeModel, raw, translatedReq, body) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(converted) + return true +} + +func applyGeminiThinkingPolicyToOpenAIRequest(translated []byte, original map[string]any) []byte { + req := map[string]any{} + if err := json.Unmarshal(translated, &req); err != nil { + return translated + } + enabled, ok := resolveGeminiThinkingOverride(original) + if !ok { + return translated + } + typ := "disabled" + if enabled { + typ = "enabled" + } + req["thinking"] = map[string]any{"type": typ} + out, err := json.Marshal(req) + if err != nil { + return translated + } + return out +} + +func resolveGeminiThinkingOverride(req map[string]any) (bool, bool) { + generationConfig, ok := req["generationConfig"].(map[string]any) + if !ok { + generationConfig, ok = req["generation_config"].(map[string]any) + } + if !ok { + return false, false + } + thinkingConfig, ok := generationConfig["thinkingConfig"].(map[string]any) + if !ok { + thinkingConfig, ok = generationConfig["thinking_config"].(map[string]any) + } + if !ok { + return false, false + } + budget, ok := numericAny(thinkingConfig["thinkingBudget"]) + if !ok { + budget, ok = numericAny(thinkingConfig["thinking_budget"]) + } + if !ok { + return false, false + } + return budget > 0, true +} + +func numericAny(raw any) (float64, bool) { + switch v := raw.(type) { + case float64: + return v, true + case float32: + return float64(v), true + case int: + return float64(v), true + case int64: + return float64(v), true + case int32: + return float64(v), true + case json.Number: + f, err := v.Float64() + return f, err == nil + default: + return 0, false + } +} + +func writeGeminiErrorFromOpenAI(w http.ResponseWriter, status int, raw []byte) { + message := strings.TrimSpace(string(raw)) + var parsed map[string]any + if err := json.Unmarshal(raw, &parsed); err == nil { + if errObj, ok := parsed["error"].(map[string]any); ok { + if msg, ok := errObj["message"].(string); ok && strings.TrimSpace(msg) != "" { + message = strings.TrimSpace(msg) + } + } + } + if message == "" { + message = http.StatusText(status) + } + writeGeminiError(w, status, message) +} + +//nolint:unused // retained for native Gemini non-stream handling path. +func (h *Handler) handleNonStreamGenerateContent(w http.ResponseWriter, resp *http.Response, model, finalPrompt string, thinkingEnabled bool, toolNames []string) { + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + writeGeminiError(w, resp.StatusCode, strings.TrimSpace(string(body))) + return + } + + result := sse.CollectStream(resp, thinkingEnabled, true) + writeJSON(w, http.StatusOK, buildGeminiGenerateContentResponse( + model, + finalPrompt, + cleanVisibleOutput(result.Thinking, false), + cleanVisibleOutput(result.Text, false), + toolNames, + )) +} + +//nolint:unused // retained for native Gemini non-stream handling path. +func buildGeminiGenerateContentResponse(model, finalPrompt, finalThinking, finalText string, toolNames []string) map[string]any { + parts := buildGeminiPartsFromFinal(finalText, finalThinking, toolNames) + usage := buildGeminiUsage(model, finalPrompt, finalThinking, finalText) + return map[string]any{ + "candidates": []map[string]any{ + { + "index": 0, + "content": map[string]any{ + "role": "model", + "parts": parts, + }, + "finishReason": "STOP", + }, + }, + "modelVersion": model, + "usageMetadata": usage, + } +} + +func buildGeminiGenerateContentResponseFromTurn(turn assistantturn.Turn) map[string]any { + parts := buildGeminiPartsFromTurn(turn) + return map[string]any{ + "candidates": []map[string]any{ + { + "index": 0, + "content": map[string]any{ + "role": "model", + "parts": parts, + }, + "finishReason": "STOP", + }, + }, + "modelVersion": turn.Model, + "usageMetadata": map[string]any{ + "promptTokenCount": turn.Usage.InputTokens, + "candidatesTokenCount": turn.Usage.OutputTokens, + "totalTokenCount": turn.Usage.TotalTokens, + }, + } +} + +func buildGeminiPartsFromTurn(turn assistantturn.Turn) []map[string]any { + thinkingPart := func() []map[string]any { + if turn.Thinking == "" { + return nil + } + return []map[string]any{{"text": turn.Thinking, "thought": true}} + } + if len(turn.ToolCalls) > 0 { + parts := thinkingPart() + if parts == nil { + parts = make([]map[string]any, 0, len(turn.ToolCalls)) + } + for _, tc := range turn.ToolCalls { + parts = append(parts, map[string]any{ + "functionCall": map[string]any{ + "name": tc.Name, + "args": tc.Input, + }, + }) + } + return parts + } + parts := thinkingPart() + if turn.Text != "" { + parts = append(parts, map[string]any{"text": turn.Text}) + } + if len(parts) == 0 { + parts = append(parts, map[string]any{"text": ""}) + } + return parts +} + +//nolint:unused // retained for native Gemini non-stream handling path. +func buildGeminiUsage(model, finalPrompt, finalThinking, finalText string) map[string]any { + promptTokens := util.CountPromptTokens(finalPrompt, model) + reasoningTokens := util.CountOutputTokens(finalThinking, model) + completionTokens := util.CountOutputTokens(finalText, model) + return map[string]any{ + "promptTokenCount": promptTokens, + "candidatesTokenCount": reasoningTokens + completionTokens, + "totalTokenCount": promptTokens + reasoningTokens + completionTokens, + } +} + +//nolint:unused // retained for native Gemini non-stream handling path. +func buildGeminiPartsFromFinal(finalText, finalThinking string, toolNames []string) []map[string]any { + detected := toolcall.ParseToolCalls(finalText, toolNames) + if len(detected) == 0 && finalThinking != "" { + detected = toolcall.ParseToolCalls(finalThinking, toolNames) + } + thinkingPart := func() []map[string]any { + if finalThinking == "" { + return nil + } + return []map[string]any{{"text": finalThinking, "thought": true}} + } + if len(detected) > 0 { + parts := thinkingPart() + if parts == nil { + parts = make([]map[string]any, 0, len(detected)) + } + for _, tc := range detected { + parts = append(parts, map[string]any{ + "functionCall": map[string]any{ + "name": tc.Name, + "args": tc.Input, + }, + }) + } + return parts + } + + parts := thinkingPart() + if finalText != "" { + parts = append(parts, map[string]any{"text": finalText}) + } + if len(parts) == 0 { + parts = append(parts, map[string]any{"text": ""}) + } + return parts +} diff --git a/internal/httpapi/gemini/handler_routes.go b/internal/httpapi/gemini/handler_routes.go new file mode 100644 index 0000000000000000000000000000000000000000..6f6c56e9ba6647fbc1409f77b746a784e27780f8 --- /dev/null +++ b/internal/httpapi/gemini/handler_routes.go @@ -0,0 +1,41 @@ +package gemini + +import ( + "net/http" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/chathistory" + "ds2api/internal/textclean" + "ds2api/internal/util" +) + +var writeJSON = util.WriteJSON + +type Handler struct { + Store ConfigReader + Auth AuthResolver + DS DeepSeekCaller + OpenAI OpenAIChatRunner + ChatHistory *chathistory.Store +} + +//nolint:unused // used by native Gemini stream/non-stream runtime helpers. +func stripReferenceMarkersEnabled() bool { + return textclean.StripReferenceMarkersEnabled() +} + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Post("/v1beta/models/{model}:generateContent", h.GenerateContent) + r.Post("/v1beta/models/{model}:streamGenerateContent", h.StreamGenerateContent) + r.Post("/v1/models/{model}:generateContent", h.GenerateContent) + r.Post("/v1/models/{model}:streamGenerateContent", h.StreamGenerateContent) +} + +func (h *Handler) GenerateContent(w http.ResponseWriter, r *http.Request) { + h.handleGenerateContent(w, r, false) +} + +func (h *Handler) StreamGenerateContent(w http.ResponseWriter, r *http.Request) { + h.handleGenerateContent(w, r, true) +} diff --git a/internal/httpapi/gemini/handler_stream_runtime.go b/internal/httpapi/gemini/handler_stream_runtime.go new file mode 100644 index 0000000000000000000000000000000000000000..6a98a4e612a079ee8f595c2f4ee1058ee637b056 --- /dev/null +++ b/internal/httpapi/gemini/handler_stream_runtime.go @@ -0,0 +1,383 @@ +package gemini + +import ( + "context" + "encoding/json" + "io" + "net/http" + "strings" + "time" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/completionruntime" + dsprotocol "ds2api/internal/deepseek/protocol" + "ds2api/internal/promptcompat" + "ds2api/internal/responsehistory" + "ds2api/internal/sse" + streamengine "ds2api/internal/stream" +) + +//nolint:unused // retained for native Gemini stream handling path. +func (h *Handler) handleStreamGenerateContent(w http.ResponseWriter, r *http.Request, resp *http.Response, model, finalPrompt string, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, historySessions ...*responsehistory.Session) { + var historySession *responsehistory.Session + if len(historySessions) > 0 { + historySession = historySessions[0] + } + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.Error(resp.StatusCode, strings.TrimSpace(string(body)), "error", "", "") + } + writeGeminiError(w, resp.StatusCode, strings.TrimSpace(string(body))) + return + } + + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + runtime := newGeminiStreamRuntime(w, rc, canFlush, model, finalPrompt, thinkingEnabled, searchEnabled, stripReferenceMarkersEnabled(), toolNames, toolsRaw, historySession) + + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: r.Context(), + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: time.Duration(dsprotocol.KeepAliveTimeout) * time.Second, + IdleTimeout: time.Duration(dsprotocol.StreamIdleTimeout) * time.Second, + MaxKeepAliveNoInput: dsprotocol.MaxKeepaliveCount, + }, streamengine.ConsumeHooks{ + OnParsed: runtime.onParsed, + OnFinalize: func(_ streamengine.StopReason, _ error) { + runtime.finalize(false) + }, + }) +} + +//nolint:unused // retained for native Gemini stream handling path. +type geminiStreamRuntime struct { + w http.ResponseWriter + rc *http.ResponseController + canFlush bool + + model string + finalPrompt string + + thinkingEnabled bool + searchEnabled bool + bufferContent bool + stripReferenceMarkers bool + toolNames []string + toolsRaw any + + accumulator *assistantturn.Accumulator + contentFilter bool + responseMessageID int + finalErrorStatus int + finalErrorMessage string + finalErrorCode string + history *responsehistory.Session +} + +func (h *Handler) handleStreamGenerateContentWithRetry(w http.ResponseWriter, r *http.Request, a *auth.RequestAuth, resp *http.Response, payload map[string]any, pow string, stdReq promptcompat.StandardRequest, model, finalPrompt string, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, historySession *responsehistory.Session) { + if resp.StatusCode != http.StatusOK { + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.Error(resp.StatusCode, strings.TrimSpace(string(body)), "error", "", "") + } + writeGeminiError(w, resp.StatusCode, strings.TrimSpace(string(body))) + return + } + + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + runtime := newGeminiStreamRuntime(w, rc, canFlush, model, finalPrompt, thinkingEnabled, searchEnabled, stripReferenceMarkersEnabled(), toolNames, toolsRaw, historySession) + + completionruntime.ExecuteStreamWithRetry(r.Context(), h.DS, a, resp, payload, pow, completionruntime.StreamRetryOptions{ + Surface: "gemini.generate_content", + Stream: true, + RetryEnabled: true, + MaxAttempts: 3, + UsagePrompt: finalPrompt, + Request: stdReq, + CurrentInputFile: h.Store, + }, completionruntime.StreamRetryHooks{ + ConsumeAttempt: func(currentResp *http.Response, allowDeferEmpty bool) (bool, bool) { + return h.consumeGeminiStreamAttempt(r.Context(), currentResp, runtime, thinkingEnabled, allowDeferEmpty) + }, + Finalize: func(_ int) { + runtime.finalize(false) + }, + ParentMessageID: func() int { + return runtime.responseMessageID + }, + OnRetryPrompt: func(prompt string) { + runtime.finalPrompt = prompt + }, + OnRetryFailure: func(status int, message, _ string) { + runtime.sendErrorChunk(status, strings.TrimSpace(message)) + }, + }) +} + +func (h *Handler) consumeGeminiStreamAttempt(ctx context.Context, resp *http.Response, runtime *geminiStreamRuntime, thinkingEnabled bool, allowDeferEmpty bool) (bool, bool) { + defer func() { _ = resp.Body.Close() }() + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: ctx, + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: time.Duration(dsprotocol.KeepAliveTimeout) * time.Second, + IdleTimeout: time.Duration(dsprotocol.StreamIdleTimeout) * time.Second, + MaxKeepAliveNoInput: dsprotocol.MaxKeepaliveCount, + }, streamengine.ConsumeHooks{ + OnParsed: runtime.onParsed, + OnFinalize: func(_ streamengine.StopReason, _ error) { + }, + }) + terminalWritten := runtime.finalize(allowDeferEmpty) + if terminalWritten { + return true, false + } + return false, true +} + +//nolint:unused // retained for native Gemini stream handling path. +func newGeminiStreamRuntime( + w http.ResponseWriter, + rc *http.ResponseController, + canFlush bool, + model string, + finalPrompt string, + thinkingEnabled bool, + searchEnabled bool, + stripReferenceMarkers bool, + toolNames []string, + toolsRaw any, + history *responsehistory.Session, +) *geminiStreamRuntime { + return &geminiStreamRuntime{ + w: w, + rc: rc, + canFlush: canFlush, + model: model, + finalPrompt: finalPrompt, + thinkingEnabled: thinkingEnabled, + searchEnabled: searchEnabled, + bufferContent: len(toolNames) > 0, + stripReferenceMarkers: stripReferenceMarkers, + toolNames: toolNames, + toolsRaw: toolsRaw, + history: history, + accumulator: assistantturn.NewAccumulator(assistantturn.AccumulatorOptions{ + ThinkingEnabled: thinkingEnabled, + SearchEnabled: searchEnabled, + StripReferenceMarkers: stripReferenceMarkers, + }), + } +} + +//nolint:unused // retained for native Gemini stream handling path. +func (s *geminiStreamRuntime) sendChunk(payload map[string]any) { + b, _ := json.Marshal(payload) + _, _ = s.w.Write([]byte("data: ")) + _, _ = s.w.Write(b) + _, _ = s.w.Write([]byte("\n\n")) + if s.canFlush { + _ = s.rc.Flush() + } +} + +func (s *geminiStreamRuntime) sendErrorChunk(status int, message string) { + msg := strings.TrimSpace(message) + if msg == "" { + msg = http.StatusText(status) + } + errorStatus := "INVALID_ARGUMENT" + switch status { + case http.StatusUnauthorized: + errorStatus = "UNAUTHENTICATED" + case http.StatusForbidden: + errorStatus = "PERMISSION_DENIED" + case http.StatusTooManyRequests: + errorStatus = "RESOURCE_EXHAUSTED" + case http.StatusNotFound: + errorStatus = "NOT_FOUND" + default: + if status >= 500 { + errorStatus = "INTERNAL" + } + } + s.sendChunk(map[string]any{ + "error": map[string]any{ + "code": status, + "message": msg, + "status": errorStatus, + }, + }) +} + +//nolint:unused // retained for native Gemini stream handling path. +func (s *geminiStreamRuntime) onParsed(parsed sse.LineResult) streamengine.ParsedDecision { + if !parsed.Parsed { + return streamengine.ParsedDecision{} + } + if parsed.ResponseMessageID > 0 { + s.responseMessageID = parsed.ResponseMessageID + } + if parsed.ContentFilter || parsed.ErrorMessage != "" || parsed.Stop { + if parsed.ContentFilter { + s.contentFilter = true + } + return streamengine.ParsedDecision{Stop: true} + } + + accumulated := s.accumulator.Apply(parsed) + for _, p := range accumulated.Parts { + if p.Type == "thinking" { + if p.VisibleText == "" || s.bufferContent { + continue + } + s.sendChunk(map[string]any{ + "candidates": []map[string]any{ + { + "index": 0, + "content": map[string]any{ + "role": "model", + "parts": []map[string]any{{"text": p.VisibleText, "thought": true}}, + }, + }, + }, + "modelVersion": s.model, + }) + continue + } + if p.RawText == "" || p.CitationOnly || p.VisibleText == "" { + continue + } + if s.bufferContent { + continue + } + s.sendChunk(map[string]any{ + "candidates": []map[string]any{ + { + "index": 0, + "content": map[string]any{ + "role": "model", + "parts": []map[string]any{{"text": p.VisibleText}}, + }, + }, + }, + "modelVersion": s.model, + }) + } + if s.history != nil { + rawText, text, rawThinking, thinking, detectionThinking := s.accumulator.Snapshot() + s.history.Progress( + responsehistory.ThinkingForArchive(rawThinking, detectionThinking, thinking), + responsehistory.TextForArchive(rawText, text), + ) + } + return streamengine.ParsedDecision{ContentSeen: accumulated.ContentSeen} +} + +//nolint:unused // retained for native Gemini stream handling path. +func (s *geminiStreamRuntime) finalize(deferEmptyOutput bool) bool { + rawText, text, rawThinking, thinking, detectionThinking := s.accumulator.Snapshot() + turn := assistantturn.BuildTurnFromStreamSnapshot(assistantturn.StreamSnapshot{ + RawText: rawText, + VisibleText: text, + RawThinking: rawThinking, + VisibleThinking: thinking, + DetectionThinking: detectionThinking, + ContentFilter: s.contentFilter, + ResponseMessageID: s.responseMessageID, + }, assistantturn.BuildOptions{ + Model: s.model, + Prompt: s.finalPrompt, + SearchEnabled: s.searchEnabled, + StripReferenceMarkers: s.stripReferenceMarkers, + ToolNames: s.toolNames, + ToolsRaw: s.toolsRaw, + }) + outcome := assistantturn.FinalizeTurn(turn, assistantturn.FinalizeOptions{}) + if outcome.ShouldFail { + if deferEmptyOutput { + s.finalErrorStatus = outcome.Error.Status + s.finalErrorMessage = outcome.Error.Message + s.finalErrorCode = outcome.Error.Code + return false + } + if s.history != nil { + s.history.Error(outcome.Error.Status, outcome.Error.Message, outcome.Error.Code, responsehistory.ThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), responsehistory.TextForArchive(turn.RawText, turn.Text)) + } + s.sendErrorChunk(outcome.Error.Status, outcome.Error.Message) + return true + } + if s.history != nil { + s.history.Success( + http.StatusOK, + responsehistory.ThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), + responsehistory.TextForArchive(turn.RawText, turn.Text), + assistantturn.FinishReason(turn), + responsehistory.GenericUsage(turn), + ) + } + + if s.bufferContent { + parts := buildGeminiPartsFromTurn(turn) + s.sendChunk(map[string]any{ + "candidates": []map[string]any{ + { + "index": 0, + "content": map[string]any{ + "role": "model", + "parts": parts, + }, + }, + }, + "modelVersion": s.model, + }) + } + + s.sendChunk(map[string]any{ + "candidates": []map[string]any{ + { + "index": 0, + "content": map[string]any{ + "role": "model", + "parts": []map[string]any{ + {"text": ""}, + }, + }, + "finishReason": "STOP", + }, + }, + "modelVersion": s.model, + "usageMetadata": map[string]any{ + "promptTokenCount": outcome.Usage.InputTokens, + "candidatesTokenCount": outcome.Usage.OutputTokens, + "totalTokenCount": outcome.Usage.TotalTokens, + }, + }) + return true +} diff --git a/internal/httpapi/gemini/handler_test.go b/internal/httpapi/gemini/handler_test.go new file mode 100644 index 0000000000000000000000000000000000000000..52672287d540bc7232cd316d4c3acc596d172a92 --- /dev/null +++ b/internal/httpapi/gemini/handler_test.go @@ -0,0 +1,599 @@ +package gemini + +import ( + "bufio" + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + dsclient "ds2api/internal/deepseek/client" +) + +type testGeminiConfig struct{} + +func (testGeminiConfig) ModelAliases() map[string]string { return nil } +func (testGeminiConfig) CurrentInputFileEnabled() bool { return true } +func (testGeminiConfig) CurrentInputFileMinChars() int { return 0 } + +type testGeminiAuth struct { + a *auth.RequestAuth + err error +} + +func (m testGeminiAuth) Determine(_ *http.Request) (*auth.RequestAuth, error) { + if m.err != nil { + return nil, m.err + } + if m.a != nil { + return m.a, nil + } + return &auth.RequestAuth{ + UseConfigToken: false, + DeepSeekToken: "direct-token", + CallerID: "caller:test", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (testGeminiAuth) Release(_ *auth.RequestAuth) {} + +//nolint:unused // reserved test double for native Gemini DS-call path coverage. +type testGeminiDS struct { + resp *http.Response + err error + uploadCalls []dsclient.UploadFileRequest + payloads []map[string]any +} + +//nolint:unused // reserved test double for native Gemini DS-call path coverage. +func (m *testGeminiDS) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +//nolint:unused // reserved test double for native Gemini DS-call path coverage. +func (m *testGeminiDS) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +//nolint:unused // reserved test double for native Gemini DS-call path coverage. +func (m *testGeminiDS) UploadFile(_ context.Context, _ *auth.RequestAuth, req dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + m.uploadCalls = append(m.uploadCalls, req) + id := "file-gemini-history" + if len(m.uploadCalls) > 1 { + id = "file-gemini-tools" + } + return &dsclient.UploadFileResult{ID: id}, nil +} + +//nolint:unused // reserved test double for native Gemini DS-call path coverage. +func (m *testGeminiDS) CallCompletion(_ context.Context, _ *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + m.payloads = append(m.payloads, payload) + if m.err != nil { + return nil, m.err + } + return m.resp, nil +} + +type geminiOpenAIErrorStub struct { + status int + body string + headers map[string]string +} + +func (s geminiOpenAIErrorStub) ChatCompletions(w http.ResponseWriter, _ *http.Request) { + for k, v := range s.headers { + w.Header().Set(k, v) + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(s.status) + _, _ = w.Write([]byte(s.body)) +} + +type geminiOpenAISuccessStub struct { + stream bool + body string + seenReq map[string]any +} + +func (s *geminiOpenAISuccessStub) ChatCompletions(w http.ResponseWriter, r *http.Request) { + if r != nil { + var req map[string]any + _ = json.NewDecoder(r.Body).Decode(&req) + s.seenReq = req + } + if s.stream { + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl-1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hello \"},\"finish_reason\":null}]}\n\n")) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl-1\",\"object\":\"chat.completion.chunk\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"world\"},\"finish_reason\":\"stop\"}]}\n\n")) + _, _ = w.Write([]byte("data: [DONE]\n\n")) + return + } + out := s.body + if strings.TrimSpace(out) == "" { + out = `{"id":"chatcmpl-1","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","tool_calls":[{"id":"call_1","type":"function","function":{"name":"eval_javascript","arguments":"{\"code\":\"1+1\"}"}}]},"finish_reason":"tool_calls"}]}` + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(out)) +} + +//nolint:unused // helper retained for native Gemini stream fixture tests. +func makeGeminiUpstreamResponse(lines ...string) *http.Response { + body := strings.Join(lines, "\n") + if !strings.HasSuffix(body, "\n") { + body += "\n" + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +func TestGeminiDirectAppliesCurrentInputFile(t *testing.T) { + ds := &testGeminiDS{ + resp: makeGeminiUpstreamResponse(`data: {"p":"response/content","v":"ok"}`), + } + historyStore := chathistory.New(filepath.Join(t.TempDir(), "history.json")) + h := &Handler{ + Store: testGeminiConfig{}, + Auth: testGeminiAuth{}, + DS: ds, + ChatHistory: historyStore, + } + reqBody := `{"contents":[{"role":"user","parts":[{"text":"hello from gemini"}]}]}` + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent", strings.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r := chi.NewRouter() + RegisterRoutes(r, h) + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected one current input upload, got %d", len(ds.uploadCalls)) + } + if strings.Contains(strings.ToLower(ds.uploadCalls[0].Filename), "history") || !strings.HasSuffix(ds.uploadCalls[0].Filename, ".txt") { + t.Fatalf("unexpected upload filename: %q", ds.uploadCalls[0].Filename) + } + if len(ds.payloads) != 1 { + t.Fatalf("expected one completion payload, got %d", len(ds.payloads)) + } + refIDs, _ := ds.payloads[0]["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-gemini-history" { + t.Fatalf("expected uploaded history ref id, got %#v", ds.payloads[0]["ref_file_ids"]) + } + prompt, _ := ds.payloads[0]["prompt"].(string) + if !strings.Contains(prompt, ds.uploadCalls[0].Filename) { + t.Fatalf("expected continuation prompt, got %q", prompt) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot history: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + full, err := historyStore.Get(snapshot.Items[0].ID) + if err != nil { + t.Fatalf("get history item: %v", err) + } + if full.Surface != "gemini.generate_content" { + t.Fatalf("unexpected surface: %q", full.Surface) + } + if full.Content != "ok" { + t.Fatalf("expected raw upstream content, got %q", full.Content) + } + if full.HistoryText != string(ds.uploadCalls[0].Data) { + t.Fatalf("expected uploaded current input file to be persisted in history text") + } + if len(full.Messages) != 1 || !strings.Contains(full.Messages[0].Content, ".txt") { + t.Fatalf("expected persisted message to match upstream continuation prompt, got %#v", full.Messages) + } +} + +func TestGeminiCurrentInputFileUploadsToolsSeparately(t *testing.T) { + ds := &testGeminiDS{ + resp: makeGeminiUpstreamResponse(`data: {"p":"response/content","v":"ok"}`), + } + h := &Handler{ + Store: testGeminiConfig{}, + Auth: testGeminiAuth{}, + DS: ds, + } + reqBody := `{ + "contents":[{"role":"user","parts":[{"text":"run code"}]}], + "tools":[{"functionDeclarations":[{"name":"eval_javascript","description":"eval","parameters":{"type":"object","properties":{"code":{"type":"string"}}}}]}] + }` + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent", strings.NewReader(reqBody)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r := chi.NewRouter() + RegisterRoutes(r, h) + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 2 { + t.Fatalf("expected history and tools uploads, got %d", len(ds.uploadCalls)) + } + if strings.Contains(strings.ToLower(ds.uploadCalls[0].Filename), "history") || !strings.HasSuffix(ds.uploadCalls[0].Filename, ".txt") || ds.uploadCalls[1].Filename != "context_tools.txt" { + t.Fatalf("unexpected upload filenames: %#v", ds.uploadCalls) + } + historyText := string(ds.uploadCalls[0].Data) + if strings.Contains(historyText, "Description: eval") { + t.Fatalf("history transcript should not embed tool descriptions, got %q", historyText) + } + toolsText := string(ds.uploadCalls[1].Data) + if !strings.Contains(toolsText, "# context_tools.txt") || !strings.Contains(toolsText, "Tool: eval_javascript") || !strings.Contains(toolsText, "Description: eval") { + t.Fatalf("expected tools transcript to include Gemini tool schema, got %q", toolsText) + } + refIDs, _ := ds.payloads[0]["ref_file_ids"].([]any) + if len(refIDs) < 2 || refIDs[0] != "file-gemini-history" || refIDs[1] != "file-gemini-tools" { + t.Fatalf("expected history and tools ref ids first, got %#v", ds.payloads[0]["ref_file_ids"]) + } + prompt, _ := ds.payloads[0]["prompt"].(string) + if !strings.Contains(prompt, "context_tools.txt") || !strings.Contains(prompt, "TOOL CALL SCHEME") { + t.Fatalf("expected live prompt to reference tools file and retain format instructions, got %q", prompt) + } + if strings.Contains(prompt, "Description: eval") { + t.Fatalf("live prompt should not inline tool descriptions, got %q", prompt) + } +} + +func TestGeminiRoutesRegistered(t *testing.T) { + h := &Handler{ + Store: testGeminiConfig{}, + Auth: testGeminiAuth{err: auth.ErrUnauthorized}, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + + paths := []string{ + "/v1beta/models/gemini-2.5-pro:generateContent", + "/v1beta/models/gemini-2.5-pro:streamGenerateContent", + "/v1/models/gemini-2.5-pro:generateContent", + "/v1/models/gemini-2.5-pro:streamGenerateContent", + } + for _, path := range paths { + req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code == http.StatusNotFound { + t.Fatalf("expected route %s to be registered, got 404", path) + } + } +} + +func TestGenerateContentReturnsFunctionCallParts(t *testing.T) { + h := &Handler{ + Store: testGeminiConfig{}, + OpenAI: &geminiOpenAISuccessStub{ + body: `{"id":"chatcmpl-1","object":"chat.completion","choices":[{"index":0,"message":{"role":"assistant","tool_calls":[{"id":"call_1","type":"function","function":{"name":"eval_javascript","arguments":"{\"code\":\"1+1\"}"}}]},"finish_reason":"tool_calls"}]}`, + }, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + + body := `{ + "contents":[{"role":"user","parts":[{"text":"call tool"}]}], + "tools":[{"functionDeclarations":[{"name":"eval_javascript","description":"eval","parameters":{"type":"object","properties":{"code":{"type":"string"}}}}]}] + }` + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent", strings.NewReader(body)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v", err) + } + candidates, _ := out["candidates"].([]any) + if len(candidates) == 0 { + t.Fatalf("expected non-empty candidates: %#v", out) + } + c0, _ := candidates[0].(map[string]any) + content, _ := c0["content"].(map[string]any) + parts, _ := content["parts"].([]any) + if len(parts) == 0 { + t.Fatalf("expected non-empty parts: %#v", content) + } + part0, _ := parts[0].(map[string]any) + functionCall, _ := part0["functionCall"].(map[string]any) + if functionCall["name"] != "eval_javascript" { + t.Fatalf("expected functionCall name eval_javascript, got %#v", functionCall) + } +} + +func TestGenerateContentMixedToolSnippetAlsoTriggersFunctionCall(t *testing.T) { + h := &Handler{Store: testGeminiConfig{}, OpenAI: &geminiOpenAISuccessStub{}} + r := chi.NewRouter() + RegisterRoutes(r, h) + + body := `{ + "contents":[{"role":"user","parts":[{"text":"call tool"}]}], + "tools":[{"functionDeclarations":[{"name":"eval_javascript","description":"eval","parameters":{"type":"object","properties":{"code":{"type":"string"}}}}]}] + }` + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent", strings.NewReader(body)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v", err) + } + candidates, _ := out["candidates"].([]any) + c0, _ := candidates[0].(map[string]any) + content, _ := c0["content"].(map[string]any) + parts, _ := content["parts"].([]any) + part0, _ := parts[0].(map[string]any) + functionCall, _ := part0["functionCall"].(map[string]any) + if functionCall["name"] != "eval_javascript" { + t.Fatalf("expected functionCall name eval_javascript for mixed snippet, got %#v", functionCall) + } +} + +func TestStreamGenerateContentEmitsSSE(t *testing.T) { + h := &Handler{ + Store: testGeminiConfig{}, + OpenAI: &geminiOpenAISuccessStub{stream: true}, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + + body := `{"contents":[{"role":"user","parts":[{"text":"hello"}]}]}` + req := httptest.NewRequest(http.MethodPost, "/v1/models/gemini-2.5-pro:streamGenerateContent?alt=sse", strings.NewReader(body)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + + frames := extractGeminiSSEFrames(t, rec.Body.String()) + if len(frames) == 0 { + t.Fatalf("expected non-empty stream frames, body=%s", rec.Body.String()) + } + last := frames[len(frames)-1] + candidates, _ := last["candidates"].([]any) + if len(candidates) == 0 { + t.Fatalf("expected finish frame candidates, got %#v", last) + } + c0, _ := candidates[0].(map[string]any) + content, _ := c0["content"].(map[string]any) + if content == nil { + t.Fatalf("expected non-null content in finish frame, got %#v", c0) + } + parts, _ := content["parts"].([]any) + if len(parts) == 0 { + t.Fatalf("expected non-empty parts in finish frame content, got %#v", content) + } +} + +func TestNativeStreamGenerateContentEmitsThoughtParts(t *testing.T) { + h := &Handler{} + resp := makeGeminiUpstreamResponse( + `data: {"p":"response/thinking_content","v":"think"}`, + `data: {"p":"response/content","v":"answer"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:streamGenerateContent", nil) + + h.handleStreamGenerateContent(rec, req, resp, "gemini-2.5-pro", "prompt", true, false, nil, nil) + + frames := extractGeminiSSEFrames(t, rec.Body.String()) + if len(frames) < 2 { + t.Fatalf("expected thought and text stream frames, body=%s", rec.Body.String()) + } + var gotThought, gotText string + for _, frame := range frames { + for _, part := range geminiPartsFromFrame(frame) { + if part["thought"] == true { + gotThought += asString(part["text"]) + } else { + gotText += asString(part["text"]) + } + } + } + if gotThought != "think" { + t.Fatalf("expected thought part, got %q body=%s", gotThought, rec.Body.String()) + } + if !strings.Contains(gotText, "answer") { + t.Fatalf("expected text part answer, got %q body=%s", gotText, rec.Body.String()) + } +} + +func TestBuildGeminiPartsFromFinalIncludesThoughtPart(t *testing.T) { + parts := buildGeminiPartsFromFinal("answer", "think", nil) + if len(parts) != 2 { + t.Fatalf("expected thought + answer parts, got %#v", parts) + } + if parts[0]["thought"] != true || parts[0]["text"] != "think" { + t.Fatalf("expected first part to be thought, got %#v", parts[0]) + } + if _, ok := parts[1]["thought"]; ok { + t.Fatalf("expected second part to be visible text, got %#v", parts[1]) + } + if parts[1]["text"] != "answer" { + t.Fatalf("expected answer text, got %#v", parts[1]) + } +} + +func TestGeminiProxyTranslatesInlineImageToOpenAIDataURL(t *testing.T) { + openAI := &geminiOpenAISuccessStub{} + h := &Handler{Store: testGeminiConfig{}, OpenAI: openAI} + r := chi.NewRouter() + RegisterRoutes(r, h) + + body := `{"contents":[{"role":"user","parts":[{"text":"hello"},{"inlineData":{"mimeType":"image/png","data":"QUJDRA=="}}]}]}` + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:generateContent", strings.NewReader(body)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + messages, _ := openAI.seenReq["messages"].([]any) + if len(messages) != 1 { + t.Fatalf("expected one translated message, got %#v", openAI.seenReq) + } + msg, _ := messages[0].(map[string]any) + content, _ := msg["content"].([]any) + if len(content) != 2 { + t.Fatalf("expected translated content blocks, got %#v", msg) + } + imageBlock, _ := content[1].(map[string]any) + if strings.TrimSpace(asString(imageBlock["type"])) != "image_url" { + t.Fatalf("expected image_url block, got %#v", imageBlock) + } + imageURL, _ := imageBlock["image_url"].(map[string]any) + if !strings.HasPrefix(strings.TrimSpace(asString(imageURL["url"])), "data:image/png;base64,") { + t.Fatalf("expected translated data url, got %#v", imageBlock) + } +} + +func TestGeminiProxyViaOpenAIDisablesThinkingBudgetZero(t *testing.T) { + openAI := &geminiOpenAISuccessStub{} + h := &Handler{Store: testGeminiConfig{}, OpenAI: openAI} + r := chi.NewRouter() + RegisterRoutes(r, h) + + body := `{"contents":[{"role":"user","parts":[{"text":"hello"}]}],"generationConfig":{"thinkingConfig":{"thinkingBudget":0}}}` + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-flash:generateContent", strings.NewReader(body)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + thinking, _ := openAI.seenReq["thinking"].(map[string]any) + if thinking["type"] != "disabled" { + t.Fatalf("expected Gemini thinkingBudget=0 to disable OpenAI thinking, got %#v", openAI.seenReq) + } +} + +func TestGeminiProxyViaOpenAIEnablesPositiveThinkingBudget(t *testing.T) { + openAI := &geminiOpenAISuccessStub{} + h := &Handler{Store: testGeminiConfig{}, OpenAI: openAI} + r := chi.NewRouter() + RegisterRoutes(r, h) + + body := `{"contents":[{"role":"user","parts":[{"text":"hello"}]}],"generationConfig":{"thinkingConfig":{"thinkingBudget":1024}}}` + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-flash:generateContent", strings.NewReader(body)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + thinking, _ := openAI.seenReq["thinking"].(map[string]any) + if thinking["type"] != "enabled" { + t.Fatalf("expected Gemini positive thinkingBudget to enable OpenAI thinking, got %#v", openAI.seenReq) + } +} + +func TestGenerateContentOpenAIProxyErrorUsesGeminiEnvelope(t *testing.T) { + h := &Handler{ + Store: testGeminiConfig{}, + OpenAI: geminiOpenAIErrorStub{ + status: http.StatusUnauthorized, + body: `{"error":{"message":"invalid api key"}}`, + headers: map[string]string{ + "WWW-Authenticate": `Bearer realm="example"`, + "Retry-After": "30", + "X-RateLimit-Remaining": "0", + }, + }, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + + req := httptest.NewRequest(http.MethodPost, "/v1/models/gemini-2.5-pro:generateContent", strings.NewReader(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`)) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusUnauthorized { + t.Fatalf("expected 401, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("expected json body: %v", err) + } + errObj, _ := out["error"].(map[string]any) + if errObj["status"] != "UNAUTHENTICATED" { + t.Fatalf("expected Gemini status UNAUTHENTICATED, got=%v", errObj["status"]) + } + if errObj["message"] != "invalid api key" { + t.Fatalf("expected parsed error message, got=%v", errObj["message"]) + } + if got := rec.Header().Get("WWW-Authenticate"); got == "" { + t.Fatalf("expected WWW-Authenticate header to be preserved") + } + if got := rec.Header().Get("Retry-After"); got != "30" { + t.Fatalf("expected Retry-After header 30, got=%q", got) + } + if got := rec.Header().Get("X-RateLimit-Remaining"); got != "0" { + t.Fatalf("expected X-RateLimit-Remaining header 0, got=%q", got) + } +} + +func extractGeminiSSEFrames(t *testing.T, body string) []map[string]any { + t.Helper() + scanner := bufio.NewScanner(strings.NewReader(body)) + out := make([]map[string]any, 0, 4) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + raw := line + if strings.HasPrefix(line, "data: ") { + raw = strings.TrimSpace(strings.TrimPrefix(line, "data: ")) + } + if raw == "" { + continue + } + var frame map[string]any + if err := json.Unmarshal([]byte(raw), &frame); err != nil { + continue + } + out = append(out, frame) + } + return out +} + +func geminiPartsFromFrame(frame map[string]any) []map[string]any { + candidates, _ := frame["candidates"].([]any) + if len(candidates) == 0 { + return nil + } + c0, _ := candidates[0].(map[string]any) + content, _ := c0["content"].(map[string]any) + rawParts, _ := content["parts"].([]any) + parts := make([]map[string]any, 0, len(rawParts)) + for _, raw := range rawParts { + part, _ := raw.(map[string]any) + if part != nil { + parts = append(parts, part) + } + } + return parts +} diff --git a/internal/httpapi/gemini/output_clean.go b/internal/httpapi/gemini/output_clean.go new file mode 100644 index 0000000000000000000000000000000000000000..4dff3210eb54c9cabdc715fec1a3b460ca0b3ff1 --- /dev/null +++ b/internal/httpapi/gemini/output_clean.go @@ -0,0 +1,14 @@ +package gemini + +import textclean "ds2api/internal/textclean" + +//nolint:unused // retained for native Gemini output post-processing path. +func cleanVisibleOutput(text string, stripReferenceMarkers bool) string { + if text == "" { + return text + } + if stripReferenceMarkers { + text = textclean.StripReferenceMarkers(text) + } + return text +} diff --git a/internal/httpapi/gemini/proxy_vercel_test.go b/internal/httpapi/gemini/proxy_vercel_test.go new file mode 100644 index 0000000000000000000000000000000000000000..4b146bc17ce38fa66101d973c83f94ba16798910 --- /dev/null +++ b/internal/httpapi/gemini/proxy_vercel_test.go @@ -0,0 +1,42 @@ +package gemini + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +type openAIProxyStub struct { + status int + body string +} + +func (s openAIProxyStub) ChatCompletions(w http.ResponseWriter, _ *http.Request) { + if s.status == 0 { + s.status = http.StatusOK + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(s.status) + _, _ = w.Write([]byte(s.body)) +} + +func TestGeminiProxyViaOpenAIVercelReleasePassthrough(t *testing.T) { + h := &Handler{OpenAI: openAIProxyStub{status: 200, body: `{"success":true}`}} + req := httptest.NewRequest(http.MethodPost, "/v1beta/models/gemini-2.5-pro:streamGenerateContent?__stream_release=1", strings.NewReader(`{"lease_id":"lease_123"}`)) + rec := httptest.NewRecorder() + + h.StreamGenerateContent(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("expected json response, got err=%v body=%s", err, rec.Body.String()) + } + if v, ok := out["success"].(bool); !ok || !v { + t.Fatalf("expected success=true passthrough, got=%v", out) + } +} diff --git a/internal/httpapi/ollama/handler_routes.go b/internal/httpapi/ollama/handler_routes.go new file mode 100644 index 0000000000000000000000000000000000000000..fb64a064d4c2ef87cc534bc27bc283a412ca9ab4 --- /dev/null +++ b/internal/httpapi/ollama/handler_routes.go @@ -0,0 +1,58 @@ +package ollama + +import ( + "ds2api/internal/config" + "ds2api/internal/util" + "encoding/json" + "github.com/go-chi/chi/v5" + "log/slog" + "net/http" +) + +var WriteJSON = util.WriteJSON + +type ConfigReader interface { + ModelAliases() map[string]string +} + +type Handler struct { + Store ConfigReader +} + +type OllamaModelRequest struct { + Model string `json:"model"` +} + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/api/version", h.GetVersion) + r.Get("/api/tags", h.ListOllamaModels) + r.Post("/api/show", h.GetOllamaModel) +} + +func (h *Handler) GetVersion(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"version":"0.23.1"}`)) +} +func (h *Handler) ListOllamaModels(w http.ResponseWriter, r *http.Request) { + WriteJSON(w, http.StatusOK, config.OllamaModelsResponse()) +} +func (h *Handler) GetOllamaModel(w http.ResponseWriter, r *http.Request) { + var payload OllamaModelRequest + if err := json.NewDecoder(r.Body).Decode(&payload); err != nil { + http.Error(w, "Invalid JSON body: "+err.Error(), http.StatusBadRequest) + return + } + defer func() { + if err := r.Body.Close(); err != nil { + slog.Warn("[ollama] failed to close request body", "error", err) + } + }() + modelID := payload.Model + model, ok := config.OllamaModelByID(h.Store, modelID) + if !ok { + http.Error(w, "Model not found.", http.StatusNotFound) + return + } + WriteJSON(w, http.StatusOK, model) +} diff --git a/internal/httpapi/ollama/handler_routes_test.go b/internal/httpapi/ollama/handler_routes_test.go new file mode 100644 index 0000000000000000000000000000000000000000..0d227790c2c85a824294cebb20b71b36cb19a5cf --- /dev/null +++ b/internal/httpapi/ollama/handler_routes_test.go @@ -0,0 +1,127 @@ +package ollama + +import ( + "encoding/json" + "github.com/go-chi/chi/v5" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +type ollamaTestSurface struct { + Store ConfigReader + handler *Handler +} + +func (h *ollamaTestSurface) apiHandler() *Handler { + if h.handler == nil { + h.handler = &Handler{Store: h.Store} + } + return h.handler +} + +func registerOllamaTestRoutes(r chi.Router, h *ollamaTestSurface) { + r.Get("/api/version", h.apiHandler().GetVersion) + r.Get("/api/tags", h.apiHandler().ListOllamaModels) + r.Post("/api/show", h.apiHandler().GetOllamaModel) +} + +func TestGetOllamaVersionRoute(t *testing.T) { + h := &ollamaTestSurface{} + r := chi.NewRouter() + registerOllamaTestRoutes(r, h) + req := httptest.NewRequest(http.MethodGet, "/api/version", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } +} + +func TestGetOllamaModelsRoute(t *testing.T) { + h := &ollamaTestSurface{} + r := chi.NewRouter() + registerOllamaTestRoutes(r, h) + req := httptest.NewRequest(http.MethodGet, "/api/tags", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } +} + +func TestGetOllamaModelRoute(t *testing.T) { + h := &ollamaTestSurface{} + r := chi.NewRouter() + registerOllamaTestRoutes(r, h) + + t.Run("direct", func(t *testing.T) { + body := `{"model":"deepseek-v4-flash"}` + req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var payload map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil { + t.Fatalf("expected valid json body, got err=%v body=%s", err, rec.Body.String()) + } + if _, ok := payload["id"]; !ok { + t.Fatalf("expected response has lowercase id field, body=%s", rec.Body.String()) + } + if _, ok := payload["ID"]; ok { + t.Fatalf("expected response does not expose uppercase ID field, body=%s", rec.Body.String()) + } + }) + + t.Run("direct_nothinking", func(t *testing.T) { + body := `{"model":"deepseek-v4-flash-nothinking"}` + req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("direct_expert", func(t *testing.T) { + body := `{"model":"deepseek-v4-pro"}` + req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("direct_vision", func(t *testing.T) { + body := `{"model":"deepseek-v4-vision"}` + req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + }) +} + +func TestGetOllamaModelRouteNotFound(t *testing.T) { + h := &ollamaTestSurface{} + r := chi.NewRouter() + registerOllamaTestRoutes(r, h) + + body := `{"model":"not-exists"}` + req := httptest.NewRequest(http.MethodPost, "/api/show", strings.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusNotFound { + t.Fatalf("expected 404, got %d body=%s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/httpapi/openai/chat/chat_history.go b/internal/httpapi/openai/chat/chat_history.go new file mode 100644 index 0000000000000000000000000000000000000000..fe97a69c4bd8b61aef3886e2fae0ed85e146c67b --- /dev/null +++ b/internal/httpapi/openai/chat/chat_history.go @@ -0,0 +1,268 @@ +package chat + +import ( + "errors" + "net/http" + "strings" + "time" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/config" + openaifmt "ds2api/internal/format/openai" + "ds2api/internal/prompt" + "ds2api/internal/promptcompat" +) + +type chatHistorySession struct { + store *chathistory.Store + entryID string + startedAt time.Time + lastPersist time.Time + finalPrompt string + startParams chathistory.StartParams + disabled bool +} + +func startChatHistory(store *chathistory.Store, r *http.Request, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) *chatHistorySession { + if store == nil || r == nil || a == nil { + return nil + } + if !store.Enabled() { + return nil + } + if !shouldCaptureChatHistory(r) { + return nil + } + entry, err := store.Start(chathistory.StartParams{ + CallerID: strings.TrimSpace(a.CallerID), + AccountID: strings.TrimSpace(a.AccountID), + Surface: "openai.chat_completions", + Model: strings.TrimSpace(stdReq.ResponseModel), + Stream: stdReq.Stream, + UserInput: extractSingleUserInput(stdReq.Messages), + Messages: extractAllMessages(stdReq.Messages), + HistoryText: stdReq.HistoryText, + FinalPrompt: stdReq.FinalPrompt, + }) + startParams := chathistory.StartParams{ + CallerID: strings.TrimSpace(a.CallerID), + AccountID: strings.TrimSpace(a.AccountID), + Surface: "openai.chat_completions", + Model: strings.TrimSpace(stdReq.ResponseModel), + Stream: stdReq.Stream, + UserInput: extractSingleUserInput(stdReq.Messages), + Messages: extractAllMessages(stdReq.Messages), + HistoryText: stdReq.HistoryText, + FinalPrompt: stdReq.FinalPrompt, + } + session := &chatHistorySession{ + store: store, + entryID: entry.ID, + startedAt: time.Now(), + lastPersist: time.Now(), + finalPrompt: stdReq.FinalPrompt, + startParams: startParams, + } + if err != nil { + if entry.ID == "" { + config.Logger.Warn("[chat_history] start failed", "error", err) + return nil + } + config.Logger.Warn("[chat_history] start persisted in memory after write failure", "error", err) + } + return session +} + +func shouldCaptureChatHistory(r *http.Request) bool { + if r == nil { + return false + } + if isVercelStreamPrepareRequest(r) || isVercelStreamReleaseRequest(r) { + return false + } + return true +} + +func extractSingleUserInput(messages []any) string { + for i := len(messages) - 1; i >= 0; i-- { + msg, ok := messages[i].(map[string]any) + if !ok { + continue + } + role := strings.ToLower(strings.TrimSpace(asString(msg["role"]))) + if role != "user" { + continue + } + if normalized := strings.TrimSpace(prompt.NormalizeContent(msg["content"])); normalized != "" { + return normalized + } + } + return "" +} + +func extractAllMessages(messages []any) []chathistory.Message { + out := make([]chathistory.Message, 0, len(messages)) + for _, raw := range messages { + msg, ok := raw.(map[string]any) + if !ok { + continue + } + role := strings.ToLower(strings.TrimSpace(asString(msg["role"]))) + content := strings.TrimSpace(prompt.NormalizeContent(msg["content"])) + if role == "" || content == "" { + continue + } + out = append(out, chathistory.Message{ + Role: role, + Content: content, + }) + } + return out +} + +func (s *chatHistorySession) progress(thinking, content string) { + if s == nil || s.store == nil || s.disabled { + return + } + now := time.Now() + if now.Sub(s.lastPersist) < 250*time.Millisecond { + return + } + s.lastPersist = now + s.persistUpdate(chathistory.UpdateParams{ + Status: "streaming", + ReasoningContent: thinking, + Content: content, + StatusCode: http.StatusOK, + ElapsedMs: time.Since(s.startedAt).Milliseconds(), + }) +} + +func (s *chatHistorySession) success(statusCode int, thinking, content, finishReason string, usage map[string]any) { + if s == nil || s.store == nil || s.disabled { + return + } + s.persistUpdate(chathistory.UpdateParams{ + Status: "success", + ReasoningContent: thinking, + Content: content, + StatusCode: statusCode, + ElapsedMs: time.Since(s.startedAt).Milliseconds(), + FinishReason: finishReason, + Usage: usage, + Completed: true, + }) +} + +func (s *chatHistorySession) error(statusCode int, message, finishReason, thinking, content string) { + if s == nil || s.store == nil || s.disabled { + return + } + s.persistUpdate(chathistory.UpdateParams{ + Status: "error", + ReasoningContent: thinking, + Content: content, + Error: message, + StatusCode: statusCode, + ElapsedMs: time.Since(s.startedAt).Milliseconds(), + FinishReason: finishReason, + Completed: true, + }) +} + +func (s *chatHistorySession) stopped(thinking, content, finishReason string) { + if s == nil || s.store == nil || s.disabled { + return + } + s.persistUpdate(chathistory.UpdateParams{ + Status: "stopped", + ReasoningContent: thinking, + Content: content, + StatusCode: http.StatusOK, + ElapsedMs: time.Since(s.startedAt).Milliseconds(), + FinishReason: finishReason, + Usage: openaifmt.BuildChatUsage(s.finalPrompt, thinking, content), + Completed: true, + }) +} + +func historyTextForArchive(raw, visible string) string { + if strings.TrimSpace(raw) != "" { + return raw + } + return visible +} + +func historyThinkingForArchive(raw, detection, visible string) string { + if strings.TrimSpace(raw) != "" { + return raw + } + if strings.TrimSpace(detection) != "" { + return detection + } + return visible +} + +func (s *chatHistorySession) retryMissingEntry() bool { + if s == nil || s.store == nil || s.disabled { + return false + } + entry, err := s.store.Start(s.startParams) + if errors.Is(err, chathistory.ErrDisabled) { + s.disabled = true + return false + } + if entry.ID == "" { + if err != nil { + config.Logger.Warn("[chat_history] recreate missing entry failed", "error", err) + } + return false + } + s.entryID = entry.ID + if err != nil { + config.Logger.Warn("[chat_history] recreate missing entry persisted in memory after write failure", "error", err) + } + return true +} + +func (s *chatHistorySession) persistUpdate(params chathistory.UpdateParams) { + if s == nil || s.store == nil || s.disabled { + return + } + if _, err := s.store.Update(s.entryID, params); err != nil { + s.handlePersistError(params, err) + } +} + +func (s *chatHistorySession) handlePersistError(params chathistory.UpdateParams, err error) { + if err == nil || s == nil { + return + } + if errors.Is(err, chathistory.ErrDisabled) { + s.disabled = true + return + } + if isChatHistoryMissingError(err) { + if s.retryMissingEntry() { + if _, retryErr := s.store.Update(s.entryID, params); retryErr != nil { + if errors.Is(retryErr, chathistory.ErrDisabled) || isChatHistoryMissingError(retryErr) { + s.disabled = true + return + } + config.Logger.Warn("[chat_history] retry after missing entry failed", "error", retryErr) + } + return + } + s.disabled = true + return + } + config.Logger.Warn("[chat_history] update failed", "error", err) +} + +func isChatHistoryMissingError(err error) bool { + if err == nil { + return false + } + return strings.Contains(strings.ToLower(err.Error()), "not found") +} diff --git a/internal/httpapi/openai/chat/chat_history_test.go b/internal/httpapi/openai/chat/chat_history_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c794f6abcbb1a36446ca496ed03c00ea5eecf3b9 --- /dev/null +++ b/internal/httpapi/openai/chat/chat_history_test.go @@ -0,0 +1,406 @@ +package chat + +import ( + "context" + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strconv" + "strings" + "sync" + "testing" + "time" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/promptcompat" +) + +func newTestChatHistoryStore(t *testing.T) *chathistory.Store { + t.Helper() + store := chathistory.New(filepath.Join(t.TempDir(), "chat_history.json")) + if err := store.Err(); err != nil { + t.Fatalf("chat history store unavailable: %v", err) + } + return store +} + +func blockChatHistoryDetailDir(t *testing.T, detailDir string) func() { + t.Helper() + blockedDir := detailDir + ".blocked" + if err := os.RemoveAll(blockedDir); err != nil { + t.Fatalf("remove blocked detail dir failed: %v", err) + } + if err := os.Rename(detailDir, blockedDir); err != nil { + t.Fatalf("move detail dir aside failed: %v", err) + } + if err := os.RemoveAll(detailDir); err != nil { + t.Fatalf("remove blocked detail path failed: %v", err) + } + if err := os.WriteFile(detailDir, []byte("blocked"), 0o644); err != nil { + t.Fatalf("write blocked detail path failed: %v", err) + } + var once sync.Once + return func() { + t.Helper() + once.Do(func() { + if err := os.RemoveAll(detailDir); err != nil { + t.Fatalf("remove blocking detail path failed: %v", err) + } + if err := os.Rename(blockedDir, detailDir); err != nil { + t.Fatalf("restore detail dir failed: %v", err) + } + }) + } +} + +func TestChatCompletionsNonStreamPersistsHistory(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + h := &Handler{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"hello world"}`, `data: [DONE]`)}, + ChatHistory: historyStore, + } + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"system","content":"be precise"},{"role":"user","content":"hi there"},{"role":"assistant","content":"previous answer"}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + item := snapshot.Items[0] + if item.Status != "success" || item.UserInput != "hi there" { + t.Fatalf("unexpected persisted history summary: %#v", item) + } + full, err := historyStore.Get(item.ID) + if err != nil { + t.Fatalf("expected detail item, got %v", err) + } + if full.Content != "hello world" { + t.Fatalf("expected detail content persisted, got %#v", full) + } + if len(full.Messages) != 3 { + t.Fatalf("expected all request messages persisted, got %#v", full.Messages) + } + if full.FinalPrompt == "" { + t.Fatalf("expected final prompt to be persisted") + } + if item.CallerID != "caller:test" { + t.Fatalf("expected caller hash persisted in summary, got %#v", item.CallerID) + } +} + +func TestChatHistoryNonStreamArchivesRawToolCallMarkup(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + entry, err := historyStore.Start(chathistory.StartParams{ + CallerID: "caller:test", + Model: "deepseek-v4-flash", + UserInput: "call tool", + }) + if err != nil { + t.Fatalf("start history failed: %v", err) + } + session := &chatHistorySession{ + store: historyStore, + entryID: entry.ID, + startedAt: time.Now(), + lastPersist: time.Now().Add(-time.Second), + finalPrompt: "call tool", + } + rawToolCall := `golang` + + h := &Handler{} + rec := httptest.NewRecorder() + resp := makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":`+strconv.Quote(rawToolCall)+`}`, `data: [DONE]`) + h.handleNonStream(rec, resp, "cid-tool-history", "deepseek-v4-flash", "prompt", 0, false, false, []string{"search"}, nil, session) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + full, err := historyStore.Get(entry.ID) + if err != nil { + t.Fatalf("get detail failed: %v", err) + } + if full.Content != rawToolCall { + t.Fatalf("expected raw tool markup archived, got %q", full.Content) + } + if full.FinishReason != "tool_calls" { + t.Fatalf("expected tool_calls finish reason, got %#v", full.FinishReason) + } +} + +func TestChatHistoryStreamArchivesRawToolCallMarkup(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + entry, err := historyStore.Start(chathistory.StartParams{ + CallerID: "caller:test", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "call tool", + }) + if err != nil { + t.Fatalf("start history failed: %v", err) + } + session := &chatHistorySession{ + store: historyStore, + entryID: entry.ID, + startedAt: time.Now(), + lastPersist: time.Now().Add(-time.Second), + finalPrompt: "call tool", + } + rawToolCall := `golang` + + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + rec := httptest.NewRecorder() + resp := makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":`+strconv.Quote(rawToolCall)+`}`, `data: [DONE]`) + h.handleStream(rec, req, resp, "cid-stream-tool-history", "deepseek-v4-flash", "prompt", 0, false, false, []string{"search"}, nil, session) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + full, err := historyStore.Get(entry.ID) + if err != nil { + t.Fatalf("get detail failed: %v", err) + } + if full.Content != rawToolCall { + t.Fatalf("expected raw streamed tool markup archived, got %q", full.Content) + } + if full.FinishReason != "tool_calls" { + t.Fatalf("expected tool_calls finish reason, got %#v", full.FinishReason) + } +} + +func TestStartChatHistoryRecoversFromTransientWriteFailure(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + restore := blockChatHistoryDetailDir(t, historyStore.DetailDir()) + t.Cleanup(restore) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + a := &auth.RequestAuth{ + CallerID: "caller:test", + AccountID: "acct:test", + } + stdReq := promptcompat.StandardRequest{ + ResponseModel: "deepseek-v4-flash", + Stream: true, + Messages: []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + FinalPrompt: "hello", + } + + session := startChatHistory(historyStore, req, a, stdReq) + if session == nil { + t.Fatalf("expected session even when initial persistence fails") + return + } + if session.disabled { + t.Fatalf("expected session to remain active after transient start failure") + } + if session.entryID == "" { + t.Fatalf("expected session entry id to be retained") + } + if err := historyStore.Err(); err != nil { + t.Fatalf("transient start failure should not latch store error: %v", err) + } + + session.lastPersist = time.Now().Add(-time.Second) + session.progress("thinking", "partial") + if session.disabled { + t.Fatalf("expected session to remain active after transient update failure") + } + if session.entryID == "" { + t.Fatalf("expected session entry id to remain set after update failure") + } + if err := historyStore.Err(); err != nil { + t.Fatalf("transient update failure should not latch store error: %v", err) + } + + restore() + + session.success(http.StatusOK, "thinking", "final answer", "stop", map[string]any{"total_tokens": 7}) + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed after restore: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one persisted item after restore, got %#v", snapshot.Items) + } + full, err := historyStore.Get(session.entryID) + if err != nil { + t.Fatalf("get restored entry failed: %v", err) + } + if full.Status != "success" || full.Content != "final answer" { + t.Fatalf("expected restored entry to persist final success, got %#v", full) + } +} + +func TestHandleStreamContextCancelledMarksHistoryStopped(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + entry, err := historyStore.Start(chathistory.StartParams{ + CallerID: "caller:test", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "hello", + }) + if err != nil { + t.Fatalf("start history failed: %v", err) + } + session := &chatHistorySession{ + store: historyStore, + entryID: entry.ID, + startedAt: time.Now(), + lastPersist: time.Now(), + finalPrompt: "hello", + } + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil).WithContext(ctx) + rec := httptest.NewRecorder() + resp := makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"hello"}`, `data: [DONE]`) + + h.handleStream(rec, req, resp, "cid-stop", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, session) + + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + full, err := historyStore.Get(snapshot.Items[0].ID) + if err != nil { + t.Fatalf("get detail failed: %v", err) + } + if full.Status != "stopped" { + t.Fatalf("expected stopped status, got %#v", full) + } +} + +func TestChatCompletionsRecordsAdminWebUISource(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + h := &Handler{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"hello world"}`, `data: [DONE]`)}, + ChatHistory: historyStore, + } + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi there"}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Ds2-Source", "admin-webui-api-tester") + rec := httptest.NewRecorder() + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected admin webui source to be recorded, got %#v", snapshot.Items) + } +} + +func TestChatCompletionsSkipsHistoryWhenDisabled(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + if _, err := historyStore.SetLimit(chathistory.DisabledLimit); err != nil { + t.Fatalf("disable history store failed: %v", err) + } + h := &Handler{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"hello world"}`, `data: [DONE]`)}, + ChatHistory: historyStore, + } + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi there"}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 0 { + t.Fatalf("expected disabled history to stay empty, got %#v", snapshot.Items) + } +} + +func TestChatCompletionsCurrentInputFilePersistsNeutralPrompt(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + ds := &inlineUploadDSStub{} + h := &Handler{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + ChatHistory: historyStore, + } + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"system","content":"system instructions"},{"role":"user","content":"first user turn"},{"role":"assistant","content":"","reasoning_content":"hidden reasoning","tool_calls":[{"name":"search","arguments":{"query":"docs"}}]},{"role":"tool","name":"search","tool_call_id":"call-1","content":"tool result"},{"role":"user","content":"latest user turn"}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + full, err := historyStore.Get(snapshot.Items[0].ID) + if err != nil { + t.Fatalf("expected detail item, got %v", err) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected current input upload to happen, got %d", len(ds.uploadCalls)) + } + if strings.Contains(strings.ToLower(ds.uploadCalls[0].Filename), "history") || !strings.HasSuffix(ds.uploadCalls[0].Filename, ".txt") { + t.Fatalf("unexpected history upload filename, got %q", ds.uploadCalls[0].Filename) + } + if full.HistoryText != string(ds.uploadCalls[0].Data) { + t.Fatalf("expected uploaded current input file to be persisted in history text") + } + if len(full.Messages) != 1 { + t.Fatalf("expected continuation prompt to be the only persisted message, got %#v", full.Messages) + } + if !strings.Contains(full.Messages[0].Content, ".txt") { + t.Fatalf("expected continuation prompt to be persisted, got %#v", full.Messages[0]) + } +} diff --git a/internal/httpapi/openai/chat/chat_stream_runtime.go b/internal/httpapi/openai/chat/chat_stream_runtime.go new file mode 100644 index 0000000000000000000000000000000000000000..2b04853407830f93037adb98dc4c26ac4a5df20a --- /dev/null +++ b/internal/httpapi/openai/chat/chat_stream_runtime.go @@ -0,0 +1,383 @@ +package chat + +import ( + "encoding/json" + "net/http" + "strings" + + "ds2api/internal/assistantturn" + openaifmt "ds2api/internal/format/openai" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" + "ds2api/internal/sse" + streamengine "ds2api/internal/stream" + "ds2api/internal/toolstream" +) + +type chatStreamRuntime struct { + w http.ResponseWriter + rc *http.ResponseController + canFlush bool + + completionID string + created int64 + model string + finalPrompt string + refFileTokens int + toolNames []string + toolsRaw any + toolChoice promptcompat.ToolChoicePolicy + + thinkingEnabled bool + searchEnabled bool + stripReferenceMarkers bool + + firstChunkSent bool + bufferToolContent bool + emitEarlyToolDeltas bool + toolCallsEmitted bool + toolCallsDoneEmitted bool + + toolSieve toolstream.State + streamToolCallIDs map[int]string + streamToolNames map[int]string + accumulator shared.StreamAccumulator + responseMessageID int + + finalThinking string + finalText string + finalFinishReason string + finalUsage map[string]any + finalErrorStatus int + finalErrorMessage string + finalErrorCode string +} + +type chatDeltaBatch struct { + runtime *chatStreamRuntime + field string + text strings.Builder +} + +func (b *chatDeltaBatch) append(field, text string) { + if text == "" { + return + } + if b.field != "" && b.field != field { + b.flush() + } + b.field = field + b.text.WriteString(text) +} + +func (b *chatDeltaBatch) flush() { + if b.field == "" || b.text.Len() == 0 { + return + } + b.runtime.sendDelta(map[string]any{b.field: b.text.String()}) + b.field = "" + b.text.Reset() +} + +func newChatStreamRuntime( + w http.ResponseWriter, + rc *http.ResponseController, + canFlush bool, + completionID string, + created int64, + model string, + finalPrompt string, + thinkingEnabled bool, + searchEnabled bool, + stripReferenceMarkers bool, + toolNames []string, + toolsRaw any, + toolChoice promptcompat.ToolChoicePolicy, + bufferToolContent bool, + emitEarlyToolDeltas bool, +) *chatStreamRuntime { + return &chatStreamRuntime{ + w: w, + rc: rc, + canFlush: canFlush, + completionID: completionID, + created: created, + model: model, + finalPrompt: finalPrompt, + toolNames: toolNames, + toolsRaw: toolsRaw, + toolChoice: toolChoice, + thinkingEnabled: thinkingEnabled, + searchEnabled: searchEnabled, + stripReferenceMarkers: stripReferenceMarkers, + bufferToolContent: bufferToolContent, + emitEarlyToolDeltas: emitEarlyToolDeltas, + streamToolCallIDs: map[int]string{}, + streamToolNames: map[int]string{}, + accumulator: shared.StreamAccumulator{ + ThinkingEnabled: thinkingEnabled, + SearchEnabled: searchEnabled, + StripReferenceMarkers: stripReferenceMarkers, + }, + } +} + +func (s *chatStreamRuntime) sendKeepAlive() { + if !s.canFlush { + return + } + _, _ = s.w.Write([]byte(": keep-alive\n\n")) + _ = s.rc.Flush() +} + +func (s *chatStreamRuntime) sendChunk(v any) { + b, _ := json.Marshal(v) + _, _ = s.w.Write([]byte("data: ")) + _, _ = s.w.Write(b) + _, _ = s.w.Write([]byte("\n\n")) + if s.canFlush { + _ = s.rc.Flush() + } +} + +func (s *chatStreamRuntime) sendDelta(delta map[string]any) { + if len(delta) == 0 { + return + } + if !s.firstChunkSent { + delta["role"] = "assistant" + s.firstChunkSent = true + } + s.sendChunk(openaifmt.BuildChatStreamChunk( + s.completionID, + s.created, + s.model, + []map[string]any{openaifmt.BuildChatStreamDeltaChoice(0, delta)}, + nil, + )) +} + +func (s *chatStreamRuntime) sendDone() { + _, _ = s.w.Write([]byte("data: [DONE]\n\n")) + if s.canFlush { + _ = s.rc.Flush() + } +} + +func (s *chatStreamRuntime) sendFailedChunk(status int, message, code string) { + s.finalErrorStatus = status + s.finalErrorMessage = message + s.finalErrorCode = code + s.sendChunk(map[string]any{ + "status_code": status, + "error": map[string]any{ + "message": message, + "type": openAIErrorType(status), + "code": code, + "param": nil, + }, + }) + s.sendDone() +} + +func (s *chatStreamRuntime) markContextCancelled() { + s.finalErrorStatus = 499 + s.finalErrorMessage = "request context cancelled" + s.finalErrorCode = string(streamengine.StopReasonContextCancelled) + s.finalThinking = s.accumulator.Thinking.String() + s.finalText = cleanVisibleOutput(s.accumulator.Text.String(), s.stripReferenceMarkers) + s.finalFinishReason = string(streamengine.StopReasonContextCancelled) +} + +func (s *chatStreamRuntime) historyText() string { + if s == nil { + return "" + } + return historyTextForArchive(s.accumulator.RawText.String(), s.finalText) +} + +func (s *chatStreamRuntime) historyThinking() string { + if s == nil { + return "" + } + return historyThinkingForArchive( + s.accumulator.RawThinking.String(), + s.accumulator.ToolDetectionThinking.String(), + s.finalThinking, + ) +} + +func (s *chatStreamRuntime) resetStreamToolCallState() { + s.streamToolCallIDs = map[int]string{} + s.streamToolNames = map[int]string{} +} + +func (s *chatStreamRuntime) finalize(finishReason string, deferEmptyOutput bool) bool { + s.finalErrorStatus = 0 + s.finalErrorMessage = "" + s.finalErrorCode = "" + finalThinking := s.accumulator.Thinking.String() + finalToolDetectionThinking := s.accumulator.ToolDetectionThinking.String() + finalText := s.accumulator.Text.String() + turn := assistantturn.BuildTurnFromStreamSnapshot(assistantturn.StreamSnapshot{ + RawText: s.accumulator.RawText.String(), + VisibleText: finalText, + RawThinking: s.accumulator.RawThinking.String(), + VisibleThinking: finalThinking, + DetectionThinking: finalToolDetectionThinking, + ContentFilter: finishReason == "content_filter", + ResponseMessageID: s.responseMessageID, + AlreadyEmittedCalls: s.toolCallsEmitted, + AlreadyEmittedToolRaw: s.toolCallsDoneEmitted, + }, assistantturn.BuildOptions{ + Model: s.model, + Prompt: s.finalPrompt, + RefFileTokens: s.refFileTokens, + SearchEnabled: s.searchEnabled, + StripReferenceMarkers: s.stripReferenceMarkers, + ToolNames: s.toolNames, + ToolsRaw: s.toolsRaw, + ToolChoice: s.toolChoice, + }) + s.finalThinking = turn.Thinking + s.finalText = turn.Text + if len(turn.ToolCalls) > 0 && !s.toolCallsDoneEmitted { + s.sendDelta(map[string]any{ + "tool_calls": formatFinalStreamToolCallsWithStableIDs(turn.ToolCalls, s.streamToolCallIDs, s.toolsRaw), + }) + s.toolCallsEmitted = true + s.toolCallsDoneEmitted = true + } else if s.bufferToolContent { + batch := chatDeltaBatch{runtime: s} + for _, evt := range toolstream.Flush(&s.toolSieve, s.toolNames) { + if len(evt.ToolCalls) > 0 { + batch.flush() + s.toolCallsEmitted = true + s.toolCallsDoneEmitted = true + s.sendDelta(map[string]any{ + "tool_calls": formatFinalStreamToolCallsWithStableIDs(evt.ToolCalls, s.streamToolCallIDs, s.toolsRaw), + }) + s.resetStreamToolCallState() + } + if evt.Content == "" { + continue + } + cleaned := cleanVisibleOutput(evt.Content, s.stripReferenceMarkers) + if cleaned == "" || (s.searchEnabled && sse.IsCitation(cleaned)) { + continue + } + batch.append("content", cleaned) + } + batch.flush() + } + + outcome := assistantturn.FinalizeTurn(turn, assistantturn.FinalizeOptions{ + AlreadyEmittedToolCalls: s.toolCallsEmitted || s.toolCallsDoneEmitted, + }) + if outcome.ShouldFail { + status, message, code := outcome.Error.Status, outcome.Error.Message, outcome.Error.Code + if deferEmptyOutput { + s.finalErrorStatus = status + s.finalErrorMessage = message + s.finalErrorCode = code + return false + } + s.sendFailedChunk(status, message, code) + return true + } + usage := assistantturn.OpenAIChatUsage(turn) + s.finalFinishReason = outcome.FinishReason + s.finalUsage = usage + s.sendChunk(openaifmt.BuildChatStreamChunk( + s.completionID, + s.created, + s.model, + []map[string]any{openaifmt.BuildChatStreamFinishChoice(0, outcome.FinishReason)}, + usage, + )) + s.sendDone() + return true +} + +func (s *chatStreamRuntime) onParsed(parsed sse.LineResult) streamengine.ParsedDecision { + if !parsed.Parsed { + return streamengine.ParsedDecision{} + } + if parsed.ResponseMessageID > 0 { + s.responseMessageID = parsed.ResponseMessageID + } + if parsed.ContentFilter { + if strings.TrimSpace(s.accumulator.Text.String()) == "" { + return streamengine.ParsedDecision{Stop: true, StopReason: streamengine.StopReason("content_filter")} + } + return streamengine.ParsedDecision{Stop: true, StopReason: streamengine.StopReasonHandlerRequested} + } + if parsed.ErrorMessage != "" { + return streamengine.ParsedDecision{Stop: true, StopReason: streamengine.StopReason("content_filter")} + } + if parsed.Stop { + return streamengine.ParsedDecision{Stop: true, StopReason: streamengine.StopReasonHandlerRequested} + } + + batch := chatDeltaBatch{runtime: s} + accumulated := s.accumulator.Apply(parsed) + for _, p := range accumulated.Parts { + if p.Type == "thinking" { + batch.append("reasoning_content", p.VisibleText) + continue + } + if p.RawText == "" { + continue + } + if p.CitationOnly { + continue + } + if !s.bufferToolContent { + batch.append("content", p.VisibleText) + } else { + events := toolstream.ProcessChunk(&s.toolSieve, p.RawText, s.toolNames) + for _, evt := range events { + if len(evt.ToolCallDeltas) > 0 { + if !s.emitEarlyToolDeltas { + continue + } + filtered := filterIncrementalToolCallDeltasByAllowed(evt.ToolCallDeltas, s.streamToolNames) + if len(filtered) == 0 { + continue + } + formatted := formatIncrementalStreamToolCallDeltas(filtered, s.streamToolCallIDs) + if len(formatted) == 0 { + continue + } + batch.flush() + tcDelta := map[string]any{ + "tool_calls": formatted, + } + s.toolCallsEmitted = true + s.sendDelta(tcDelta) + continue + } + if len(evt.ToolCalls) > 0 { + batch.flush() + s.toolCallsEmitted = true + s.toolCallsDoneEmitted = true + tcDelta := map[string]any{ + "tool_calls": formatFinalStreamToolCallsWithStableIDs(evt.ToolCalls, s.streamToolCallIDs, s.toolsRaw), + } + s.sendDelta(tcDelta) + s.resetStreamToolCallState() + continue + } + if evt.Content != "" { + cleaned := cleanVisibleOutput(evt.Content, s.stripReferenceMarkers) + if cleaned == "" || (s.searchEnabled && sse.IsCitation(cleaned)) { + continue + } + batch.append("content", cleaned) + } + } + } + } + batch.flush() + return streamengine.ParsedDecision{ContentSeen: accumulated.ContentSeen} +} diff --git a/internal/httpapi/openai/chat/chat_stream_runtime_test.go b/internal/httpapi/openai/chat/chat_stream_runtime_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3f3387fad2eb6bd4189e3661656957130226c248 --- /dev/null +++ b/internal/httpapi/openai/chat/chat_stream_runtime_test.go @@ -0,0 +1,77 @@ +package chat + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "ds2api/internal/promptcompat" +) + +func TestChatStreamKeepAliveUsesCommentOnly(t *testing.T) { + rec := httptest.NewRecorder() + runtime := newChatStreamRuntime( + rec, + http.NewResponseController(rec), + true, + "chatcmpl-test", + time.Now().Unix(), + "deepseek-v4-flash", + "prompt", + false, + false, + true, + nil, + nil, + promptcompat.DefaultToolChoicePolicy(), + false, + false, + ) + + runtime.sendKeepAlive() + + body := rec.Body.String() + if !strings.Contains(body, ": keep-alive\n\n") { + t.Fatalf("expected keep-alive comment, got %q", body) + } + frames, done := parseSSEDataFrames(t, body) + if done { + t.Fatalf("keep-alive must not emit [DONE], body=%q", body) + } + if len(frames) != 0 { + t.Fatalf("keep-alive must not emit JSON data frames, got %#v body=%q", frames, body) + } +} + +func TestChatStreamFinalizeEnforcesRequiredToolChoice(t *testing.T) { + rec := httptest.NewRecorder() + runtime := newChatStreamRuntime( + rec, + http.NewResponseController(rec), + true, + "chatcmpl-test", + time.Now().Unix(), + "deepseek-v4-flash", + "prompt", + false, + false, + true, + []string{"Write"}, + nil, + promptcompat.ToolChoicePolicy{Mode: promptcompat.ToolChoiceRequired}, + true, + false, + ) + + if !runtime.finalize("stop", false) { + t.Fatalf("expected terminal error to be written") + } + if runtime.finalErrorCode != "tool_choice_violation" { + t.Fatalf("expected tool_choice_violation, got %q body=%s", runtime.finalErrorCode, rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "tool_choice requires") { + t.Fatalf("expected tool choice error in stream body, got %s", rec.Body.String()) + } +} diff --git a/internal/httpapi/openai/chat/empty_retry_runtime.go b/internal/httpapi/openai/chat/empty_retry_runtime.go new file mode 100644 index 0000000000000000000000000000000000000000..3494b6de0f3d8da5f64ddf79f6a3060fae41f688 --- /dev/null +++ b/internal/httpapi/openai/chat/empty_retry_runtime.go @@ -0,0 +1,220 @@ +package chat + +import ( + "context" + "io" + "net/http" + "time" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/completionruntime" + "ds2api/internal/config" + dsprotocol "ds2api/internal/deepseek/protocol" + openaifmt "ds2api/internal/format/openai" + "ds2api/internal/promptcompat" + "ds2api/internal/sse" + streamengine "ds2api/internal/stream" +) + +func (h *Handler) handleNonStreamWithRetry(w http.ResponseWriter, ctx context.Context, a *auth.RequestAuth, resp *http.Response, payload map[string]any, pow, completionID, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, historySession *chatHistorySession) { + if resp.StatusCode != http.StatusOK { + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.error(resp.StatusCode, string(body), "error", "", "") + } + writeOpenAIError(w, resp.StatusCode, string(body)) + return + } + stdReq := promptcompat.StandardRequest{ + Surface: "chat.completions", + ResponseModel: model, + PromptTokenText: finalPrompt, + FinalPrompt: finalPrompt, + RefFileTokens: refFileTokens, + Thinking: thinkingEnabled, + Search: searchEnabled, + ToolNames: toolNames, + ToolsRaw: toolsRaw, + ToolChoice: promptcompat.DefaultToolChoicePolicy(), + } + retryEnabled := h != nil && h.DS != nil && emptyOutputRetryEnabled() + result, outErr := completionruntime.ExecuteNonStreamStartedWithRetry(ctx, h.DS, a, completionruntime.StartResult{ + SessionID: completionID, + Payload: payload, + Pow: pow, + Response: resp, + Request: stdReq, + }, completionruntime.Options{ + RetryEnabled: retryEnabled, + RetryMaxAttempts: emptyOutputRetryMaxAttempts(), + }) + if outErr != nil { + if historySession != nil { + historySession.error(outErr.Status, outErr.Message, outErr.Code, historyThinkingForArchive(result.Turn.RawThinking, result.Turn.DetectionThinking, result.Turn.Thinking), historyTextForArchive(result.Turn.RawText, result.Turn.Text)) + } + writeOpenAIErrorWithCode(w, outErr.Status, outErr.Message, outErr.Code) + return + } + respBody := openaifmt.BuildChatCompletionWithToolCalls(result.SessionID, model, result.Turn.Prompt, result.Turn.Thinking, result.Turn.Text, result.Turn.ToolCalls, toolsRaw) + respBody["usage"] = assistantturn.OpenAIChatUsage(result.Turn) + outcome := assistantturn.FinalizeTurn(result.Turn, assistantturn.FinalizeOptions{}) + if historySession != nil { + historySession.success(http.StatusOK, historyThinkingForArchive(result.Turn.RawThinking, result.Turn.DetectionThinking, result.Turn.Thinking), historyTextForArchive(result.Turn.RawText, result.Turn.Text), outcome.FinishReason, assistantturn.OpenAIChatUsage(result.Turn)) + } + writeJSON(w, http.StatusOK, respBody) +} + +func (h *Handler) handleStreamWithRetry(w http.ResponseWriter, r *http.Request, a *auth.RequestAuth, resp *http.Response, payload map[string]any, pow, completionID string, sessionIDRef *string, stdReq promptcompat.StandardRequest, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, toolChoice promptcompat.ToolChoicePolicy, historySession *chatHistorySession) { + streamRuntime, initialType, ok := h.prepareChatStreamRuntime(w, resp, completionID, model, finalPrompt, refFileTokens, thinkingEnabled, searchEnabled, toolNames, toolsRaw, toolChoice, historySession) + if !ok { + return + } + completionruntime.ExecuteStreamWithRetry(r.Context(), h.DS, a, resp, payload, pow, completionruntime.StreamRetryOptions{ + Surface: "chat.completions", + Stream: true, + RetryEnabled: emptyOutputRetryEnabled(), + RetryMaxAttempts: emptyOutputRetryMaxAttempts(), + MaxAttempts: 3, + UsagePrompt: finalPrompt, + Request: stdReq, + CurrentInputFile: h.Store, + }, completionruntime.StreamRetryHooks{ + ConsumeAttempt: func(currentResp *http.Response, allowDeferEmpty bool) (bool, bool) { + return h.consumeChatStreamAttempt(r, currentResp, streamRuntime, initialType, thinkingEnabled, historySession, allowDeferEmpty) + }, + Finalize: func(attempts int) { + streamRuntime.finalize("stop", false) + recordChatStreamHistory(streamRuntime, historySession) + config.Logger.Info("[openai_empty_retry] terminal empty output", "surface", "chat.completions", "stream", true, "retry_attempts", attempts, "success_source", "none") + }, + ParentMessageID: func() int { + return streamRuntime.responseMessageID + }, + OnRetryPrompt: func(prompt string) { + streamRuntime.finalPrompt = prompt + }, + OnRetryFailure: func(status int, message, code string) { + failChatStreamRetry(streamRuntime, historySession, status, message, code) + }, + OnAccountSwitch: func(sessionID string) { + if sessionIDRef != nil { + *sessionIDRef = sessionID + } + }, + OnTerminal: func(attempts int) { + logChatStreamTerminal(streamRuntime, attempts) + }, + }) +} + +func (h *Handler) prepareChatStreamRuntime(w http.ResponseWriter, resp *http.Response, completionID, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, toolChoice promptcompat.ToolChoicePolicy, historySession *chatHistorySession) (*chatStreamRuntime, string, bool) { + if resp.StatusCode != http.StatusOK { + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.error(resp.StatusCode, string(body), "error", "", "") + } + writeOpenAIError(w, resp.StatusCode, string(body)) + return nil, "", false + } + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + if !canFlush { + config.Logger.Warn("[stream] response writer does not support flush; streaming may be buffered") + } + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + streamRuntime := newChatStreamRuntime( + w, rc, canFlush, completionID, time.Now().Unix(), model, finalPrompt, + thinkingEnabled, searchEnabled, stripReferenceMarkersEnabled(), toolNames, toolsRaw, + toolChoice, + len(toolNames) > 0, h.toolcallFeatureMatchEnabled() && h.toolcallEarlyEmitHighConfidence(), + ) + streamRuntime.refFileTokens = refFileTokens + return streamRuntime, initialType, true +} + +func (h *Handler) consumeChatStreamAttempt(r *http.Request, resp *http.Response, streamRuntime *chatStreamRuntime, initialType string, thinkingEnabled bool, historySession *chatHistorySession, allowDeferEmpty bool) (bool, bool) { + defer func() { _ = resp.Body.Close() }() + finalReason := "stop" + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: r.Context(), + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: time.Duration(dsprotocol.KeepAliveTimeout) * time.Second, + IdleTimeout: time.Duration(dsprotocol.StreamIdleTimeout) * time.Second, + MaxKeepAliveNoInput: dsprotocol.MaxKeepaliveCount, + }, streamengine.ConsumeHooks{ + OnKeepAlive: streamRuntime.sendKeepAlive, + OnParsed: func(parsed sse.LineResult) streamengine.ParsedDecision { + decision := streamRuntime.onParsed(parsed) + if historySession != nil { + historySession.progress(streamRuntime.historyThinking(), streamRuntime.historyText()) + } + return decision + }, + OnFinalize: func(reason streamengine.StopReason, _ error) { + if string(reason) == "content_filter" { + finalReason = "content_filter" + } + }, + OnContextDone: func() { + streamRuntime.markContextCancelled() + if historySession != nil { + historySession.stopped(streamRuntime.historyThinking(), streamRuntime.historyText(), string(streamengine.StopReasonContextCancelled)) + } + }, + }) + if streamRuntime.finalErrorCode == string(streamengine.StopReasonContextCancelled) { + return true, false + } + terminalWritten := streamRuntime.finalize(finalReason, allowDeferEmpty && finalReason != "content_filter") + if terminalWritten { + recordChatStreamHistory(streamRuntime, historySession) + return true, false + } + return false, true +} + +func recordChatStreamHistory(streamRuntime *chatStreamRuntime, historySession *chatHistorySession) { + if historySession == nil { + return + } + if streamRuntime.finalErrorMessage != "" { + historySession.error(streamRuntime.finalErrorStatus, streamRuntime.finalErrorMessage, streamRuntime.finalErrorCode, streamRuntime.historyThinking(), streamRuntime.historyText()) + return + } + historySession.success(http.StatusOK, streamRuntime.historyThinking(), streamRuntime.historyText(), streamRuntime.finalFinishReason, streamRuntime.finalUsage) +} + +func failChatStreamRetry(streamRuntime *chatStreamRuntime, historySession *chatHistorySession, status int, message, code string) { + streamRuntime.sendFailedChunk(status, message, code) + if historySession != nil { + historySession.error(status, message, code, streamRuntime.historyThinking(), streamRuntime.historyText()) + } +} + +func logChatStreamTerminal(streamRuntime *chatStreamRuntime, attempts int) { + source := "first_attempt" + if attempts > 0 { + source = "synthetic_retry" + } + if streamRuntime.finalErrorCode == string(streamengine.StopReasonContextCancelled) { + config.Logger.Info("[openai_empty_retry] terminal cancelled", "surface", "chat.completions", "stream", true, "retry_attempts", attempts, "error_code", streamRuntime.finalErrorCode) + return + } + if streamRuntime.finalErrorMessage != "" { + config.Logger.Info("[openai_empty_retry] terminal empty output", "surface", "chat.completions", "stream", true, "retry_attempts", attempts, "success_source", "none", "error_code", streamRuntime.finalErrorCode) + return + } + config.Logger.Info("[openai_empty_retry] completed", "surface", "chat.completions", "stream", true, "retry_attempts", attempts, "success_source", source) +} diff --git a/internal/httpapi/openai/chat/empty_retry_runtime_test.go b/internal/httpapi/openai/chat/empty_retry_runtime_test.go new file mode 100644 index 0000000000000000000000000000000000000000..9cf5d39bb6740fc611896c24223a2533888eb4ba --- /dev/null +++ b/internal/httpapi/openai/chat/empty_retry_runtime_test.go @@ -0,0 +1,87 @@ +package chat + +import ( + "context" + "net/http" + "net/http/httptest" + "testing" + "time" + + "ds2api/internal/chathistory" + "ds2api/internal/promptcompat" + "ds2api/internal/stream" +) + +func TestConsumeChatStreamAttemptMarksContextCancelledState(t *testing.T) { + historyStore := newTestChatHistoryStore(t) + entry, err := historyStore.Start(chathistory.StartParams{ + CallerID: "caller:test", + Model: "deepseek-v4-flash", + Stream: true, + UserInput: "hello", + }) + if err != nil { + t.Fatalf("start history failed: %v", err) + } + session := &chatHistorySession{ + store: historyStore, + entryID: entry.ID, + startedAt: time.Now(), + lastPersist: time.Now(), + finalPrompt: "prompt", + } + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil).WithContext(ctx) + rec := httptest.NewRecorder() + streamRuntime := newChatStreamRuntime( + rec, + http.NewResponseController(rec), + true, + "cid-cancelled", + time.Now().Unix(), + "deepseek-v4-flash", + "prompt", + false, + false, + true, + nil, + nil, + promptcompat.DefaultToolChoicePolicy(), + false, + false, + ) + resp := makeOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"hello"}`, + `data: [DONE]`, + ) + + h := &Handler{} + terminalWritten, retryable := h.consumeChatStreamAttempt(req, resp, streamRuntime, "text", false, session, true) + if !terminalWritten || retryable { + t.Fatalf("expected cancelled attempt to terminate without retry, got terminalWritten=%v retryable=%v", terminalWritten, retryable) + } + if got, want := streamRuntime.finalErrorCode, string(stream.StopReasonContextCancelled); got != want { + t.Fatalf("expected cancelled final error code %q, got %q", want, got) + } + if streamRuntime.finalErrorMessage == "" { + t.Fatalf("expected cancelled final error message to be preserved") + } + + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot failed: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + full, err := historyStore.Get(snapshot.Items[0].ID) + if err != nil { + t.Fatalf("get detail failed: %v", err) + } + if full.Status != "stopped" { + t.Fatalf("expected stopped status, got %#v", full) + } +} diff --git a/internal/httpapi/openai/chat/handler.go b/internal/httpapi/openai/chat/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..d91091d2382023f1ba34b7b61ac3a30a9bc501f7 --- /dev/null +++ b/internal/httpapi/openai/chat/handler.go @@ -0,0 +1,129 @@ +package chat + +import ( + "context" + "net/http" + "sync" + "time" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/httpapi/openai/files" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" + "ds2api/internal/textclean" + "ds2api/internal/toolcall" + "ds2api/internal/toolstream" +) + +const openAIGeneralMaxSize = shared.GeneralMaxSize + +var writeJSON = shared.WriteJSON + +type Handler struct { + Store shared.ConfigReader + Auth shared.AuthResolver + DS shared.DeepSeekCaller + ChatHistory *chathistory.Store + + leaseMu sync.Mutex + streamLeases map[string]streamLease +} + +type streamLease struct { + Auth *auth.RequestAuth + Standard promptcompat.StandardRequest + SessionID string + ExpiresAt time.Time +} + +func stripReferenceMarkersEnabled() bool { + return textclean.StripReferenceMarkersEnabled() +} + +func (h *Handler) applyCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { + if h == nil { + return stdReq, nil + } + stdReq = shared.ApplyThinkingInjection(h.Store, stdReq) + svc := history.Service{Store: h.Store, DS: h.DS} + out, err := svc.ApplyCurrentInputFile(ctx, a, stdReq) + if err != nil || out.CurrentInputFileApplied { + return out, err + } + return out, nil +} + +func (h *Handler) preprocessInlineFileInputs(ctx context.Context, a *auth.RequestAuth, req map[string]any) error { + if h == nil { + return nil + } + return (&files.Handler{Store: h.Store, Auth: h.Auth, DS: h.DS, ChatHistory: h.ChatHistory}).PreprocessInlineFileInputs(ctx, a, req) +} + +func (h *Handler) toolcallFeatureMatchEnabled() bool { + if h == nil { + return shared.ToolcallFeatureMatchEnabled(nil) + } + return shared.ToolcallFeatureMatchEnabled(h.Store) +} + +func (h *Handler) toolcallEarlyEmitHighConfidence() bool { + if h == nil { + return shared.ToolcallEarlyEmitHighConfidence(nil) + } + return shared.ToolcallEarlyEmitHighConfidence(h.Store) +} + +func writeOpenAIError(w http.ResponseWriter, status int, message string) { + shared.WriteOpenAIError(w, status, message) +} + +func writeOpenAIErrorWithCode(w http.ResponseWriter, status int, message, code string) { + shared.WriteOpenAIErrorWithCode(w, status, message, code) +} + +func openAIErrorType(status int) string { + return shared.OpenAIErrorType(status) +} + +func writeOpenAIInlineFileError(w http.ResponseWriter, err error) { + files.WriteInlineFileError(w, err) +} + +func mapCurrentInputFileError(err error) (int, string) { + return history.MapError(err) +} + +func requestTraceID(r *http.Request) string { + return shared.RequestTraceID(r) +} + +func asString(v any) string { + return shared.AsString(v) +} + +func cleanVisibleOutput(text string, stripReferenceMarkers bool) string { + return shared.CleanVisibleOutput(text, stripReferenceMarkers) +} + +func emptyOutputRetryEnabled() bool { + return shared.EmptyOutputRetryEnabled() +} + +func emptyOutputRetryMaxAttempts() int { + return shared.EmptyOutputRetryMaxAttempts() +} + +func formatIncrementalStreamToolCallDeltas(deltas []toolstream.ToolCallDelta, ids map[int]string) []map[string]any { + return shared.FormatIncrementalStreamToolCallDeltas(deltas, ids) +} + +func filterIncrementalToolCallDeltasByAllowed(deltas []toolstream.ToolCallDelta, seenNames map[int]string) []toolstream.ToolCallDelta { + return shared.FilterIncrementalToolCallDeltasByAllowed(deltas, seenNames) +} + +func formatFinalStreamToolCallsWithStableIDs(calls []toolcall.ParsedToolCall, ids map[int]string, toolsRaw any) []map[string]any { + return shared.FormatFinalStreamToolCallsWithStableIDs(calls, ids, toolsRaw) +} diff --git a/internal/httpapi/openai/chat/handler_chat.go b/internal/httpapi/openai/chat/handler_chat.go new file mode 100644 index 0000000000000000000000000000000000000000..c46278bdf7b7b95e9caa6df3df136c972705af72 --- /dev/null +++ b/internal/httpapi/openai/chat/handler_chat.go @@ -0,0 +1,284 @@ +package chat + +import ( + "context" + "encoding/json" + "io" + "net/http" + "strings" + "time" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/completionruntime" + "ds2api/internal/config" + dsprotocol "ds2api/internal/deepseek/protocol" + openaifmt "ds2api/internal/format/openai" + "ds2api/internal/promptcompat" + "ds2api/internal/sse" + streamengine "ds2api/internal/stream" +) + +func (h *Handler) ChatCompletions(w http.ResponseWriter, r *http.Request) { + if isVercelStreamReleaseRequest(r) { + h.handleVercelStreamRelease(w, r) + return + } + if isVercelStreamPowRequest(r) { + h.handleVercelStreamPow(w, r) + return + } + if isVercelStreamSwitchRequest(r) { + h.handleVercelStreamSwitch(w, r) + return + } + if isVercelStreamPrepareRequest(r) { + h.handleVercelStreamPrepare(w, r) + return + } + + a, err := h.Auth.Determine(r) + if err != nil { + status := http.StatusUnauthorized + detail := err.Error() + if err == auth.ErrNoAccount { + status = http.StatusTooManyRequests + } + writeOpenAIError(w, status, detail) + return + } + var sessionID string + defer func() { + h.autoDeleteRemoteSession(r.Context(), a, sessionID) + h.Auth.Release(a) + }() + + r = r.WithContext(auth.WithAuth(r.Context(), a)) + + r.Body = http.MaxBytesReader(w, r.Body, openAIGeneralMaxSize) + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + if strings.Contains(strings.ToLower(err.Error()), "too large") { + writeOpenAIError(w, http.StatusRequestEntityTooLarge, "request body too large") + return + } + writeOpenAIError(w, http.StatusBadRequest, "invalid json") + return + } + if err := h.preprocessInlineFileInputs(r.Context(), a, req); err != nil { + writeOpenAIInlineFileError(w, err) + return + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, requestTraceID(r)) + if err != nil { + writeOpenAIError(w, http.StatusBadRequest, err.Error()) + return + } + stdReq, err = h.applyCurrentInputFile(r.Context(), a, stdReq) + if err != nil { + status, message := mapCurrentInputFileError(err) + writeOpenAIError(w, status, message) + return + } + historySession := startChatHistory(h.ChatHistory, r, a, stdReq) + + if !stdReq.Stream { + result, outErr := completionruntime.ExecuteNonStreamWithRetry(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + RetryEnabled: true, + CurrentInputFile: h.Store, + }) + sessionID = result.SessionID + if outErr != nil { + if historySession != nil { + historySession.error(outErr.Status, outErr.Message, outErr.Code, historyThinkingForArchive(result.Turn.RawThinking, result.Turn.DetectionThinking, result.Turn.Thinking), historyTextForArchive(result.Turn.RawText, result.Turn.Text)) + } + writeOpenAIErrorWithCode(w, outErr.Status, outErr.Message, outErr.Code) + return + } + respBody := openaifmt.BuildChatCompletionWithToolCalls(result.SessionID, stdReq.ResponseModel, result.Turn.Prompt, result.Turn.Thinking, result.Turn.Text, result.Turn.ToolCalls, stdReq.ToolsRaw) + respBody["usage"] = assistantturn.OpenAIChatUsage(result.Turn) + finishReason := assistantturn.FinalizeTurn(result.Turn, assistantturn.FinalizeOptions{}).FinishReason + if historySession != nil { + historySession.success(http.StatusOK, historyThinkingForArchive(result.Turn.RawThinking, result.Turn.DetectionThinking, result.Turn.Thinking), historyTextForArchive(result.Turn.RawText, result.Turn.Text), finishReason, assistantturn.OpenAIChatUsage(result.Turn)) + } + writeJSON(w, http.StatusOK, respBody) + return + } + + start, outErr := completionruntime.StartCompletion(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + CurrentInputFile: h.Store, + }) + sessionID = start.SessionID + if outErr != nil { + if historySession != nil { + historySession.error(outErr.Status, outErr.Message, outErr.Code, "", "") + } + writeOpenAIErrorWithCode(w, outErr.Status, outErr.Message, outErr.Code) + return + } + streamReq := start.Request + refFileTokens := streamReq.RefFileTokens + h.handleStreamWithRetry(w, r, a, start.Response, start.Payload, start.Pow, sessionID, &sessionID, streamReq, streamReq.ResponseModel, streamReq.PromptTokenText, refFileTokens, streamReq.Thinking, streamReq.Search, streamReq.ToolNames, streamReq.ToolsRaw, streamReq.ToolChoice, historySession) +} + +func (h *Handler) autoDeleteRemoteSession(ctx context.Context, a *auth.RequestAuth, sessionID string) { + mode := h.Store.AutoDeleteMode() + if mode == "none" || a.DeepSeekToken == "" { + return + } + + deleteBaseCtx := context.WithoutCancel(ctx) + deleteCtx, cancel := context.WithTimeout(deleteBaseCtx, 10*time.Second) + defer cancel() + + switch mode { + case "single": + if sessionID == "" { + config.Logger.Warn("[auto_delete_sessions] skipped single-session delete because session_id is empty", "account", a.AccountID) + return + } + _, err := h.DS.DeleteSessionForToken(deleteCtx, a.DeepSeekToken, sessionID) + if err != nil { + config.Logger.Warn("[auto_delete_sessions] failed", "account", a.AccountID, "mode", mode, "session_id", sessionID, "error", err) + return + } + config.Logger.Debug("[auto_delete_sessions] success", "account", a.AccountID, "mode", mode, "session_id", sessionID) + case "all": + if err := h.DS.DeleteAllSessionsForToken(deleteCtx, a.DeepSeekToken); err != nil { + config.Logger.Warn("[auto_delete_sessions] failed", "account", a.AccountID, "mode", mode, "error", err) + return + } + config.Logger.Debug("[auto_delete_sessions] success", "account", a.AccountID, "mode", mode) + default: + config.Logger.Warn("[auto_delete_sessions] unknown mode", "account", a.AccountID, "mode", mode) + } +} + +func (h *Handler) handleNonStream(w http.ResponseWriter, resp *http.Response, completionID, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, historySession *chatHistorySession) { + if resp.StatusCode != http.StatusOK { + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.error(resp.StatusCode, string(body), "error", "", "") + } + writeOpenAIError(w, resp.StatusCode, string(body)) + return + } + result := sse.CollectStream(resp, thinkingEnabled, true) + + turn := assistantturn.BuildTurnFromCollected(result, assistantturn.BuildOptions{ + Model: model, + Prompt: finalPrompt, + RefFileTokens: refFileTokens, + SearchEnabled: searchEnabled, + ToolNames: toolNames, + ToolsRaw: toolsRaw, + ToolChoice: promptcompat.DefaultToolChoicePolicy(), + }) + outcome := assistantturn.FinalizeTurn(turn, assistantturn.FinalizeOptions{}) + if outcome.ShouldFail { + status, message, code := outcome.Error.Status, outcome.Error.Message, outcome.Error.Code + if historySession != nil { + historySession.error(status, message, code, historyThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), historyTextForArchive(turn.RawText, turn.Text)) + } + writeOpenAIErrorWithCode(w, status, message, code) + return + } + respBody := openaifmt.BuildChatCompletionWithToolCalls(completionID, model, finalPrompt, turn.Thinking, turn.Text, turn.ToolCalls, toolsRaw) + respBody["usage"] = assistantturn.OpenAIChatUsage(turn) + if historySession != nil { + historySession.success(http.StatusOK, historyThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), historyTextForArchive(turn.RawText, turn.Text), outcome.FinishReason, assistantturn.OpenAIChatUsage(turn)) + } + writeJSON(w, http.StatusOK, respBody) +} + +func (h *Handler) handleStream(w http.ResponseWriter, r *http.Request, resp *http.Response, completionID, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, historySession *chatHistorySession) { + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.error(resp.StatusCode, string(body), "error", "", "") + } + writeOpenAIError(w, resp.StatusCode, string(body)) + return + } + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + if !canFlush { + config.Logger.Warn("[stream] response writer does not support flush; streaming may be buffered") + } + + created := time.Now().Unix() + bufferToolContent := len(toolNames) > 0 + emitEarlyToolDeltas := h.toolcallFeatureMatchEnabled() && h.toolcallEarlyEmitHighConfidence() + stripReferenceMarkers := stripReferenceMarkersEnabled() + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + + streamRuntime := newChatStreamRuntime( + w, + rc, + canFlush, + completionID, + created, + model, + finalPrompt, + thinkingEnabled, + searchEnabled, + stripReferenceMarkers, + toolNames, + toolsRaw, + promptcompat.DefaultToolChoicePolicy(), + bufferToolContent, + emitEarlyToolDeltas, + ) + streamRuntime.refFileTokens = refFileTokens + + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: r.Context(), + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: time.Duration(dsprotocol.KeepAliveTimeout) * time.Second, + IdleTimeout: time.Duration(dsprotocol.StreamIdleTimeout) * time.Second, + MaxKeepAliveNoInput: dsprotocol.MaxKeepaliveCount, + }, streamengine.ConsumeHooks{ + OnKeepAlive: func() { + streamRuntime.sendKeepAlive() + }, + OnParsed: func(parsed sse.LineResult) streamengine.ParsedDecision { + decision := streamRuntime.onParsed(parsed) + if historySession != nil { + historySession.progress(streamRuntime.historyThinking(), streamRuntime.historyText()) + } + return decision + }, + OnFinalize: func(reason streamengine.StopReason, _ error) { + if string(reason) == "content_filter" { + streamRuntime.finalize("content_filter", false) + } else { + streamRuntime.finalize("stop", false) + } + if historySession == nil { + return + } + if streamRuntime.finalErrorMessage != "" { + historySession.error(streamRuntime.finalErrorStatus, streamRuntime.finalErrorMessage, streamRuntime.finalErrorCode, streamRuntime.historyThinking(), streamRuntime.historyText()) + return + } + historySession.success(http.StatusOK, streamRuntime.historyThinking(), streamRuntime.historyText(), streamRuntime.finalFinishReason, streamRuntime.finalUsage) + }, + OnContextDone: func() { + streamRuntime.markContextCancelled() + if historySession != nil { + historySession.stopped(streamRuntime.historyThinking(), streamRuntime.historyText(), string(streamengine.StopReasonContextCancelled)) + } + }, + }) +} diff --git a/internal/httpapi/openai/chat/handler_chat_auto_delete_test.go b/internal/httpapi/openai/chat/handler_chat_auto_delete_test.go new file mode 100644 index 0000000000000000000000000000000000000000..243cbc9efcaf66f01bf91bf63a880077cc934b9f --- /dev/null +++ b/internal/httpapi/openai/chat/handler_chat_auto_delete_test.go @@ -0,0 +1,141 @@ +package chat + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "ds2api/internal/auth" + dsclient "ds2api/internal/deepseek/client" +) + +type autoDeleteModeDSStub struct { + resp *http.Response + singleCalls int + allCalls int + lastSessionID string + lastCtxErr error +} + +func (m *autoDeleteModeDSStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +func (m *autoDeleteModeDSStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m *autoDeleteModeDSStub) UploadFile(_ context.Context, _ *auth.RequestAuth, _ dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + return &dsclient.UploadFileResult{ID: "file-id", Filename: "file.txt", Bytes: 1, Status: "uploaded"}, nil +} + +func (m *autoDeleteModeDSStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + return m.resp, nil +} + +func (m *autoDeleteModeDSStub) DeleteSessionForToken(_ context.Context, _ string, sessionID string) (*dsclient.DeleteSessionResult, error) { + m.singleCalls++ + m.lastSessionID = sessionID + return &dsclient.DeleteSessionResult{SessionID: sessionID, Success: true}, nil +} + +func (m *autoDeleteModeDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + m.allCalls++ + return nil +} + +func (m *autoDeleteModeDSStub) DeleteSessionForTokenCtx(ctx context.Context, _ string, sessionID string) (*dsclient.DeleteSessionResult, error) { + m.singleCalls++ + m.lastSessionID = sessionID + m.lastCtxErr = ctx.Err() + return &dsclient.DeleteSessionResult{SessionID: sessionID, Success: true}, nil +} + +func TestChatCompletionsAutoDeleteModes(t *testing.T) { + tests := []struct { + name string + mode string + wantSingle int + wantAll int + }{ + {name: "none", mode: "none"}, + {name: "single", mode: "single", wantSingle: 1}, + {name: "all", mode: "all", wantAll: 1}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + ds := &autoDeleteModeDSStub{ + resp: makeOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"hello"}`, + "data: [DONE]", + ), + } + h := &Handler{ + Store: mockOpenAIConfig{ + autoDeleteMode: tc.mode, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status=%d body=%s", rec.Code, rec.Body.String()) + } + if ds.singleCalls != tc.wantSingle { + t.Fatalf("single delete calls=%d want=%d", ds.singleCalls, tc.wantSingle) + } + if ds.allCalls != tc.wantAll { + t.Fatalf("all delete calls=%d want=%d", ds.allCalls, tc.wantAll) + } + if tc.wantSingle > 0 && ds.lastSessionID != "session-id" { + t.Fatalf("expected single delete for session-id, got %q", ds.lastSessionID) + } + }) + } +} + +type autoDeleteCtxDSStub struct { + autoDeleteModeDSStub +} + +func (m *autoDeleteCtxDSStub) DeleteSessionForToken(ctx context.Context, token string, sessionID string) (*dsclient.DeleteSessionResult, error) { + return m.DeleteSessionForTokenCtx(ctx, token, sessionID) +} + +func (m *autoDeleteCtxDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + m.allCalls++ + return nil +} + +func TestAutoDeleteRemoteSessionIgnoresCanceledParentContext(t *testing.T) { + ds := &autoDeleteCtxDSStub{} + h := &Handler{ + Store: mockOpenAIConfig{ + autoDeleteMode: "single", + }, + DS: ds, + } + a := &auth.RequestAuth{DeepSeekToken: "token", AccountID: "acct"} + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + h.autoDeleteRemoteSession(ctx, a, "session-id") + + if ds.singleCalls != 1 { + t.Fatalf("single delete calls=%d want=1", ds.singleCalls) + } + if ds.lastCtxErr != nil { + t.Fatalf("delete ctx should not inherit cancellation, got %v", ds.lastCtxErr) + } +} diff --git a/internal/httpapi/openai/chat/handler_toolcall_test.go b/internal/httpapi/openai/chat/handler_toolcall_test.go new file mode 100644 index 0000000000000000000000000000000000000000..a42d7d47a65232c3b9f50fe1ca3ba9c4c5770063 --- /dev/null +++ b/internal/httpapi/openai/chat/handler_toolcall_test.go @@ -0,0 +1,603 @@ +package chat + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func makeSSEHTTPResponse(lines ...string) *http.Response { + body := strings.Join(lines, "\n") + if !strings.HasSuffix(body, "\n") { + body += "\n" + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +func decodeJSONBody(t *testing.T, body string) map[string]any { + t.Helper() + var out map[string]any + if err := json.Unmarshal([]byte(body), &out); err != nil { + t.Fatalf("decode json failed: %v, body=%s", err, body) + } + return out +} + +func parseSSEDataFrames(t *testing.T, body string) ([]map[string]any, bool) { + t.Helper() + lines := strings.Split(body, "\n") + frames := make([]map[string]any, 0, len(lines)) + done := false + for _, line := range lines { + line = strings.TrimSpace(line) + if !strings.HasPrefix(line, "data:") { + continue + } + payload := strings.TrimSpace(strings.TrimPrefix(line, "data:")) + if payload == "" { + continue + } + if payload == "[DONE]" { + done = true + continue + } + var frame map[string]any + if err := json.Unmarshal([]byte(payload), &frame); err != nil { + t.Fatalf("decode sse frame failed: %v, payload=%s", err, payload) + } + frames = append(frames, frame) + } + return frames, done +} + +func streamHasToolCallsDelta(frames []map[string]any) bool { + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if _, ok := delta["tool_calls"]; ok { + return true + } + } + } + return false +} + +func streamFinishReason(frames []map[string]any) string { + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + if reason, ok := choice["finish_reason"].(string); ok && reason != "" { + return reason + } + } + } + return "" +} + +func TestHandleNonStreamSingleAttemptReturns503WhenUpstreamOutputEmpty(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/content","v":""}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + + h.handleNonStream(rec, resp, "cid-empty", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, nil) + if rec.Code != http.StatusServiceUnavailable { + t.Fatalf("expected status 503 for empty upstream output, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "upstream_unavailable" { + t.Fatalf("expected code=upstream_unavailable, got %#v", out) + } +} + +func TestHandleNonStreamSingleAttemptReturnsContentFilterErrorWhenUpstreamFilteredWithoutOutput(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"code":"content_filter"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + + h.handleNonStream(rec, resp, "cid-empty-filtered", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, nil) + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected status 400 for filtered upstream output, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "content_filter" { + t.Fatalf("expected code=content_filter, got %#v", out) + } +} + +func TestHandleNonStreamSingleAttemptReturns429WhenUpstreamHasOnlyThinking(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"Only thinking"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + + h.handleNonStream(rec, resp, "cid-thinking-only", "deepseek-v4-pro", "prompt", 0, true, false, nil, nil, nil) + if rec.Code != http.StatusTooManyRequests { + t.Fatalf("expected status 429 for thinking-only upstream output, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "upstream_empty_output" { + t.Fatalf("expected code=upstream_empty_output, got %#v", out) + } +} + +func TestHandleNonStreamPromotesThinkingToolCallsWhenTextEmpty(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"from-thinking"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + + h.handleNonStream(rec, resp, "cid-thinking-tool", "deepseek-v4-pro", "prompt", 0, true, false, []string{"search"}, nil, nil) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 for thinking tool calls, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + choices, _ := out["choices"].([]any) + if len(choices) == 0 { + t.Fatalf("expected choices, got %#v", out) + } + choice, _ := choices[0].(map[string]any) + if got := asString(choice["finish_reason"]); got != "tool_calls" { + t.Fatalf("expected finish_reason=tool_calls, got %#v", choice["finish_reason"]) + } + message, _ := choice["message"].(map[string]any) + toolCalls, _ := message["tool_calls"].([]any) + if len(toolCalls) != 1 { + t.Fatalf("expected one tool call, got %#v", message["tool_calls"]) + } + if content, exists := message["content"]; !exists || content != nil { + t.Fatalf("expected content nil when tool call promoted, got %#v", message["content"]) + } +} + +func TestHandleNonStreamPromotesHiddenThinkingDSMLToolCallsWhenTextEmpty(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"<|DSML|tool_calls><|DSML|invoke name=\"search\"><|DSML|parameter name=\"q\">from-hidden-thinking"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + + h.handleNonStream(rec, resp, "cid-hidden-thinking-tool", "deepseek-v4-pro", "prompt", 0, false, false, []string{"search"}, nil, nil) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 for hidden thinking tool calls, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + choices, _ := out["choices"].([]any) + choice, _ := choices[0].(map[string]any) + message, _ := choice["message"].(map[string]any) + if _, ok := message["reasoning_content"]; ok { + t.Fatalf("expected hidden thinking not to be exposed, got %#v", message) + } + toolCalls, _ := message["tool_calls"].([]any) + if len(toolCalls) != 1 { + t.Fatalf("expected one hidden-thinking tool call, got %#v", message["tool_calls"]) + } + if got := asString(choice["finish_reason"]); got != "tool_calls" { + t.Fatalf("expected finish_reason=tool_calls, got %#v", choice["finish_reason"]) + } +} + +func TestHandleStreamToolsPlainTextStreamsBeforeFinish(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/content","v":"你好,"}`, + `data: {"p":"response/content","v":"这是普通文本回复。"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid6", "deepseek-v4-flash", "prompt", 0, false, false, []string{"search"}, nil, nil) + + 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 plain text: %s", rec.Body.String()) + } + content := strings.Builder{} + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if c, ok := delta["content"].(string); ok { + content.WriteString(c) + } + } + } + if got := content.String(); got == "" { + t.Fatalf("expected streamed content in tool mode plain text, body=%s", rec.Body.String()) + } + if streamFinishReason(frames) != "stop" { + t.Fatalf("expected finish_reason=stop, body=%s", rec.Body.String()) + } +} + +func TestHandleStreamThinkingDisabledDoesNotLeakHiddenFragmentContinuations(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/fragments","o":"APPEND","v":[{"type":"THINK","content":"我们"}]}`, + `data: {"p":"response/fragments/-1/content","v":"被"}`, + `data: {"v":"要求"}`, + `data: {"p":"response/fragments","o":"APPEND","v":[{"type":"RESPONSE","content":"答"}]}`, + `data: {"p":"response/fragments/-1/content","v":"案"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid-hidden-fragment", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, nil) + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + content := strings.Builder{} + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if c, ok := delta["content"].(string); ok { + content.WriteString(c) + } + } + } + if got := content.String(); got != "答案" { + t.Fatalf("expected only visible response text, got %q body=%s", got, rec.Body.String()) + } +} + +func TestHandleStreamEmitsSingleChoiceFramesForMultipleParsedParts(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/fragments","o":"APPEND","v":[{"type":"THINK","content":"我们"},{"type":"THINK","content":"被"},{"type":"THINK","content":"要求"},{"type":"RESPONSE","content":"答"},{"type":"RESPONSE","content":"案"}]}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid-multi-parts", "deepseek-v4-pro", "prompt", 0, true, false, nil, nil, nil) + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + var reasoning, content strings.Builder + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + if len(choices) != 1 { + t.Fatalf("expected exactly one choice per stream frame, got %d frame=%#v body=%s", len(choices), frame, rec.Body.String()) + } + choice, _ := choices[0].(map[string]any) + delta, _ := choice["delta"].(map[string]any) + reasoning.WriteString(asString(delta["reasoning_content"])) + content.WriteString(asString(delta["content"])) + } + if got := reasoning.String(); got != "我们被要求" { + t.Fatalf("first-choice-only client would miss reasoning tokens: got %q body=%s", got, rec.Body.String()) + } + if got := content.String(); got != "答案" { + t.Fatalf("first-choice-only client would miss content tokens: got %q body=%s", got, rec.Body.String()) + } +} + +func TestHandleStreamCoalescesSmallContentDeltas(t *testing.T) { + h := &Handler{} + lines := make([]string, 0, 101) + for i := 0; i < 100; i++ { + b, _ := json.Marshal(map[string]any{ + "p": "response/content", + "v": "字", + }) + lines = append(lines, "data: "+string(b)) + } + lines = append(lines, "data: [DONE]") + resp := makeSSEHTTPResponse(lines...) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid-coalesce", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, nil) + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + var content strings.Builder + contentDeltaFrames := 0 + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + if len(choices) != 1 { + t.Fatalf("expected exactly one choice per stream frame, got %d frame=%#v body=%s", len(choices), frame, rec.Body.String()) + } + choice, _ := choices[0].(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if c, ok := delta["content"].(string); ok { + contentDeltaFrames++ + content.WriteString(c) + } + } + if got, want := content.String(), strings.Repeat("字", 100); got != want { + t.Fatalf("coalesced stream content mismatch: got %q want %q body=%s", got, want, rec.Body.String()) + } + if contentDeltaFrames >= 100 { + t.Fatalf("expected coalescing to reduce 100 tiny content frames, got %d body=%s", contentDeltaFrames, rec.Body.String()) + } +} + +func TestHandleStreamIncompleteCapturedToolJSONFlushesAsTextOnFinalize(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/content","v":"{\"tool_calls\":[{\"name\":\"search\""}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid10", "deepseek-v4-flash", "prompt", 0, false, false, []string{"search"}, nil, nil) + + 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 incomplete json, body=%s", rec.Body.String()) + } + content := strings.Builder{} + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if c, ok := delta["content"].(string); ok { + content.WriteString(c) + } + } + } + if !strings.Contains(strings.ToLower(content.String()), "tool_calls") || !strings.Contains(content.String(), "{") { + t.Fatalf("expected incomplete capture to flush as plain text instead of stalling, got=%q", content.String()) + } +} + +func TestHandleStreamPromotesThinkingToolCallsOnFinalizeWithoutMidstreamIntercept(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"from-thinking"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid-thinking-stream", "deepseek-v4-pro", "prompt", 0, true, false, []string{"search"}, nil, nil) + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + if !streamHasToolCallsDelta(frames) { + t.Fatalf("expected tool_calls delta from finalize fallback, body=%s", rec.Body.String()) + } + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if asString(delta["reasoning_content"]) != "" { + t.Fatalf("did not expect leaked reasoning_content markup, body=%s", rec.Body.String()) + } + } + } + if streamFinishReason(frames) != "tool_calls" { + t.Fatalf("expected finish_reason=tool_calls, body=%s", rec.Body.String()) + } +} + +func TestHandleStreamPromotesHiddenThinkingDSMLToolCallsOnFinalize(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/thinking_content","v":"<|DSML|tool_calls><|DSML|invoke name=\"search\"><|DSML|parameter name=\"q\">from-hidden-thinking"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid-hidden-thinking-stream", "deepseek-v4-pro", "prompt", 0, false, false, []string{"search"}, nil, nil) + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + if !streamHasToolCallsDelta(frames) { + t.Fatalf("expected tool_calls delta from hidden thinking fallback, body=%s", rec.Body.String()) + } + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if asString(delta["reasoning_content"]) != "" { + t.Fatalf("did not expect hidden reasoning_content delta, body=%s", rec.Body.String()) + } + } + } + if streamFinishReason(frames) != "tool_calls" { + t.Fatalf("expected finish_reason=tool_calls, body=%s", rec.Body.String()) + } +} + +func TestHandleStreamEmitsDistinctToolCallIDsAcrossSeparateToolBlocks(t *testing.T) { + h := &Handler{} + resp := makeSSEHTTPResponse( + `data: {"p":"response/content","v":"前置文本\n\n \n README.MD\n \n"}`, + `data: {"p":"response/content","v":"中间文本\n\n \n golang\n \n"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + + h.handleStream(rec, req, resp, "cid-multi", "deepseek-v4-flash", "prompt", 0, false, false, []string{"read_file", "search"}, nil, nil) + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + + ids := make([]string, 0, 2) + seen := make(map[string]struct{}) + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + toolCalls, _ := delta["tool_calls"].([]any) + for _, rawCall := range toolCalls { + call, _ := rawCall.(map[string]any) + id := asString(call["id"]) + if id == "" { + continue + } + if _, ok := seen[id]; ok { + continue + } + seen[id] = struct{}{} + ids = append(ids, id) + } + } + } + + if len(ids) != 2 { + t.Fatalf("expected two distinct tool call ids, got %#v body=%s", ids, rec.Body.String()) + } + if ids[0] == ids[1] { + t.Fatalf("expected distinct tool call ids across blocks, got %#v body=%s", ids, rec.Body.String()) + } +} + +func TestHandleStreamCoercesSchemaDeclaredStringArgumentsOnFinalize(t *testing.T) { + h := &Handler{} + line := func(v string) string { + b, _ := json.Marshal(map[string]any{"p": "response/content", "v": v}) + return "data: " + string(b) + } + resp := makeSSEHTTPResponse( + line(`{"input":{"content":{"message":"hi"},"taskId":1}}`), + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + toolsRaw := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "Write", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + "taskId": map[string]any{"type": "string"}, + }, + }, + }, + }, + } + + h.handleStream(rec, req, resp, "cid-string-protect", "deepseek-v4-flash", "prompt", 0, false, false, []string{"Write"}, toolsRaw, nil) + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + for _, frame := range frames { + choices, _ := frame["choices"].([]any) + for _, item := range choices { + choice, _ := item.(map[string]any) + delta, _ := choice["delta"].(map[string]any) + toolCalls, _ := delta["tool_calls"].([]any) + if len(toolCalls) == 0 { + continue + } + call, _ := toolCalls[0].(map[string]any) + fn, _ := call["function"].(map[string]any) + args := map[string]any{} + if err := json.Unmarshal([]byte(asString(fn["arguments"])), &args); err != nil { + t.Fatalf("decode streamed tool arguments failed: %v", err) + } + if args["content"] != `{"message":"hi"}` { + t.Fatalf("expected streamed content stringified by schema, got %#v", args["content"]) + } + if args["taskId"] != "1" { + t.Fatalf("expected streamed taskId stringified by schema, got %#v", args["taskId"]) + } + return + } + } + t.Fatalf("expected at least one streamed tool call delta, body=%s", rec.Body.String()) +} + +func TestHandleNonStreamWithRetryIncludesRefFileTokensInUsage(t *testing.T) { + h := &Handler{} + + run := func(refFileTokens int) map[string]any { + resp := makeSSEHTTPResponse( + `data: {"p":"response/content","v":"hello world"}`, + `data: [DONE]`, + ) + rec := httptest.NewRecorder() + h.handleNonStreamWithRetry(rec, context.Background(), nil, resp, nil, "", "cid-ref", "deepseek-v4-flash", "prompt", refFileTokens, false, false, nil, nil, nil) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + return decodeJSONBody(t, rec.Body.String()) + } + + base := run(0) + withRef := run(7) + + baseUsage, _ := base["usage"].(map[string]any) + refUsage, _ := withRef["usage"].(map[string]any) + if baseUsage == nil || refUsage == nil { + t.Fatalf("expected usage objects, base=%#v ref=%#v", base["usage"], withRef["usage"]) + } + + getInt := func(m map[string]any, key string) int { + t.Helper() + v, ok := m[key].(float64) + if !ok { + t.Fatalf("expected numeric %s, got %#v", key, m[key]) + } + return int(v) + } + + if got := getInt(refUsage, "prompt_tokens") - getInt(baseUsage, "prompt_tokens"); got != 7 { + t.Fatalf("expected prompt_tokens delta 7, got %d", got) + } + if got := getInt(refUsage, "total_tokens") - getInt(baseUsage, "total_tokens"); got != 7 { + t.Fatalf("expected total_tokens delta 7, got %d", got) + } +} diff --git a/internal/httpapi/openai/chat/test_helpers_test.go b/internal/httpapi/openai/chat/test_helpers_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8a8baa9f042ed8ff5ff762cf6f9fd4b52c0d8c1f --- /dev/null +++ b/internal/httpapi/openai/chat/test_helpers_test.go @@ -0,0 +1,208 @@ +package chat + +import ( + "context" + "fmt" + "io" + "net/http" + "strings" + + "ds2api/internal/auth" + dsclient "ds2api/internal/deepseek/client" +) + +type mockOpenAIConfig struct { + aliases map[string]string + autoDeleteMode string + toolMode string + earlyEmit string + responsesTTL int + embedProv string + currentInputEnabled bool + currentInputMin int + thinkingInjection *bool + thinkingPrompt string +} + +func (m mockOpenAIConfig) ModelAliases() map[string]string { return m.aliases } +func (m mockOpenAIConfig) ToolcallMode() string { return m.toolMode } +func (m mockOpenAIConfig) ToolcallEarlyEmitConfidence() string { return m.earlyEmit } +func (m mockOpenAIConfig) ResponsesStoreTTLSeconds() int { return m.responsesTTL } +func (m mockOpenAIConfig) EmbeddingsProvider() string { return m.embedProv } +func (m mockOpenAIConfig) AutoDeleteMode() string { + if m.autoDeleteMode == "" { + return "none" + } + return m.autoDeleteMode +} +func (m mockOpenAIConfig) AutoDeleteSessions() bool { return false } +func (m mockOpenAIConfig) CurrentInputFileEnabled() bool { return m.currentInputEnabled } +func (m mockOpenAIConfig) CurrentInputFileMinChars() int { + return m.currentInputMin +} +func (m mockOpenAIConfig) ThinkingInjectionEnabled() bool { + if m.thinkingInjection == nil { + return false + } + return *m.thinkingInjection +} +func (m mockOpenAIConfig) ThinkingInjectionPrompt() string { return m.thinkingPrompt } + +type streamStatusAuthStub struct{} + +func (streamStatusAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: false, + DeepSeekToken: "direct-token", + CallerID: "caller:test", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (streamStatusAuthStub) DetermineCaller(_ *http.Request) (*auth.RequestAuth, error) { + return (&streamStatusAuthStub{}).Determine(nil) +} + +func (streamStatusAuthStub) Release(_ *auth.RequestAuth) {} + +type streamStatusManagedAuthStub struct{} + +func (streamStatusManagedAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: true, + DeepSeekToken: "managed-token", + CallerID: "caller:test", + AccountID: "acct:test", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (streamStatusManagedAuthStub) DetermineCaller(_ *http.Request) (*auth.RequestAuth, error) { + return (&streamStatusManagedAuthStub{}).Determine(nil) +} + +func (streamStatusManagedAuthStub) Release(_ *auth.RequestAuth) {} + +type streamStatusDSStub struct { + resp *http.Response +} + +func (m streamStatusDSStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +func (m streamStatusDSStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m streamStatusDSStub) UploadFile(_ context.Context, _ *auth.RequestAuth, _ dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + return &dsclient.UploadFileResult{ID: "file-id", Filename: "file.txt", Bytes: 1, Status: "uploaded"}, nil +} + +func (m streamStatusDSStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + return m.resp, nil +} + +func (m streamStatusDSStub) DeleteSessionForToken(_ context.Context, _ string, _ string) (*dsclient.DeleteSessionResult, error) { + return &dsclient.DeleteSessionResult{Success: true}, nil +} + +func (m streamStatusDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +func makeOpenAISSEHTTPResponse(lines ...string) *http.Response { + body := strings.Join(lines, "\n") + if !strings.HasSuffix(body, "\n") { + body += "\n" + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +type inlineUploadDSStub struct { + uploadCalls []dsclient.UploadFileRequest + lastCtx context.Context + completionReq map[string]any + createSession string + uploadErr error + completionResp *http.Response +} + +func (m *inlineUploadDSStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + if strings.TrimSpace(m.createSession) == "" { + return "session-id", nil + } + return m.createSession, nil +} + +func (m *inlineUploadDSStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m *inlineUploadDSStub) UploadFile(ctx context.Context, _ *auth.RequestAuth, req dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + m.lastCtx = ctx + m.uploadCalls = append(m.uploadCalls, req) + if m.uploadErr != nil { + return nil, m.uploadErr + } + id := "file-inline-1" + if len(m.uploadCalls) > 1 { + id = "file-inline-" + fmt.Sprint(len(m.uploadCalls)) + } + return &dsclient.UploadFileResult{ + ID: id, + Filename: req.Filename, + Bytes: int64(len(req.Data)), + Status: "uploaded", + Purpose: req.Purpose, + }, nil +} + +func (m *inlineUploadDSStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + m.completionReq = payload + if m.completionResp != nil { + return m.completionResp, nil + } + return makeOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"ok"}`, + `data: [DONE]`, + ), nil +} + +func (m *inlineUploadDSStub) DeleteSessionForToken(_ context.Context, _ string, _ string) (*dsclient.DeleteSessionResult, error) { + return &dsclient.DeleteSessionResult{Success: true}, nil +} + +func (m *inlineUploadDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +func historySplitTestMessages() []any { + toolCalls := []any{ + map[string]any{ + "name": "search", + "arguments": map[string]any{"query": "docs"}, + }, + } + return []any{ + map[string]any{"role": "system", "content": "system instructions"}, + map[string]any{"role": "user", "content": "first user turn"}, + map[string]any{ + "role": "assistant", + "content": "", + "reasoning_content": "hidden reasoning", + "tool_calls": toolCalls, + }, + map[string]any{ + "role": "tool", + "name": "search", + "tool_call_id": "call-1", + "content": "tool result", + }, + map[string]any{"role": "user", "content": "latest user turn"}, + } +} diff --git a/internal/httpapi/openai/chat/vercel_prepare_test.go b/internal/httpapi/openai/chat/vercel_prepare_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8ee5b031933a6e30de09d906ba985eaefc428ef6 --- /dev/null +++ b/internal/httpapi/openai/chat/vercel_prepare_test.go @@ -0,0 +1,507 @@ +package chat + +import ( + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/promptcompat" +) + +func TestIsVercelStreamPrepareRequest(t *testing.T) { + req := httptest.NewRequest("POST", "/v1/chat/completions?__stream_prepare=1", nil) + if !isVercelStreamPrepareRequest(req) { + t.Fatalf("expected prepare request to be detected") + } + + req2 := httptest.NewRequest("POST", "/v1/chat/completions", nil) + if isVercelStreamPrepareRequest(req2) { + t.Fatalf("expected non-prepare request") + } +} + +func TestIsVercelStreamReleaseRequest(t *testing.T) { + req := httptest.NewRequest("POST", "/v1/chat/completions?__stream_release=1", nil) + if !isVercelStreamReleaseRequest(req) { + t.Fatalf("expected release request to be detected") + } + + req2 := httptest.NewRequest("POST", "/v1/chat/completions", nil) + if isVercelStreamReleaseRequest(req2) { + t.Fatalf("expected non-release request") + } +} + +func TestVercelInternalSecret(t *testing.T) { + t.Run("prefer explicit secret", func(t *testing.T) { + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + t.Setenv("DS2API_ADMIN_KEY", "admin-fallback") + if got := vercelInternalSecret(); got != "stream-secret" { + t.Fatalf("expected explicit secret, got %q", got) + } + }) + + t.Run("fallback to admin key", func(t *testing.T) { + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "") + t.Setenv("DS2API_ADMIN_KEY", "admin-fallback") + if got := vercelInternalSecret(); got != "admin-fallback" { + t.Fatalf("expected admin key fallback, got %q", got) + } + }) + + t.Run("default admin when env missing", func(t *testing.T) { + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "") + t.Setenv("DS2API_ADMIN_KEY", "") + if got := vercelInternalSecret(); got != "admin" { + t.Fatalf("expected default admin fallback, got %q", got) + } + }) +} + +func TestStreamLeaseLifecycle(t *testing.T) { + h := &Handler{} + leaseID := h.holdStreamLease(&auth.RequestAuth{UseConfigToken: false}, promptcompat.StandardRequest{}, "test-session-id") + if leaseID == "" { + t.Fatalf("expected non-empty lease id") + } + if lease, ok := h.releaseStreamLease(leaseID); !ok { + t.Fatalf("expected lease release success") + } else if lease.SessionID != "test-session-id" { + t.Fatalf("expected released session id, got %q", lease.SessionID) + } + if _, ok := h.releaseStreamLease(leaseID); ok { + t.Fatalf("expected duplicate release to fail") + } +} + +func TestStreamLeaseTTL(t *testing.T) { + t.Setenv("DS2API_VERCEL_STREAM_LEASE_TTL_SECONDS", "120") + if got := streamLeaseTTL(); got != 120*time.Second { + t.Fatalf("expected ttl=120s, got %v", got) + } + t.Setenv("DS2API_VERCEL_STREAM_LEASE_TTL_SECONDS", "invalid") + if got := streamLeaseTTL(); got != 15*time.Minute { + t.Fatalf("expected default ttl on invalid value, got %v", got) + } +} + +func TestHandleVercelStreamPrepareAppliesCurrentInputFile(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + + ds := &inlineUploadDSStub{} + h := &Handler{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": true, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__stream_prepare=1", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Ds2-Internal-Token", "stream-secret") + rec := httptest.NewRecorder() + + h.handleVercelStreamPrepare(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected 1 current input upload, got %d", len(ds.uploadCalls)) + } + + var body map[string]any + if err := json.NewDecoder(rec.Body).Decode(&body); err != nil { + t.Fatalf("decode failed: %v", err) + } + payload, _ := body["payload"].(map[string]any) + if payload == nil { + t.Fatalf("expected payload object, got %#v", body["payload"]) + } + promptText, _ := payload["prompt"].(string) + if !strings.Contains(promptText, ".txt") { + t.Fatalf("expected continuation prompt, got %s", promptText) + } + if strings.Contains(promptText, "first user turn") || strings.Contains(promptText, "latest user turn") { + t.Fatalf("expected original turns hidden from prompt, got %s", promptText) + } + refIDs, _ := payload["ref_file_ids"].([]any) + if len(refIDs) == 0 || refIDs[0] != "file-inline-1" { + t.Fatalf("expected uploaded history file first in ref_file_ids, got %#v", payload["ref_file_ids"]) + } +} + +func TestHandleVercelStreamPrepareUsesHalfwidthDSMLToolPrompt(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + + h := &Handler{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: &inlineUploadDSStub{}, + } + + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": []any{ + map[string]any{"role": "user", "content": "search docs"}, + }, + "tools": []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "query": map[string]any{"type": "string"}, + }, + "required": []any{"query"}, + }, + }, + }, + }, + "stream": true, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__stream_prepare=1", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Ds2-Internal-Token", "stream-secret") + rec := httptest.NewRecorder() + + h.handleVercelStreamPrepare(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var body map[string]any + if err := json.NewDecoder(rec.Body).Decode(&body); err != nil { + t.Fatalf("decode failed: %v", err) + } + finalPrompt, _ := body["final_prompt"].(string) + payload, _ := body["payload"].(map[string]any) + payloadPrompt, _ := payload["prompt"].(string) + for label, promptText := range map[string]string{"final_prompt": finalPrompt, "payload.prompt": payloadPrompt} { + if !strings.Contains(promptText, "<|DSML|tool_calls>") || !strings.Contains(promptText, "Tag punctuation alphabet: ASCII < > / = \" plus the halfwidth pipe |.") { + t.Fatalf("expected %s to contain halfwidth DSML tool instructions, got %q", label, promptText) + } + if strings.Contains(promptText, "\uff5c") || strings.Contains(promptText, "full"+"width vertical bar") { + t.Fatalf("expected %s not to contain legacy pipe guidance, got %q", label, promptText) + } + } + toolNames, _ := body["tool_names"].([]any) + if len(toolNames) != 1 || toolNames[0] != "search" { + t.Fatalf("expected prepared tool names to align with request tools, got %#v", body["tool_names"]) + } +} + +type vercelReleaseAutoDeleteDSStub struct { + resp *http.Response + deleteCallCount int + deletedSessionID string + deletedToken string + deleteErr error + events *[]string +} + +func (m *vercelReleaseAutoDeleteDSStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +func (m *vercelReleaseAutoDeleteDSStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m *vercelReleaseAutoDeleteDSStub) UploadFile(_ context.Context, _ *auth.RequestAuth, _ dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + return &dsclient.UploadFileResult{ID: "file-id", Filename: "file.txt", Bytes: 1, Status: "uploaded"}, nil +} + +func (m *vercelReleaseAutoDeleteDSStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + return m.resp, nil +} + +func (m *vercelReleaseAutoDeleteDSStub) DeleteSessionForToken(_ context.Context, token string, sessionID string) (*dsclient.DeleteSessionResult, error) { + if m.events != nil { + *m.events = append(*m.events, "delete") + } + m.deleteCallCount++ + m.deletedSessionID = sessionID + m.deletedToken = token + if m.deleteErr != nil { + return nil, m.deleteErr + } + return &dsclient.DeleteSessionResult{SessionID: sessionID, Success: true}, nil +} + +func (m *vercelReleaseAutoDeleteDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +type vercelReleaseAuthStub struct { + events *[]string +} + +func (a *vercelReleaseAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{DeepSeekToken: "test-token", AccountID: "test-account"}, nil +} + +func (a *vercelReleaseAuthStub) DetermineCaller(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{DeepSeekToken: "test-token", AccountID: "test-account"}, nil +} + +func (a *vercelReleaseAuthStub) Release(_ *auth.RequestAuth) { + if a.events != nil { + *a.events = append(*a.events, "release") + } +} + +func TestHandleVercelStreamReleaseTriggersAutoDelete(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + + events := []string{} + ds := &vercelReleaseAutoDeleteDSStub{events: &events} + h := &Handler{ + Store: mockOpenAIConfig{ + autoDeleteMode: "single", + }, + Auth: &vercelReleaseAuthStub{events: &events}, + DS: ds, + } + + leaseID := h.holdStreamLease(&auth.RequestAuth{DeepSeekToken: "test-token", AccountID: "test-account"}, promptcompat.StandardRequest{}, "session-to-delete") + if leaseID == "" { + t.Fatalf("expected non-empty lease id") + } + + reqBody := map[string]any{"lease_id": leaseID} + reqJSON, _ := json.Marshal(reqBody) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__stream_release=1", strings.NewReader(string(reqJSON))) + req.Header.Set("X-Ds2-Internal-Token", "stream-secret") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.handleVercelStreamRelease(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if ds.deleteCallCount != 1 { + t.Fatalf("expected auto delete call count=1, got %d", ds.deleteCallCount) + } + if ds.deletedSessionID != "session-to-delete" { + t.Fatalf("expected deleted session id=session-to-delete, got %q", ds.deletedSessionID) + } + if got, want := strings.Join(events, ","), "delete,release"; got != want { + t.Fatalf("expected auto-delete before auth release, got %s", got) + } +} + +func TestHandleVercelStreamPrepareUploadsToolsSeparately(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + + ds := &inlineUploadDSStub{} + h := &Handler{ + Store: mockOpenAIConfig{currentInputEnabled: true}, + Auth: streamStatusAuthStub{}, + DS: ds, + } + + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": []any{ + map[string]any{"role": "user", "content": "search docs"}, + }, + "tools": []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{"type": "object"}, + }, + }, + }, + "stream": true, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__stream_prepare=1", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Ds2-Internal-Token", "stream-secret") + rec := httptest.NewRecorder() + + h.handleVercelStreamPrepare(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 2 { + t.Fatalf("expected history and tools uploads, got %d", len(ds.uploadCalls)) + } + if strings.Contains(strings.ToLower(ds.uploadCalls[0].Filename), "history") || !strings.HasSuffix(ds.uploadCalls[0].Filename, ".txt") || ds.uploadCalls[1].Filename != "context_tools.txt" { + t.Fatalf("unexpected upload filenames: %#v", ds.uploadCalls) + } + if strings.Contains(string(ds.uploadCalls[0].Data), "Description: search docs") { + t.Fatalf("history transcript should not embed tool descriptions, got %q", string(ds.uploadCalls[0].Data)) + } + + var body map[string]any + if err := json.NewDecoder(rec.Body).Decode(&body); err != nil { + t.Fatalf("decode failed: %v", err) + } + finalPrompt, _ := body["final_prompt"].(string) + payload, _ := body["payload"].(map[string]any) + payloadPrompt, _ := payload["prompt"].(string) + for label, promptText := range map[string]string{"final_prompt": finalPrompt, "payload.prompt": payloadPrompt} { + if !strings.Contains(promptText, "context_tools.txt") || !strings.Contains(promptText, "TOOL CALL SCHEME") { + t.Fatalf("expected %s to reference tools file and retain tool instructions, got %q", label, promptText) + } + if strings.Contains(promptText, "Description: search docs") { + t.Fatalf("expected %s not to inline tool descriptions, got %q", label, promptText) + } + } + refIDs, _ := payload["ref_file_ids"].([]any) + if len(refIDs) < 2 || refIDs[0] != "file-inline-1" || refIDs[1] != "file-inline-2" { + t.Fatalf("expected history and tools ref ids first, got %#v", payload["ref_file_ids"]) + } +} + +func TestHandleVercelStreamPrepareMapsCurrentInputFileManagedAuthFailureTo401(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + + ds := &inlineUploadDSStub{ + uploadErr: &dsclient.RequestFailure{Op: "upload file", Kind: dsclient.FailureManagedUnauthorized, Message: "expired token"}, + } + h := &Handler{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusManagedAuthStub{}, + DS: ds, + } + + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": true, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__stream_prepare=1", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer managed-key") + req.Header.Set("Content-Type", "application/json") + req.Header.Set("X-Ds2-Internal-Token", "stream-secret") + rec := httptest.NewRecorder() + + h.handleVercelStreamPrepare(rec, req) + + if rec.Code != http.StatusUnauthorized { + t.Fatalf("expected 401, got %d body=%s", rec.Code, rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "Please re-login the account in admin") { + t.Fatalf("expected managed auth error message, got %s", rec.Body.String()) + } +} + +func TestHandleVercelStreamSwitchReuploadsCurrentInputFile(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[ + {"email":"acc1@test.com","password":"pwd"}, + {"email":"acc2@test.com","password":"pwd"} + ] + }`) + store := config.LoadStore() + resolver := auth.NewResolver(store, account.NewPool(store), func(_ context.Context, acc config.Account) (string, error) { + return "token-" + acc.Identifier(), nil + }) + authReq := httptest.NewRequest(http.MethodPost, "/", nil) + authReq.Header.Set("Authorization", "Bearer managed-key") + a, err := resolver.Determine(authReq) + if err != nil { + t.Fatalf("determine failed: %v", err) + } + defer resolver.Release(a) + + ds := &inlineUploadDSStub{} + h := &Handler{ + Store: mockOpenAIConfig{currentInputEnabled: true}, + Auth: resolver, + DS: ds, + } + stdReq := promptcompat.StandardRequest{ + RequestedModel: "deepseek-v4-flash", + ResolvedModel: "deepseek-v4-flash", + ResponseModel: "deepseek-v4-flash", + CurrentInputFilename: "ctx-fixed-1234.txt", + FinalPrompt: "已附上下文文件「ctx-fixed-1234.txt」。直接基于该文件完成当前请求,不要重复之前内容。 可用工具与参数说明在 context_tools.txt 中,仅可依据该文件调用工具。", + PromptTokenText: "# context_context.txt\n\n=== 1 ===\n[r=1]\nhello\n\n# context_tools.txt\nTool descriptions and parameter schemas for this request.\n\nYou have access to these tools:\n\nTool: search\nDescription: search docs\nParameters: {\"type\":\"object\"}\n", + HistoryText: "# context_context.txt\n\n=== 1 ===\n[r=1]\nhello\n", + CurrentInputFileApplied: true, + CurrentInputFileID: "file-old", + CurrentToolsFileID: "file-old-tools", + ToolsRaw: []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{"type": "object"}, + }, + }, + }, + RefFileIDs: []string{"file-old", "file-old-tools", "client-file"}, + Thinking: true, + } + leaseID := h.holdStreamLease(a, stdReq, "") + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__stream_switch=1", strings.NewReader(`{"lease_id":"`+leaseID+`"}`)) + req.Header.Set("X-Ds2-Internal-Token", "stream-secret") + rec := httptest.NewRecorder() + + h.handleVercelStreamSwitch(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 2 { + t.Fatalf("expected current input and tools reupload on switched account, got %d", len(ds.uploadCalls)) + } + if strings.Contains(strings.ToLower(ds.uploadCalls[0].Filename), "history") || !strings.HasSuffix(ds.uploadCalls[0].Filename, ".txt") || ds.uploadCalls[1].Filename != "context_tools.txt" { + t.Fatalf("unexpected reupload filenames: %#v", ds.uploadCalls) + } + var body map[string]any + if err := json.NewDecoder(rec.Body).Decode(&body); err != nil { + t.Fatalf("decode failed: %v", err) + } + if body["deepseek_token"] != "token-acc2@test.com" { + t.Fatalf("expected switched account token, got %#v", body["deepseek_token"]) + } + payload, _ := body["payload"].(map[string]any) + refIDs, _ := payload["ref_file_ids"].([]any) + if len(refIDs) != 3 || refIDs[0] != "file-inline-1" || refIDs[1] != "file-inline-2" || refIDs[2] != "client-file" { + t.Fatalf("expected reuploaded current input ref plus client ref, got %#v", payload["ref_file_ids"]) + } + promptText, _ := payload["prompt"].(string) + if !strings.Contains(promptText, "context_tools.txt") { + t.Fatalf("expected switched payload prompt to retain tools file reference, got %q", promptText) + } +} diff --git a/internal/httpapi/openai/chat/vercel_stream.go b/internal/httpapi/openai/chat/vercel_stream.go new file mode 100644 index 0000000000000000000000000000000000000000..77b216a33f5fa70b8ee721e79037daf812a5cdb9 --- /dev/null +++ b/internal/httpapi/openai/chat/vercel_stream.go @@ -0,0 +1,438 @@ +package chat + +import ( + "crypto/subtle" + "encoding/json" + "net/http" + "os" + "strconv" + "strings" + "time" + + "ds2api/internal/auth" + "ds2api/internal/config" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/promptcompat" + "ds2api/internal/util" + + "github.com/google/uuid" +) + +func (h *Handler) handleVercelStreamPrepare(w http.ResponseWriter, r *http.Request) { + if !config.IsVercel() { + http.NotFound(w, r) + return + } + h.sweepExpiredStreamLeases() + internalSecret := vercelInternalSecret() + internalToken := strings.TrimSpace(r.Header.Get("X-Ds2-Internal-Token")) + if internalSecret == "" || subtle.ConstantTimeCompare([]byte(internalToken), []byte(internalSecret)) != 1 { + writeOpenAIError(w, http.StatusUnauthorized, "unauthorized internal request") + return + } + + a, err := h.Auth.Determine(r) + if err != nil { + status := http.StatusUnauthorized + if err == auth.ErrNoAccount { + status = http.StatusTooManyRequests + } + writeOpenAIError(w, status, err.Error()) + return + } + leased := false + defer func() { + if !leased { + h.Auth.Release(a) + } + }() + r = r.WithContext(auth.WithAuth(r.Context(), a)) + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeOpenAIError(w, http.StatusBadRequest, "invalid json") + return + } + if err := h.preprocessInlineFileInputs(r.Context(), a, req); err != nil { + writeOpenAIInlineFileError(w, err) + return + } + if !util.ToBool(req["stream"]) { + writeOpenAIError(w, http.StatusBadRequest, "stream must be true") + return + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, requestTraceID(r)) + if err != nil { + writeOpenAIError(w, http.StatusBadRequest, err.Error()) + return + } + if !stdReq.Stream { + writeOpenAIError(w, http.StatusBadRequest, "stream must be true") + return + } + stdReq, err = h.applyCurrentInputFile(r.Context(), a, stdReq) + if err != nil { + status, message := mapCurrentInputFileError(err) + writeOpenAIError(w, status, message) + return + } + + sessionID, err := h.DS.CreateSession(r.Context(), a, 3) + if err != nil { + if a.UseConfigToken { + writeOpenAIError(w, http.StatusUnauthorized, "Account token is invalid. Please re-login the account in admin.") + } else { + writeOpenAIError(w, http.StatusUnauthorized, "Invalid token. If this should be a DS2API key, add it to config.keys first.") + } + return + } + powHeader, err := h.DS.GetPow(r.Context(), a, 3) + if err != nil { + writeOpenAIError(w, http.StatusUnauthorized, "Failed to get PoW (invalid token or unknown error).") + return + } + if strings.TrimSpace(a.DeepSeekToken) == "" { + writeOpenAIError(w, http.StatusUnauthorized, "Invalid token. If this should be a DS2API key, add it to config.keys first.") + return + } + + payload := stdReq.CompletionPayload(sessionID) + leaseID := h.holdStreamLease(a, stdReq, sessionID) + if leaseID == "" { + writeOpenAIError(w, http.StatusInternalServerError, "failed to create stream lease") + return + } + leased = true + writeJSON(w, http.StatusOK, map[string]any{ + "session_id": sessionID, + "lease_id": leaseID, + "model": stdReq.ResponseModel, + "final_prompt": stdReq.FinalPrompt, + "thinking_enabled": stdReq.Thinking, + "search_enabled": stdReq.Search, + "tool_names": stdReq.ToolNames, + "deepseek_token": a.DeepSeekToken, + "pow_header": powHeader, + "payload": payload, + }) +} + +func (h *Handler) handleVercelStreamRelease(w http.ResponseWriter, r *http.Request) { + if !config.IsVercel() { + http.NotFound(w, r) + return + } + h.sweepExpiredStreamLeases() + internalSecret := vercelInternalSecret() + internalToken := strings.TrimSpace(r.Header.Get("X-Ds2-Internal-Token")) + if internalSecret == "" || subtle.ConstantTimeCompare([]byte(internalToken), []byte(internalSecret)) != 1 { + writeOpenAIError(w, http.StatusUnauthorized, "unauthorized internal request") + return + } + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeOpenAIError(w, http.StatusBadRequest, "invalid json") + return + } + leaseID, _ := req["lease_id"].(string) + leaseID = strings.TrimSpace(leaseID) + if leaseID == "" { + writeOpenAIError(w, http.StatusBadRequest, "lease_id is required") + return + } + lease, ok := h.releaseStreamLease(leaseID) + if !ok { + writeOpenAIError(w, http.StatusNotFound, "stream lease not found") + return + } + if h.Auth != nil && lease.Auth != nil { + defer h.Auth.Release(lease.Auth) + } + if lease.Auth != nil { + h.autoDeleteRemoteSession(r.Context(), lease.Auth, lease.SessionID) + } + writeJSON(w, http.StatusOK, map[string]any{"success": true}) +} + +func (h *Handler) handleVercelStreamPow(w http.ResponseWriter, r *http.Request) { + if !config.IsVercel() { + http.NotFound(w, r) + return + } + internalSecret := vercelInternalSecret() + internalToken := strings.TrimSpace(r.Header.Get("X-Ds2-Internal-Token")) + if internalSecret == "" || subtle.ConstantTimeCompare([]byte(internalToken), []byte(internalSecret)) != 1 { + writeOpenAIError(w, http.StatusUnauthorized, "unauthorized internal request") + return + } + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeOpenAIError(w, http.StatusBadRequest, "invalid json") + return + } + leaseID, _ := req["lease_id"].(string) + leaseID = strings.TrimSpace(leaseID) + if leaseID == "" { + writeOpenAIError(w, http.StatusBadRequest, "lease_id is required") + return + } + leaseAuth := h.lookupStreamLeaseAuth(leaseID) + if leaseAuth == nil { + writeOpenAIError(w, http.StatusNotFound, "stream lease not found or expired") + return + } + powHeader, err := h.DS.GetPow(r.Context(), leaseAuth, 3) + if err != nil { + writeOpenAIError(w, http.StatusInternalServerError, "Failed to get PoW.") + return + } + writeJSON(w, http.StatusOK, map[string]any{ + "pow_header": powHeader, + }) +} + +func (h *Handler) handleVercelStreamSwitch(w http.ResponseWriter, r *http.Request) { + if !config.IsVercel() { + http.NotFound(w, r) + return + } + h.sweepExpiredStreamLeases() + internalSecret := vercelInternalSecret() + internalToken := strings.TrimSpace(r.Header.Get("X-Ds2-Internal-Token")) + if internalSecret == "" || subtle.ConstantTimeCompare([]byte(internalToken), []byte(internalSecret)) != 1 { + writeOpenAIError(w, http.StatusUnauthorized, "unauthorized internal request") + return + } + + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + writeOpenAIError(w, http.StatusBadRequest, "invalid json") + return + } + leaseID, _ := req["lease_id"].(string) + leaseID = strings.TrimSpace(leaseID) + if leaseID == "" { + writeOpenAIError(w, http.StatusBadRequest, "lease_id is required") + return + } + lease, ok := h.lookupStreamLease(leaseID) + if !ok || lease.Auth == nil { + writeOpenAIError(w, http.StatusNotFound, "stream lease not found or expired") + return + } + a := lease.Auth + if !a.UseConfigToken || !a.SwitchAccount(r.Context()) { + writeOpenAIErrorWithCode(w, http.StatusTooManyRequests, "Upstream account hit a rate limit and returned reasoning without visible output.", "upstream_empty_output") + return + } + + stdReq := lease.Standard + var err error + if stdReq.CurrentInputFileApplied { + stdReq, err = (history.Service{Store: h.Store, DS: h.DS}).ReuploadAppliedCurrentInputFile(r.Context(), a, stdReq) + if err != nil { + status, message := mapCurrentInputFileError(err) + writeOpenAIError(w, status, message) + return + } + } + sessionID, err := h.DS.CreateSession(r.Context(), a, 3) + if err != nil { + writeOpenAIError(w, http.StatusUnauthorized, "Account token is invalid. Please re-login the account in admin.") + return + } + powHeader, err := h.DS.GetPow(r.Context(), a, 3) + if err != nil { + writeOpenAIError(w, http.StatusUnauthorized, "Failed to get PoW (invalid token or unknown error).") + return + } + if strings.TrimSpace(a.DeepSeekToken) == "" { + writeOpenAIError(w, http.StatusUnauthorized, "Account token is invalid. Please re-login the account in admin.") + return + } + h.updateStreamLeaseState(leaseID, stdReq, sessionID) + writeJSON(w, http.StatusOK, map[string]any{ + "session_id": sessionID, + "lease_id": leaseID, + "model": stdReq.ResponseModel, + "final_prompt": stdReq.FinalPrompt, + "thinking_enabled": stdReq.Thinking, + "search_enabled": stdReq.Search, + "tool_names": stdReq.ToolNames, + "deepseek_token": a.DeepSeekToken, + "pow_header": powHeader, + "payload": stdReq.CompletionPayload(sessionID), + }) +} + +func isVercelStreamPrepareRequest(r *http.Request) bool { + if r == nil { + return false + } + return strings.TrimSpace(r.URL.Query().Get("__stream_prepare")) == "1" +} + +func isVercelStreamReleaseRequest(r *http.Request) bool { + if r == nil { + return false + } + return strings.TrimSpace(r.URL.Query().Get("__stream_release")) == "1" +} + +func isVercelStreamPowRequest(r *http.Request) bool { + if r == nil { + return false + } + return strings.TrimSpace(r.URL.Query().Get("__stream_pow")) == "1" +} + +func isVercelStreamSwitchRequest(r *http.Request) bool { + if r == nil { + return false + } + return strings.TrimSpace(r.URL.Query().Get("__stream_switch")) == "1" +} + +func vercelInternalSecret() string { + if v := strings.TrimSpace(os.Getenv("DS2API_VERCEL_INTERNAL_SECRET")); v != "" { + return v + } + if v := strings.TrimSpace(os.Getenv("DS2API_ADMIN_KEY")); v != "" { + return v + } + return "admin" +} + +func (h *Handler) holdStreamLease(a *auth.RequestAuth, stdReq promptcompat.StandardRequest, sessionID string) string { + if a == nil { + return "" + } + now := time.Now() + ttl := streamLeaseTTL() + if ttl <= 0 { + ttl = 15 * time.Minute + } + + h.leaseMu.Lock() + expired := h.popExpiredLeasesLocked(now) + if h.streamLeases == nil { + h.streamLeases = make(map[string]streamLease) + } + leaseID := newLeaseID() + h.streamLeases[leaseID] = streamLease{ + Auth: a, + Standard: stdReq, + SessionID: sessionID, + ExpiresAt: now.Add(ttl), + } + h.leaseMu.Unlock() + h.releaseExpiredAuths(expired) + return leaseID +} + +func (h *Handler) lookupStreamLease(leaseID string) (streamLease, bool) { + leaseID = strings.TrimSpace(leaseID) + if leaseID == "" { + return streamLease{}, false + } + h.leaseMu.Lock() + lease, ok := h.streamLeases[leaseID] + h.leaseMu.Unlock() + if !ok || time.Now().After(lease.ExpiresAt) { + return streamLease{}, false + } + return lease, true +} + +func (h *Handler) lookupStreamLeaseAuth(leaseID string) *auth.RequestAuth { + lease, ok := h.lookupStreamLease(leaseID) + if !ok { + return nil + } + return lease.Auth +} + +func (h *Handler) updateStreamLeaseState(leaseID string, stdReq promptcompat.StandardRequest, sessionID string) { + leaseID = strings.TrimSpace(leaseID) + if leaseID == "" { + return + } + h.leaseMu.Lock() + defer h.leaseMu.Unlock() + lease, ok := h.streamLeases[leaseID] + if !ok { + return + } + lease.Standard = stdReq + lease.SessionID = sessionID + h.streamLeases[leaseID] = lease +} + +func (h *Handler) releaseStreamLease(leaseID string) (streamLease, bool) { + leaseID = strings.TrimSpace(leaseID) + if leaseID == "" { + return streamLease{}, false + } + + h.leaseMu.Lock() + expired := h.popExpiredLeasesLocked(time.Now()) + lease, ok := h.streamLeases[leaseID] + if ok { + delete(h.streamLeases, leaseID) + } + h.leaseMu.Unlock() + h.releaseExpiredAuths(expired) + + if !ok { + return streamLease{}, false + } + return lease, true +} + +func (h *Handler) popExpiredLeasesLocked(now time.Time) []*auth.RequestAuth { + if len(h.streamLeases) == 0 { + return nil + } + expired := make([]*auth.RequestAuth, 0) + for leaseID, lease := range h.streamLeases { + if now.After(lease.ExpiresAt) { + delete(h.streamLeases, leaseID) + expired = append(expired, lease.Auth) + } + } + return expired +} + +func (h *Handler) releaseExpiredAuths(expired []*auth.RequestAuth) { + if h.Auth == nil || len(expired) == 0 { + return + } + for _, a := range expired { + h.Auth.Release(a) + } +} + +func (h *Handler) sweepExpiredStreamLeases() { + h.leaseMu.Lock() + expired := h.popExpiredLeasesLocked(time.Now()) + h.leaseMu.Unlock() + h.releaseExpiredAuths(expired) +} + +func streamLeaseTTL() time.Duration { + raw := strings.TrimSpace(os.Getenv("DS2API_VERCEL_STREAM_LEASE_TTL_SECONDS")) + if raw == "" { + return 15 * time.Minute + } + seconds, err := strconv.Atoi(raw) + if err != nil || seconds <= 0 { + return 15 * time.Minute + } + return time.Duration(seconds) * time.Second +} + +func newLeaseID() string { + return strings.ReplaceAll(uuid.NewString(), "-", "") +} diff --git a/internal/httpapi/openai/citation_links_test.go b/internal/httpapi/openai/citation_links_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3c891ab617411e5452781fc2e1ad6a8b509fd6ca --- /dev/null +++ b/internal/httpapi/openai/citation_links_test.go @@ -0,0 +1,84 @@ +package openai + +import "testing" + +func TestReplaceCitationMarkersWithLinks(t *testing.T) { + raw := "这是一条更新[citation:1],更多信息见[citation:2]。" + links := map[int]string{ + 1: "https://example.com/news-1", + 2: "https://example.com/news-2", + } + + got := replaceCitationMarkersWithLinks(raw, links) + want := "这是一条更新[1](https://example.com/news-1),更多信息见[2](https://example.com/news-2)。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} + +func TestReplaceCitationMarkersWithLinksKeepsUnknownIndex(t *testing.T) { + raw := "只有一个来源[citation:1],未知来源[citation:3]。" + links := map[int]string{1: "https://example.com/a"} + + got := replaceCitationMarkersWithLinks(raw, links) + want := "只有一个来源[1](https://example.com/a),未知来源[citation:3]。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} + +func TestReplaceCitationMarkersWithLinksSupportsReferenceMarker(t *testing.T) { + raw := "新闻摘要[reference:1],详情[reference:2]。" + links := map[int]string{ + 1: "https://example.com/r1", + 2: "https://example.com/r2", + } + + got := replaceCitationMarkersWithLinks(raw, links) + want := "新闻摘要[1](https://example.com/r1),详情[2](https://example.com/r2)。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} + +func TestReplaceCitationMarkersWithLinksSupportsReferenceZeroBased(t *testing.T) { + raw := "来源[reference:0] 与 [reference:1]。" + links := map[int]string{ + 1: "https://example.com/first", + 2: "https://example.com/second", + } + + got := replaceCitationMarkersWithLinks(raw, links) + want := "来源[0](https://example.com/first) 与 [1](https://example.com/second)。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} + +func TestReplaceCitationMarkersWithLinksKeepsCitationOneBasedWithZeroBasedReference(t *testing.T) { + raw := "引用[citation:1],来源[reference:0],后续[reference:1]。" + links := map[int]string{ + 1: "https://example.com/first", + 2: "https://example.com/second", + } + + got := replaceCitationMarkersWithLinks(raw, links) + want := "引用[1](https://example.com/first),来源[0](https://example.com/first),后续[1](https://example.com/second)。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} + +func TestReplaceCitationMarkersWithLinksDetectsSpacedReferenceZeroBased(t *testing.T) { + raw := "来源[reference: 0] 与 [reference: 1]。" + links := map[int]string{ + 1: "https://example.com/first", + 2: "https://example.com/second", + } + + got := replaceCitationMarkersWithLinks(raw, links) + want := "来源[0](https://example.com/first) 与 [1](https://example.com/second)。" + if got != want { + t.Fatalf("expected %q, got %q", want, got) + } +} diff --git a/internal/httpapi/openai/deps_injection_test.go b/internal/httpapi/openai/deps_injection_test.go new file mode 100644 index 0000000000000000000000000000000000000000..03a7c000ccac6b82ade02df358daf56c3313729c --- /dev/null +++ b/internal/httpapi/openai/deps_injection_test.go @@ -0,0 +1,109 @@ +package openai + +import ( + "strings" + "testing" + + "ds2api/internal/promptcompat" +) + +type mockOpenAIConfig struct { + aliases map[string]string + autoDeleteMode string + toolMode string + earlyEmit string + responsesTTL int + embedProv string + currentInputEnabled bool + currentInputMin int + thinkingInjection *bool + thinkingPrompt string +} + +func (m mockOpenAIConfig) ModelAliases() map[string]string { return m.aliases } +func (m mockOpenAIConfig) ToolcallMode() string { return m.toolMode } +func (m mockOpenAIConfig) ToolcallEarlyEmitConfidence() string { return m.earlyEmit } +func (m mockOpenAIConfig) ResponsesStoreTTLSeconds() int { return m.responsesTTL } +func (m mockOpenAIConfig) EmbeddingsProvider() string { return m.embedProv } +func (m mockOpenAIConfig) AutoDeleteMode() string { + if m.autoDeleteMode == "" { + return "none" + } + return m.autoDeleteMode +} +func (m mockOpenAIConfig) AutoDeleteSessions() bool { return false } +func (m mockOpenAIConfig) CurrentInputFileEnabled() bool { return m.currentInputEnabled } +func (m mockOpenAIConfig) CurrentInputFileMinChars() int { + return m.currentInputMin +} +func (m mockOpenAIConfig) ThinkingInjectionEnabled() bool { + if m.thinkingInjection == nil { + return false + } + return *m.thinkingInjection +} +func (m mockOpenAIConfig) ThinkingInjectionPrompt() string { return m.thinkingPrompt } + +func TestNormalizeOpenAIChatRequestWithConfigInterface(t *testing.T) { + cfg := mockOpenAIConfig{ + aliases: map[string]string{ + "my-model": "deepseek-v4-flash-search", + }, + } + req := map[string]any{ + "model": "my-model", + "messages": []any{map[string]any{"role": "user", "content": "hello"}}, + } + out, err := promptcompat.NormalizeOpenAIChatRequest(cfg, req, "") + if err != nil { + t.Fatalf("promptcompat.NormalizeOpenAIChatRequest error: %v", err) + } + if out.ResolvedModel != "deepseek-v4-flash-search" { + t.Fatalf("resolved model mismatch: got=%q", out.ResolvedModel) + } + if !out.Search || !out.Thinking { + t.Fatalf("unexpected model flags: thinking=%v search=%v", out.Thinking, out.Search) + } +} + +func TestNormalizeOpenAIChatRequestDisablesThinkingForNoThinkingModel(t *testing.T) { + cfg := mockOpenAIConfig{} + req := map[string]any{ + "model": "deepseek-v4-pro-nothinking", + "messages": []any{map[string]any{"role": "user", "content": "hello"}}, + "reasoning_effort": "high", + } + out, err := promptcompat.NormalizeOpenAIChatRequest(cfg, req, "") + if err != nil { + t.Fatalf("promptcompat.NormalizeOpenAIChatRequest error: %v", err) + } + if out.ResolvedModel != "deepseek-v4-pro-nothinking" { + t.Fatalf("resolved model mismatch: got=%q", out.ResolvedModel) + } + if out.Thinking { + t.Fatalf("expected nothinking model to force thinking off") + } + if out.Search { + t.Fatalf("expected search=false for deepseek-v4-pro-nothinking, got=%v", out.Search) + } +} + +func TestNormalizeOpenAIResponsesRequestAlwaysAcceptsWideInput(t *testing.T) { + req := map[string]any{ + "model": "deepseek-v4-flash", + "input": "hi", + } + + out, err := promptcompat.NormalizeOpenAIResponsesRequest(mockOpenAIConfig{ + aliases: map[string]string{}, + }, req, "") + if err != nil { + t.Fatalf("unexpected error for wide input request: %v", err) + } + if out.Surface != "openai_responses" { + t.Fatalf("unexpected surface: %q", out.Surface) + } + if !strings.Contains(out.FinalPrompt, "User: hi") { + t.Fatalf("unexpected final prompt: %q", out.FinalPrompt) + } +} diff --git a/internal/httpapi/openai/embeddings/embeddings_handler.go b/internal/httpapi/openai/embeddings/embeddings_handler.go new file mode 100644 index 0000000000000000000000000000000000000000..8c5b340db712cb7f650ecccf66816236a4ddcb44 --- /dev/null +++ b/internal/httpapi/openai/embeddings/embeddings_handler.go @@ -0,0 +1,152 @@ +package embeddings + +import ( + "crypto/sha256" + "encoding/binary" + "encoding/json" + "fmt" + "net/http" + "strings" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/config" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/util" +) + +type Handler struct { + Store shared.ConfigReader + Auth shared.AuthResolver + DS shared.DeepSeekCaller + ChatHistory *chathistory.Store +} + +func (h *Handler) Embeddings(w http.ResponseWriter, r *http.Request) { + a, err := h.Auth.Determine(r) + if err != nil { + status := http.StatusUnauthorized + detail := err.Error() + if err == auth.ErrNoAccount { + status = http.StatusTooManyRequests + } + shared.WriteOpenAIError(w, status, detail) + return + } + defer h.Auth.Release(a) + + r.Body = http.MaxBytesReader(w, r.Body, shared.GeneralMaxSize) + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + if strings.Contains(strings.ToLower(err.Error()), "too large") { + shared.WriteOpenAIError(w, http.StatusRequestEntityTooLarge, "request body too large") + return + } + shared.WriteOpenAIError(w, http.StatusBadRequest, "invalid json") + return + } + model, _ := req["model"].(string) + model = strings.TrimSpace(model) + if model == "" { + shared.WriteOpenAIError(w, http.StatusBadRequest, "Request must include 'model'.") + return + } + if _, ok := config.ResolveModel(h.Store, model); !ok { + shared.WriteOpenAIError(w, http.StatusBadRequest, fmt.Sprintf("Model '%s' is not available.", model)) + return + } + + inputs := ExtractEmbeddingInputs(req["input"]) + if len(inputs) == 0 { + shared.WriteOpenAIError(w, http.StatusBadRequest, "Request must include non-empty 'input'.") + return + } + + provider := "" + if h.Store != nil { + provider = strings.ToLower(strings.TrimSpace(h.Store.EmbeddingsProvider())) + } + if provider == "" { + shared.WriteOpenAIError(w, http.StatusNotImplemented, "Embeddings provider is not configured. Set embeddings.provider in config.") + return + } + switch provider { + case "mock", "deterministic", "builtin": + // supported local deterministic provider + default: + shared.WriteOpenAIError(w, http.StatusNotImplemented, fmt.Sprintf("Embeddings provider '%s' is not supported.", provider)) + return + } + + data := make([]map[string]any, 0, len(inputs)) + totalTokens := 0 + for i, input := range inputs { + totalTokens += util.EstimateTokens(input) + data = append(data, map[string]any{ + "object": "embedding", + "index": i, + "embedding": DeterministicEmbedding(input), + }) + } + shared.WriteJSON(w, http.StatusOK, map[string]any{ + "object": "list", + "data": data, + "model": model, + "usage": map[string]any{ + "prompt_tokens": totalTokens, + "total_tokens": totalTokens, + }, + }) +} + +func ExtractEmbeddingInputs(raw any) []string { + switch v := raw.(type) { + case string: + s := strings.TrimSpace(v) + if s == "" { + return nil + } + return []string{s} + case []any: + out := make([]string, 0, len(v)) + for _, item := range v { + switch iv := item.(type) { + case string: + s := strings.TrimSpace(iv) + if s != "" { + out = append(out, s) + } + case []any: + // Token array input support: convert to stable string form. + out = append(out, fmt.Sprintf("%v", iv)) + default: + s := strings.TrimSpace(fmt.Sprintf("%v", iv)) + if s != "" { + out = append(out, s) + } + } + } + return out + default: + return nil + } +} + +func DeterministicEmbedding(input string) []float64 { + // Keep response shape stable without external dependencies. + const dims = 64 + out := make([]float64, dims) + seed := sha256.Sum256([]byte(input)) + buf := seed[:] + for i := 0; i < dims; i++ { + if len(buf) < 4 { + next := sha256.Sum256(buf) + buf = next[:] + } + v := binary.BigEndian.Uint32(buf[:4]) + buf = buf[4:] + // map [0, 2^32) -> [-1, 1] + out[i] = (float64(v)/2147483647.5 - 1.0) + } + return out +} diff --git a/internal/httpapi/openai/embeddings_route_test.go b/internal/httpapi/openai/embeddings_route_test.go new file mode 100644 index 0000000000000000000000000000000000000000..6962a05329d0a70be58167620d4398a7a84c18cf --- /dev/null +++ b/internal/httpapi/openai/embeddings_route_test.go @@ -0,0 +1,96 @@ +package openai + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" +) + +func newResolverWithConfigJSON(t *testing.T, cfgJSON string) (*config.Store, *auth.Resolver) { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", cfgJSON) + store := config.LoadStore() + pool := account.NewPool(store) + resolver := auth.NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "unused", nil + }) + return store, resolver +} + +func TestEmbeddingsRouteContract(t *testing.T) { + store, resolver := newResolverWithConfigJSON(t, `{"embeddings":{"provider":"deterministic"}}`) + h := &openAITestSurface{Store: store, Auth: resolver} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + t.Run("unauthorized", func(t *testing.T) { + body := bytes.NewBufferString(`{"model":"gpt-4o","input":"hello"}`) + req := httptest.NewRequest(http.MethodPost, "/v1/embeddings", body) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("expected 401, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("ok", func(t *testing.T) { + body := bytes.NewBufferString(`{"model":"gpt-4o","input":["a","b"]}`) + req := httptest.NewRequest(http.MethodPost, "/v1/embeddings", body) + req.Header.Set("Authorization", "Bearer test-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v", err) + } + if out["object"] != "list" { + t.Fatalf("unexpected object: %#v", out["object"]) + } + data, _ := out["data"].([]any) + if len(data) != 2 { + t.Fatalf("expected 2 embeddings, got %d", len(data)) + } + }) +} + +func TestEmbeddingsRouteProviderMissing(t *testing.T) { + store, resolver := newResolverWithConfigJSON(t, `{}`) + h := &openAITestSurface{Store: store, Auth: resolver} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + body := bytes.NewBufferString(`{"model":"gpt-4o","input":"hello"}`) + req := httptest.NewRequest(http.MethodPost, "/v1/embeddings", body) + req.Header.Set("Authorization", "Bearer test-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusNotImplemented { + t.Fatalf("expected 501, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v", err) + } + errObj, _ := out["error"].(map[string]any) + if _, ok := errObj["code"]; !ok { + t.Fatalf("expected error.code in response: %#v", out) + } + if _, ok := errObj["param"]; !ok { + t.Fatalf("expected error.param in response: %#v", out) + } +} diff --git a/internal/httpapi/openai/error_shape_test.go b/internal/httpapi/openai/error_shape_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8c73e4b00ac611599644283c541782cb92381456 --- /dev/null +++ b/internal/httpapi/openai/error_shape_test.go @@ -0,0 +1,34 @@ +package openai + +import ( + "encoding/json" + "net/http" + "net/http/httptest" + "testing" +) + +func TestWriteOpenAIErrorIncludesUnifiedFields(t *testing.T) { + rec := httptest.NewRecorder() + writeOpenAIError(rec, http.StatusBadRequest, "invalid input") + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d", rec.Code) + } + + var body map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatalf("decode body: %v", err) + } + errObj, _ := body["error"].(map[string]any) + if errObj["message"] != "invalid input" { + t.Fatalf("unexpected message: %v", errObj["message"]) + } + if errObj["type"] != "invalid_request_error" { + t.Fatalf("unexpected type: %v", errObj["type"]) + } + if errObj["code"] != "invalid_request" { + t.Fatalf("unexpected code: %v", errObj["code"]) + } + if _, ok := errObj["param"]; !ok { + t.Fatal("expected param field") + } +} diff --git a/internal/httpapi/openai/file_inline_upload_test.go b/internal/httpapi/openai/file_inline_upload_test.go new file mode 100644 index 0000000000000000000000000000000000000000..88978e283eeffd05de3afea3d0c7ea84e29be40c --- /dev/null +++ b/internal/httpapi/openai/file_inline_upload_test.go @@ -0,0 +1,327 @@ +package openai + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" + dsclient "ds2api/internal/deepseek/client" +) + +type inlineUploadDSStub struct { + uploadCalls []dsclient.UploadFileRequest + lastCtx context.Context + completionReq map[string]any + createSession string + uploadErr error + completionResp *http.Response +} + +func (m *inlineUploadDSStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + if strings.TrimSpace(m.createSession) == "" { + return "session-id", nil + } + return m.createSession, nil +} + +func (m *inlineUploadDSStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m *inlineUploadDSStub) UploadFile(ctx context.Context, _ *auth.RequestAuth, req dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + m.lastCtx = ctx + m.uploadCalls = append(m.uploadCalls, req) + if m.uploadErr != nil { + return nil, m.uploadErr + } + id := "file-inline-1" + if len(m.uploadCalls) > 1 { + id = "file-inline-" + fmt.Sprint(len(m.uploadCalls)) + } + return &dsclient.UploadFileResult{ + ID: id, + Filename: req.Filename, + Bytes: int64(len(req.Data)), + Status: "uploaded", + Purpose: req.Purpose, + }, nil +} + +func (m *inlineUploadDSStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + m.completionReq = payload + if m.completionResp != nil { + return m.completionResp, nil + } + return makeOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"ok"}`, + `data: [DONE]`, + ), nil +} + +func (m *inlineUploadDSStub) DeleteSessionForToken(_ context.Context, _ string, _ string) (*dsclient.DeleteSessionResult, error) { + return &dsclient.DeleteSessionResult{Success: true}, nil +} + +func (m *inlineUploadDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +func TestPreprocessInlineFileInputsReplacesDataURLAndCollectsRefFileIDs(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{DS: ds} + req := map[string]any{ + "messages": []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{ + "type": "image_url", + "image_url": map[string]any{"url": "data:image/png;base64,QUJDRA=="}, + }, + }, + }, + }, + } + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + + if err := h.preprocessInlineFileInputs(ctx, &auth.RequestAuth{DeepSeekToken: "token"}, req); err != nil { + t.Fatalf("preprocess failed: %v", err) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected 1 upload, got %d", len(ds.uploadCalls)) + } + if ds.uploadCalls[0].ModelType != "default" { + t.Fatalf("expected default model type when request omits model, got %q", ds.uploadCalls[0].ModelType) + } + if ds.lastCtx != ctx { + t.Fatalf("expected upload to use request context") + } + if ds.uploadCalls[0].ContentType != "image/png" { + t.Fatalf("expected image/png, got %q", ds.uploadCalls[0].ContentType) + } + if ds.uploadCalls[0].Filename != "image.png" { + t.Fatalf("expected inferred filename image.png, got %q", ds.uploadCalls[0].Filename) + } + messages, _ := req["messages"].([]any) + first, _ := messages[0].(map[string]any) + content, _ := first["content"].([]any) + block, _ := content[0].(map[string]any) + if block["type"] != "input_image" { + t.Fatalf("expected input_image replacement, got %#v", block) + } + if block["file_id"] != "file-inline-1" { + t.Fatalf("expected file-inline-1 replacement id, got %#v", block) + } + refIDs, _ := req["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-inline-1" { + t.Fatalf("unexpected ref_file_ids: %#v", req["ref_file_ids"]) + } +} + +func TestPreprocessInlineFileInputsDeduplicatesIdenticalPayloads(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{DS: ds} + req := map[string]any{ + "messages": []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{"type": "image_url", "image_url": map[string]any{"url": "data:image/png;base64,QUJDRA=="}}, + map[string]any{"type": "image_url", "image_url": map[string]any{"url": "data:image/png;base64,QUJDRA=="}}, + }, + }, + }, + } + + if err := h.preprocessInlineFileInputs(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, req); err != nil { + t.Fatalf("preprocess failed: %v", err) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected deduplicated single upload, got %d", len(ds.uploadCalls)) + } + refIDs, _ := req["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-inline-1" { + t.Fatalf("unexpected ref_file_ids after dedupe: %#v", req["ref_file_ids"]) + } +} + +func TestChatCompletionsUploadsInlineFilesBeforeCompletion(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + reqBody := `{"model":"deepseek-v4-vision","messages":[{"role":"user","content":[{"type":"input_text","text":"hi"},{"type":"image_url","image_url":{"url":"data:image/png;base64,QUJDRA=="}}]}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected 1 upload call, got %d", len(ds.uploadCalls)) + } + if ds.uploadCalls[0].ModelType != "vision" { + t.Fatalf("expected vision model type for vision request, got %q", ds.uploadCalls[0].ModelType) + } + if ds.completionReq == nil { + t.Fatal("expected completion payload to be captured") + } + refIDs, _ := ds.completionReq["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-inline-1" { + t.Fatalf("unexpected completion ref_file_ids: %#v", ds.completionReq["ref_file_ids"]) + } +} + +func TestResponsesUploadsInlineFilesBeforeCompletion(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + reqBody := `{"model":"deepseek-v4-pro","input":[{"role":"user","content":[{"type":"input_text","text":"hi"},{"type":"input_image","image_url":{"url":"data:image/png;base64,QUJDRA=="}}]}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected 1 upload call, got %d", len(ds.uploadCalls)) + } + if ds.uploadCalls[0].ModelType != "expert" { + t.Fatalf("expected expert model type for pro request, got %q", ds.uploadCalls[0].ModelType) + } + refIDs, _ := ds.completionReq["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-inline-1" { + t.Fatalf("unexpected completion ref_file_ids: %#v", ds.completionReq["ref_file_ids"]) + } +} + +func TestChatCompletionsInlineUploadFailureReturnsBadRequest(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,%%%"}}]}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } + if ds.completionReq != nil { + t.Fatalf("did not expect completion call on upload decode error") + } +} + +func TestChatCompletionsInlineUploadLimitReturnsBadRequest(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + content := []any{map[string]any{"type": "input_text", "text": "hi"}} + for i := 0; i < 51; i++ { + content = append(content, map[string]any{ + "type": "image_url", + "image_url": map[string]any{"url": "data:image/png;base64,QUJDRA=="}, + }) + } + body, err := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": []any{map[string]any{ + "role": "user", + "content": content, + }}, + "stream": false, + }) + if err != nil { + t.Fatalf("marshal request: %v", err) + } + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(string(body))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "exceeded maximum of 50 inline files per request") { + t.Fatalf("expected inline file limit error, got body=%s", rec.Body.String()) + } + if ds.completionReq != nil { + t.Fatalf("did not expect completion call after inline file limit error") + } +} + +func TestResponsesInlineUploadFailureReturnsInternalServerError(t *testing.T) { + ds := &inlineUploadDSStub{uploadErr: errors.New("boom")} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + reqBody := `{"model":"deepseek-v4-flash","input":[{"role":"user","content":[{"type":"image_url","image_url":{"url":"data:image/png;base64,QUJDRA=="}}]}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusInternalServerError { + t.Fatalf("expected 500, got %d body=%s", rec.Code, rec.Body.String()) + } + if ds.completionReq != nil { + t.Fatalf("did not expect completion call after upload failure") + } +} + +func TestVercelPrepareUploadsInlineFilesBeforeLeasePayload(t *testing.T) { + t.Setenv("VERCEL", "1") + t.Setenv("DS2API_VERCEL_INTERNAL_SECRET", "stream-secret") + ds := &inlineUploadDSStub{} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":[{"type":"input_text","text":"hi"},{"type":"image_url","image_url":{"url":"data:image/png;base64,QUJDRA=="}}]}],"stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions?__stream_prepare=1", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("X-Ds2-Internal-Token", "stream-secret") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected 1 upload call, got %d", len(ds.uploadCalls)) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + payload, _ := out["payload"].(map[string]any) + if payload == nil { + t.Fatalf("expected payload in prepare response, got %#v", out) + } + refIDs, _ := payload["ref_file_ids"].([]any) + if len(refIDs) != 1 || refIDs[0] != "file-inline-1" { + t.Fatalf("unexpected payload ref_file_ids: %#v", payload["ref_file_ids"]) + } +} diff --git a/internal/httpapi/openai/files/file_inline_upload.go b/internal/httpapi/openai/files/file_inline_upload.go new file mode 100644 index 0000000000000000000000000000000000000000..bb3ddcef75f0833b75f1075c72204f77c060d2ad --- /dev/null +++ b/internal/httpapi/openai/files/file_inline_upload.go @@ -0,0 +1,402 @@ +package files + +import ( + "context" + "crypto/sha256" + "encoding/base64" + "fmt" + "mime" + "net/http" + "net/url" + "path/filepath" + "strings" + + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" +) + +const maxInlineFilesPerRequest = 50 + +type inlineFileUploadError struct { + status int + message string + err error +} + +func (e *inlineFileUploadError) Error() string { + if e == nil { + return "" + } + if strings.TrimSpace(e.message) != "" { + return e.message + } + if e.err != nil { + return e.err.Error() + } + return "inline file processing failed" +} + +type inlineUploadState struct { + ctx context.Context + handler *Handler + auth *auth.RequestAuth + modelType string + uploadedByID map[string]string + uploadCount int + inlineFileBytes int +} + +type inlineDecodedFile struct { + Data []byte + ContentType string + Filename string + ReplacementType string +} + +func (h *Handler) PreprocessInlineFileInputs(ctx context.Context, a *auth.RequestAuth, req map[string]any) error { + if h == nil || h.DS == nil || len(req) == 0 { + return nil + } + modelType := "default" + if requestedModel, ok := req["model"].(string); ok { + if resolvedModel, ok := config.ResolveModel(h.Store, requestedModel); ok { + if resolvedType, ok := config.GetModelType(resolvedModel); ok { + modelType = resolvedType + } + } + } + state := &inlineUploadState{ + ctx: ctx, + handler: h, + auth: a, + modelType: modelType, + uploadedByID: map[string]string{}, + } + for _, key := range []string{"messages", "input", "attachments"} { + if raw, ok := req[key]; ok { + updated, err := state.walk(raw) + if err != nil { + return err + } + req[key] = updated + } + } + if refIDs := promptcompat.CollectOpenAIRefFileIDs(req); len(refIDs) > 0 { + req["ref_file_ids"] = stringsToAnySlice(refIDs) + } + if state.inlineFileBytes > 0 { + req["_inline_file_bytes"] = state.inlineFileBytes + } + return nil +} + +func WriteInlineFileError(w http.ResponseWriter, err error) { + inlineErr, ok := err.(*inlineFileUploadError) + if !ok || inlineErr == nil { + shared.WriteOpenAIError(w, http.StatusInternalServerError, "Failed to process file input.") + return + } + status := inlineErr.status + if status == 0 { + status = http.StatusInternalServerError + } + message := strings.TrimSpace(inlineErr.message) + if message == "" { + message = "Failed to process file input." + } + shared.WriteOpenAIError(w, status, message) +} + +func (s *inlineUploadState) walk(raw any) (any, error) { + switch x := raw.(type) { + case []any: + out := make([]any, len(x)) + for i, item := range x { + updated, err := s.walk(item) + if err != nil { + return nil, err + } + out[i] = updated + } + return out, nil + case map[string]any: + if replacement, replaced, err := s.tryUploadBlock(x); replaced || err != nil { + return replacement, err + } + for _, key := range []string{"messages", "input", "attachments", "content", "files", "items", "data", "source", "file", "image_url"} { + if nested, ok := x[key]; ok { + updated, err := s.walk(nested) + if err != nil { + return nil, err + } + x[key] = updated + } + } + return x, nil + default: + return raw, nil + } +} + +func (s *inlineUploadState) tryUploadBlock(block map[string]any) (map[string]any, bool, error) { + decoded, ok, err := decodeOpenAIInlineFileBlock(block) + if err != nil { + return nil, true, &inlineFileUploadError{status: http.StatusBadRequest, message: err.Error(), err: err} + } + if !ok { + return nil, false, nil + } + if s.uploadCount >= maxInlineFilesPerRequest { + err := fmt.Errorf("exceeded maximum of %d inline files per request", maxInlineFilesPerRequest) + return nil, true, &inlineFileUploadError{status: http.StatusBadRequest, message: err.Error(), err: err} + } + fileID, err := s.uploadInlineFile(decoded) + if err != nil { + return nil, true, &inlineFileUploadError{status: http.StatusInternalServerError, message: "Failed to upload inline file.", err: err} + } + s.uploadCount++ + s.inlineFileBytes += len(decoded.Data) + replacement := map[string]any{ + "type": decoded.ReplacementType, + "file_id": fileID, + } + if decoded.Filename != "" { + replacement["filename"] = decoded.Filename + } + if decoded.ContentType != "" { + replacement["mime_type"] = decoded.ContentType + } + return replacement, true, nil +} + +func (s *inlineUploadState) uploadInlineFile(file inlineDecodedFile) (string, error) { + sum := sha256.Sum256(append([]byte(file.ContentType+"\x00"+file.Filename+"\x00"), file.Data...)) + cacheKey := fmt.Sprintf("%x", sum[:]) + if fileID, ok := s.uploadedByID[cacheKey]; ok && strings.TrimSpace(fileID) != "" { + return fileID, nil + } + contentType := strings.TrimSpace(file.ContentType) + if contentType == "" { + contentType = http.DetectContentType(file.Data) + } + result, err := s.handler.DS.UploadFile(s.ctx, s.auth, dsclient.UploadFileRequest{ + Filename: file.Filename, + ContentType: contentType, + ModelType: s.modelType, + Data: file.Data, + }, 3) + if err != nil { + return "", err + } + fileID := strings.TrimSpace(result.ID) + if fileID == "" { + return "", fmt.Errorf("upload succeeded without file id") + } + s.uploadedByID[cacheKey] = fileID + return fileID, nil +} + +func decodeOpenAIInlineFileBlock(block map[string]any) (inlineDecodedFile, bool, error) { + if block == nil { + return inlineDecodedFile{}, false, nil + } + if strings.TrimSpace(shared.AsString(block["file_id"])) != "" { + return inlineDecodedFile{}, false, nil + } + if nested, ok := block["file"].(map[string]any); ok { + decoded, matched, err := decodeOpenAIInlineFileBlock(nested) + if err != nil || !matched { + return decoded, matched, err + } + if decoded.Filename == "" { + decoded.Filename = pickInlineFilename(block, decoded.ContentType, defaultInlinePrefix(decoded.ReplacementType)) + } + return decoded, true, nil + } + blockType := strings.ToLower(strings.TrimSpace(shared.AsString(block["type"]))) + if raw, matched := extractInlineImageDataURL(block); matched { + data, contentType, err := decodeInlinePayload(raw, contentTypeFromMap(block)) + if err != nil { + return inlineDecodedFile{}, true, fmt.Errorf("invalid image input") + } + return inlineDecodedFile{ + Data: data, + ContentType: contentType, + Filename: pickInlineFilename(block, contentType, "image"), + ReplacementType: "input_image", + }, true, nil + } + if raw, matched := extractInlineFilePayload(block, blockType); matched { + data, contentType, err := decodeInlinePayload(raw, contentTypeFromMap(block)) + if err != nil { + return inlineDecodedFile{}, true, fmt.Errorf("invalid file input") + } + return inlineDecodedFile{ + Data: data, + ContentType: contentType, + Filename: pickInlineFilename(block, contentType, defaultInlinePrefix(blockType)), + ReplacementType: "input_file", + }, true, nil + } + return inlineDecodedFile{}, false, nil +} + +func extractInlineImageDataURL(block map[string]any) (string, bool) { + imageURL := block["image_url"] + switch x := imageURL.(type) { + case string: + if isDataURL(x) { + return strings.TrimSpace(x), true + } + case map[string]any: + if raw := strings.TrimSpace(shared.AsString(x["url"])); isDataURL(raw) { + return raw, true + } + } + if raw := strings.TrimSpace(shared.AsString(block["url"])); isDataURL(raw) { + return raw, true + } + return "", false +} + +func extractInlineFilePayload(block map[string]any, blockType string) (string, bool) { + for _, value := range []any{block["file_data"], block["base64"], block["data"]} { + if raw := strings.TrimSpace(shared.AsString(value)); raw != "" { + if strings.Contains(blockType, "file") || block["file_data"] != nil || block["filename"] != nil || block["file_name"] != nil || block["name"] != nil { + return raw, true + } + } + } + return "", false +} + +func decodeInlinePayload(raw string, explicitContentType string) ([]byte, string, error) { + raw = strings.TrimSpace(raw) + if raw == "" { + return nil, "", fmt.Errorf("empty payload") + } + if isDataURL(raw) { + return decodeDataURL(raw, explicitContentType) + } + decoded, err := decodeBase64Flexible(raw) + if err != nil { + return nil, "", err + } + contentType := strings.TrimSpace(explicitContentType) + if contentType == "" && len(decoded) > 0 { + contentType = http.DetectContentType(decoded) + } + return decoded, contentType, nil +} + +func decodeDataURL(raw string, explicitContentType string) ([]byte, string, error) { + raw = strings.TrimSpace(raw) + if !isDataURL(raw) { + return nil, "", fmt.Errorf("unsupported data url") + } + header, payload, ok := strings.Cut(raw, ",") + if !ok { + return nil, "", fmt.Errorf("invalid data url") + } + meta := strings.TrimSpace(strings.TrimPrefix(header, "data:")) + contentType := strings.TrimSpace(explicitContentType) + if contentType == "" { + contentType = "application/octet-stream" + if meta != "" { + parts := strings.Split(meta, ";") + if len(parts) > 0 && strings.TrimSpace(parts[0]) != "" { + contentType = strings.TrimSpace(parts[0]) + } + } + } + if strings.Contains(strings.ToLower(meta), ";base64") { + decoded, err := decodeBase64Flexible(payload) + if err != nil { + return nil, "", err + } + return decoded, contentType, nil + } + decoded, err := url.PathUnescape(payload) + if err != nil { + return nil, "", err + } + return []byte(decoded), contentType, nil +} + +func decodeBase64Flexible(raw string) ([]byte, error) { + raw = strings.TrimSpace(raw) + for _, enc := range []*base64.Encoding{base64.StdEncoding, base64.RawStdEncoding, base64.URLEncoding, base64.RawURLEncoding} { + decoded, err := enc.DecodeString(raw) + if err == nil { + return decoded, nil + } + } + return nil, fmt.Errorf("invalid base64 payload") +} + +func contentTypeFromMap(block map[string]any) string { + for _, value := range []any{block["mime_type"], block["mimeType"], block["content_type"], block["contentType"], block["media_type"], block["mediaType"]} { + if contentType := strings.TrimSpace(shared.AsString(value)); contentType != "" { + return contentType + } + } + if imageURL, ok := block["image_url"].(map[string]any); ok { + for _, value := range []any{imageURL["mime_type"], imageURL["mimeType"], imageURL["content_type"], imageURL["contentType"]} { + if contentType := strings.TrimSpace(shared.AsString(value)); contentType != "" { + return contentType + } + } + } + return "" +} + +func pickInlineFilename(block map[string]any, contentType string, prefix string) string { + for _, value := range []any{block["filename"], block["file_name"], block["name"]} { + if name := strings.TrimSpace(shared.AsString(value)); name != "" { + return filepath.Base(name) + } + } + if prefix == "" { + prefix = "upload" + } + ext := ".bin" + if parsedType := strings.TrimSpace(contentType); parsedType != "" { + if comma := strings.Index(parsedType, ";"); comma >= 0 { + parsedType = strings.TrimSpace(parsedType[:comma]) + } + if exts, err := mime.ExtensionsByType(parsedType); err == nil && len(exts) > 0 && strings.TrimSpace(exts[0]) != "" { + ext = exts[0] + } + } + return prefix + ext +} + +func defaultInlinePrefix(blockType string) string { + blockType = strings.ToLower(strings.TrimSpace(blockType)) + if strings.Contains(blockType, "image") { + return "image" + } + return "upload" +} + +func isDataURL(raw string) bool { + return strings.HasPrefix(strings.ToLower(strings.TrimSpace(raw)), "data:") +} + +func stringsToAnySlice(items []string) []any { + out := make([]any, 0, len(items)) + for _, item := range items { + trimmed := strings.TrimSpace(item) + if trimmed == "" { + continue + } + out = append(out, trimmed) + } + if len(out) == 0 { + return nil + } + return out +} diff --git a/internal/httpapi/openai/files/handler_files.go b/internal/httpapi/openai/files/handler_files.go new file mode 100644 index 0000000000000000000000000000000000000000..ad1a4668e17aacd1987e7a3307ffac002b157545 --- /dev/null +++ b/internal/httpapi/openai/files/handler_files.go @@ -0,0 +1,188 @@ +package files + +import ( + "context" + "errors" + "io" + "net/http" + "strings" + "time" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/httpapi/openai/shared" +) + +const openAIUploadMaxMemory = 32 << 20 + +type Handler struct { + Store shared.ConfigReader + Auth shared.AuthResolver + DS shared.DeepSeekCaller + ChatHistory *chathistory.Store +} + +type fileFetcher interface { + FetchUploadedFile(ctx context.Context, a *auth.RequestAuth, fileID string) (*dsclient.UploadFileResult, error) +} + +func (h *Handler) UploadFile(w http.ResponseWriter, r *http.Request) { + a, err := h.Auth.Determine(r) + if err != nil { + status := http.StatusUnauthorized + detail := err.Error() + if err == auth.ErrNoAccount { + status = http.StatusTooManyRequests + } + shared.WriteOpenAIError(w, status, detail) + return + } + defer h.Auth.Release(a) + if !strings.HasPrefix(strings.ToLower(strings.TrimSpace(r.Header.Get("Content-Type"))), "multipart/form-data") { + shared.WriteOpenAIError(w, http.StatusBadRequest, "content-type must be multipart/form-data") + return + } + // Enforce a hard cap on the total request body size to prevent OOM + r.Body = http.MaxBytesReader(w, r.Body, shared.UploadMaxSize) + if err := r.ParseMultipartForm(openAIUploadMaxMemory); err != nil { + if strings.Contains(strings.ToLower(err.Error()), "too large") { + shared.WriteOpenAIError(w, http.StatusRequestEntityTooLarge, "file size exceeds limit") + return + } + shared.WriteOpenAIError(w, http.StatusBadRequest, "invalid multipart form") + return + } + if r.MultipartForm != nil { + defer func() { _ = r.MultipartForm.RemoveAll() }() + } + r = r.WithContext(auth.WithAuth(r.Context(), a)) + file, header, err := r.FormFile("file") + if err != nil { + shared.WriteOpenAIError(w, http.StatusBadRequest, "file is required") + return + } + defer func() { _ = file.Close() }() + data, err := io.ReadAll(file) + if err != nil { + shared.WriteOpenAIError(w, http.StatusBadRequest, "failed to read uploaded file") + return + } + contentType := strings.TrimSpace(header.Header.Get("Content-Type")) + if contentType == "" && len(data) > 0 { + contentType = http.DetectContentType(data) + } + modelType := resolveUploadModelType(h.Store, r) + result, err := h.DS.UploadFile(r.Context(), a, dsclient.UploadFileRequest{ + Filename: header.Filename, + ContentType: contentType, + Purpose: strings.TrimSpace(r.FormValue("purpose")), + ModelType: modelType, + Data: data, + }, 3) + if err != nil { + shared.WriteOpenAIError(w, http.StatusInternalServerError, "Failed to upload file.") + return + } + if result != nil && result.AccountID == "" { + result.AccountID = a.AccountID + } + shared.WriteJSON(w, http.StatusOK, buildOpenAIFileObject(result)) +} + +func (h *Handler) RetrieveFile(w http.ResponseWriter, r *http.Request) { + a, err := h.Auth.Determine(r) + if err != nil { + status := http.StatusUnauthorized + detail := err.Error() + if err == auth.ErrNoAccount { + status = http.StatusTooManyRequests + } + shared.WriteOpenAIError(w, status, detail) + return + } + defer h.Auth.Release(a) + + fileID := strings.TrimSpace(chi.URLParam(r, "file_id")) + if fileID == "" { + shared.WriteOpenAIError(w, http.StatusBadRequest, "file_id is required") + return + } + fetcher, ok := h.DS.(fileFetcher) + if !ok { + shared.WriteOpenAIError(w, http.StatusNotImplemented, "file retrieval is not available") + return + } + result, err := fetcher.FetchUploadedFile(r.Context(), a, fileID) + if err != nil { + if errors.Is(err, dsclient.ErrUploadFileNotFound) { + shared.WriteOpenAIError(w, http.StatusNotFound, "file not found") + return + } + shared.WriteOpenAIError(w, http.StatusInternalServerError, "Failed to retrieve file.") + return + } + if result != nil && result.AccountID == "" { + result.AccountID = a.AccountID + } + shared.WriteJSON(w, http.StatusOK, buildOpenAIFileObject(result)) +} + +func resolveUploadModelType(store shared.ConfigReader, r *http.Request) string { + for _, candidate := range []string{r.FormValue("model_type"), r.Header.Get("X-Model-Type")} { + if modelType := normalizeUploadModelType(candidate); modelType != "" { + return modelType + } + } + requestedModel := strings.TrimSpace(r.FormValue("model")) + if requestedModel != "" { + if resolvedModel, ok := config.ResolveModel(store, requestedModel); ok { + if modelType, ok := config.GetModelType(resolvedModel); ok { + return modelType + } + } + } + return "default" +} + +func normalizeUploadModelType(raw string) string { + switch strings.ToLower(strings.TrimSpace(raw)) { + case "default", "expert", "vision": + return strings.ToLower(strings.TrimSpace(raw)) + default: + return "" + } +} + +func buildOpenAIFileObject(result *dsclient.UploadFileResult) map[string]any { + if result == nil { + obj := map[string]any{ + "id": "", + "object": "file", + "bytes": 0, + "created_at": time.Now().Unix(), + "filename": "", + "purpose": "", + "status": "uploaded", + "status_details": nil, + } + return obj + } + obj := map[string]any{ + "id": result.ID, + "object": "file", + "bytes": result.Bytes, + "created_at": time.Now().Unix(), + "filename": result.Filename, + "purpose": result.Purpose, + "status": result.Status, + "status_details": nil, + } + if result.AccountID != "" { + obj["account_id"] = result.AccountID + } + return obj +} diff --git a/internal/httpapi/openai/files_route_test.go b/internal/httpapi/openai/files_route_test.go new file mode 100644 index 0000000000000000000000000000000000000000..722b7957d6028c1498280556ffb8a990bfd07334 --- /dev/null +++ b/internal/httpapi/openai/files_route_test.go @@ -0,0 +1,269 @@ +package openai + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "mime/multipart" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" + dsclient "ds2api/internal/deepseek/client" +) + +type managedFilesAuthStub struct{} + +func (managedFilesAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: true, + DeepSeekToken: "managed-token", + CallerID: "caller:test", + AccountID: "acct-123", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (managedFilesAuthStub) DetermineCaller(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: true, + DeepSeekToken: "managed-token", + CallerID: "caller:test", + AccountID: "acct-123", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (managedFilesAuthStub) Release(_ *auth.RequestAuth) {} + +type filesRouteDSStub struct { + lastReq dsclient.UploadFileRequest + upload *dsclient.UploadFileResult + fetched *dsclient.UploadFileResult + err error +} + +func (m *filesRouteDSStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "", nil +} + +func (m *filesRouteDSStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "", nil +} + +func (m *filesRouteDSStub) UploadFile(_ context.Context, _ *auth.RequestAuth, req dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + m.lastReq = req + if m.err != nil { + return nil, m.err + } + if m.upload != nil { + return m.upload, nil + } + return &dsclient.UploadFileResult{ID: "file-123", Filename: req.Filename, Bytes: int64(len(req.Data)), Purpose: req.Purpose, Status: "uploaded"}, nil +} + +func (m *filesRouteDSStub) FetchUploadedFile(_ context.Context, _ *auth.RequestAuth, fileID string) (*dsclient.UploadFileResult, error) { + if m.err != nil { + return nil, m.err + } + if m.fetched != nil { + return m.fetched, nil + } + return &dsclient.UploadFileResult{ID: fileID, Filename: "notes.txt", Bytes: 11, Purpose: "assistants", Status: "processed"}, nil +} + +func (m *filesRouteDSStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + return nil, errors.New("not implemented") +} + +func (m *filesRouteDSStub) DeleteSessionForToken(_ context.Context, _ string, _ string) (*dsclient.DeleteSessionResult, error) { + return &dsclient.DeleteSessionResult{Success: true}, nil +} + +func (m *filesRouteDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +func newMultipartUploadRequest(t *testing.T, purpose string, filename string, data []byte, model string) *http.Request { + t.Helper() + var body bytes.Buffer + writer := multipart.NewWriter(&body) + if purpose != "" { + if err := writer.WriteField("purpose", purpose); err != nil { + t.Fatalf("write purpose failed: %v", err) + } + } + if model != "" { + if err := writer.WriteField("model", model); err != nil { + t.Fatalf("write model failed: %v", err) + } + } + part, err := writer.CreateFormFile("file", filename) + if err != nil { + t.Fatalf("create form file failed: %v", err) + } + if _, err := part.Write(data); err != nil { + t.Fatalf("write file failed: %v", err) + } + if err := writer.Close(); err != nil { + t.Fatalf("close writer failed: %v", err) + } + req := httptest.NewRequest(http.MethodPost, "/v1/files", &body) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", writer.FormDataContentType()) + return req +} + +func TestFilesRouteUploadSuccess(t *testing.T) { + ds := &filesRouteDSStub{} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + req := newMultipartUploadRequest(t, "assistants", "notes.txt", []byte("hello world"), "deepseek-v4-vision") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if ds.lastReq.Filename != "notes.txt" { + t.Fatalf("expected filename notes.txt, got %q", ds.lastReq.Filename) + } + if ds.lastReq.Purpose != "assistants" { + t.Fatalf("expected purpose assistants, got %q", ds.lastReq.Purpose) + } + if ds.lastReq.ModelType != "vision" { + t.Fatalf("expected vision model type, got %q", ds.lastReq.ModelType) + } + if string(ds.lastReq.Data) != "hello world" { + t.Fatalf("unexpected uploaded data: %q", string(ds.lastReq.Data)) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + if out["object"] != "file" { + t.Fatalf("expected file object, got %#v", out) + } + if out["id"] != "file-123" { + t.Fatalf("expected file id file-123, got %#v", out["id"]) + } + if out["filename"] != "notes.txt" { + t.Fatalf("expected filename notes.txt, got %#v", out["filename"]) + } +} + +func TestFilesRouteUploadIncludesAccountIDForManagedAccount(t *testing.T) { + ds := &filesRouteDSStub{} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: managedFilesAuthStub{}, DS: ds} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + req := newMultipartUploadRequest(t, "assistants", "notes.txt", []byte("hello world"), "deepseek-v4-vision") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + if out["account_id"] != "acct-123" { + t.Fatalf("expected account_id acct-123, got %#v", out["account_id"]) + } +} + +func TestFilesRouteRetrieveSuccess(t *testing.T) { + ds := &filesRouteDSStub{fetched: &dsclient.UploadFileResult{ + ID: "file-123", + Filename: "notes.txt", + Bytes: 11, + Purpose: "assistants", + Status: "processed", + }} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: managedFilesAuthStub{}, DS: ds} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + req := httptest.NewRequest(http.MethodGet, "/v1/files/file-123", nil) + req.Header.Set("Authorization", "Bearer direct-token") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + if out["id"] != "file-123" || out["filename"] != "notes.txt" || out["status"] != "processed" { + t.Fatalf("unexpected file object: %#v", out) + } + if out["account_id"] != "acct-123" { + t.Fatalf("expected account_id acct-123, got %#v", out["account_id"]) + } +} + +func TestFilesRouteRetrieveNotFound(t *testing.T) { + ds := &filesRouteDSStub{err: dsclient.ErrUploadFileNotFound} + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: ds} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + req := httptest.NewRequest(http.MethodGet, "/v1/files/missing-file", nil) + req.Header.Set("Authorization", "Bearer direct-token") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusNotFound { + t.Fatalf("expected 404, got %d body=%s", rec.Code, rec.Body.String()) + } +} + +func TestFilesRouteRejectsNonMultipart(t *testing.T) { + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: &filesRouteDSStub{}} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + req := httptest.NewRequest(http.MethodPost, "/v1/files", bytes.NewBufferString(`{"purpose":"assistants"}`)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } +} + +func TestFilesRouteRequiresFileField(t *testing.T) { + h := &openAITestSurface{Store: mockOpenAIConfig{}, Auth: streamStatusAuthStub{}, DS: &filesRouteDSStub{}} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + var body bytes.Buffer + writer := multipart.NewWriter(&body) + if err := writer.WriteField("purpose", "assistants"); err != nil { + t.Fatalf("write field failed: %v", err) + } + if err := writer.Close(); err != nil { + t.Fatalf("close writer failed: %v", err) + } + req := httptest.NewRequest(http.MethodPost, "/v1/files", &body) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", writer.FormDataContentType()) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/httpapi/openai/history/current_input_file.go b/internal/httpapi/openai/history/current_input_file.go new file mode 100644 index 0000000000000000000000000000000000000000..db3887137c20d0afe894c19d204d933467e0f960 --- /dev/null +++ b/internal/httpapi/openai/history/current_input_file.go @@ -0,0 +1,330 @@ +package history + +import ( + "context" + "crypto/rand" + "math/big" + "strings" + "time" + "unicode/utf8" + + "ds2api/internal/auth" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" +) + +const ( + currentToolsFilename = promptcompat.CurrentToolsContextFilename + currentInputContentType = "text/plain; charset=utf-8" + currentInputPurpose = "assistants" +) + +type CurrentInputConfigReader interface { + CurrentInputFileEnabled() bool + CurrentInputFileMinChars() int +} + +type CurrentInputUploader interface { + UploadFile(ctx context.Context, a *auth.RequestAuth, req dsclient.UploadFileRequest, maxAttempts int) (*dsclient.UploadFileResult, error) +} + +type Service struct { + Store CurrentInputConfigReader + DS CurrentInputUploader +} + +func (s Service) ApplyCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { + if stdReq.CurrentInputFileApplied || s.Store == nil || s.DS == nil || !s.Store.CurrentInputFileEnabled() { + return stdReq, nil + } + modelID := strings.TrimSpace(stdReq.ResolvedModel) + if modelID == "" { + modelID = strings.TrimSpace(stdReq.RequestedModel) + } + modelType, ok := config.GetModelType(modelID) + if !ok || modelType != "default" { + return stdReq, nil + } + _, latestText := latestUserInputForFile(stdReq.Messages) + if strings.TrimSpace(latestText) == "" { + return stdReq, nil + } + minChars := s.Store.CurrentInputFileMinChars() + if minChars > 0 && utf8.RuneCountInString(latestText) < minChars { + return stdReq, nil + } + historyText := promptcompat.BuildOpenAICurrentInputContextTranscript(stdReq.Messages) + if strings.TrimSpace(historyText) == "" { + return stdReq, nil + } + filename := strings.TrimSpace(stdReq.CurrentInputFilename) + if filename == "" { + filename = randomContextFilename() + } + toolsText, toolNames := promptcompat.BuildOpenAIToolsContextTranscript(stdReq.ToolsRaw, stdReq.ToolChoice) + hasToolsFile := strings.TrimSpace(toolsText) != "" + + historyResult, err := s.DS.UploadFile(ctx, a, dsclient.UploadFileRequest{ + Filename: filename, + ContentType: currentInputContentType, + Purpose: currentInputPurpose, + ModelType: modelType, + Data: []byte(historyText), + }, 3) + if err != nil { + return stdReq, err + } + historyID := "" + if historyResult != nil { + historyID = strings.TrimSpace(historyResult.ID) + } + + toolsID := "" + if hasToolsFile { + toolsResult, err := s.DS.UploadFile(ctx, a, dsclient.UploadFileRequest{ + Filename: currentToolsFilename, + ContentType: currentInputContentType, + Purpose: currentInputPurpose, + ModelType: modelType, + Data: []byte(toolsText), + }, 3) + if err != nil { + return stdReq, err + } + if toolsResult != nil { + toolsID = strings.TrimSpace(toolsResult.ID) + } + } + + promptMessages := make([]any, 0, 2) + if hasToolsFile { + toolInstructions, instructionNames := promptcompat.BuildOpenAIToolPromptInstructions(stdReq.ToolsRaw, stdReq.ToolChoice) + if len(stdReq.ToolNames) == 0 { + if len(instructionNames) > 0 { + stdReq.ToolNames = instructionNames + } else if len(toolNames) > 0 { + stdReq.ToolNames = toolNames + } + } + systemText := "Tool descriptions and parameter schemas are attached in context_tools.txt. Use only those tools." + if strings.TrimSpace(toolInstructions) != "" { + systemText += "\n\n" + toolInstructions + } + promptMessages = append(promptMessages, map[string]any{ + "role": "system", + "content": systemText, + }) + } else if len(stdReq.ToolNames) == 0 && len(toolNames) > 0 { + stdReq.ToolNames = toolNames + } + promptMessages = append(promptMessages, map[string]any{ + "role": "user", + "content": currentInputFilePrompt(filename, hasToolsFile), + }) + finalPrompt, _ := promptcompat.BuildOpenAIPrompt(promptMessages, nil, "", stdReq.ToolChoice, stdReq.Thinking) + if strings.TrimSpace(finalPrompt) == "" { + finalPrompt = currentInputFilePrompt(filename, hasToolsFile) + } + + promptTokenText := historyText + if strings.TrimSpace(toolsText) != "" { + if !strings.HasSuffix(promptTokenText, "\n") { + promptTokenText += "\n" + } + promptTokenText += toolsText + } + + stdReq.CurrentInputFileApplied = true + stdReq.CurrentInputFileID = historyID + stdReq.CurrentInputFilename = filename + stdReq.CurrentToolsFileID = toolsID + stdReq.HistoryText = historyText + stdReq.Messages = promptMessages + stdReq.FinalPrompt = finalPrompt + stdReq.PromptTokenText = promptTokenText + stdReq.RefFileIDs = prependUniqueRefFileIDs(stdReq.RefFileIDs, historyID, toolsID) + return stdReq, nil +} + +func (s Service) ReuploadAppliedCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { + if !stdReq.CurrentInputFileApplied || s.Store == nil || s.DS == nil { + return stdReq, nil + } + modelID := strings.TrimSpace(stdReq.ResolvedModel) + if modelID == "" { + modelID = strings.TrimSpace(stdReq.RequestedModel) + } + modelType, ok := config.GetModelType(modelID) + if !ok || modelType != "default" { + return stdReq, nil + } + historyText := stdReq.HistoryText + if strings.TrimSpace(historyText) == "" { + return stdReq, nil + } + filename := strings.TrimSpace(stdReq.CurrentInputFilename) + if filename == "" { + filename = randomContextFilename() + } + toolsText, _ := promptcompat.BuildOpenAIToolsContextTranscript(stdReq.ToolsRaw, stdReq.ToolChoice) + hasToolsFile := strings.TrimSpace(toolsText) != "" + + newHistory, err := s.DS.UploadFile(ctx, a, dsclient.UploadFileRequest{ + Filename: filename, + ContentType: currentInputContentType, + Purpose: currentInputPurpose, + ModelType: modelType, + Data: []byte(historyText), + }, 3) + if err != nil { + return stdReq, err + } + newHistoryID := "" + if newHistory != nil { + newHistoryID = strings.TrimSpace(newHistory.ID) + } + + newToolsID := "" + if hasToolsFile { + newTools, err := s.DS.UploadFile(ctx, a, dsclient.UploadFileRequest{ + Filename: currentToolsFilename, + ContentType: currentInputContentType, + Purpose: currentInputPurpose, + ModelType: modelType, + Data: []byte(toolsText), + }, 3) + if err != nil { + return stdReq, err + } + if newTools != nil { + newToolsID = strings.TrimSpace(newTools.ID) + } + } + + stdReq.RefFileIDs = replaceGeneratedCurrentInputRefs(stdReq.RefFileIDs, stdReq.CurrentInputFileID, stdReq.CurrentToolsFileID, newHistoryID, newToolsID) + stdReq.CurrentInputFileID = newHistoryID + stdReq.CurrentInputFilename = filename + stdReq.CurrentToolsFileID = newToolsID + return stdReq, nil +} + +func latestUserInputForFile(messages []any) (int, string) { + for i := len(messages) - 1; i >= 0; i-- { + msg, ok := messages[i].(map[string]any) + if !ok { + continue + } + role := strings.ToLower(strings.TrimSpace(shared.AsString(msg["role"]))) + if role != "user" { + continue + } + text := promptcompat.NormalizeOpenAIContentForPrompt(msg["content"]) + if strings.TrimSpace(text) == "" { + return -1, "" + } + return i, text + } + return -1, "" +} + +func currentInputFilePrompt(filename string, hasToolsFile bool) string { + base := []string{ + "已附上下文文件「%s」。请直接回答最新问题,不要提及文件、上下文或提示词本身。", + "请从文件 %s 继续处理本轮任务,给出答案,避免说明你在使用上下文文件。", + "上下文已存入 %s,请将其视为唯一上下文并直接答复;不要复述提示或提到文件。", + } + prompt := pickPromptVariant(base) + text := strings.ReplaceAll(prompt, "%s", filename) + if hasToolsFile { + text += " 可用工具与参数说明在 context_tools.txt 中,仅可依据该文件调用工具。" + } + return text +} + +func pickPromptVariant(variants []string) string { + if len(variants) == 0 { + return "" + } + idx := 0 + if len(variants) > 1 { + max := big.NewInt(int64(len(variants))) + if v, err := rand.Int(rand.Reader, max); err == nil { + idx = int(v.Int64()) + } else { + idx = int(time.Now().UnixNano()) % len(variants) + if idx < 0 { + idx = 0 + } + } + } + return variants[idx] +} + +func randomContextFilename() string { + const letters = "abcdefghijklmnopqrstuvwxyz0123456789" + length := 12 + max := big.NewInt(int64(len(letters))) + b := make([]byte, 0, length) + for i := 0; i < length; i++ { + idx, err := rand.Int(rand.Reader, max) + if err != nil { + idx = big.NewInt(int64(time.Now().UnixNano() % int64(len(letters)))) + } + b = append(b, letters[idx.Int64()]) + } + return "ctx-" + string(b) + ".txt" +} + +func prependUniqueRefFileIDs(existing []string, fileIDs ...string) []string { + out := make([]string, 0, len(existing)+len(fileIDs)) + seen := map[string]struct{}{} + for _, fileID := range fileIDs { + trimmed := strings.TrimSpace(fileID) + if trimmed == "" { + continue + } + key := strings.ToLower(trimmed) + if _, ok := seen[key]; ok { + continue + } + out = append(out, trimmed) + seen[key] = struct{}{} + } + for _, id := range existing { + trimmed := strings.TrimSpace(id) + if trimmed == "" { + continue + } + key := strings.ToLower(trimmed) + if _, ok := seen[key]; ok { + continue + } + out = append(out, trimmed) + seen[key] = struct{}{} + } + return out +} + +func replaceGeneratedCurrentInputRefs(existing []string, oldHistoryID, oldToolsID, newHistoryID, newToolsID string) []string { + filtered := make([]string, 0, len(existing)) + old := map[string]struct{}{} + for _, id := range []string{oldHistoryID, oldToolsID} { + trimmed := strings.ToLower(strings.TrimSpace(id)) + if trimmed != "" { + old[trimmed] = struct{}{} + } + } + for _, id := range existing { + trimmed := strings.TrimSpace(id) + if trimmed == "" { + continue + } + if _, ok := old[strings.ToLower(trimmed)]; ok { + continue + } + filtered = append(filtered, trimmed) + } + return prependUniqueRefFileIDs(filtered, newHistoryID, newToolsID) +} diff --git a/internal/httpapi/openai/history/history_split_error.go b/internal/httpapi/openai/history/history_split_error.go new file mode 100644 index 0000000000000000000000000000000000000000..df7c503a3e85c570f52af67a4d7b60c9a9e8c49f --- /dev/null +++ b/internal/httpapi/openai/history/history_split_error.go @@ -0,0 +1,18 @@ +package history + +import ( + "net/http" + + dsclient "ds2api/internal/deepseek/client" +) + +func MapError(err error) (int, string) { + switch { + case dsclient.IsManagedUnauthorizedError(err): + return http.StatusUnauthorized, "Account token is invalid. Please re-login the account in admin." + case dsclient.IsDirectUnauthorizedError(err): + return http.StatusUnauthorized, "Invalid token. If this should be a DS2API key, add it to config.keys first." + default: + return http.StatusInternalServerError, err.Error() + } +} diff --git a/internal/httpapi/openai/history_split_test.go b/internal/httpapi/openai/history_split_test.go new file mode 100644 index 0000000000000000000000000000000000000000..4226bf6ac57084a2ca3b3bfbdfb2d558b9cdadf8 --- /dev/null +++ b/internal/httpapi/openai/history_split_test.go @@ -0,0 +1,732 @@ +package openai + +import ( + "context" + "encoding/json" + "errors" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/promptcompat" + "ds2api/internal/util" +) + +func historySplitTestMessages() []any { + toolCalls := []any{ + map[string]any{ + "name": "search", + "arguments": map[string]any{"query": "docs"}, + }, + } + return []any{ + map[string]any{"role": "system", "content": "system instructions"}, + map[string]any{"role": "user", "content": "first user turn"}, + map[string]any{ + "role": "assistant", + "content": "", + "reasoning_content": "hidden reasoning", + "tool_calls": toolCalls, + }, + map[string]any{ + "role": "tool", + "name": "search", + "tool_call_id": "call-1", + "content": "tool result", + }, + map[string]any{"role": "user", "content": "latest user turn"}, + } +} + +type streamStatusManagedAuthStub struct{} + +func (streamStatusManagedAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: true, + DeepSeekToken: "managed-token", + CallerID: "caller:test", + AccountID: "acct:test", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (streamStatusManagedAuthStub) DetermineCaller(_ *http.Request) (*auth.RequestAuth, error) { + return (&streamStatusManagedAuthStub{}).Determine(nil) +} + +func (streamStatusManagedAuthStub) Release(_ *auth.RequestAuth) {} + +func TestBuildOpenAICurrentInputContextTranscriptUsesNumberedHistorySections(t *testing.T) { + transcript := buildOpenAICurrentInputContextTranscript(historySplitTestMessages()) + + if strings.Contains(transcript, "[file content end]") || strings.Contains(transcript, "[file content begin]") || strings.Contains(transcript, "[file name]:") { + t.Fatalf("expected transcript without file wrapper tags, got %q", transcript) + } + if !strings.Contains(transcript, "# context_context.txt") { + t.Fatalf("expected history transcript header, got %q", transcript) + } + for _, want := range []string{ + "=== 1 ===", + "[r=0]", + "=== 2 ===", + "[r=1]", + "=== 3 ===", + "[r=2]", + "=== 4 ===", + "[r=3]", + "=== 5 ===", + "first user turn", + "tool result", + "latest user turn", + "[reasoning_content]", + "hidden reasoning", + "<|DSML|tool_calls>", + } { + if !strings.Contains(transcript, want) { + t.Fatalf("expected transcript to contain %q, got %q", want, transcript) + } + } +} + +func TestApplyCurrentInputFileSkipsShortInputWhenThresholdNotReached(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + currentInputMin: 10, + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-flash", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply current input file failed: %v", err) + } + if len(ds.uploadCalls) != 0 { + t.Fatalf("expected no upload on first turn, got %d", len(ds.uploadCalls)) + } + if out.FinalPrompt != stdReq.FinalPrompt { + t.Fatalf("expected prompt unchanged on first turn") + } +} + +func TestApplyThinkingInjectionAppendsLatestUserPrompt(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + thinkingInjection: boolPtr(true), + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-flash", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply thinking injection failed: %v", err) + } + if len(ds.uploadCalls) != 0 { + t.Fatalf("expected no upload for first short turn, got %d", len(ds.uploadCalls)) + } + if out.FinalPrompt != stdReq.FinalPrompt { + t.Fatalf("expected prompt unchanged when thinking injection is disabled") + } +} + +func TestApplyThinkingInjectionUsesCustomPrompt(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + thinkingInjection: boolPtr(true), + thinkingPrompt: "custom thinking format", + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-flash", + "messages": []any{ + map[string]any{"role": "user", "content": "hello"}, + }, + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply thinking injection failed: %v", err) + } + if out.FinalPrompt != stdReq.FinalPrompt { + t.Fatalf("expected prompt unchanged when thinking injection is disabled") + } +} + +func TestApplyCurrentInputFileDisabledPassThrough(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: false, + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-vision", + "messages": historySplitTestMessages(), + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply current input file failed: %v", err) + } + if len(ds.uploadCalls) != 0 { + t.Fatalf("expected no uploads when both split modes are disabled, got %d", len(ds.uploadCalls)) + } + if out.CurrentInputFileApplied || out.HistoryText != "" { + t.Fatalf("expected direct pass-through, got current_input=%v history=%q", out.CurrentInputFileApplied, out.HistoryText) + } + if !strings.Contains(out.FinalPrompt, "first user turn") || !strings.Contains(out.FinalPrompt, "latest user turn") { + t.Fatalf("expected original prompt context to stay inline, got %s", out.FinalPrompt) + } +} + +func TestApplyCurrentInputFileUploadsFirstTurnWithNumberedHistoryTranscript(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + currentInputMin: 10, + thinkingInjection: boolPtr(true), + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-flash", + "messages": []any{ + map[string]any{"role": "user", "content": "first turn content that is long enough"}, + }, + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply current input file failed: %v", err) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected current input upload, got %d", len(ds.uploadCalls)) + } + if !out.CurrentInputFileApplied { + t.Fatalf("expected current input file to be applied") + } + if !strings.Contains(out.FinalPrompt, ds.uploadCalls[0].Filename) { + t.Fatalf("expected continuation prompt, got %q", out.FinalPrompt) + } + historyText := string(ds.uploadCalls[0].Data) + if !strings.Contains(historyText, "# context_context.txt") || !strings.Contains(historyText, "=== 1 ===") { + t.Fatalf("expected numbered history transcript, got %q", historyText) + } +} + +func TestApplyCurrentInputFilePreservesFullContextPromptForTokenCounting(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + currentInputMin: 0, + thinkingInjection: boolPtr(true), + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-vision", + "messages": historySplitTestMessages(), + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply current input file failed: %v", err) + } + if out.FinalPrompt != stdReq.FinalPrompt { + t.Fatalf("expected live prompt unchanged when current input file is disabled") + } +} + +func TestApplyCurrentInputFileUploadsFullContextFile(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + currentInputMin: 0, + thinkingInjection: boolPtr(true), + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-vision", + "messages": historySplitTestMessages(), + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply current input file failed: %v", err) + } + if out.CurrentInputFileApplied { + t.Fatalf("expected current input file to remain disabled") + } + if len(ds.uploadCalls) != 0 { + t.Fatalf("expected no current input upload, got %d", len(ds.uploadCalls)) + } + if out.FinalPrompt != stdReq.FinalPrompt { + t.Fatalf("expected live prompt unchanged when current input file is disabled") + } +} + +func TestApplyCurrentInputFileUploadsToolsContextSeparately(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + currentInputMin: 0, + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "tools": []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + }, + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply current input file failed: %v", err) + } + if len(ds.uploadCalls) != 2 { + t.Fatalf("expected history and tools uploads, got %d", len(ds.uploadCalls)) + } + if strings.Contains(strings.ToLower(ds.uploadCalls[0].Filename), "history") || !strings.HasSuffix(ds.uploadCalls[0].Filename, ".txt") || ds.uploadCalls[1].Filename != "context_tools.txt" { + t.Fatalf("unexpected upload filenames: %#v", ds.uploadCalls) + } + historyText := string(ds.uploadCalls[0].Data) + if strings.Contains(historyText, "Description: search docs") { + t.Fatalf("history transcript should not embed tool descriptions, got %q", historyText) + } + toolsText := string(ds.uploadCalls[1].Data) + if !strings.Contains(toolsText, "# context_tools.txt") || !strings.Contains(toolsText, "Tool: search") || !strings.Contains(toolsText, "Description: search docs") { + t.Fatalf("expected tools transcript to include schema, got %q", toolsText) + } + if !strings.Contains(out.FinalPrompt, "context_tools.txt") || !strings.Contains(out.FinalPrompt, "TOOL CALL SCHEME") { + t.Fatalf("expected prompt to reference tools file and include tool instructions, got %q", out.FinalPrompt) + } +} + +func TestApplyCurrentInputFileCarriesHistoryText(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + DS: ds, + } + req := map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + } + stdReq, err := promptcompat.NormalizeOpenAIChatRequest(h.Store, req, "") + if err != nil { + t.Fatalf("normalize failed: %v", err) + } + + out, err := h.applyCurrentInputFile(context.Background(), &auth.RequestAuth{DeepSeekToken: "token"}, stdReq) + if err != nil { + t.Fatalf("apply current input file failed: %v", err) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected history upload, got %d", len(ds.uploadCalls)) + } + if !out.CurrentInputFileApplied { + t.Fatalf("expected current input file to be applied") + } + if out.HistoryText != string(ds.uploadCalls[0].Data) { + t.Fatalf("expected history text to match uploaded file") + } +} + +func TestChatCompletionsCurrentInputFileUploadsContextAndKeepsNeutralPrompt(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": false, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected 1 upload call, got %d", len(ds.uploadCalls)) + } + upload := ds.uploadCalls[0] + if strings.Contains(strings.ToLower(upload.Filename), "history") || !strings.HasSuffix(upload.Filename, ".txt") { + t.Fatalf("unexpected upload filename: %q", upload.Filename) + } + if upload.Purpose != "assistants" { + t.Fatalf("unexpected purpose: %q", upload.Purpose) + } + historyText := string(upload.Data) + if strings.Contains(historyText, "[file content end]") || strings.Contains(historyText, "[file content begin]") || strings.Contains(historyText, "[file name]:") { + t.Fatalf("expected history transcript without file wrapper tags, got %s", historyText) + } + if !strings.Contains(historyText, "# context_context.txt") || !strings.Contains(historyText, "=== 1 ===") { + t.Fatalf("expected history transcript to use numbered sections, got %s", historyText) + } + if !strings.Contains(historyText, "latest user turn") { + t.Fatalf("expected full context to include latest turn, got %s", historyText) + } + if ds.completionReq == nil { + t.Fatal("expected completion payload to be captured") + } + promptText, _ := ds.completionReq["prompt"].(string) + if !strings.Contains(promptText, upload.Filename) { + t.Fatalf("expected continuation-oriented prompt, got %s", promptText) + } + if strings.Contains(promptText, "first user turn") || strings.Contains(promptText, "latest user turn") { + t.Fatalf("expected prompt to hide original turns, got %s", promptText) + } + refIDs, _ := ds.completionReq["ref_file_ids"].([]any) + if len(refIDs) == 0 || refIDs[0] != "file-inline-1" { + t.Fatalf("expected uploaded current input file to be first ref_file_id, got %#v", ds.completionReq["ref_file_ids"]) + } + var body map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatalf("decode response failed: %v", err) + } + usage, _ := body["usage"].(map[string]any) + promptTokens := int(usage["prompt_tokens"].(float64)) + neutralCount := util.CountPromptTokens(promptText, "deepseek-v4-flash") + if promptTokens <= neutralCount { + t.Fatalf("expected prompt_tokens to exceed neutral live prompt count (includes file context), got=%d neutral=%d", promptTokens, neutralCount) + } +} + +func TestResponsesCurrentInputFileUploadsContextAndKeepsNeutralPrompt(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": false, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected 1 upload call, got %d", len(ds.uploadCalls)) + } + historyText := string(ds.uploadCalls[0].Data) + if !strings.Contains(historyText, "# context_context.txt") || !strings.Contains(historyText, "=== 1 ===") { + t.Fatalf("expected uploaded history text to use numbered transcript format, got %s", historyText) + } + if ds.completionReq == nil { + t.Fatal("expected completion payload to be captured") + } + promptText, _ := ds.completionReq["prompt"].(string) + if !strings.Contains(promptText, ds.uploadCalls[0].Filename) { + t.Fatalf("expected continuation-oriented prompt, got %s", promptText) + } + if strings.Contains(promptText, "first user turn") || strings.Contains(promptText, "latest user turn") { + t.Fatalf("expected prompt to hide original turns, got %s", promptText) + } + var body map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatalf("decode response failed: %v", err) + } + usage, _ := body["usage"].(map[string]any) + inputTokens := int(usage["input_tokens"].(float64)) + neutralCount := util.CountPromptTokens(promptText, "deepseek-v4-flash") + if inputTokens <= neutralCount { + t.Fatalf("expected input_tokens to exceed neutral live prompt count (includes file context), got=%d neutral=%d", inputTokens, neutralCount) + } +} + +func TestResponsesCurrentInputFileUploadsToolsSeparately(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "tools": []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{"type": "object"}, + }, + }, + }, + "stream": false, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 2 { + t.Fatalf("expected history and tools uploads, got %d", len(ds.uploadCalls)) + } + if strings.Contains(strings.ToLower(ds.uploadCalls[0].Filename), "history") || !strings.HasSuffix(ds.uploadCalls[0].Filename, ".txt") || ds.uploadCalls[1].Filename != "context_tools.txt" { + t.Fatalf("unexpected upload filenames: %#v", ds.uploadCalls) + } + historyText := string(ds.uploadCalls[0].Data) + if strings.Contains(historyText, "Description: search docs") { + t.Fatalf("history transcript should not embed tool descriptions, got %q", historyText) + } + toolsText := string(ds.uploadCalls[1].Data) + if !strings.Contains(toolsText, "# context_tools.txt") || !strings.Contains(toolsText, "Tool: search") || !strings.Contains(toolsText, "Description: search docs") { + t.Fatalf("expected tools transcript to include schema, got %q", toolsText) + } + promptText, _ := ds.completionReq["prompt"].(string) + if !strings.Contains(promptText, "context_tools.txt") || !strings.Contains(promptText, "TOOL CALL SCHEME") { + t.Fatalf("expected live prompt to reference tools file and retain format instructions, got %q", promptText) + } + if strings.Contains(promptText, "Description: search docs") { + t.Fatalf("live prompt should not inline tool descriptions, got %q", promptText) + } + refIDs, _ := ds.completionReq["ref_file_ids"].([]any) + if len(refIDs) < 2 || refIDs[0] != "file-inline-1" || refIDs[1] != "file-inline-2" { + t.Fatalf("expected history and tools ref ids first, got %#v", ds.completionReq["ref_file_ids"]) + } +} + +func TestChatCompletionsCurrentInputFileMapsManagedAuthFailureTo401(t *testing.T) { + ds := &inlineUploadDSStub{ + uploadErr: &dsclient.RequestFailure{Op: "upload file", Kind: dsclient.FailureManagedUnauthorized, Message: "expired token"}, + } + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusManagedAuthStub{}, + DS: ds, + } + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": false, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer managed-key") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusUnauthorized { + t.Fatalf("expected 401, got %d body=%s", rec.Code, rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "Please re-login the account in admin") { + t.Fatalf("expected managed auth error message, got %s", rec.Body.String()) + } +} + +func TestResponsesCurrentInputFileMapsDirectAuthFailureTo401(t *testing.T) { + ds := &inlineUploadDSStub{ + uploadErr: &dsclient.RequestFailure{Op: "upload file", Kind: dsclient.FailureDirectUnauthorized, Message: "invalid token"}, + } + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": false, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusUnauthorized { + t.Fatalf("expected 401, got %d body=%s", rec.Code, rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "Invalid token") { + t.Fatalf("expected direct auth error message, got %s", rec.Body.String()) + } +} + +func TestChatCompletionsCurrentInputFileUploadFailureReturnsInternalServerError(t *testing.T) { + ds := &inlineUploadDSStub{uploadErr: errors.New("boom")} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": false, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusInternalServerError { + t.Fatalf("expected 500, got %d body=%s", rec.Code, rec.Body.String()) + } +} + +func TestCurrentInputFileWorksAcrossAutoDeleteModes(t *testing.T) { + for _, mode := range []string{"none", "single", "all"} { + t.Run(mode, func(t *testing.T) { + ds := &inlineUploadDSStub{} + h := &openAITestSurface{ + Store: mockOpenAIConfig{ + autoDeleteMode: mode, + currentInputEnabled: true, + }, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody, _ := json.Marshal(map[string]any{ + "model": "deepseek-v4-flash", + "messages": historySplitTestMessages(), + "stream": false, + }) + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(string(reqBody))) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + h.ChatCompletions(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.uploadCalls) != 1 { + t.Fatalf("expected current input upload for mode=%s, got %d", mode, len(ds.uploadCalls)) + } + historyText := string(ds.uploadCalls[0].Data) + if !strings.Contains(historyText, "# context_context.txt") || !strings.Contains(historyText, "=== 1 ===") { + t.Fatalf("expected uploaded history text to use numbered transcript format, got %s", historyText) + } + if ds.completionReq == nil { + t.Fatalf("expected completion payload for mode=%s", mode) + } + promptText, _ := ds.completionReq["prompt"].(string) + if strings.Contains(promptText, "first user turn") || strings.Contains(promptText, "latest user turn") { + t.Fatalf("unexpected prompt for mode=%s: %s", mode, promptText) + } + }) + } +} + +func boolPtr(v bool) *bool { + return &v +} diff --git a/internal/httpapi/openai/leaked_output_sanitize_test.go b/internal/httpapi/openai/leaked_output_sanitize_test.go new file mode 100644 index 0000000000000000000000000000000000000000..939f73fb070bc2f2ad7f0613059e1d697c9f1457 --- /dev/null +++ b/internal/httpapi/openai/leaked_output_sanitize_test.go @@ -0,0 +1,118 @@ +package openai + +import "testing" + +func TestSanitizeLeakedOutputRemovesEmptyJSONFence(t *testing.T) { + raw := "before\n```json\n```\nafter" + got := sanitizeLeakedOutput(raw) + if got != "before\n\nafter" { + t.Fatalf("unexpected sanitized empty json fence: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesLeakedWireToolCallAndResult(t *testing.T) { + raw := "开始\n[{\"function\":{\"arguments\":\"{\\\"command\\\":\\\"java -version\\\"}\",\"name\":\"exec\"},\"id\":\"callb9a321\",\"type\":\"function\"}]< | Tool | >{\"content\":\"openjdk version 21\",\"tool_call_id\":\"callb9a321\"}\n结束" + got := sanitizeLeakedOutput(raw) + if got != "开始\n\n结束" { + t.Fatalf("unexpected sanitize result for leaked wire format: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesStandaloneMetaMarkers(t *testing.T) { + raw := "A<| end_of_sentence |><| Assistant |>B<| end_of_thinking |>C<|end▁of▁thinking|>D<|end▁of▁sentence|>E<| end_of_toolresults |>F<|end▁of▁instructions|>G" + got := sanitizeLeakedOutput(raw) + if got != "ABCDEFG" { + t.Fatalf("unexpected sanitize result for meta markers: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesFullwidthDelimitedMetaMarkers(t *testing.T) { + fw := "\uff5c" + raw := "A<" + fw + "end▁of▁sentence" + fw + ">B<" + fw + " Assistant " + fw + ">C<" + fw + "end_of_toolresults" + fw + ">D" + got := sanitizeLeakedOutput(raw) + if got != "ABCD" { + t.Fatalf("unexpected sanitize result for fullwidth-delimited meta markers: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesThinkAndBosMarkers(t *testing.T) { + raw := "ABC<|begin▁of▁sentence|>D<| begin_of_sentence |>E<|begin_of_sentence|>F" + got := sanitizeLeakedOutput(raw) + if got != "ABCDEF" { + t.Fatalf("unexpected sanitize result for think/BOS markers: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesThoughtMarkers(t *testing.T) { + raw := "A<|▁of▁thought|>B<| of_thought |>C<| begin_of_thought |>D<| end_of_thought |>E" + got := sanitizeLeakedOutput(raw) + if got != "ABCDE" { + t.Fatalf("unexpected sanitize result for leaked thought markers: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesFullwidthDelimitedBosAndThoughtMarkers(t *testing.T) { + fw := "\uff5c" + raw := "A<" + fw + "begin▁of▁sentence" + fw + ">B<" + fw + "▁of▁thought" + fw + ">C<" + fw + " begin_of_thought " + fw + ">D" + got := sanitizeLeakedOutput(raw) + if got != "ABCD" { + t.Fatalf("unexpected sanitize result for fullwidth-delimited BOS/thought markers: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesDanglingThinkBlock(t *testing.T) { + raw := "Answer prefixinternal reasoning that never closes" + got := sanitizeLeakedOutput(raw) + if got != "Answer prefix" { + t.Fatalf("unexpected sanitize result for dangling think block: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesCompleteDSMLToolCallWrapper(t *testing.T) { + raw := "前置文本\n<|DSML|tool_calls>\n<|DSML|invoke name=\"Bash\">\n<|DSML|parameter name=\"command\">\n\n\n后置文本" + got := sanitizeLeakedOutput(raw) + if got != "前置文本\n\n后置文本" { + t.Fatalf("unexpected sanitize result for leaked dsml wrapper: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesAgentXMLLeaks(t *testing.T) { + raw := "Done.Some final answer" + got := sanitizeLeakedOutput(raw) + if got != "Done.Some final answer" { + t.Fatalf("unexpected sanitize result for agent XML leak: %q", got) + } +} + +func TestSanitizeLeakedOutputPreservesStandaloneResultTags(t *testing.T) { + raw := "Example XML: value" + got := sanitizeLeakedOutput(raw) + if got != raw { + t.Fatalf("unexpected sanitize result for standalone result tag: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesDanglingAgentXMLOpeningTags(t *testing.T) { + raw := "Done.Some final answer" + got := sanitizeLeakedOutput(raw) + if got != "Done.Some final answer" { + t.Fatalf("unexpected sanitize result for dangling opening tags: %q", got) + } +} + +func TestSanitizeLeakedOutputRemovesDanglingAgentXMLClosingTags(t *testing.T) { + raw := "Done.Some final answer" + got := sanitizeLeakedOutput(raw) + if got != "Done.Some final answer" { + t.Fatalf("unexpected sanitize result for dangling closing tags: %q", got) + } +} + +func TestSanitizeLeakedOutputPreservesUnrelatedResultTagsWhenWrapperLeaks(t *testing.T) { + raw := "Done.Some final answer\nExample XML: value" + got := sanitizeLeakedOutput(raw) + want := "Done.Some final answer\nExample XML: value" + if got != want { + t.Fatalf("unexpected sanitize result for mixed leaked wrapper + xml example: %q", got) + } +} diff --git a/internal/httpapi/openai/models_route_test.go b/internal/httpapi/openai/models_route_test.go new file mode 100644 index 0000000000000000000000000000000000000000..60b014d9952ca8649c920b24330b659e793c2ed4 --- /dev/null +++ b/internal/httpapi/openai/models_route_test.go @@ -0,0 +1,82 @@ +package openai + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" +) + +func TestGetModelRouteDirectAndAlias(t *testing.T) { + h := &openAITestSurface{} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + t.Run("direct", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/models/deepseek-v4-flash", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("direct_nothinking", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/models/deepseek-v4-flash-nothinking", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("direct_expert", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/models/deepseek-v4-pro", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("direct_vision", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/models/deepseek-v4-vision", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("alias", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/models/gpt-4.1", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 for alias, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("alias_nothinking", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/models/claude-sonnet-4-6-nothinking", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 for nothinking alias, got %d body=%s", rec.Code, rec.Body.String()) + } + }) +} + +func TestGetModelRouteNotFound(t *testing.T) { + h := &openAITestSurface{} + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + + req := httptest.NewRequest(http.MethodGet, "/v1/models/not-exists", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusNotFound { + t.Fatalf("expected 404, got %d body=%s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/httpapi/openai/responses/empty_retry_runtime.go b/internal/httpapi/openai/responses/empty_retry_runtime.go new file mode 100644 index 0000000000000000000000000000000000000000..5166f9c7a7c8fdd7707c30968ddaf21072f9e1b5 --- /dev/null +++ b/internal/httpapi/openai/responses/empty_retry_runtime.go @@ -0,0 +1,134 @@ +package responses + +import ( + "io" + "net/http" + "strings" + "time" + + "ds2api/internal/auth" + "ds2api/internal/completionruntime" + "ds2api/internal/config" + dsprotocol "ds2api/internal/deepseek/protocol" + "ds2api/internal/promptcompat" + "ds2api/internal/responsehistory" + streamengine "ds2api/internal/stream" +) + +func (h *Handler) handleResponsesStreamWithRetry(w http.ResponseWriter, r *http.Request, a *auth.RequestAuth, resp *http.Response, payload map[string]any, pow, owner, responseID string, stdReq promptcompat.StandardRequest, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, toolChoice promptcompat.ToolChoicePolicy, traceID string, historySession *responsehistory.Session) { + streamRuntime, initialType, ok := h.prepareResponsesStreamRuntime(w, resp, owner, responseID, model, finalPrompt, refFileTokens, thinkingEnabled, searchEnabled, toolNames, toolsRaw, toolChoice, traceID, historySession) + if !ok { + return + } + completionruntime.ExecuteStreamWithRetry(r.Context(), h.DS, a, resp, payload, pow, completionruntime.StreamRetryOptions{ + Surface: "responses", + Stream: true, + RetryEnabled: emptyOutputRetryEnabled(), + RetryMaxAttempts: emptyOutputRetryMaxAttempts(), + MaxAttempts: 3, + UsagePrompt: finalPrompt, + Request: stdReq, + CurrentInputFile: h.Store, + }, completionruntime.StreamRetryHooks{ + ConsumeAttempt: func(currentResp *http.Response, allowDeferEmpty bool) (bool, bool) { + return h.consumeResponsesStreamAttempt(r, currentResp, streamRuntime, initialType, thinkingEnabled, allowDeferEmpty) + }, + Finalize: func(attempts int) { + streamRuntime.finalize("stop", false) + config.Logger.Info("[openai_empty_retry] terminal empty output", "surface", "responses", "stream", true, "retry_attempts", attempts, "success_source", "none", "error_code", streamRuntime.finalErrorCode) + }, + ParentMessageID: func() int { + return streamRuntime.responseMessageID + }, + OnRetryPrompt: func(prompt string) { + streamRuntime.finalPrompt = prompt + }, + OnRetryFailure: func(status int, message, code string) { + streamRuntime.failResponse(status, strings.TrimSpace(message), code) + }, + OnTerminal: func(attempts int) { + logResponsesStreamTerminal(streamRuntime, attempts) + }, + }) +} + +func (h *Handler) prepareResponsesStreamRuntime(w http.ResponseWriter, resp *http.Response, owner, responseID, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, toolChoice promptcompat.ToolChoicePolicy, traceID string, historySession *responsehistory.Session) (*responsesStreamRuntime, string, bool) { + if resp.StatusCode != http.StatusOK { + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + if historySession != nil { + historySession.Error(resp.StatusCode, strings.TrimSpace(string(body)), "error", "", "") + } + writeOpenAIError(w, resp.StatusCode, strings.TrimSpace(string(body))) + return nil, "", false + } + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + streamRuntime := newResponsesStreamRuntime( + w, rc, canFlush, responseID, model, finalPrompt, thinkingEnabled, searchEnabled, + stripReferenceMarkersEnabled(), toolNames, toolsRaw, len(toolNames) > 0, + h.toolcallFeatureMatchEnabled() && h.toolcallEarlyEmitHighConfidence(), + toolChoice, traceID, func(obj map[string]any) { + h.getResponseStore().put(owner, responseID, obj) + }, historySession, + ) + streamRuntime.refFileTokens = refFileTokens + streamRuntime.sendCreated() + return streamRuntime, initialType, true +} + +func (h *Handler) consumeResponsesStreamAttempt(r *http.Request, resp *http.Response, streamRuntime *responsesStreamRuntime, initialType string, thinkingEnabled bool, allowDeferEmpty bool) (bool, bool) { + defer func() { _ = resp.Body.Close() }() + finalReason := "stop" + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: r.Context(), + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: time.Duration(dsprotocol.KeepAliveTimeout) * time.Second, + IdleTimeout: time.Duration(dsprotocol.StreamIdleTimeout) * time.Second, + MaxKeepAliveNoInput: dsprotocol.MaxKeepaliveCount, + }, streamengine.ConsumeHooks{ + OnParsed: streamRuntime.onParsed, + OnFinalize: func(reason streamengine.StopReason, _ error) { + if string(reason) == "content_filter" { + finalReason = "content_filter" + } + }, + OnContextDone: func() { + streamRuntime.markContextCancelled() + }, + }) + if streamRuntime.finalErrorCode == string(streamengine.StopReasonContextCancelled) { + return true, false + } + terminalWritten := streamRuntime.finalize(finalReason, allowDeferEmpty && finalReason != "content_filter") + if terminalWritten { + return true, false + } + return false, true +} + +func logResponsesStreamTerminal(streamRuntime *responsesStreamRuntime, attempts int) { + source := "first_attempt" + if attempts > 0 { + source = "synthetic_retry" + } + if streamRuntime.finalErrorCode == string(streamengine.StopReasonContextCancelled) { + config.Logger.Info("[openai_empty_retry] terminal cancelled", "surface", "responses", "stream", true, "retry_attempts", attempts, "error_code", streamRuntime.finalErrorCode) + return + } + if streamRuntime.failed { + config.Logger.Info("[openai_empty_retry] terminal empty output", "surface", "responses", "stream", true, "retry_attempts", attempts, "success_source", "none", "error_code", streamRuntime.finalErrorCode) + return + } + config.Logger.Info("[openai_empty_retry] completed", "surface", "responses", "stream", true, "retry_attempts", attempts, "success_source", source) +} diff --git a/internal/httpapi/openai/responses/empty_retry_runtime_test.go b/internal/httpapi/openai/responses/empty_retry_runtime_test.go new file mode 100644 index 0000000000000000000000000000000000000000..00aefec14d02fc7c0480970d62e4134b8a8dc860 --- /dev/null +++ b/internal/httpapi/openai/responses/empty_retry_runtime_test.go @@ -0,0 +1,71 @@ +package responses + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "ds2api/internal/promptcompat" + "ds2api/internal/stream" +) + +func makeResponsesOpenAISSEHTTPResponse(lines ...string) *http.Response { + body := strings.Join(lines, "\n") + if !strings.HasSuffix(body, "\n") { + body += "\n" + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +func TestConsumeResponsesStreamAttemptMarksContextCancelledState(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil).WithContext(ctx) + rec := httptest.NewRecorder() + streamRuntime := newResponsesStreamRuntime( + rec, + http.NewResponseController(rec), + true, + "resp-cancelled", + "deepseek-v4-flash", + "prompt", + false, + false, + true, + nil, + nil, + false, + false, + promptcompat.DefaultToolChoicePolicy(), + "", + nil, + nil, + ) + resp := makeResponsesOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"hello"}`, + `data: [DONE]`, + ) + + h := &Handler{} + terminalWritten, retryable := h.consumeResponsesStreamAttempt(req, resp, streamRuntime, "text", false, true) + if !terminalWritten || retryable { + t.Fatalf("expected cancelled attempt to terminate without retry, got terminalWritten=%v retryable=%v", terminalWritten, retryable) + } + if !streamRuntime.failed { + t.Fatalf("expected cancelled response stream to be marked failed") + } + if got, want := streamRuntime.finalErrorCode, string(stream.StopReasonContextCancelled); got != want { + t.Fatalf("expected cancelled final error code %q, got %q", want, got) + } + if streamRuntime.finalErrorMessage == "" { + t.Fatalf("expected cancelled final error message to be preserved") + } +} diff --git a/internal/httpapi/openai/responses/handler.go b/internal/httpapi/openai/responses/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..da8e2e14c629bfb06f6cff1480df6b6474f6e349 --- /dev/null +++ b/internal/httpapi/openai/responses/handler.go @@ -0,0 +1,108 @@ +package responses + +import ( + "context" + "net/http" + "sync" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/httpapi/openai/files" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" + "ds2api/internal/textclean" + "ds2api/internal/toolstream" +) + +const openAIGeneralMaxSize = shared.GeneralMaxSize + +var writeJSON = shared.WriteJSON + +type Handler struct { + Store shared.ConfigReader + Auth shared.AuthResolver + DS shared.DeepSeekCaller + ChatHistory *chathistory.Store + + responsesMu sync.Mutex + responses *responseStore +} + +func stripReferenceMarkersEnabled() bool { + return textclean.StripReferenceMarkersEnabled() +} + +func (h *Handler) applyCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { + if h == nil { + return stdReq, nil + } + stdReq = shared.ApplyThinkingInjection(h.Store, stdReq) + svc := history.Service{Store: h.Store, DS: h.DS} + out, err := svc.ApplyCurrentInputFile(ctx, a, stdReq) + if err != nil || out.CurrentInputFileApplied { + return out, err + } + return out, nil +} + +func (h *Handler) preprocessInlineFileInputs(ctx context.Context, a *auth.RequestAuth, req map[string]any) error { + if h == nil { + return nil + } + return (&files.Handler{Store: h.Store, Auth: h.Auth, DS: h.DS, ChatHistory: h.ChatHistory}).PreprocessInlineFileInputs(ctx, a, req) +} + +func (h *Handler) toolcallFeatureMatchEnabled() bool { + if h == nil { + return shared.ToolcallFeatureMatchEnabled(nil) + } + return shared.ToolcallFeatureMatchEnabled(h.Store) +} + +func (h *Handler) toolcallEarlyEmitHighConfidence() bool { + if h == nil { + return shared.ToolcallEarlyEmitHighConfidence(nil) + } + return shared.ToolcallEarlyEmitHighConfidence(h.Store) +} + +func writeOpenAIError(w http.ResponseWriter, status int, message string) { + shared.WriteOpenAIError(w, status, message) +} + +func writeOpenAIErrorWithCode(w http.ResponseWriter, status int, message, code string) { + shared.WriteOpenAIErrorWithCode(w, status, message, code) +} + +func openAIErrorType(status int) string { + return shared.OpenAIErrorType(status) +} + +func writeOpenAIInlineFileError(w http.ResponseWriter, err error) { + files.WriteInlineFileError(w, err) +} + +func mapCurrentInputFileError(err error) (int, string) { + return history.MapError(err) +} + +func requestTraceID(r *http.Request) string { + return shared.RequestTraceID(r) +} + +func cleanVisibleOutput(text string, stripReferenceMarkers bool) string { + return shared.CleanVisibleOutput(text, stripReferenceMarkers) +} + +func emptyOutputRetryEnabled() bool { + return shared.EmptyOutputRetryEnabled() +} + +func emptyOutputRetryMaxAttempts() int { + return shared.EmptyOutputRetryMaxAttempts() +} + +func filterIncrementalToolCallDeltasByAllowed(deltas []toolstream.ToolCallDelta, seenNames map[int]string) []toolstream.ToolCallDelta { + return shared.FilterIncrementalToolCallDeltasByAllowed(deltas, seenNames) +} diff --git a/internal/httpapi/openai/responses/ref_file_tokens.go b/internal/httpapi/openai/responses/ref_file_tokens.go new file mode 100644 index 0000000000000000000000000000000000000000..a530340ef013ae551a5d911aa92ee893a581562b --- /dev/null +++ b/internal/httpapi/openai/responses/ref_file_tokens.go @@ -0,0 +1,26 @@ +package responses + +// addRefFileTokensToUsage adds inline-uploaded file token estimates to an existing +// usage map inside a response object. This keeps the token accounting aware of file +// content that the upstream model processes but that is not part of the prompt text. +func addRefFileTokensToUsage(obj map[string]any, refFileTokens int) { + if refFileTokens <= 0 || obj == nil { + return + } + usage, ok := obj["usage"].(map[string]any) + if !ok || usage == nil { + return + } + for _, key := range []string{"input_tokens", "prompt_tokens"} { + if v, ok := usage[key]; ok { + if n, ok := v.(int); ok { + usage[key] = n + refFileTokens + } + } + } + if v, ok := usage["total_tokens"]; ok { + if n, ok := v.(int); ok { + usage["total_tokens"] = n + refFileTokens + } + } +} diff --git a/internal/httpapi/openai/responses/response_store.go b/internal/httpapi/openai/responses/response_store.go new file mode 100644 index 0000000000000000000000000000000000000000..8d7ec75d0d46e93d7829644e735b11576ad40fef --- /dev/null +++ b/internal/httpapi/openai/responses/response_store.go @@ -0,0 +1,109 @@ +package responses + +import ( + "sync" + "time" + + "ds2api/internal/auth" +) + +type storedResponse struct { + Owner string + Value map[string]any + ExpiresAt time.Time +} + +type responseStore struct { + mu sync.Mutex + ttl time.Duration + items map[string]storedResponse +} + +func newResponseStore(ttl time.Duration) *responseStore { + if ttl <= 0 { + ttl = 15 * time.Minute + } + return &responseStore{ + ttl: ttl, + items: make(map[string]storedResponse), + } +} + +func responseStoreKey(owner, id string) string { + return owner + "\x00" + id +} + +func responseStoreOwner(a *auth.RequestAuth) string { + if a == nil { + return "" + } + return a.CallerID +} + +func (s *responseStore) put(owner, id string, value map[string]any) { + if s == nil || owner == "" || id == "" || value == nil { + return + } + now := time.Now() + s.mu.Lock() + defer s.mu.Unlock() + s.sweepLocked(now) + s.items[responseStoreKey(owner, id)] = storedResponse{ + Owner: owner, + Value: cloneAnyMap(value), + ExpiresAt: now.Add(s.ttl), + } +} + +func (s *responseStore) get(owner, id string) (map[string]any, bool) { + if s == nil || owner == "" || id == "" { + return nil, false + } + now := time.Now() + s.mu.Lock() + defer s.mu.Unlock() + s.sweepLocked(now) + item, ok := s.items[responseStoreKey(owner, id)] + if !ok { + return nil, false + } + if item.Owner != owner { + return nil, false + } + return cloneAnyMap(item.Value), true +} + +func (s *responseStore) sweepLocked(now time.Time) { + for k, v := range s.items { + if now.After(v.ExpiresAt) { + delete(s.items, k) + } + } +} + +func cloneAnyMap(in map[string]any) map[string]any { + if in == nil { + return nil + } + out := make(map[string]any, len(in)) + for k, v := range in { + out[k] = v + } + return out +} + +func (h *Handler) getResponseStore() *responseStore { + if h == nil { + return nil + } + h.responsesMu.Lock() + defer h.responsesMu.Unlock() + if h.responses == nil { + ttl := 15 * time.Minute + if h.Store != nil { + ttl = time.Duration(h.Store.ResponsesStoreTTLSeconds()) * time.Second + } + h.responses = newResponseStore(ttl) + } + return h.responses +} diff --git a/internal/httpapi/openai/responses/responses_embeddings_test.go b/internal/httpapi/openai/responses/responses_embeddings_test.go new file mode 100644 index 0000000000000000000000000000000000000000..cfff04b5668af7f6ee30c098d943fd1db21ae3ab --- /dev/null +++ b/internal/httpapi/openai/responses/responses_embeddings_test.go @@ -0,0 +1,227 @@ +package responses + +import ( + "strings" + "testing" + "time" + + "ds2api/internal/httpapi/openai/embeddings" + "ds2api/internal/promptcompat" +) + +func TestNormalizeResponsesInputAsMessagesString(t *testing.T) { + msgs := promptcompat.NormalizeResponsesInputAsMessages("hello") + if len(msgs) != 1 { + t.Fatalf("expected one message, got %d", len(msgs)) + } + m, _ := msgs[0].(map[string]any) + if m["role"] != "user" || m["content"] != "hello" { + t.Fatalf("unexpected message: %#v", m) + } +} + +func TestResponsesMessagesFromRequestWithInstructions(t *testing.T) { + req := map[string]any{ + "model": "gpt-4.1", + "input": "ping", + "instructions": "system text", + } + msgs := promptcompat.ResponsesMessagesFromRequest(req) + if len(msgs) != 2 { + t.Fatalf("expected two messages, got %d", len(msgs)) + } + sys, _ := msgs[0].(map[string]any) + if sys["role"] != "system" { + t.Fatalf("unexpected first message: %#v", sys) + } +} + +func TestNormalizeResponsesInputAsMessagesObjectRoleContentBlocks(t *testing.T) { + msgs := promptcompat.NormalizeResponsesInputAsMessages(map[string]any{ + "role": "user", + "content": []any{ + map[string]any{"type": "input_text", "text": "line-1"}, + map[string]any{"type": "input_text", "text": "line-2"}, + }, + }) + if len(msgs) != 1 { + t.Fatalf("expected one message, got %d", len(msgs)) + } + m, _ := msgs[0].(map[string]any) + if m["role"] != "user" { + t.Fatalf("unexpected role: %#v", m) + } + if strings.TrimSpace(promptcompat.NormalizeOpenAIContentForPrompt(m["content"])) != "line-1\nline-2" { + t.Fatalf("unexpected content: %#v", m["content"]) + } +} + +func TestNormalizeResponsesInputAsMessagesFunctionCallOutput(t *testing.T) { + msgs := promptcompat.NormalizeResponsesInputAsMessages([]any{ + map[string]any{ + "type": "function_call_output", + "call_id": "call_123", + "output": map[string]any{"ok": true}, + }, + }) + if len(msgs) != 1 { + t.Fatalf("expected one message, got %d", len(msgs)) + } + m, _ := msgs[0].(map[string]any) + if m["role"] != "tool" { + t.Fatalf("expected tool role, got %#v", m) + } + if m["tool_call_id"] != "call_123" { + t.Fatalf("expected tool_call_id propagated, got %#v", m) + } +} + +func TestNormalizeResponsesInputAsMessagesBackfillsToolResultNameFromCallID(t *testing.T) { + msgs := promptcompat.NormalizeResponsesInputAsMessages([]any{ + map[string]any{ + "type": "function_call", + "call_id": "call_999", + "name": "search", + "arguments": `{"q":"golang"}`, + }, + map[string]any{ + "type": "function_call_output", + "call_id": "call_999", + "output": map[string]any{"ok": true}, + }, + }) + if len(msgs) != 2 { + t.Fatalf("expected two messages, got %d", len(msgs)) + } + toolMsg, _ := msgs[1].(map[string]any) + if toolMsg["role"] != "tool" { + t.Fatalf("expected tool role, got %#v", toolMsg) + } + if toolMsg["name"] != "search" { + t.Fatalf("expected tool name backfilled from call_id, got %#v", toolMsg["name"]) + } +} + +func TestNormalizeResponsesInputAsMessagesFunctionCallItem(t *testing.T) { + msgs := promptcompat.NormalizeResponsesInputAsMessages([]any{ + map[string]any{ + "type": "function_call", + "call_id": "call_456", + "name": "search", + "arguments": `{"q":"golang"}`, + }, + }) + if len(msgs) != 1 { + t.Fatalf("expected one message, got %d", len(msgs)) + } + m, _ := msgs[0].(map[string]any) + if m["role"] != "assistant" { + t.Fatalf("expected assistant role, got %#v", m["role"]) + } + toolCalls, _ := m["tool_calls"].([]any) + if len(toolCalls) != 1 { + t.Fatalf("expected one tool_call, got %#v", m["tool_calls"]) + } + call, _ := toolCalls[0].(map[string]any) + if call["id"] != "call_456" { + t.Fatalf("expected call id preserved, got %#v", call) + } + if call["type"] != "function" { + t.Fatalf("expected function type, got %#v", call) + } + fn, _ := call["function"].(map[string]any) + if fn["name"] != "search" { + t.Fatalf("expected call name preserved, got %#v", call) + } + if fn["arguments"] != `{"q":"golang"}` { + t.Fatalf("expected call arguments preserved, got %#v", call) + } +} + +func TestNormalizeResponsesInputAsMessagesFunctionCallItemPreservesConcatenatedArguments(t *testing.T) { + msgs := promptcompat.NormalizeResponsesInputAsMessages([]any{ + map[string]any{ + "type": "function_call", + "call_id": "call_456", + "name": "search", + "arguments": `{}{"q":"golang"}`, + }, + }) + if len(msgs) != 1 { + t.Fatalf("expected one message, got %d", len(msgs)) + } + m, _ := msgs[0].(map[string]any) + toolCalls, _ := m["tool_calls"].([]any) + call, _ := toolCalls[0].(map[string]any) + fn, _ := call["function"].(map[string]any) + if fn["arguments"] != `{}{"q":"golang"}` { + t.Fatalf("expected original concatenated call arguments preserved, got %#v", fn["arguments"]) + } +} + +func TestCollectOpenAIRefFileIDs(t *testing.T) { + got := promptcompat.CollectOpenAIRefFileIDs(map[string]any{ + "ref_file_ids": []any{"file-top", "file-dup"}, + "attachments": []any{ + map[string]any{"file_id": "file-attachment"}, + }, + "input": []any{ + map[string]any{ + "type": "message", + "content": []any{ + map[string]any{"type": "input_file", "file_id": "file-input"}, + map[string]any{"type": "input_file", "id": "file-dup"}, + }, + }, + }, + }) + want := []string{"file-top", "file-dup", "file-attachment", "file-input"} + if len(got) != len(want) { + t.Fatalf("expected %d file ids, got %#v", len(want), got) + } + for i, id := range want { + if got[i] != id { + t.Fatalf("unexpected file ids at %d: got=%#v want=%#v", i, got, want) + } + } +} + +func TestExtractEmbeddingInputs(t *testing.T) { + got := embeddings.ExtractEmbeddingInputs([]any{"a", "b"}) + if len(got) != 2 || got[0] != "a" || got[1] != "b" { + t.Fatalf("unexpected inputs: %#v", got) + } +} + +func TestDeterministicEmbeddingStable(t *testing.T) { + a := embeddings.DeterministicEmbedding("hello") + b := embeddings.DeterministicEmbedding("hello") + if len(a) != 64 || len(b) != 64 { + t.Fatalf("expected 64 dims, got %d and %d", len(a), len(b)) + } + for i := range a { + if a[i] != b[i] { + t.Fatalf("expected stable embedding at %d: %v != %v", i, a[i], b[i]) + } + } +} + +func TestResponseStorePutGet(t *testing.T) { + st := newResponseStore(100 * time.Millisecond) + st.put("owner_1", "resp_1", map[string]any{"id": "resp_1"}) + got, ok := st.get("owner_1", "resp_1") + if !ok { + t.Fatal("expected stored response") + } + if got["id"] != "resp_1" { + t.Fatalf("unexpected response payload: %#v", got) + } +} + +func TestResponseStoreTenantIsolation(t *testing.T) { + st := newResponseStore(100 * time.Millisecond) + st.put("owner_a", "resp_1", map[string]any{"id": "resp_1"}) + if _, ok := st.get("owner_b", "resp_1"); ok { + t.Fatal("expected owner_b to be isolated from owner_a response") + } +} diff --git a/internal/httpapi/openai/responses/responses_handler.go b/internal/httpapi/openai/responses/responses_handler.go new file mode 100644 index 0000000000000000000000000000000000000000..f34daed88c99ddcc353dc665c36de323c7c7eeb3 --- /dev/null +++ b/internal/httpapi/openai/responses/responses_handler.go @@ -0,0 +1,270 @@ +package responses + +import ( + "ds2api/internal/toolcall" + "encoding/json" + "io" + "net/http" + "strings" + "time" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/completionruntime" + "ds2api/internal/config" + dsprotocol "ds2api/internal/deepseek/protocol" + openaifmt "ds2api/internal/format/openai" + "ds2api/internal/promptcompat" + "ds2api/internal/responsehistory" + "ds2api/internal/sse" + streamengine "ds2api/internal/stream" +) + +func (h *Handler) GetResponseByID(w http.ResponseWriter, r *http.Request) { + a, err := h.Auth.DetermineCaller(r) + if err != nil { + writeOpenAIError(w, http.StatusUnauthorized, err.Error()) + return + } + + id := strings.TrimSpace(chi.URLParam(r, "response_id")) + if id == "" { + writeOpenAIError(w, http.StatusBadRequest, "response_id is required.") + return + } + owner := responseStoreOwner(a) + if owner == "" { + writeOpenAIError(w, http.StatusUnauthorized, "unauthorized") + return + } + st := h.getResponseStore() + item, ok := st.get(owner, id) + if !ok { + writeOpenAIError(w, http.StatusNotFound, "Response not found.") + return + } + writeJSON(w, http.StatusOK, item) +} + +func (h *Handler) Responses(w http.ResponseWriter, r *http.Request) { + a, err := h.Auth.Determine(r) + if err != nil { + status := http.StatusUnauthorized + detail := err.Error() + if err == auth.ErrNoAccount { + status = http.StatusTooManyRequests + } + writeOpenAIError(w, status, detail) + return + } + defer h.Auth.Release(a) + r = r.WithContext(auth.WithAuth(r.Context(), a)) + owner := responseStoreOwner(a) + if owner == "" { + writeOpenAIError(w, http.StatusUnauthorized, "unauthorized") + return + } + + r.Body = http.MaxBytesReader(w, r.Body, openAIGeneralMaxSize) + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + if strings.Contains(strings.ToLower(err.Error()), "too large") { + writeOpenAIError(w, http.StatusRequestEntityTooLarge, "request body too large") + return + } + writeOpenAIError(w, http.StatusBadRequest, "invalid json") + return + } + if err := h.preprocessInlineFileInputs(r.Context(), a, req); err != nil { + writeOpenAIInlineFileError(w, err) + return + } + traceID := requestTraceID(r) + stdReq, err := promptcompat.NormalizeOpenAIResponsesRequest(h.Store, req, traceID) + if err != nil { + writeOpenAIError(w, http.StatusBadRequest, err.Error()) + return + } + stdReq, err = h.applyCurrentInputFile(r.Context(), a, stdReq) + if err != nil { + status, message := mapCurrentInputFileError(err) + writeOpenAIError(w, status, message) + return + } + + responseID := "resp_" + strings.ReplaceAll(uuid.NewString(), "-", "") + historySession := responsehistory.Start(responsehistory.StartParams{ + Store: h.ChatHistory, + Request: r, + Auth: a, + Surface: "openai.responses", + Standard: stdReq, + }) + if !stdReq.Stream { + result, outErr := completionruntime.ExecuteNonStreamWithRetry(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + RetryEnabled: true, + CurrentInputFile: h.Store, + }) + if outErr != nil { + if historySession != nil { + historySession.ErrorTurn(outErr.Status, outErr.Message, outErr.Code, result.Turn) + } + writeOpenAIErrorWithCode(w, outErr.Status, outErr.Message, outErr.Code) + return + } + if historySession != nil { + historySession.SuccessTurn(http.StatusOK, result.Turn, assistantturn.OpenAIResponsesUsage(result.Turn)) + } + responseObj := openaifmt.BuildResponseObjectWithToolCalls(responseID, stdReq.ResponseModel, result.Turn.Prompt, result.Turn.Thinking, result.Turn.Text, result.Turn.ToolCalls, stdReq.ToolsRaw) + responseObj["usage"] = assistantturn.OpenAIResponsesUsage(result.Turn) + h.getResponseStore().put(owner, responseID, responseObj) + writeJSON(w, http.StatusOK, responseObj) + return + } + + start, outErr := completionruntime.StartCompletion(r.Context(), h.DS, a, stdReq, completionruntime.Options{ + CurrentInputFile: h.Store, + }) + if outErr != nil { + if historySession != nil { + historySession.Error(outErr.Status, outErr.Message, outErr.Code, "", "") + } + writeOpenAIErrorWithCode(w, outErr.Status, outErr.Message, outErr.Code) + return + } + + streamReq := start.Request + refFileTokens := streamReq.RefFileTokens + h.handleResponsesStreamWithRetry(w, r, a, start.Response, start.Payload, start.Pow, owner, responseID, streamReq, streamReq.ResponseModel, streamReq.PromptTokenText, refFileTokens, streamReq.Thinking, streamReq.Search, streamReq.ToolNames, streamReq.ToolsRaw, streamReq.ToolChoice, traceID, historySession) +} + +func (h *Handler) handleResponsesNonStream(w http.ResponseWriter, resp *http.Response, owner, responseID, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, toolChoice promptcompat.ToolChoicePolicy, traceID string) { + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + writeOpenAIError(w, resp.StatusCode, strings.TrimSpace(string(body))) + return + } + result := sse.CollectStream(resp, thinkingEnabled, true) + + turn := assistantturn.BuildTurnFromCollected(result, assistantturn.BuildOptions{ + Model: model, + Prompt: finalPrompt, + RefFileTokens: refFileTokens, + SearchEnabled: searchEnabled, + ToolNames: toolNames, + ToolsRaw: toolsRaw, + ToolChoice: toolChoice, + }) + logResponsesToolPolicyRejection(traceID, toolChoice, turn.ParsedToolCalls, "text") + outcome := assistantturn.FinalizeTurn(turn, assistantturn.FinalizeOptions{}) + if outcome.ShouldFail { + writeOpenAIErrorWithCode(w, outcome.Error.Status, outcome.Error.Message, outcome.Error.Code) + return + } + + responseObj := openaifmt.BuildResponseObjectWithToolCalls(responseID, model, finalPrompt, turn.Thinking, turn.Text, turn.ToolCalls, toolsRaw) + responseObj["usage"] = assistantturn.OpenAIResponsesUsage(turn) + h.getResponseStore().put(owner, responseID, responseObj) + writeJSON(w, http.StatusOK, responseObj) +} + +func (h *Handler) handleResponsesStream(w http.ResponseWriter, r *http.Request, resp *http.Response, owner, responseID, model, finalPrompt string, refFileTokens int, thinkingEnabled, searchEnabled bool, toolNames []string, toolsRaw any, toolChoice promptcompat.ToolChoicePolicy, traceID string) { + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + body, _ := io.ReadAll(resp.Body) + writeOpenAIError(w, resp.StatusCode, strings.TrimSpace(string(body))) + return + } + w.Header().Set("Content-Type", "text/event-stream") + w.Header().Set("Cache-Control", "no-cache, no-transform") + w.Header().Set("Connection", "keep-alive") + w.Header().Set("X-Accel-Buffering", "no") + rc := http.NewResponseController(w) + _, canFlush := w.(http.Flusher) + + initialType := "text" + if thinkingEnabled { + initialType = "thinking" + } + bufferToolContent := len(toolNames) > 0 + emitEarlyToolDeltas := h.toolcallFeatureMatchEnabled() && h.toolcallEarlyEmitHighConfidence() + stripReferenceMarkers := stripReferenceMarkersEnabled() + + streamRuntime := newResponsesStreamRuntime( + w, + rc, + canFlush, + responseID, + model, + finalPrompt, + thinkingEnabled, + searchEnabled, + stripReferenceMarkers, + toolNames, + toolsRaw, + bufferToolContent, + emitEarlyToolDeltas, + toolChoice, + traceID, + func(obj map[string]any) { + h.getResponseStore().put(owner, responseID, obj) + }, + nil, + ) + streamRuntime.refFileTokens = refFileTokens + streamRuntime.sendCreated() + + streamengine.ConsumeSSE(streamengine.ConsumeConfig{ + Context: r.Context(), + Body: resp.Body, + ThinkingEnabled: thinkingEnabled, + InitialType: initialType, + KeepAliveInterval: time.Duration(dsprotocol.KeepAliveTimeout) * time.Second, + IdleTimeout: time.Duration(dsprotocol.StreamIdleTimeout) * time.Second, + MaxKeepAliveNoInput: dsprotocol.MaxKeepaliveCount, + }, streamengine.ConsumeHooks{ + OnParsed: streamRuntime.onParsed, + OnFinalize: func(reason streamengine.StopReason, _ error) { + if string(reason) == "content_filter" { + streamRuntime.finalize("content_filter", false) + return + } + streamRuntime.finalize("stop", false) + }, + }) +} + +func logResponsesToolPolicyRejection(traceID string, policy promptcompat.ToolChoicePolicy, parsed toolcall.ToolCallParseResult, channel string) { + rejected := filteredRejectedToolNamesForLog(parsed.RejectedToolNames) + if !parsed.RejectedByPolicy || len(rejected) == 0 { + return + } + config.Logger.Warn( + "[responses] rejected tool calls by policy", + "trace_id", strings.TrimSpace(traceID), + "channel", channel, + "tool_choice_mode", policy.Mode, + "rejected_tool_names", strings.Join(rejected, ","), + ) +} + +func filteredRejectedToolNamesForLog(names []string) []string { + if len(names) == 0 { + return nil + } + out := make([]string, 0, len(names)) + for _, name := range names { + trimmed := strings.TrimSpace(name) + switch strings.ToLower(trimmed) { + case "", "tool_name": + continue + default: + out = append(out, trimmed) + } + } + return out +} diff --git a/internal/httpapi/openai/responses/responses_history_test.go b/internal/httpapi/openai/responses/responses_history_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8eb76f19084f1fcf7d4c4f503040aacbf3fe9c1f --- /dev/null +++ b/internal/httpapi/openai/responses/responses_history_test.go @@ -0,0 +1,100 @@ +package responses + +import ( + "context" + "io" + "net/http" + "net/http/httptest" + "path/filepath" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + dsclient "ds2api/internal/deepseek/client" +) + +type responsesHistoryDS struct { + payload map[string]any +} + +func (d *responsesHistoryDS) CreateSession(context.Context, *auth.RequestAuth, int) (string, error) { + return "session-id", nil +} + +func (d *responsesHistoryDS) GetPow(context.Context, *auth.RequestAuth, int) (string, error) { + return "pow", nil +} + +func (d *responsesHistoryDS) UploadFile(context.Context, *auth.RequestAuth, dsclient.UploadFileRequest, int) (*dsclient.UploadFileResult, error) { + return &dsclient.UploadFileResult{ID: "file-id"}, nil +} + +func (d *responsesHistoryDS) CallCompletion(_ context.Context, _ *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + d.payload = payload + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader("data: {\"p\":\"response/content\",\"v\":\"ok\"}\n")), + }, nil +} + +func (d *responsesHistoryDS) DeleteSessionForToken(context.Context, string, string) (*dsclient.DeleteSessionResult, error) { + return &dsclient.DeleteSessionResult{Success: true}, nil +} + +func (d *responsesHistoryDS) DeleteAllSessionsForToken(context.Context, string) error { + return nil +} + +func TestResponsesRecordsResponseHistory(t *testing.T) { + store, resolver := newDirectTokenResolver(t) + historyStore := chathistory.New(filepath.Join(t.TempDir(), "history.json")) + ds := &responsesHistoryDS{} + h := &Handler{ + Store: store, + Auth: resolver, + DS: ds, + ChatHistory: historyStore, + } + r := chi.NewRouter() + RegisterRoutes(r, h) + + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(`{"model":"deepseek-v4-flash","input":"hello responses"}`)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if ds.payload == nil { + t.Fatalf("expected upstream payload to be sent") + } + snapshot, err := historyStore.Snapshot() + if err != nil { + t.Fatalf("snapshot history: %v", err) + } + if len(snapshot.Items) != 1 { + t.Fatalf("expected one history item, got %d", len(snapshot.Items)) + } + item, err := historyStore.Get(snapshot.Items[0].ID) + if err != nil { + t.Fatalf("get history item: %v", err) + } + if item.Surface != "openai.responses" { + t.Fatalf("unexpected surface: %q", item.Surface) + } + if !strings.Contains(item.UserInput, ".txt") { + t.Fatalf("unexpected user input: %q", item.UserInput) + } + if !strings.Contains(item.HistoryText, "hello responses") { + t.Fatalf("expected original input in persisted history text, got %q", item.HistoryText) + } + if item.Content != "ok" { + t.Fatalf("expected raw upstream content, got %q", item.Content) + } +} diff --git a/internal/httpapi/openai/responses/responses_route_test.go b/internal/httpapi/openai/responses/responses_route_test.go new file mode 100644 index 0000000000000000000000000000000000000000..1d6a847b67e93e97db9fc2139e6bc60543fd51a6 --- /dev/null +++ b/internal/httpapi/openai/responses/responses_route_test.go @@ -0,0 +1,176 @@ +package responses + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/config" +) + +func newDirectTokenResolver(t *testing.T) (*config.Store, *auth.Resolver) { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", `{"keys":[],"accounts":[]}`) + store := config.LoadStore() + pool := account.NewPool(store) + resolver := auth.NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "unused", nil + }) + return store, resolver +} + +func newManagedKeyResolver(t *testing.T) (*config.Store, *auth.Resolver) { + t.Helper() + t.Setenv("DS2API_CONFIG_JSON", `{ + "keys":["managed-key"], + "accounts":[{"email":"acc@example.com","password":"pwd","token":"account-token"}] + }`) + t.Setenv("DS2API_ACCOUNT_MAX_INFLIGHT", "1") + t.Setenv("DS2API_ACCOUNT_MAX_QUEUE", "0") + store := config.LoadStore() + pool := account.NewPool(store) + resolver := auth.NewResolver(store, pool, func(_ context.Context, _ config.Account) (string, error) { + return "unused", nil + }) + return store, resolver +} + +func authForToken(t *testing.T, resolver *auth.Resolver, token string) *auth.RequestAuth { + t.Helper() + req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_test", nil) + req.Header.Set("Authorization", "Bearer "+token) + a, err := resolver.Determine(req) + if err != nil { + t.Fatalf("determine auth failed: %v", err) + } + return a +} + +func TestGetResponseByIDRequiresAuthAndIsTenantIsolated(t *testing.T) { + store, resolver := newDirectTokenResolver(t) + h := &Handler{Store: store, Auth: resolver} + r := chi.NewRouter() + RegisterRoutes(r, h) + + ownerA := responseStoreOwner(authForToken(t, resolver, "token-a")) + h.getResponseStore().put(ownerA, "resp_test", map[string]any{ + "id": "resp_test", + "object": "response", + }) + + t.Run("unauthorized", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_test", nil) + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusUnauthorized { + t.Fatalf("expected 401, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("cross-tenant-not-found", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_test", nil) + req.Header.Set("Authorization", "Bearer token-b") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusNotFound { + t.Fatalf("expected 404, got %d body=%s", rec.Code, rec.Body.String()) + } + }) + + t.Run("same-tenant-ok", func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_test", nil) + req.Header.Set("Authorization", "Bearer token-a") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + var body map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatalf("decode body failed: %v", err) + } + if body["id"] != "resp_test" { + t.Fatalf("unexpected body: %#v", body) + } + }) +} + +func TestResponsesRouteValidationContract(t *testing.T) { + store, resolver := newDirectTokenResolver(t) + h := &Handler{Store: store, Auth: resolver} + r := chi.NewRouter() + RegisterRoutes(r, h) + + tests := []struct { + name string + body string + }{ + {name: "missing_model", body: `{"input":"hello"}`}, + {name: "missing_input_and_messages", body: `{"model":"gpt-4o"}`}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodPost, "/v1/responses", bytes.NewBufferString(tc.body)) + req.Header.Set("Authorization", "Bearer token-a") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400, got %d body=%s", rec.Code, rec.Body.String()) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v", err) + } + errObj, _ := out["error"].(map[string]any) + if _, ok := errObj["code"]; !ok { + t.Fatalf("expected error.code: %#v", out) + } + if _, ok := errObj["param"]; !ok { + t.Fatalf("expected error.param: %#v", out) + } + }) + } +} + +func TestGetResponseByIDManagedKeySkipsAccountPoolPressure(t *testing.T) { + store, resolver := newManagedKeyResolver(t) + h := &Handler{Store: store, Auth: resolver} + r := chi.NewRouter() + RegisterRoutes(r, h) + + ownerReq := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_test", nil) + ownerReq.Header.Set("Authorization", "Bearer managed-key") + ownerAuth, err := resolver.DetermineCaller(ownerReq) + if err != nil { + t.Fatalf("determine caller failed: %v", err) + } + owner := responseStoreOwner(ownerAuth) + h.getResponseStore().put(owner, "resp_test", map[string]any{ + "id": "resp_test", + "object": "response", + }) + + occupyReq := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil) + occupyReq.Header.Set("Authorization", "Bearer managed-key") + occupied, err := resolver.Determine(occupyReq) + if err != nil { + t.Fatalf("expected first acquire to succeed: %v", err) + } + defer resolver.Release(occupied) + + req := httptest.NewRequest(http.MethodGet, "/v1/responses/resp_test", nil) + req.Header.Set("Authorization", "Bearer managed-key") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 under pool pressure, got %d body=%s", rec.Code, rec.Body.String()) + } +} diff --git a/internal/httpapi/openai/responses/responses_stream_delta_batch.go b/internal/httpapi/openai/responses/responses_stream_delta_batch.go new file mode 100644 index 0000000000000000000000000000000000000000..84c8c7fc8a6d71cdc96a3b296b0cac5081057506 --- /dev/null +++ b/internal/httpapi/openai/responses/responses_stream_delta_batch.go @@ -0,0 +1,39 @@ +package responses + +import ( + "strings" + + openaifmt "ds2api/internal/format/openai" +) + +type responsesDeltaBatch struct { + runtime *responsesStreamRuntime + kind string + text strings.Builder +} + +func (b *responsesDeltaBatch) append(kind, text string) { + if text == "" { + return + } + if b.kind != "" && b.kind != kind { + b.flush() + } + b.kind = kind + b.text.WriteString(text) +} + +func (b *responsesDeltaBatch) flush() { + if b.kind == "" || b.text.Len() == 0 { + return + } + text := b.text.String() + switch b.kind { + case "reasoning": + b.runtime.sendEvent("response.reasoning.delta", openaifmt.BuildResponsesReasoningDeltaPayload(b.runtime.responseID, text)) + case "text": + b.runtime.emitTextDelta(text) + } + b.kind = "" + b.text.Reset() +} diff --git a/internal/httpapi/openai/responses/responses_stream_runtime_core.go b/internal/httpapi/openai/responses/responses_stream_runtime_core.go new file mode 100644 index 0000000000000000000000000000000000000000..524808e8af354a82fabfc01ab7fd2f6218eb8404 --- /dev/null +++ b/internal/httpapi/openai/responses/responses_stream_runtime_core.go @@ -0,0 +1,298 @@ +package responses + +import ( + "ds2api/internal/assistantturn" + "ds2api/internal/toolcall" + "net/http" + "strings" + + "ds2api/internal/config" + openaifmt "ds2api/internal/format/openai" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" + "ds2api/internal/responsehistory" + "ds2api/internal/sse" + streamengine "ds2api/internal/stream" + "ds2api/internal/toolstream" +) + +type responsesStreamRuntime struct { + w http.ResponseWriter + rc *http.ResponseController + canFlush bool + + responseID string + model string + finalPrompt string + refFileTokens int + toolNames []string + toolsRaw any + traceID string + toolChoice promptcompat.ToolChoicePolicy + + thinkingEnabled bool + searchEnabled bool + stripReferenceMarkers bool + + bufferToolContent bool + emitEarlyToolDeltas bool + toolCallsEmitted bool + toolCallsDoneEmitted bool + + sieve toolstream.State + accumulator shared.StreamAccumulator + visibleText strings.Builder + responseMessageID int + streamToolCallIDs map[int]string + functionItemIDs map[int]string + functionOutputIDs map[int]int + functionArgs map[int]string + functionDone map[int]bool + functionAdded map[int]bool + functionNames map[int]string + messageItemID string + messageOutputID int + nextOutputID int + messageAdded bool + messagePartAdded bool + sequence int + failed bool + finalErrorStatus int + finalErrorMessage string + finalErrorCode string + + persistResponse func(obj map[string]any) + history *responsehistory.Session +} + +func newResponsesStreamRuntime( + w http.ResponseWriter, + rc *http.ResponseController, + canFlush bool, + responseID string, + model string, + finalPrompt string, + thinkingEnabled bool, + searchEnabled bool, + stripReferenceMarkers bool, + toolNames []string, + toolsRaw any, + bufferToolContent bool, + emitEarlyToolDeltas bool, + toolChoice promptcompat.ToolChoicePolicy, + traceID string, + persistResponse func(obj map[string]any), + history *responsehistory.Session, +) *responsesStreamRuntime { + return &responsesStreamRuntime{ + w: w, + rc: rc, + canFlush: canFlush, + responseID: responseID, + model: model, + finalPrompt: finalPrompt, + thinkingEnabled: thinkingEnabled, + searchEnabled: searchEnabled, + stripReferenceMarkers: stripReferenceMarkers, + toolNames: toolNames, + toolsRaw: toolsRaw, + bufferToolContent: bufferToolContent, + emitEarlyToolDeltas: emitEarlyToolDeltas, + streamToolCallIDs: map[int]string{}, + functionItemIDs: map[int]string{}, + functionOutputIDs: map[int]int{}, + functionArgs: map[int]string{}, + functionDone: map[int]bool{}, + functionAdded: map[int]bool{}, + functionNames: map[int]string{}, + messageOutputID: -1, + toolChoice: toolChoice, + traceID: traceID, + persistResponse: persistResponse, + history: history, + accumulator: shared.StreamAccumulator{ + ThinkingEnabled: thinkingEnabled, + SearchEnabled: searchEnabled, + StripReferenceMarkers: stripReferenceMarkers, + }, + } +} + +func (s *responsesStreamRuntime) failResponse(status int, message, code string) { + s.failed = true + s.finalErrorStatus = status + s.finalErrorMessage = message + s.finalErrorCode = code + failedResp := map[string]any{ + "id": s.responseID, + "type": "response", + "object": "response", + "model": s.model, + "status": "failed", + "status_code": status, + "output": []any{}, + "output_text": "", + "error": map[string]any{ + "message": message, + "type": openAIErrorType(status), + "code": code, + "param": nil, + }, + } + if s.persistResponse != nil { + s.persistResponse(failedResp) + } + if s.history != nil { + s.history.Error(status, message, code, responsehistory.ThinkingForArchive(s.accumulator.RawThinking.String(), s.accumulator.ToolDetectionThinking.String(), s.accumulator.Thinking.String()), responsehistory.TextForArchive(s.accumulator.RawText.String(), s.accumulator.Text.String())) + } + s.sendEvent("response.failed", openaifmt.BuildResponsesFailedPayload(s.responseID, s.model, status, message, code)) + s.sendDone() +} + +func (s *responsesStreamRuntime) markContextCancelled() { + s.failed = true + s.finalErrorStatus = 499 + s.finalErrorMessage = "request context cancelled" + s.finalErrorCode = string(streamengine.StopReasonContextCancelled) +} + +func (s *responsesStreamRuntime) finalize(finishReason string, deferEmptyOutput bool) bool { + s.failed = false + s.finalErrorStatus = 0 + s.finalErrorMessage = "" + s.finalErrorCode = "" + if s.bufferToolContent { + s.processToolStreamEvents(toolstream.Flush(&s.sieve, s.toolNames), true, true) + } + + finalThinking := s.accumulator.Thinking.String() + finalToolDetectionThinking := s.accumulator.ToolDetectionThinking.String() + finalText := s.accumulator.Text.String() + turn := assistantturn.BuildTurnFromStreamSnapshot(assistantturn.StreamSnapshot{ + RawText: s.accumulator.RawText.String(), + VisibleText: finalText, + RawThinking: s.accumulator.RawThinking.String(), + VisibleThinking: finalThinking, + DetectionThinking: finalToolDetectionThinking, + ContentFilter: finishReason == "content_filter", + ResponseMessageID: s.responseMessageID, + AlreadyEmittedCalls: s.toolCallsEmitted, + AlreadyEmittedToolRaw: s.toolCallsDoneEmitted, + }, assistantturn.BuildOptions{ + Model: s.model, + Prompt: s.finalPrompt, + RefFileTokens: s.refFileTokens, + SearchEnabled: s.searchEnabled, + StripReferenceMarkers: s.stripReferenceMarkers, + ToolNames: s.toolNames, + ToolsRaw: s.toolsRaw, + ToolChoice: s.toolChoice, + }) + textParsed := turn.ParsedToolCalls + detected := turn.ToolCalls + s.logToolPolicyRejections(textParsed) + + if len(detected) > 0 { + s.toolCallsEmitted = true + if !s.toolCallsDoneEmitted { + s.emitFunctionCallDoneEvents(detected) + } + } + + s.closeMessageItem() + + outcome := assistantturn.FinalizeTurn(turn, assistantturn.FinalizeOptions{ + AlreadyEmittedToolCalls: s.toolCallsEmitted || s.toolCallsDoneEmitted, + }) + if outcome.ShouldFail { + status, message, code := outcome.Error.Status, outcome.Error.Message, outcome.Error.Code + if deferEmptyOutput { + s.finalErrorStatus = status + s.finalErrorMessage = message + s.finalErrorCode = code + return false + } + s.failResponse(status, message, code) + return true + } + s.closeIncompleteFunctionItems() + + obj := s.buildCompletedResponseObject(turn.Thinking, turn.Text, detected) + if s.persistResponse != nil { + s.persistResponse(obj) + } + if s.history != nil { + s.history.Success( + http.StatusOK, + responsehistory.ThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), + responsehistory.TextForArchive(turn.RawText, turn.Text), + outcome.FinishReason, + assistantturn.OpenAIResponsesUsage(turn), + ) + } + s.sendEvent("response.completed", openaifmt.BuildResponsesCompletedPayload(obj)) + s.sendDone() + return true +} + +func (s *responsesStreamRuntime) logToolPolicyRejections(textParsed toolcall.ToolCallParseResult) { + logRejected := func(parsed toolcall.ToolCallParseResult, channel string) { + rejected := filteredRejectedToolNamesForLog(parsed.RejectedToolNames) + if !parsed.RejectedByPolicy || len(rejected) == 0 { + return + } + config.Logger.Warn( + "[responses] rejected tool calls by policy", + "trace_id", strings.TrimSpace(s.traceID), + "channel", channel, + "tool_choice_mode", s.toolChoice.Mode, + "rejected_tool_names", strings.Join(rejected, ","), + ) + } + logRejected(textParsed, "text") +} + +func (s *responsesStreamRuntime) onParsed(parsed sse.LineResult) streamengine.ParsedDecision { + if !parsed.Parsed { + return streamengine.ParsedDecision{} + } + if parsed.ResponseMessageID > 0 { + s.responseMessageID = parsed.ResponseMessageID + } + if parsed.ContentFilter || parsed.ErrorMessage != "" { + return streamengine.ParsedDecision{Stop: true, StopReason: streamengine.StopReason("content_filter")} + } + if parsed.Stop { + return streamengine.ParsedDecision{Stop: true} + } + + batch := responsesDeltaBatch{runtime: s} + accumulated := s.accumulator.Apply(parsed) + for _, p := range accumulated.Parts { + if p.Type == "thinking" { + batch.append("reasoning", p.VisibleText) + continue + } + if p.RawText == "" { + continue + } + if p.CitationOnly { + continue + } + if !s.bufferToolContent { + batch.append("text", p.VisibleText) + continue + } + batch.flush() + s.processToolStreamEvents(toolstream.ProcessChunk(&s.sieve, p.RawText, s.toolNames), true, true) + } + + batch.flush() + if s.history != nil { + s.history.Progress( + responsehistory.ThinkingForArchive(s.accumulator.RawThinking.String(), s.accumulator.ToolDetectionThinking.String(), s.accumulator.Thinking.String()), + responsehistory.TextForArchive(s.accumulator.RawText.String(), s.accumulator.Text.String()), + ) + } + return streamengine.ParsedDecision{ContentSeen: accumulated.ContentSeen} +} diff --git a/internal/httpapi/openai/responses/responses_stream_runtime_events.go b/internal/httpapi/openai/responses/responses_stream_runtime_events.go new file mode 100644 index 0000000000000000000000000000000000000000..20b91085560b3aaeb767f29a3dff595bec77e8de --- /dev/null +++ b/internal/httpapi/openai/responses/responses_stream_runtime_events.go @@ -0,0 +1,69 @@ +package responses + +import ( + "encoding/json" + + openaifmt "ds2api/internal/format/openai" + "ds2api/internal/sse" + "ds2api/internal/toolstream" +) + +func (s *responsesStreamRuntime) nextSequence() int { + s.sequence++ + return s.sequence +} + +func (s *responsesStreamRuntime) sendEvent(event string, payload map[string]any) { + if payload == nil { + payload = map[string]any{} + } + if _, ok := payload["sequence_number"]; !ok { + payload["sequence_number"] = s.nextSequence() + } + b, _ := json.Marshal(payload) + _, _ = s.w.Write([]byte("event: " + event + "\n")) + _, _ = s.w.Write([]byte("data: ")) + _, _ = s.w.Write(b) + _, _ = s.w.Write([]byte("\n\n")) + if s.canFlush { + _ = s.rc.Flush() + } +} + +func (s *responsesStreamRuntime) sendCreated() { + s.sendEvent("response.created", openaifmt.BuildResponsesCreatedPayload(s.responseID, s.model)) +} + +func (s *responsesStreamRuntime) sendDone() { + _, _ = s.w.Write([]byte("data: [DONE]\n\n")) + if s.canFlush { + _ = s.rc.Flush() + } +} + +func (s *responsesStreamRuntime) processToolStreamEvents(events []toolstream.Event, emitContent bool, resetAfterToolCalls bool) { + for _, evt := range events { + if emitContent && evt.Content != "" { + cleaned := cleanVisibleOutput(evt.Content, s.stripReferenceMarkers) + if cleaned != "" && (!s.searchEnabled || !sse.IsCitation(cleaned)) { + s.emitTextDelta(cleaned) + } + } + if len(evt.ToolCallDeltas) > 0 { + if !s.emitEarlyToolDeltas { + continue + } + filtered := filterIncrementalToolCallDeltasByAllowed(evt.ToolCallDeltas, s.functionNames) + if len(filtered) == 0 { + continue + } + s.emitFunctionCallDeltaEvents(filtered) + } + if len(evt.ToolCalls) > 0 { + s.emitFunctionCallDoneEvents(evt.ToolCalls) + if resetAfterToolCalls { + s.resetStreamToolCallState() + } + } + } +} diff --git a/internal/httpapi/openai/responses/responses_stream_runtime_toolcalls.go b/internal/httpapi/openai/responses/responses_stream_runtime_toolcalls.go new file mode 100644 index 0000000000000000000000000000000000000000..0f388d7b7e202a3a0708a2b7f47241d02f8e3c0e --- /dev/null +++ b/internal/httpapi/openai/responses/responses_stream_runtime_toolcalls.go @@ -0,0 +1,257 @@ +package responses + +import ( + "ds2api/internal/toolcall" + "ds2api/internal/toolstream" + "encoding/json" + "strings" + + openaifmt "ds2api/internal/format/openai" + + "github.com/google/uuid" +) + +func (s *responsesStreamRuntime) allocateOutputIndex() int { + idx := s.nextOutputID + s.nextOutputID++ + return idx +} + +func (s *responsesStreamRuntime) ensureMessageItemID() string { + if strings.TrimSpace(s.messageItemID) != "" { + return s.messageItemID + } + s.messageItemID = "msg_" + strings.ReplaceAll(uuid.NewString(), "-", "") + return s.messageItemID +} + +func (s *responsesStreamRuntime) ensureMessageOutputIndex() int { + if s.messageOutputID >= 0 { + return s.messageOutputID + } + s.messageOutputID = s.allocateOutputIndex() + return s.messageOutputID +} + +func (s *responsesStreamRuntime) ensureMessageItemAdded() { + if s.messageAdded { + return + } + itemID := s.ensureMessageItemID() + item := map[string]any{ + "id": itemID, + "type": "message", + "role": "assistant", + "status": "in_progress", + } + s.sendEvent( + "response.output_item.added", + openaifmt.BuildResponsesOutputItemAddedPayload(s.responseID, itemID, s.ensureMessageOutputIndex(), item), + ) + s.messageAdded = true +} + +func (s *responsesStreamRuntime) ensureMessageContentPartAdded() { + if s.messagePartAdded { + return + } + s.ensureMessageItemAdded() + s.sendEvent( + "response.content_part.added", + openaifmt.BuildResponsesContentPartAddedPayload( + s.responseID, + s.ensureMessageItemID(), + s.ensureMessageOutputIndex(), + 0, + map[string]any{"type": "output_text", "text": ""}, + ), + ) + s.messagePartAdded = true +} + +func (s *responsesStreamRuntime) emitTextDelta(content string) { + if content == "" { + return + } + s.ensureMessageContentPartAdded() + s.visibleText.WriteString(content) + s.sendEvent( + "response.output_text.delta", + openaifmt.BuildResponsesTextDeltaPayload( + s.responseID, + s.ensureMessageItemID(), + s.ensureMessageOutputIndex(), + 0, + content, + ), + ) +} + +func (s *responsesStreamRuntime) closeMessageItem() { + if !s.messageAdded { + return + } + itemID := s.ensureMessageItemID() + outputIndex := s.ensureMessageOutputIndex() + text := s.visibleText.String() + if s.messagePartAdded { + s.sendEvent( + "response.output_text.done", + openaifmt.BuildResponsesTextDonePayload( + s.responseID, + itemID, + outputIndex, + 0, + text, + ), + ) + s.sendEvent( + "response.content_part.done", + openaifmt.BuildResponsesContentPartDonePayload( + s.responseID, + itemID, + outputIndex, + 0, + map[string]any{"type": "output_text", "text": text}, + ), + ) + s.messagePartAdded = false + } + item := map[string]any{ + "id": itemID, + "type": "message", + "role": "assistant", + "status": "completed", + "content": []map[string]any{ + { + "type": "output_text", + "text": text, + }, + }, + } + s.sendEvent( + "response.output_item.done", + openaifmt.BuildResponsesOutputItemDonePayload(s.responseID, itemID, outputIndex, item), + ) +} + +func (s *responsesStreamRuntime) ensureFunctionItemID(callIndex int) string { + if id, ok := s.functionItemIDs[callIndex]; ok && strings.TrimSpace(id) != "" { + return id + } + id := "fc_" + strings.ReplaceAll(uuid.NewString(), "-", "") + s.functionItemIDs[callIndex] = id + return id +} + +func (s *responsesStreamRuntime) ensureToolCallID(callIndex int) string { + if id, ok := s.streamToolCallIDs[callIndex]; ok && strings.TrimSpace(id) != "" { + return id + } + id := "call_" + strings.ReplaceAll(uuid.NewString(), "-", "") + s.streamToolCallIDs[callIndex] = id + return id +} + +func (s *responsesStreamRuntime) resetStreamToolCallState() { + s.streamToolCallIDs = map[int]string{} + s.functionItemIDs = map[int]string{} + s.functionOutputIDs = map[int]int{} + s.functionArgs = map[int]string{} + s.functionDone = map[int]bool{} + s.functionAdded = map[int]bool{} + s.functionNames = map[int]string{} +} + +func (s *responsesStreamRuntime) ensureFunctionOutputIndex(callIndex int) int { + if idx, ok := s.functionOutputIDs[callIndex]; ok { + return idx + } + idx := s.allocateOutputIndex() + s.functionOutputIDs[callIndex] = idx + return idx +} + +func (s *responsesStreamRuntime) ensureFunctionItemAdded(callIndex int, name string) { + if strings.TrimSpace(name) != "" { + s.functionNames[callIndex] = strings.TrimSpace(name) + } + if s.functionAdded[callIndex] { + return + } + fnName := strings.TrimSpace(s.functionNames[callIndex]) + if fnName == "" { + return + } + outputIndex := s.ensureFunctionOutputIndex(callIndex) + itemID := s.ensureFunctionItemID(callIndex) + callID := s.ensureToolCallID(callIndex) + item := map[string]any{ + "id": itemID, + "type": "function_call", + "call_id": callID, + "name": fnName, + "arguments": "", + "status": "in_progress", + } + s.sendEvent( + "response.output_item.added", + openaifmt.BuildResponsesOutputItemAddedPayload(s.responseID, itemID, outputIndex, item), + ) + s.functionAdded[callIndex] = true + s.toolCallsEmitted = true +} + +func (s *responsesStreamRuntime) emitFunctionCallDeltaEvents(deltas []toolstream.ToolCallDelta) { + for _, d := range deltas { + s.ensureFunctionItemAdded(d.Index, d.Name) + if strings.TrimSpace(d.Arguments) == "" { + continue + } + s.functionArgs[d.Index] += d.Arguments + outputIndex := s.ensureFunctionOutputIndex(d.Index) + itemID := s.ensureFunctionItemID(d.Index) + callID := s.ensureToolCallID(d.Index) + s.sendEvent( + "response.function_call_arguments.delta", + openaifmt.BuildResponsesFunctionCallArgumentsDeltaPayload(s.responseID, itemID, outputIndex, callID, d.Arguments), + ) + } +} + +func (s *responsesStreamRuntime) emitFunctionCallDoneEvents(calls []toolcall.ParsedToolCall) { + normalizedCalls := toolcall.NormalizeParsedToolCallsForSchemas(calls, s.toolsRaw) + for idx, tc := range normalizedCalls { + if strings.TrimSpace(tc.Name) == "" { + continue + } + s.ensureFunctionItemAdded(idx, tc.Name) + if s.functionDone[idx] { + continue + } + outputIndex := s.ensureFunctionOutputIndex(idx) + itemID := s.ensureFunctionItemID(idx) + callID := s.ensureToolCallID(idx) + argsBytes, _ := json.Marshal(tc.Input) + args := string(argsBytes) + s.functionArgs[idx] = args + s.sendEvent( + "response.function_call_arguments.done", + openaifmt.BuildResponsesFunctionCallArgumentsDonePayload(s.responseID, itemID, outputIndex, callID, tc.Name, args), + ) + item := map[string]any{ + "id": itemID, + "type": "function_call", + "call_id": callID, + "name": tc.Name, + "arguments": args, + "status": "completed", + } + s.sendEvent( + "response.output_item.done", + openaifmt.BuildResponsesOutputItemDonePayload(s.responseID, itemID, outputIndex, item), + ) + s.functionDone[idx] = true + s.toolCallsDoneEmitted = true + } +} diff --git a/internal/httpapi/openai/responses/responses_stream_runtime_toolcalls_finalize.go b/internal/httpapi/openai/responses/responses_stream_runtime_toolcalls_finalize.go new file mode 100644 index 0000000000000000000000000000000000000000..06d367320f2ed7f3d065372daaeeb25220d0dd3b --- /dev/null +++ b/internal/httpapi/openai/responses/responses_stream_runtime_toolcalls_finalize.go @@ -0,0 +1,177 @@ +package responses + +import ( + "ds2api/internal/toolcall" + "encoding/json" + "sort" + "strings" + + openaifmt "ds2api/internal/format/openai" +) + +func (s *responsesStreamRuntime) closeIncompleteFunctionItems() { + if len(s.functionAdded) == 0 { + return + } + indices := make([]int, 0, len(s.functionAdded)) + for idx, added := range s.functionAdded { + if !added || s.functionDone[idx] { + continue + } + indices = append(indices, idx) + } + if len(indices) == 0 { + return + } + sort.Ints(indices) + for _, idx := range indices { + name := strings.TrimSpace(s.functionNames[idx]) + if name == "" { + continue + } + args := strings.TrimSpace(s.functionArgs[idx]) + if args == "" { + args = "{}" + } + outputIndex := s.ensureFunctionOutputIndex(idx) + itemID := s.ensureFunctionItemID(idx) + callID := s.ensureToolCallID(idx) + s.sendEvent( + "response.function_call_arguments.done", + openaifmt.BuildResponsesFunctionCallArgumentsDonePayload(s.responseID, itemID, outputIndex, callID, name, args), + ) + item := map[string]any{ + "id": itemID, + "type": "function_call", + "call_id": callID, + "name": name, + "arguments": args, + "status": "completed", + } + s.sendEvent( + "response.output_item.done", + openaifmt.BuildResponsesOutputItemDonePayload(s.responseID, itemID, outputIndex, item), + ) + s.functionDone[idx] = true + s.toolCallsDoneEmitted = true + } +} + +func (s *responsesStreamRuntime) buildCompletedResponseObject(finalThinking, finalText string, calls []toolcall.ParsedToolCall) map[string]any { + type indexedItem struct { + index int + item map[string]any + } + indexed := make([]indexedItem, 0, len(calls)+1) + + if s.messageAdded { + text := s.visibleText.String() + indexed = append(indexed, indexedItem{ + index: s.ensureMessageOutputIndex(), + item: map[string]any{ + "id": s.ensureMessageItemID(), + "type": "message", + "role": "assistant", + "status": "completed", + "content": []map[string]any{ + { + "type": "output_text", + "text": text, + }, + }, + }, + }) + } else if len(calls) > 0 && strings.TrimSpace(finalThinking) != "" { + indexed = append(indexed, indexedItem{ + index: s.ensureMessageOutputIndex(), + item: map[string]any{ + "id": s.ensureMessageItemID(), + "type": "message", + "role": "assistant", + "status": "completed", + "content": []map[string]any{ + { + "type": "reasoning", + "text": finalThinking, + }, + }, + }, + }) + } else if len(calls) == 0 { + content := make([]map[string]any, 0, 2) + if finalThinking != "" { + content = append(content, map[string]any{ + "type": "reasoning", + "text": finalThinking, + }) + } + if finalText != "" { + content = append(content, map[string]any{ + "type": "output_text", + "text": finalText, + }) + } + if len(content) > 0 { + indexed = append(indexed, indexedItem{ + index: s.ensureMessageOutputIndex(), + item: map[string]any{ + "id": s.ensureMessageItemID(), + "type": "message", + "role": "assistant", + "status": "completed", + "content": content, + }, + }) + } + } + + normalizedCalls := toolcall.NormalizeParsedToolCallsForSchemas(calls, s.toolsRaw) + for idx, tc := range normalizedCalls { + if strings.TrimSpace(tc.Name) == "" { + continue + } + argsBytes, _ := json.Marshal(tc.Input) + indexed = append(indexed, indexedItem{ + index: s.ensureFunctionOutputIndex(idx), + item: map[string]any{ + "id": s.ensureFunctionItemID(idx), + "type": "function_call", + "call_id": s.ensureToolCallID(idx), + "name": tc.Name, + "arguments": string(argsBytes), + "status": "completed", + }, + }) + } + + sort.SliceStable(indexed, func(i, j int) bool { + return indexed[i].index < indexed[j].index + }) + output := make([]any, 0, len(indexed)) + for _, it := range indexed { + output = append(output, it.item) + } + + outputText := s.visibleText.String() + if outputText == "" && len(calls) == 0 { + if finalText != "" { + outputText = finalText + } else if finalThinking != "" { + outputText = finalThinking + } + } + + obj := openaifmt.BuildResponseObjectFromItems( + s.responseID, + s.model, + s.finalPrompt, + finalThinking, + finalText, + output, + outputText, + ) + if s.refFileTokens > 0 { + addRefFileTokensToUsage(obj, s.refFileTokens) + } + return obj +} diff --git a/internal/httpapi/openai/responses/responses_stream_test.go b/internal/httpapi/openai/responses/responses_stream_test.go new file mode 100644 index 0000000000000000000000000000000000000000..dac0e54a9c2d6995c1a8f29f760cb90d661a9f63 --- /dev/null +++ b/internal/httpapi/openai/responses/responses_stream_test.go @@ -0,0 +1,623 @@ +package responses + +import ( + "bufio" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "ds2api/internal/promptcompat" +) + +func TestHandleResponsesStreamDoesNotEmitReasoningTextCompatEvents(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + b, _ := json.Marshal(map[string]any{ + "p": "response/thinking_content", + "v": "thought", + }) + streamBody := "data: " + string(b) + "\n" + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-v4-pro", "prompt", 0, true, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + + body := rec.Body.String() + if !strings.Contains(body, "event: response.reasoning.delta") { + t.Fatalf("expected response.reasoning.delta event, body=%s", body) + } + if strings.Contains(body, "event: response.reasoning_text.delta") || strings.Contains(body, "event: response.reasoning_text.done") { + t.Fatalf("did not expect response.reasoning_text.* compatibility events, body=%s", body) + } +} + +func TestHandleResponsesStreamEmitsOutputTextDoneBeforeContentPartDone(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + sseLine := func(v string) string { + b, _ := json.Marshal(map[string]any{ + "p": "response/content", + "v": v, + }) + return "data: " + string(b) + "\n" + } + + streamBody := sseLine("hello") + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + body := rec.Body.String() + if !strings.Contains(body, "event: response.output_text.done") { + t.Fatalf("expected response.output_text.done payload, body=%s", body) + } + textDoneIdx := strings.Index(body, "event: response.output_text.done") + partDoneIdx := strings.Index(body, "event: response.content_part.done") + if textDoneIdx < 0 || partDoneIdx < 0 { + t.Fatalf("expected output_text.done + content_part.done, body=%s", body) + } + if textDoneIdx > partDoneIdx { + t.Fatalf("expected output_text.done before content_part.done, body=%s", body) + } +} + +func TestHandleResponsesStreamOutputTextDeltaCarriesItemIndexes(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + sseLine := func(v string) string { + b, _ := json.Marshal(map[string]any{ + "p": "response/content", + "v": v, + }) + return "data: " + string(b) + "\n" + } + + streamBody := sseLine("hello") + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + body := rec.Body.String() + + deltaPayload, ok := extractSSEEventPayload(body, "response.output_text.delta") + if !ok { + t.Fatalf("expected response.output_text.delta payload, body=%s", body) + } + if strings.TrimSpace(asString(deltaPayload["item_id"])) == "" { + t.Fatalf("expected non-empty item_id in output_text.delta, payload=%#v", deltaPayload) + } + if _, ok := deltaPayload["output_index"]; !ok { + t.Fatalf("expected output_index in output_text.delta, payload=%#v", deltaPayload) + } + if _, ok := deltaPayload["content_index"]; !ok { + t.Fatalf("expected content_index in output_text.delta, payload=%#v", deltaPayload) + } +} + +func TestHandleResponsesStreamCoalescesSmallOutputTextDeltas(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + var streamBody strings.Builder + for i := 0; i < 100; i++ { + b, _ := json.Marshal(map[string]any{ + "p": "response/content", + "v": "字", + }) + streamBody.WriteString("data: ") + streamBody.WriteString(string(b)) + streamBody.WriteString("\n") + } + streamBody.WriteString("data: [DONE]\n") + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody.String())), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_coalesce", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + + payloads := extractSSEEventPayloads(rec.Body.String(), "response.output_text.delta") + if len(payloads) == 0 { + t.Fatalf("expected response.output_text.delta payloads, body=%s", rec.Body.String()) + } + var content strings.Builder + for _, payload := range payloads { + content.WriteString(asString(payload["delta"])) + } + if got, want := content.String(), strings.Repeat("字", 100); got != want { + t.Fatalf("coalesced response content mismatch: got %q want %q body=%s", got, want, rec.Body.String()) + } + if len(payloads) >= 100 { + t.Fatalf("expected coalescing to reduce 100 tiny text deltas, got %d body=%s", len(payloads), rec.Body.String()) + } + if !strings.Contains(rec.Body.String(), "event: response.completed") { + t.Fatalf("expected completed event, body=%s", rec.Body.String()) + } +} + +func TestHandleResponsesStreamEmitsDistinctToolCallIDsAcrossSeparateToolBlocks(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + sseLine := func(v string) string { + b, _ := json.Marshal(map[string]any{ + "p": "response/content", + "v": v, + }) + return "data: " + string(b) + "\n" + } + + streamBody := sseLine("前置文本\n\n \n README.MD\n \n") + + sseLine("中间文本\n\n \n golang\n \n") + + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, false, false, []string{"read_file", "search"}, nil, promptcompat.DefaultToolChoicePolicy(), "") + + body := rec.Body.String() + doneEvents := extractSSEEventPayloads(body, "response.function_call_arguments.done") + if len(doneEvents) < 2 { + t.Fatalf("expected at least two function call done events, got %d body=%s", len(doneEvents), body) + } + + ids := make([]string, 0, 2) + seen := make(map[string]struct{}) + for _, payload := range doneEvents { + callID := asString(payload["call_id"]) + if callID == "" { + continue + } + if _, ok := seen[callID]; ok { + continue + } + seen[callID] = struct{}{} + ids = append(ids, callID) + } + + if len(ids) != 2 { + t.Fatalf("expected two distinct call ids, got %#v body=%s", ids, body) + } + if ids[0] == ids[1] { + t.Fatalf("expected distinct call ids across blocks, got %#v body=%s", ids, body) + } +} + +func TestHandleResponsesStreamRequiredToolChoiceFailure(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + sseLine := func(v string) string { + b, _ := json.Marshal(map[string]any{ + "p": "response/content", + "v": v, + }) + return "data: " + string(b) + "\n" + } + + streamBody := sseLine("plain text only") + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + policy := promptcompat.ToolChoicePolicy{ + Mode: promptcompat.ToolChoiceRequired, + Allowed: map[string]struct{}{"read_file": {}}, + } + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, false, false, []string{"read_file"}, nil, policy, "") + + body := rec.Body.String() + if !strings.Contains(body, "event: response.failed") { + t.Fatalf("expected response.failed event for required tool_choice violation, body=%s", body) + } + if strings.Contains(body, "event: response.completed") { + t.Fatalf("did not expect response.completed after failure, body=%s", body) + } +} + +func TestHandleResponsesStreamFailsWhenUpstreamHasOnlyThinking(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + sseLine := func(path, value string) string { + b, _ := json.Marshal(map[string]any{ + "p": path, + "v": value, + }) + return "data: " + string(b) + "\n" + } + + streamBody := sseLine("response/thinking_content", "Only thinking") + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-v4-pro", "prompt", 0, true, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + + body := rec.Body.String() + if !strings.Contains(body, "event: response.failed") { + t.Fatalf("expected response.failed event, body=%s", body) + } + if strings.Contains(body, "event: response.completed") { + t.Fatalf("did not expect response.completed, body=%s", body) + } + payload, ok := extractSSEEventPayload(body, "response.failed") + if !ok { + t.Fatalf("expected response.failed payload, body=%s", body) + } + errObj, _ := payload["error"].(map[string]any) + if asString(errObj["code"]) != "upstream_empty_output" { + t.Fatalf("expected code=upstream_empty_output, got %#v", payload) + } +} + +func TestHandleResponsesStreamPromotesThinkingToolCallsOnFinalizeWithoutMidstreamIntercept(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + sseLine := func(path, value string) string { + b, _ := json.Marshal(map[string]any{ + "p": path, + "v": value, + }) + return "data: " + string(b) + "\n" + } + + streamBody := sseLine("response/thinking_content", `README.MD`) + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-v4-pro", "prompt", 0, true, false, []string{"read_file"}, nil, promptcompat.DefaultToolChoicePolicy(), "") + + body := rec.Body.String() + if strings.Contains(body, "event: response.reasoning.delta") { + t.Fatalf("did not expect leaked reasoning delta in stream body, got %s", body) + } + if !strings.Contains(body, "event: response.function_call_arguments.done") { + t.Fatalf("expected finalize fallback function call event, got %s", body) + } + if strings.Contains(body, "event: response.failed") { + t.Fatalf("did not expect response.failed, body=%s", body) + } +} + +func TestHandleResponsesStreamPromotesHiddenThinkingDSMLToolCallsOnFinalize(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + + sseLine := func(path, value string) string { + b, _ := json.Marshal(map[string]any{ + "p": path, + "v": value, + }) + return "data: " + string(b) + "\n" + } + + streamBody := sseLine("response/thinking_content", `<|DSML|tool_calls><|DSML|invoke name="read_file"><|DSML|parameter name="path">README.MD`) + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + policy := promptcompat.ToolChoicePolicy{ + Mode: promptcompat.ToolChoiceRequired, + Allowed: map[string]struct{}{"read_file": {}}, + } + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_hidden", "deepseek-v4-pro", "prompt", 0, false, false, []string{"read_file"}, nil, policy, "") + + body := rec.Body.String() + if strings.Contains(body, "event: response.reasoning.delta") { + t.Fatalf("did not expect hidden reasoning delta in stream body, got %s", body) + } + if !strings.Contains(body, "event: response.function_call_arguments.done") { + t.Fatalf("expected hidden-thinking fallback function call event, got %s", body) + } + if strings.Contains(body, "event: response.failed") { + t.Fatalf("did not expect response.failed, body=%s", body) + } +} + +func TestHandleResponsesNonStreamRequiredToolChoiceViolation(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader( + `data: {"p":"response/content","v":"plain text only"}` + "\n" + + `data: [DONE]` + "\n", + )), + } + policy := promptcompat.ToolChoicePolicy{ + Mode: promptcompat.ToolChoiceRequired, + Allowed: map[string]struct{}{"read_file": {}}, + } + + h.handleResponsesNonStream(rec, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, false, false, []string{"read_file"}, nil, policy, "") + if rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("expected 422 for required tool_choice violation, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "tool_choice_violation" { + t.Fatalf("expected code=tool_choice_violation, got %#v", out) + } +} + +func TestHandleResponsesNonStreamRequiredToolChoiceIgnoresThinkingToolPayloadWhenTextExists(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader( + `data: {"p":"response/thinking_content","v":"{\"tool_calls\":[{\"name\":\"read_file\",\"input\":{\"path\":\"README.MD\"}}]}"}` + "\n" + + `data: {"p":"response/content","v":"plain text only"}` + "\n" + + `data: [DONE]` + "\n", + )), + } + policy := promptcompat.ToolChoicePolicy{ + Mode: promptcompat.ToolChoiceRequired, + Allowed: map[string]struct{}{"read_file": {}}, + } + + h.handleResponsesNonStream(rec, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, true, false, []string{"read_file"}, nil, policy, "") + if rec.Code != http.StatusUnprocessableEntity { + t.Fatalf("expected 422 for required tool_choice violation, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "tool_choice_violation" { + t.Fatalf("expected code=tool_choice_violation, got %#v", out) + } +} + +func TestHandleResponsesNonStreamSingleAttemptReturns503WhenUpstreamOutputEmpty(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader( + `data: {"p":"response/content","v":""}` + "\n" + + `data: [DONE]` + "\n", + )), + } + + h.handleResponsesNonStream(rec, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + if rec.Code != http.StatusServiceUnavailable { + t.Fatalf("expected 503 for empty upstream output, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "upstream_unavailable" { + t.Fatalf("expected code=upstream_unavailable, got %#v", out) + } +} + +func TestHandleResponsesNonStreamSingleAttemptReturnsContentFilterErrorWhenUpstreamFilteredWithoutOutput(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader( + `data: {"code":"content_filter"}` + "\n" + + `data: [DONE]` + "\n", + )), + } + + h.handleResponsesNonStream(rec, resp, "owner-a", "resp_test", "deepseek-v4-flash", "prompt", 0, false, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400 for filtered empty upstream output, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "content_filter" { + t.Fatalf("expected code=content_filter, got %#v", out) + } +} + +func TestHandleResponsesNonStreamSingleAttemptReturns429WhenUpstreamHasOnlyThinking(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader( + `data: {"p":"response/thinking_content","v":"Only thinking"}` + "\n" + + `data: [DONE]` + "\n", + )), + } + + h.handleResponsesNonStream(rec, resp, "owner-a", "resp_test", "deepseek-v4-pro", "prompt", 0, true, false, nil, nil, promptcompat.DefaultToolChoicePolicy(), "") + if rec.Code != http.StatusTooManyRequests { + t.Fatalf("expected 429 for thinking-only upstream output, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + errObj, _ := out["error"].(map[string]any) + if asString(errObj["code"]) != "upstream_empty_output" { + t.Fatalf("expected code=upstream_empty_output, got %#v", out) + } +} + +func TestHandleResponsesNonStreamPromotesThinkingToolCallsWhenTextEmpty(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader( + `data: {"p":"response/thinking_content","v":"README.MD"}` + "\n" + + `data: [DONE]` + "\n", + )), + } + + h.handleResponsesNonStream(rec, resp, "owner-a", "resp_test", "deepseek-v4-pro", "prompt", 0, true, false, []string{"read_file"}, nil, promptcompat.DefaultToolChoicePolicy(), "") + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 for thinking tool calls, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + output, _ := out["output"].([]any) + if len(output) != 1 { + t.Fatalf("expected one output item, got %#v", out["output"]) + } + first, _ := output[0].(map[string]any) + if got := asString(first["type"]); got != "function_call" { + t.Fatalf("expected function_call output, got %#v", first["type"]) + } +} + +func TestHandleResponsesNonStreamPromotesHiddenThinkingDSMLToolCallsWhenTextEmpty(t *testing.T) { + h := &Handler{} + rec := httptest.NewRecorder() + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader( + `data: {"p":"response/thinking_content","v":"<|DSML|tool_calls><|DSML|invoke name=\"read_file\"><|DSML|parameter name=\"path\">README.MD"}` + "\n" + + `data: [DONE]` + "\n", + )), + } + + policy := promptcompat.ToolChoicePolicy{ + Mode: promptcompat.ToolChoiceRequired, + Allowed: map[string]struct{}{"read_file": {}}, + } + h.handleResponsesNonStream(rec, resp, "owner-a", "resp_hidden", "deepseek-v4-pro", "prompt", 0, false, false, []string{"read_file"}, nil, policy, "") + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 for hidden thinking tool calls, got %d body=%s", rec.Code, rec.Body.String()) + } + out := decodeJSONBody(t, rec.Body.String()) + output, _ := out["output"].([]any) + if len(output) != 1 { + t.Fatalf("expected one output item, got %#v", out["output"]) + } + first, _ := output[0].(map[string]any) + if got := asString(first["type"]); got != "function_call" { + t.Fatalf("expected function_call output, got %#v", first["type"]) + } + if strings.Contains(rec.Body.String(), "reasoning") { + t.Fatalf("did not expect hidden reasoning in response body, got %s", rec.Body.String()) + } +} + +func TestHandleResponsesStreamCoercesSchemaDeclaredStringArguments(t *testing.T) { + h := &Handler{} + req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil) + rec := httptest.NewRecorder() + toolsRaw := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "Write", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + "taskId": map[string]any{"type": "string"}, + }, + }, + }, + }, + } + sseLine := func(v string) string { + b, _ := json.Marshal(map[string]any{"p": "response/content", "v": v}) + return "data: " + string(b) + "\n" + } + streamBody := sseLine(`{"input":{"content":{"message":"hi"},"taskId":1}}`) + "data: [DONE]\n" + resp := &http.Response{ + StatusCode: http.StatusOK, + Body: io.NopCloser(strings.NewReader(streamBody)), + } + + h.handleResponsesStream(rec, req, resp, "owner-a", "resp_string_protect", "deepseek-v4-flash", "prompt", 0, false, false, []string{"Write"}, toolsRaw, promptcompat.DefaultToolChoicePolicy(), "") + + payload, ok := extractSSEEventPayload(rec.Body.String(), "response.function_call_arguments.done") + if !ok { + t.Fatalf("expected response.function_call_arguments.done payload, body=%s", rec.Body.String()) + } + args := map[string]any{} + if err := json.Unmarshal([]byte(asString(payload["arguments"])), &args); err != nil { + t.Fatalf("decode streamed response arguments failed: %v", err) + } + if args["content"] != `{"message":"hi"}` { + t.Fatalf("expected response content stringified by schema, got %#v", args["content"]) + } + if args["taskId"] != "1" { + t.Fatalf("expected response taskId stringified by schema, got %#v", args["taskId"]) + } +} + +func extractSSEEventPayload(body, targetEvent string) (map[string]any, bool) { + scanner := bufio.NewScanner(strings.NewReader(body)) + matched := false + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if strings.HasPrefix(line, "event: ") { + evt := strings.TrimSpace(strings.TrimPrefix(line, "event: ")) + matched = evt == targetEvent + continue + } + if !matched || !strings.HasPrefix(line, "data: ") { + continue + } + raw := strings.TrimSpace(strings.TrimPrefix(line, "data: ")) + if raw == "" || raw == "[DONE]" { + continue + } + var payload map[string]any + if err := json.Unmarshal([]byte(raw), &payload); err != nil { + return nil, false + } + return payload, true + } + return nil, false +} + +func extractSSEEventPayloads(body, targetEvent string) []map[string]any { + scanner := bufio.NewScanner(strings.NewReader(body)) + matched := false + out := make([]map[string]any, 0, 4) + for scanner.Scan() { + line := strings.TrimSpace(scanner.Text()) + if strings.HasPrefix(line, "event: ") { + evt := strings.TrimSpace(strings.TrimPrefix(line, "event: ")) + matched = evt == targetEvent + continue + } + if !matched || !strings.HasPrefix(line, "data: ") { + continue + } + raw := strings.TrimSpace(strings.TrimPrefix(line, "data: ")) + if raw == "" || raw == "[DONE]" { + continue + } + var payload map[string]any + if err := json.Unmarshal([]byte(raw), &payload); err != nil { + continue + } + out = append(out, payload) + } + return out +} diff --git a/internal/httpapi/openai/responses/test_helpers_test.go b/internal/httpapi/openai/responses/test_helpers_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f239aa5b90cede56a622c1ab13b7200782f4d3f8 --- /dev/null +++ b/internal/httpapi/openai/responses/test_helpers_test.go @@ -0,0 +1,28 @@ +package responses + +import ( + "encoding/json" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/httpapi/openai/shared" +) + +func asString(v any) string { + return shared.AsString(v) +} + +func decodeJSONBody(t *testing.T, body string) map[string]any { + t.Helper() + var out map[string]any + if err := json.Unmarshal([]byte(body), &out); err != nil { + t.Fatalf("decode json failed: %v, body=%s", err, body) + } + return out +} + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Post("/v1/responses", h.Responses) + r.Get("/v1/responses/{response_id}", h.GetResponseByID) +} diff --git a/internal/httpapi/openai/shared/assistant_toolcalls.go b/internal/httpapi/openai/shared/assistant_toolcalls.go new file mode 100644 index 0000000000000000000000000000000000000000..f90860fb7445bc397f5ff0a0b183ffebe00bd148 --- /dev/null +++ b/internal/httpapi/openai/shared/assistant_toolcalls.go @@ -0,0 +1,26 @@ +package shared + +import ( + "strings" + + "ds2api/internal/toolcall" +) + +func DetectAssistantToolCalls(rawText, visibleText, exposedThinking, detectionThinking string, toolNames []string) toolcall.ToolCallParseResult { + textParsed := toolcall.ParseStandaloneToolCallsDetailed(rawText, toolNames) + if len(textParsed.Calls) > 0 { + return textParsed + } + if strings.TrimSpace(visibleText) != "" { + return textParsed + } + thinking := detectionThinking + if strings.TrimSpace(thinking) == "" { + thinking = exposedThinking + } + thinkingParsed := toolcall.ParseStandaloneToolCallsDetailed(thinking, toolNames) + if len(thinkingParsed.Calls) > 0 { + return thinkingParsed + } + return textParsed +} diff --git a/internal/httpapi/openai/shared/citation_links.go b/internal/httpapi/openai/shared/citation_links.go new file mode 100644 index 0000000000000000000000000000000000000000..b4e2f336d7bca57b46f2c42208eb91fc7df7c11b --- /dev/null +++ b/internal/httpapi/openai/shared/citation_links.go @@ -0,0 +1,49 @@ +package shared + +import ( + "fmt" + "regexp" + "strconv" + "strings" +) + +var citationMarkerPattern = regexp.MustCompile(`(?i)\[(citation|reference):\s*(\d+)\]`) + +func ReplaceCitationMarkersWithLinks(text string, links map[int]string) string { + if strings.TrimSpace(text) == "" || len(links) == 0 { + return text + } + zeroBasedReference := hasZeroBasedReferenceMarker(text) + return citationMarkerPattern.ReplaceAllStringFunc(text, func(match string) string { + sub := citationMarkerPattern.FindStringSubmatch(match) + if len(sub) < 3 { + return match + } + idx, err := strconv.Atoi(strings.TrimSpace(sub[2])) + if err != nil || idx < 0 { + return match + } + lookupIdx := idx + if strings.EqualFold(sub[1], "reference") && zeroBasedReference { + lookupIdx = idx + 1 + } + url := strings.TrimSpace(links[lookupIdx]) + if url == "" { + return match + } + return fmt.Sprintf("[%d](%s)", idx, url) + }) +} + +func hasZeroBasedReferenceMarker(text string) bool { + for _, sub := range citationMarkerPattern.FindAllStringSubmatch(text, -1) { + if len(sub) < 3 || !strings.EqualFold(sub[1], "reference") { + continue + } + idx, err := strconv.Atoi(strings.TrimSpace(sub[2])) + if err == nil && idx == 0 { + return true + } + } + return false +} diff --git a/internal/httpapi/openai/shared/deps.go b/internal/httpapi/openai/shared/deps.go new file mode 100644 index 0000000000000000000000000000000000000000..eca93a767e0d007cb90f8b4ab506d37521f1a26f --- /dev/null +++ b/internal/httpapi/openai/shared/deps.go @@ -0,0 +1,61 @@ +package shared + +import ( + "context" + "net/http" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/util" +) + +const ( + // UploadMaxSize limits total multipart request body size (100 MiB). + UploadMaxSize = 100 << 20 + // GeneralMaxSize limits total JSON request body size (100 MiB). + GeneralMaxSize = 100 << 20 +) + +type AuthResolver interface { + Determine(req *http.Request) (*auth.RequestAuth, error) + DetermineCaller(req *http.Request) (*auth.RequestAuth, error) + Release(a *auth.RequestAuth) +} + +type DeepSeekCaller interface { + CreateSession(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + GetPow(ctx context.Context, a *auth.RequestAuth, maxAttempts int) (string, error) + UploadFile(ctx context.Context, a *auth.RequestAuth, req dsclient.UploadFileRequest, maxAttempts int) (*dsclient.UploadFileResult, error) + CallCompletion(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, maxAttempts int) (*http.Response, error) + DeleteSessionForToken(ctx context.Context, token string, sessionID string) (*dsclient.DeleteSessionResult, error) + DeleteAllSessionsForToken(ctx context.Context, token string) error +} + +type ConfigReader interface { + ModelAliases() map[string]string + ToolcallMode() string + ToolcallEarlyEmitConfidence() string + ResponsesStoreTTLSeconds() int + EmbeddingsProvider() string + AutoDeleteMode() string + AutoDeleteSessions() bool + CurrentInputFileEnabled() bool + CurrentInputFileMinChars() int + ThinkingInjectionEnabled() bool + ThinkingInjectionPrompt() string +} + +type Deps struct { + Store ConfigReader + Auth AuthResolver + DS DeepSeekCaller + ChatHistory *chathistory.Store +} + +var WriteJSON = util.WriteJSON + +var _ AuthResolver = (*auth.Resolver)(nil) +var _ DeepSeekCaller = (*dsclient.Client)(nil) +var _ ConfigReader = (*config.Store)(nil) diff --git a/internal/httpapi/openai/shared/empty_retry.go b/internal/httpapi/openai/shared/empty_retry.go new file mode 100644 index 0000000000000000000000000000000000000000..cf4c56938a42fe16af551fb775521cce06e3d8b1 --- /dev/null +++ b/internal/httpapi/openai/shared/empty_retry.go @@ -0,0 +1,56 @@ +package shared + +import "strings" + +const EmptyOutputRetrySuffix = "Please provide a non-empty final answer or tool call." + +func EmptyOutputRetryEnabled() bool { + return true +} + +func EmptyOutputRetryMaxAttempts() int { + return 1 +} + +func ClonePayloadWithEmptyOutputRetryPrompt(payload map[string]any) map[string]any { + return ClonePayloadForEmptyOutputRetry(payload, 0) +} + +// ClonePayloadForEmptyOutputRetry creates a retry payload with the suffix +// appended and, if parentMessageID > 0, sets parent_message_id so the +// retry is submitted as a proper follow-up turn in the same DeepSeek +// session rather than a disconnected root message. +func ClonePayloadForEmptyOutputRetry(payload map[string]any, parentMessageID int) map[string]any { + clone := make(map[string]any, len(payload)) + for k, v := range payload { + clone[k] = v + } + original, _ := payload["prompt"].(string) + clone["prompt"] = AppendEmptyOutputRetrySuffix(original) + if parentMessageID > 0 { + clone["parent_message_id"] = parentMessageID + } + return clone +} + +func AppendEmptyOutputRetrySuffix(prompt string) string { + prompt = strings.TrimRight(prompt, "\r\n\t ") + if prompt == "" { + return EmptyOutputRetrySuffix + } + return prompt + "\n\n" + EmptyOutputRetrySuffix +} + +func UsagePromptWithEmptyOutputRetry(originalPrompt string, retryAttempts int) string { + if retryAttempts <= 0 { + return originalPrompt + } + parts := make([]string, 0, retryAttempts+1) + parts = append(parts, originalPrompt) + next := originalPrompt + for i := 0; i < retryAttempts; i++ { + next = AppendEmptyOutputRetrySuffix(next) + parts = append(parts, next) + } + return strings.Join(parts, "\n") +} diff --git a/internal/httpapi/openai/shared/handler_errors.go b/internal/httpapi/openai/shared/handler_errors.go new file mode 100644 index 0000000000000000000000000000000000000000..52f399ea2df9b0e33e4b7654f7901f4400a46fdf --- /dev/null +++ b/internal/httpapi/openai/shared/handler_errors.go @@ -0,0 +1,63 @@ +package shared + +import "net/http" + +func WriteOpenAIError(w http.ResponseWriter, status int, message string) { + WriteOpenAIErrorWithCode(w, status, message, "") +} + +func WriteOpenAIErrorWithCode(w http.ResponseWriter, status int, message, code string) { + if code == "" { + code = OpenAIErrorCode(status) + } + WriteJSON(w, status, map[string]any{ + "error": map[string]any{ + "message": message, + "type": OpenAIErrorType(status), + "code": code, + "param": nil, + }, + }) +} + +func OpenAIErrorType(status int) string { + switch status { + case http.StatusBadRequest: + return "invalid_request_error" + case http.StatusUnauthorized: + return "authentication_error" + case http.StatusForbidden: + return "permission_error" + case http.StatusTooManyRequests: + return "rate_limit_error" + case http.StatusServiceUnavailable: + return "service_unavailable_error" + default: + if status >= 500 { + return "api_error" + } + return "invalid_request_error" + } +} + +func OpenAIErrorCode(status int) string { + switch status { + case http.StatusBadRequest: + return "invalid_request" + case http.StatusUnauthorized: + return "authentication_failed" + case http.StatusForbidden: + return "forbidden" + case http.StatusTooManyRequests: + return "rate_limit_exceeded" + case http.StatusNotFound: + return "not_found" + case http.StatusServiceUnavailable: + return "service_unavailable" + default: + if status >= 500 { + return "internal_error" + } + return "invalid_request" + } +} diff --git a/internal/httpapi/openai/shared/handler_toolcall_format.go b/internal/httpapi/openai/shared/handler_toolcall_format.go new file mode 100644 index 0000000000000000000000000000000000000000..b4beb376dc60eac03b3ce89e89f328318fd075b4 --- /dev/null +++ b/internal/httpapi/openai/shared/handler_toolcall_format.go @@ -0,0 +1,102 @@ +package shared + +import ( + "ds2api/internal/toolcall" + "encoding/json" + "strings" + + "github.com/google/uuid" + + "ds2api/internal/toolstream" +) + +func FormatIncrementalStreamToolCallDeltas(deltas []toolstream.ToolCallDelta, ids map[int]string) []map[string]any { + if len(deltas) == 0 { + return nil + } + out := make([]map[string]any, 0, len(deltas)) + for _, d := range deltas { + if d.Name == "" && d.Arguments == "" { + continue + } + callID, ok := ids[d.Index] + if !ok || callID == "" { + callID = "call_" + strings.ReplaceAll(uuid.NewString(), "-", "") + ids[d.Index] = callID + } + item := map[string]any{ + "index": d.Index, + "id": callID, + "type": "function", + } + fn := map[string]any{} + if d.Name != "" { + fn["name"] = d.Name + } + if d.Arguments != "" { + fn["arguments"] = d.Arguments + } + if len(fn) > 0 { + item["function"] = fn + } + out = append(out, item) + } + return out +} + +func FilterIncrementalToolCallDeltasByAllowed(deltas []toolstream.ToolCallDelta, seenNames map[int]string) []toolstream.ToolCallDelta { + if len(deltas) == 0 { + return nil + } + out := make([]toolstream.ToolCallDelta, 0, len(deltas)) + for _, d := range deltas { + if d.Name != "" { + if seenNames != nil { + seenNames[d.Index] = d.Name + } + out = append(out, d) + continue + } + if seenNames == nil { + out = append(out, d) + continue + } + name := strings.TrimSpace(seenNames[d.Index]) + if name == "" { + continue + } + out = append(out, d) + } + return out +} + +func FormatFinalStreamToolCallsWithStableIDs(calls []toolcall.ParsedToolCall, ids map[int]string, toolsRaw any) []map[string]any { + if len(calls) == 0 { + return nil + } + normalizedCalls := toolcall.NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + out := make([]map[string]any, 0, len(calls)) + for i, c := range normalizedCalls { + callID := "" + if ids != nil { + callID = strings.TrimSpace(ids[i]) + } + if callID == "" { + callID = "call_" + strings.ReplaceAll(uuid.NewString(), "-", "") + if ids != nil { + ids[i] = callID + } + } + args, _ := json.Marshal(c.Input) + out = append(out, map[string]any{ + "index": i, + "id": callID, + "type": "function", + "function": map[string]any{ + "name": c.Name, + "arguments": string(args), + }, + }) + } + return out +} diff --git a/internal/httpapi/openai/shared/handler_toolcall_policy.go b/internal/httpapi/openai/shared/handler_toolcall_policy.go new file mode 100644 index 0000000000000000000000000000000000000000..181a62771efeb3c2bd81207e50e565c28aed5655 --- /dev/null +++ b/internal/httpapi/openai/shared/handler_toolcall_policy.go @@ -0,0 +1,9 @@ +package shared + +func ToolcallFeatureMatchEnabled(_ ConfigReader) bool { + return true +} + +func ToolcallEarlyEmitHighConfidence(_ ConfigReader) bool { + return true +} diff --git a/internal/httpapi/openai/shared/leaked_output_sanitize.go b/internal/httpapi/openai/shared/leaked_output_sanitize.go new file mode 100644 index 0000000000000000000000000000000000000000..9293e78f5d86ea4688449249917072b0dba9ca59 --- /dev/null +++ b/internal/httpapi/openai/shared/leaked_output_sanitize.go @@ -0,0 +1,158 @@ +package shared + +import ( + "regexp" + "strings" + + "ds2api/internal/toolcall" +) + +var emptyJSONFencePattern = regexp.MustCompile("(?is)```json\\s*```") +var leakedToolCallArrayPattern = regexp.MustCompile(`(?is)\[\{\s*"function"\s*:\s*\{[\s\S]*?\}\s*,\s*"id"\s*:\s*"call[^"]*"\s*,\s*"type"\s*:\s*"function"\s*}\]`) +var leakedToolResultBlobPattern = regexp.MustCompile(`(?is)<\s*\|\s*tool\s*\|\s*>\s*\{[\s\S]*?"tool_call_id"\s*:\s*"call[^"]*"\s*}`) + +var leakedThinkTagPattern = regexp.MustCompile(`(?is)`) + +// leakedBOSMarkerPattern matches DeepSeek BOS markers with halfwidth or +// legacy U+FF5C fullwidth delimiters: +// - ASCII underscore: <|begin_of_sentence|> +// - U+2581 variant: <|begin▁of▁sentence|> +var leakedBOSMarkerPattern = regexp.MustCompile(`(?i)<[\|\x{ff5c}]\s*begin[_▁]of[_▁]sentence\s*[\|\x{ff5c}]>`) + +// leakedThoughtMarkerPattern matches leaked thought control markers in both +// explicit and compact forms: +// - ASCII underscore: <| of_thought |>, <| begin_of_thought |> +// - U+2581 variant: <|▁of▁thought|>, <|begin▁of▁thought|> +var leakedThoughtMarkerPattern = regexp.MustCompile(`(?i)<[\|\x{ff5c}]\s*(?:begin[_▁])?[_▁]*of[_▁]thought\s*[\|\x{ff5c}]>`) + +// leakedMetaMarkerPattern matches the remaining DeepSeek special tokens with +// halfwidth or legacy U+FF5C fullwidth delimiters: +// - ASCII underscore: <|end_of_sentence|>, <|end_of_toolresults|>, <|end_of_instructions|> +// - U+2581 variant: <|end▁of▁sentence|>, <|end▁of▁toolresults|>, <|end▁of▁instructions|> +var leakedMetaMarkerPattern = regexp.MustCompile(`(?i)<[\|\x{ff5c}]\s*(?:assistant|tool|end[_▁]of[_▁]sentence|end[_▁]of[_▁]thinking|end[_▁]of[_▁]thought|end[_▁]of[_▁]toolresults|end[_▁]of[_▁]instructions)\s*[\|\x{ff5c}]>`) + +// leakedAgentXMLBlockPatterns catch agent-style XML blocks that leak through +// when the sieve fails to capture them. These are applied only to complete +// wrapper blocks so standalone "" examples in normal output remain +// untouched. +var leakedAgentXMLBlockPatterns = []*regexp.Regexp{ + regexp.MustCompile(`(?is)]*>(.*?)`), + regexp.MustCompile(`(?is)]*>(.*?)`), + regexp.MustCompile(`(?is)]*>(.*?)`), +} + +var leakedAgentWrapperTagPattern = regexp.MustCompile(`(?is)]*>`) +var leakedAgentWrapperPlusResultOpenPattern = regexp.MustCompile(`(?is)<(?:attempt_completion|ask_followup_question|new_task)\b[^>]*>\s*`) +var leakedAgentResultPlusWrapperClosePattern = regexp.MustCompile(`(?is)\s*]*>`) +var leakedAgentResultTagPattern = regexp.MustCompile(`(?is)`) + +func sanitizeLeakedOutput(text string) string { + if text == "" { + return text + } + out := emptyJSONFencePattern.ReplaceAllString(text, "") + out = leakedToolCallArrayPattern.ReplaceAllString(out, "") + out = leakedToolResultBlobPattern.ReplaceAllString(out, "") + out = stripDanglingThinkSuffix(out) + out = leakedThinkTagPattern.ReplaceAllString(out, "") + out = leakedBOSMarkerPattern.ReplaceAllString(out, "") + out = leakedThoughtMarkerPattern.ReplaceAllString(out, "") + out = leakedMetaMarkerPattern.ReplaceAllString(out, "") + out = stripLeakedToolCallWrapperBlocks(out) + out = sanitizeLeakedAgentXMLBlocks(out) + return out +} + +func stripLeakedToolCallWrapperBlocks(text string) string { + if text == "" { + return text + } + var b strings.Builder + pos := 0 + for pos < len(text) { + tag, ok := toolcall.FindToolMarkupTagOutsideIgnored(text, pos) + if !ok { + b.WriteString(text[pos:]) + break + } + if tag.Start > pos { + b.WriteString(text[pos:tag.Start]) + } + if tag.Closing || tag.Name != "tool_calls" { + b.WriteString(text[tag.Start : tag.End+1]) + pos = tag.End + 1 + continue + } + closeTag, ok := toolcall.FindMatchingToolMarkupClose(text, tag) + if !ok { + b.WriteString(text[tag.Start : tag.End+1]) + pos = tag.End + 1 + continue + } + pos = closeTag.End + 1 + } + return b.String() +} + +func stripDanglingThinkSuffix(text string) string { + matches := leakedThinkTagPattern.FindAllStringIndex(text, -1) + if len(matches) == 0 { + return text + } + depth := 0 + lastOpen := -1 + for _, loc := range matches { + tag := strings.ToLower(text[loc[0]:loc[1]]) + compact := strings.ReplaceAll(strings.ReplaceAll(strings.TrimSpace(tag), " ", ""), "\t", "") + if strings.HasPrefix(compact, " 0 { + depth-- + if depth == 0 { + lastOpen = -1 + } + } + continue + } + if depth == 0 { + lastOpen = loc[0] + } + depth++ + } + if depth == 0 || lastOpen < 0 { + return text + } + prefix := text[:lastOpen] + if strings.TrimSpace(prefix) == "" { + return "" + } + return prefix +} + +func sanitizeLeakedAgentXMLBlocks(text string) string { + out := text + for _, pattern := range leakedAgentXMLBlockPatterns { + out = pattern.ReplaceAllStringFunc(out, func(match string) string { + submatches := pattern.FindStringSubmatch(match) + if len(submatches) < 2 { + return match + } + // Preserve the inner text so leaked agent instructions do not erase + // the actual answer, but strip the wrapper/result markup itself. + return leakedAgentResultTagPattern.ReplaceAllString(submatches[1], "") + }) + } + // Fallback for truncated output streams: strip any dangling wrapper tags + // that were not part of a complete block replacement. If we detect leaked + // wrapper tags, strip only adjacent tags to avoid exposing agent + // markup without altering unrelated user-visible examples. + if leakedAgentWrapperTagPattern.MatchString(out) { + out = leakedAgentWrapperPlusResultOpenPattern.ReplaceAllStringFunc(out, func(match string) string { + return leakedAgentResultTagPattern.ReplaceAllString(match, "") + }) + out = leakedAgentResultPlusWrapperClosePattern.ReplaceAllStringFunc(out, func(match string) string { + return leakedAgentResultTagPattern.ReplaceAllString(match, "") + }) + out = leakedAgentWrapperTagPattern.ReplaceAllString(out, "") + } + return out +} diff --git a/internal/httpapi/openai/shared/models.go b/internal/httpapi/openai/shared/models.go new file mode 100644 index 0000000000000000000000000000000000000000..81ba607486abb4d215d59cdffb2d02caff38a94d --- /dev/null +++ b/internal/httpapi/openai/shared/models.go @@ -0,0 +1,28 @@ +package shared + +import ( + "net/http" + "strings" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/config" +) + +type ModelsHandler struct { + Store ConfigReader +} + +func (h *ModelsHandler) ListModels(w http.ResponseWriter, _ *http.Request) { + WriteJSON(w, http.StatusOK, config.OpenAIModelsResponse()) +} + +func (h *ModelsHandler) GetModel(w http.ResponseWriter, r *http.Request) { + modelID := strings.TrimSpace(chi.URLParam(r, "model_id")) + model, ok := config.OpenAIModelByID(h.Store, modelID) + if !ok { + WriteOpenAIError(w, http.StatusNotFound, "Model not found.") + return + } + WriteJSON(w, http.StatusOK, model) +} diff --git a/internal/httpapi/openai/shared/output_clean.go b/internal/httpapi/openai/shared/output_clean.go new file mode 100644 index 0000000000000000000000000000000000000000..a8905656819744186195367585c07be157c7db3a --- /dev/null +++ b/internal/httpapi/openai/shared/output_clean.go @@ -0,0 +1,13 @@ +package shared + +import textclean "ds2api/internal/textclean" + +func CleanVisibleOutput(text string, stripReferenceMarkers bool) string { + if text == "" { + return text + } + if stripReferenceMarkers { + text = textclean.StripReferenceMarkers(text) + } + return sanitizeLeakedOutput(text) +} diff --git a/internal/httpapi/openai/shared/stream_accumulator.go b/internal/httpapi/openai/shared/stream_accumulator.go new file mode 100644 index 0000000000000000000000000000000000000000..472748ee596e528062b40903054bc44fa8368dc1 --- /dev/null +++ b/internal/httpapi/openai/shared/stream_accumulator.go @@ -0,0 +1,104 @@ +package shared + +import ( + "strings" + + "ds2api/internal/sse" +) + +type StreamAccumulator struct { + ThinkingEnabled bool + SearchEnabled bool + StripReferenceMarkers bool + + RawThinking strings.Builder + Thinking strings.Builder + ToolDetectionThinking strings.Builder + RawText strings.Builder + Text strings.Builder +} + +type StreamPartDelta struct { + Type string + RawText string + VisibleText string + CitationOnly bool +} + +type StreamAccumulatorResult struct { + ContentSeen bool + Parts []StreamPartDelta +} + +func (a *StreamAccumulator) Apply(parsed sse.LineResult) StreamAccumulatorResult { + out := StreamAccumulatorResult{} + for _, p := range parsed.ToolDetectionThinkingParts { + trimmed := sse.TrimContinuationOverlapFromBuilder(&a.ToolDetectionThinking, p.Text) + if trimmed != "" { + a.ToolDetectionThinking.WriteString(trimmed) + } + } + for _, p := range parsed.Parts { + if p.Type == "thinking" { + delta := a.applyThinkingPart(p.Text) + if delta.RawText != "" { + out.ContentSeen = true + } + if delta.RawText != "" || delta.VisibleText != "" { + out.Parts = append(out.Parts, delta) + } + continue + } + delta := a.applyTextPart(p.Text) + if delta.RawText != "" { + out.ContentSeen = true + } + if delta.RawText != "" || delta.VisibleText != "" || delta.CitationOnly { + out.Parts = append(out.Parts, delta) + } + } + return out +} + +func (a *StreamAccumulator) applyThinkingPart(text string) StreamPartDelta { + rawTrimmed := sse.TrimContinuationOverlapFromBuilder(&a.RawThinking, text) + if rawTrimmed != "" { + a.RawThinking.WriteString(rawTrimmed) + } + delta := StreamPartDelta{Type: "thinking", RawText: rawTrimmed} + if !a.ThinkingEnabled || rawTrimmed == "" { + return delta + } + cleanedText := CleanVisibleOutput(rawTrimmed, a.StripReferenceMarkers) + if cleanedText == "" { + return delta + } + trimmed := sse.TrimContinuationOverlapFromBuilder(&a.Thinking, cleanedText) + if trimmed == "" { + return delta + } + a.Thinking.WriteString(trimmed) + delta.VisibleText = trimmed + return delta +} + +func (a *StreamAccumulator) applyTextPart(text string) StreamPartDelta { + rawTrimmed := sse.TrimContinuationOverlapFromBuilder(&a.RawText, text) + if rawTrimmed == "" { + return StreamPartDelta{Type: "text"} + } + a.RawText.WriteString(rawTrimmed) + delta := StreamPartDelta{Type: "text", RawText: rawTrimmed} + if a.SearchEnabled && sse.IsCitation(rawTrimmed) { + delta.CitationOnly = true + return delta + } + cleanedText := CleanVisibleOutput(rawTrimmed, a.StripReferenceMarkers) + trimmed := sse.TrimContinuationOverlapFromBuilder(&a.Text, cleanedText) + if trimmed == "" { + return delta + } + a.Text.WriteString(trimmed) + delta.VisibleText = trimmed + return delta +} diff --git a/internal/httpapi/openai/shared/stream_accumulator_test.go b/internal/httpapi/openai/shared/stream_accumulator_test.go new file mode 100644 index 0000000000000000000000000000000000000000..1f4fe9374618a514cdcfcd42bddca7527ceb55c5 --- /dev/null +++ b/internal/httpapi/openai/shared/stream_accumulator_test.go @@ -0,0 +1,115 @@ +package shared + +import ( + "testing" + + "ds2api/internal/sse" +) + +func TestStreamAccumulatorAppliesThinkingAndTextDedupe(t *testing.T) { + acc := StreamAccumulator{ThinkingEnabled: true, StripReferenceMarkers: true} + thinkingPrefix := "this is a long thinking snapshot prefix used by DeepSeek continue replay" + textPrefix := "this is a long visible answer snapshot prefix used by DeepSeek continue replay" + first := acc.Apply(sse.LineResult{ + Parsed: true, + Parts: []sse.ContentPart{ + {Type: "thinking", Text: thinkingPrefix}, + {Type: "text", Text: textPrefix}, + }, + }) + second := acc.Apply(sse.LineResult{ + Parsed: true, + Parts: []sse.ContentPart{ + {Type: "thinking", Text: thinkingPrefix + " next"}, + {Type: "text", Text: textPrefix + " world"}, + }, + }) + + if !first.ContentSeen || !second.ContentSeen { + t.Fatalf("expected both chunks to mark content seen") + } + if got := acc.RawThinking.String(); got != thinkingPrefix+" next" { + t.Fatalf("raw thinking = %q", got) + } + if got := acc.Thinking.String(); got != thinkingPrefix+" next" { + t.Fatalf("thinking = %q", got) + } + if got := acc.RawText.String(); got != textPrefix+" world" { + t.Fatalf("raw text = %q", got) + } + if got := acc.Text.String(); got != textPrefix+" world" { + t.Fatalf("text = %q", got) + } + if got := second.Parts[0].VisibleText; got != " next" { + t.Fatalf("thinking delta = %q", got) + } + if got := second.Parts[1].VisibleText; got != " world" { + t.Fatalf("text delta = %q", got) + } +} + +func TestStreamAccumulatorKeepsHiddenThinkingForToolDetection(t *testing.T) { + acc := StreamAccumulator{ThinkingEnabled: false, StripReferenceMarkers: true} + result := acc.Apply(sse.LineResult{ + Parsed: true, + Parts: []sse.ContentPart{ + {Type: "thinking", Text: ""}, + }, + ToolDetectionThinkingParts: []sse.ContentPart{ + {Type: "thinking", Text: "detect"}, + {Type: "thinking", Text: " tools"}, + }, + }) + + if !result.ContentSeen { + t.Fatalf("expected hidden thinking to count as upstream content") + } + if got := acc.RawThinking.String(); got != "" { + t.Fatalf("raw thinking = %q", got) + } + if got := acc.Thinking.String(); got != "" { + t.Fatalf("visible thinking = %q", got) + } + if got := acc.ToolDetectionThinking.String(); got != "detect tools" { + t.Fatalf("tool detection thinking = %q", got) + } +} + +func TestStreamAccumulatorSuppressesCitationTextWhenSearchEnabled(t *testing.T) { + acc := StreamAccumulator{SearchEnabled: true, StripReferenceMarkers: true} + result := acc.Apply(sse.LineResult{ + Parsed: true, + Parts: []sse.ContentPart{{Type: "text", Text: "[citation:1]"}}, + }) + + if !result.ContentSeen { + t.Fatalf("expected citation chunk to mark upstream content") + } + if len(result.Parts) != 1 || !result.Parts[0].CitationOnly { + t.Fatalf("expected citation-only delta, got %#v", result.Parts) + } + if got := acc.RawText.String(); got != "[citation:1]" { + t.Fatalf("raw text = %q", got) + } + if got := acc.Text.String(); got != "" { + t.Fatalf("visible text = %q", got) + } +} + +func TestStreamAccumulatorStripsInlineCitationAndReferenceMarkers(t *testing.T) { + acc := StreamAccumulator{SearchEnabled: true, StripReferenceMarkers: true} + result := acc.Apply(sse.LineResult{ + Parsed: true, + Parts: []sse.ContentPart{{Type: "text", Text: "广州天气[citation:1] 多云[reference:0]"}}, + }) + + if !result.ContentSeen { + t.Fatalf("expected marker chunk to mark upstream content") + } + if got := acc.Text.String(); got != "广州天气 多云" { + t.Fatalf("visible text = %q", got) + } + if len(result.Parts) != 1 || result.Parts[0].VisibleText != "广州天气 多云" { + t.Fatalf("unexpected parts: %#v", result.Parts) + } +} diff --git a/internal/httpapi/openai/shared/string_helpers.go b/internal/httpapi/openai/shared/string_helpers.go new file mode 100644 index 0000000000000000000000000000000000000000..2c334a9a43ceb9b550c467b20ae5aa1b58a72d5d --- /dev/null +++ b/internal/httpapi/openai/shared/string_helpers.go @@ -0,0 +1,8 @@ +package shared + +func AsString(v any) string { + if s, ok := v.(string); ok { + return s + } + return "" +} diff --git a/internal/httpapi/openai/shared/thinking_injection.go b/internal/httpapi/openai/shared/thinking_injection.go new file mode 100644 index 0000000000000000000000000000000000000000..5f1df29d6eb5161344a99c320df03d4f82377e18 --- /dev/null +++ b/internal/httpapi/openai/shared/thinking_injection.go @@ -0,0 +1,7 @@ +package shared + +import "ds2api/internal/promptcompat" + +func ApplyThinkingInjection(store ConfigReader, stdReq promptcompat.StandardRequest) promptcompat.StandardRequest { + return stdReq +} diff --git a/internal/httpapi/openai/shared/trace.go b/internal/httpapi/openai/shared/trace.go new file mode 100644 index 0000000000000000000000000000000000000000..06dd9f983e9411f10471018115064231fe4bf149 --- /dev/null +++ b/internal/httpapi/openai/shared/trace.go @@ -0,0 +1,21 @@ +package shared + +import ( + "net/http" + "strings" + + "github.com/go-chi/chi/v5/middleware" +) + +func RequestTraceID(r *http.Request) string { + if r == nil { + return "" + } + if q := strings.TrimSpace(r.URL.Query().Get("__trace_id")); q != "" { + return q + } + if h := strings.TrimSpace(r.Header.Get("X-Ds2-Test-Trace")); h != "" { + return h + } + return strings.TrimSpace(middleware.GetReqID(r.Context())) +} diff --git a/internal/httpapi/openai/shared/upstream_empty.go b/internal/httpapi/openai/shared/upstream_empty.go new file mode 100644 index 0000000000000000000000000000000000000000..3660f7811447bfd2570543b93283ca5624f68bdb --- /dev/null +++ b/internal/httpapi/openai/shared/upstream_empty.go @@ -0,0 +1,30 @@ +package shared + +import ( + "net/http" + "strings" +) + +func ShouldWriteUpstreamEmptyOutputError(text, thinking string) bool { + return strings.TrimSpace(text) == "" +} + +func UpstreamEmptyOutputDetail(contentFilter bool, text, thinking string) (int, string, string) { + _ = text + if contentFilter { + return http.StatusBadRequest, "Upstream content filtered the response and returned no output.", "content_filter" + } + if thinking != "" { + return http.StatusTooManyRequests, "Upstream account hit a rate limit and returned reasoning without visible output.", "upstream_empty_output" + } + return http.StatusServiceUnavailable, "Upstream service is unavailable and returned no output.", "upstream_unavailable" +} + +func WriteUpstreamEmptyOutputError(w http.ResponseWriter, text, thinking string, contentFilter bool) bool { + if !ShouldWriteUpstreamEmptyOutputError(text, thinking) { + return false + } + status, message, code := UpstreamEmptyOutputDetail(contentFilter, text, thinking) + WriteOpenAIErrorWithCode(w, status, message, code) + return true +} diff --git a/internal/httpapi/openai/stream_status_test.go b/internal/httpapi/openai/stream_status_test.go new file mode 100644 index 0000000000000000000000000000000000000000..be9808f4b29d82724f22f9f42f89cbd2f048be82 --- /dev/null +++ b/internal/httpapi/openai/stream_status_test.go @@ -0,0 +1,597 @@ +package openai + +import ( + "context" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + chimw "github.com/go-chi/chi/v5/middleware" + + "ds2api/internal/auth" + dsclient "ds2api/internal/deepseek/client" +) + +type streamStatusAuthStub struct{} + +func (streamStatusAuthStub) Determine(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: false, + DeepSeekToken: "direct-token", + CallerID: "caller:test", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (streamStatusAuthStub) DetermineCaller(_ *http.Request) (*auth.RequestAuth, error) { + return &auth.RequestAuth{ + UseConfigToken: false, + DeepSeekToken: "direct-token", + CallerID: "caller:test", + TriedAccounts: map[string]bool{}, + }, nil +} + +func (streamStatusAuthStub) Release(_ *auth.RequestAuth) {} + +type streamStatusDSStub struct { + resp *http.Response +} + +func (m streamStatusDSStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +func (m streamStatusDSStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m streamStatusDSStub) UploadFile(_ context.Context, _ *auth.RequestAuth, _ dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + return &dsclient.UploadFileResult{ID: "file-id", Filename: "file.txt", Bytes: 1, Status: "uploaded"}, nil +} + +func (m streamStatusDSStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, _ map[string]any, _ string, _ int) (*http.Response, error) { + return m.resp, nil +} + +func (m streamStatusDSStub) DeleteSessionForToken(_ context.Context, _ string, _ string) (*dsclient.DeleteSessionResult, error) { + return &dsclient.DeleteSessionResult{Success: true}, nil +} + +func (m streamStatusDSStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +type streamStatusDSSeqStub struct { + resps []*http.Response + payloads []map[string]any +} + +func (m *streamStatusDSSeqStub) CreateSession(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "session-id", nil +} + +func (m *streamStatusDSSeqStub) GetPow(_ context.Context, _ *auth.RequestAuth, _ int) (string, error) { + return "pow", nil +} + +func (m *streamStatusDSSeqStub) UploadFile(_ context.Context, _ *auth.RequestAuth, _ dsclient.UploadFileRequest, _ int) (*dsclient.UploadFileResult, error) { + return &dsclient.UploadFileResult{ID: "file-id", Filename: "file.txt", Bytes: 1, Status: "uploaded"}, nil +} + +func (m *streamStatusDSSeqStub) CallCompletion(_ context.Context, _ *auth.RequestAuth, payload map[string]any, _ string, _ int) (*http.Response, error) { + clone := make(map[string]any, len(payload)) + for k, v := range payload { + clone[k] = v + } + m.payloads = append(m.payloads, clone) + idx := len(m.payloads) - 1 + if idx >= len(m.resps) { + idx = len(m.resps) - 1 + } + return m.resps[idx], nil +} + +func (m *streamStatusDSSeqStub) DeleteSessionForToken(_ context.Context, _ string, _ string) (*dsclient.DeleteSessionResult, error) { + return &dsclient.DeleteSessionResult{Success: true}, nil +} + +func (m *streamStatusDSSeqStub) DeleteAllSessionsForToken(_ context.Context, _ string) error { + return nil +} + +func makeOpenAISSEHTTPResponse(lines ...string) *http.Response { + body := strings.Join(lines, "\n") + if !strings.HasSuffix(body, "\n") { + body += "\n" + } + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +func newOpenAITestRouter(h *openAITestSurface) http.Handler { + r := chi.NewRouter() + registerOpenAITestRoutes(r, h) + return r +} + +func captureStatusMiddleware(statuses *[]int) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ww := chimw.NewWrapResponseWriter(w, r.ProtoMajor) + next.ServeHTTP(ww, r) + *statuses = append(*statuses, ww.Status()) + }) + } +} + +func TestChatCompletionsStreamStatusCapturedAs200(t *testing.T) { + statuses := make([]int, 0, 1) + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"hello"}`, "data: [DONE]")}, + } + r := chi.NewRouter() + r.Use(captureStatusMiddleware(&statuses)) + registerOpenAITestRoutes(r, h) + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(statuses) != 1 { + t.Fatalf("expected one captured status, got %d", len(statuses)) + } + if statuses[0] != http.StatusOK { + t.Fatalf("expected captured status 200 (not 000), got %d", statuses[0]) + } +} + +func TestResponsesStreamStatusCapturedAs200(t *testing.T) { + statuses := make([]int, 0, 1) + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"hello"}`, "data: [DONE]")}, + } + r := chi.NewRouter() + r.Use(captureStatusMiddleware(&statuses)) + registerOpenAITestRoutes(r, h) + + reqBody := `{"model":"deepseek-v4-flash","input":"hi","stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(statuses) != 1 { + t.Fatalf("expected one captured status, got %d", len(statuses)) + } + if statuses[0] != http.StatusOK { + t.Fatalf("expected captured status 200 (not 000), got %d", statuses[0]) + } +} + +func TestChatCompletionsStreamContentFilterStopsNormallyWithoutLeak(t *testing.T) { + statuses := make([]int, 0, 1) + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"合法前缀"}`, + `data: {"p":"response/status","v":"CONTENT_FILTER","accumulated_token_usage":77}`, + `data: {"p":"response/content","v":"CONTENT_FILTER你好,这个问题我暂时无法回答,让我们换个话题再聊聊吧。"}`, + )}, + } + r := chi.NewRouter() + r.Use(captureStatusMiddleware(&statuses)) + registerOpenAITestRoutes(r, h) + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(statuses) != 1 || statuses[0] != http.StatusOK { + t.Fatalf("expected captured status 200, got %#v", statuses) + } + if strings.Contains(rec.Body.String(), "这个问题我暂时无法回答") { + t.Fatalf("expected leaked content-filter suffix to be hidden, body=%s", rec.Body.String()) + } + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + if len(frames) == 0 { + t.Fatalf("expected at least one json frame, body=%s", rec.Body.String()) + } + last := frames[len(frames)-1] + choices, _ := last["choices"].([]any) + if len(choices) != 1 { + t.Fatalf("expected one choice in final frame, got %#v", last) + } + choice, _ := choices[0].(map[string]any) + if choice["finish_reason"] != "stop" { + t.Fatalf("expected finish_reason=stop for content-filter upstream stop, got %#v", choice["finish_reason"]) + } +} + +func TestChatCompletionsStreamEmitsFailureFrameWhenUpstreamOutputEmpty(t *testing.T) { + statuses := make([]int, 0, 1) + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse("data: [DONE]")}, + } + r := chi.NewRouter() + r.Use(captureStatusMiddleware(&statuses)) + registerOpenAITestRoutes(r, h) + + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(statuses) != 1 || statuses[0] != http.StatusOK { + t.Fatalf("expected captured status 200, got %#v", statuses) + } + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + if len(frames) != 1 { + t.Fatalf("expected one failure frame, got %#v body=%s", frames, rec.Body.String()) + } + last := frames[0] + statusCode, ok := last["status_code"].(float64) + if !ok || int(statusCode) != http.StatusServiceUnavailable { + t.Fatalf("expected status_code=503, got %#v body=%s", last["status_code"], rec.Body.String()) + } + errObj, _ := last["error"].(map[string]any) + if asString(errObj["code"]) != "upstream_unavailable" { + t.Fatalf("expected code=upstream_unavailable, got %#v", last) + } +} + +func TestChatCompletionsStreamRetriesEmptyOutputOnSameSession(t *testing.T) { + ds := &streamStatusDSSeqStub{resps: []*http.Response{ + makeOpenAISSEHTTPResponse(`data: {"response_message_id":42,"p":"response/thinking_content","v":"plan"}`, "data: [DONE]"), + makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"visible"}`, "data: [DONE]"), + }} + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody := `{"model":"deepseek-v4-pro","messages":[{"role":"user","content":"hi"}],"stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + newOpenAITestRouter(h).ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.payloads) != 2 { + t.Fatalf("expected one synthetic retry call, got %d", len(ds.payloads)) + } + if ds.payloads[0]["chat_session_id"] != ds.payloads[1]["chat_session_id"] { + t.Fatalf("expected retry to reuse session, payloads=%#v", ds.payloads) + } + retryPrompt := asString(ds.payloads[1]["prompt"]) + if !strings.Contains(retryPrompt, "Please provide a non-empty final answer or tool call.") { + t.Fatalf("expected retry suffix in prompt, got %q", retryPrompt) + } + // Verify multi-turn chaining: retry must set parent_message_id from first call's response_message_id. + if parentID, ok := ds.payloads[1]["parent_message_id"].(int); !ok || parentID != 42 { + t.Fatalf("expected retry parent_message_id=42, got %#v", ds.payloads[1]["parent_message_id"]) + } + + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + doneCount := strings.Count(rec.Body.String(), "data: [DONE]") + if doneCount != 1 { + t.Fatalf("expected one [DONE], got %d body=%s", doneCount, rec.Body.String()) + } + if len(frames) != 3 { + t.Fatalf("expected reasoning, content, finish frames, got %#v body=%s", frames, rec.Body.String()) + } + id := asString(frames[0]["id"]) + for _, frame := range frames[1:] { + if asString(frame["id"]) != id { + t.Fatalf("expected same completion id across retry stream, frames=%#v", frames) + } + } + choices, _ := frames[1]["choices"].([]any) + choice, _ := choices[0].(map[string]any) + delta, _ := choice["delta"].(map[string]any) + if asString(delta["content"]) != "visible" { + t.Fatalf("expected retry content delta, got %#v body=%s", delta, rec.Body.String()) + } +} + +func TestChatCompletionsNonStreamRetriesThinkingOnlyOutput(t *testing.T) { + ds := &streamStatusDSSeqStub{resps: []*http.Response{ + makeOpenAISSEHTTPResponse(`data: {"response_message_id":99,"p":"response/thinking_content","v":"plan"}`, "data: [DONE]"), + makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"visible"}`, "data: [DONE]"), + }} + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody := `{"model":"deepseek-v4-pro","messages":[{"role":"user","content":"hi"}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + newOpenAITestRouter(h).ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 after retry, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.payloads) != 2 { + t.Fatalf("expected one synthetic retry call, got %d", len(ds.payloads)) + } + // Verify multi-turn chaining. + if parentID, ok := ds.payloads[1]["parent_message_id"].(int); !ok || parentID != 99 { + t.Fatalf("expected retry parent_message_id=99, got %#v", ds.payloads[1]["parent_message_id"]) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + choices, _ := out["choices"].([]any) + choice, _ := choices[0].(map[string]any) + message, _ := choice["message"].(map[string]any) + if asString(message["content"]) != "visible" { + t.Fatalf("expected retry visible content, got %#v", message) + } +} + +func TestChatCompletionsContentFilterDoesNotRetry(t *testing.T) { + ds := &streamStatusDSSeqStub{resps: []*http.Response{ + makeOpenAISSEHTTPResponse(`data: {"code":"content_filter"}`), + makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"visible"}`, "data: [DONE]"), + }} + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody := `{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"hi"}],"stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + newOpenAITestRouter(h).ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected content_filter 400, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.payloads) != 1 { + t.Fatalf("expected no retry on content_filter, got %d calls", len(ds.payloads)) + } +} + +func TestResponsesStreamUsageIgnoresBatchAccumulatedTokenUsage(t *testing.T) { + statuses := make([]int, 0, 1) + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"hello"}`, + `data: {"p":"response","o":"BATCH","v":[{"p":"accumulated_token_usage","v":190},{"p":"quasi_status","v":"FINISHED"}]}`, + )}, + } + r := chi.NewRouter() + r.Use(captureStatusMiddleware(&statuses)) + registerOpenAITestRoutes(r, h) + + reqBody := `{"model":"deepseek-v4-flash","input":"hi","stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(statuses) != 1 || statuses[0] != http.StatusOK { + t.Fatalf("expected captured status 200, got %#v", statuses) + } + frames, done := parseSSEDataFrames(t, rec.Body.String()) + if !done { + t.Fatalf("expected [DONE], body=%s", rec.Body.String()) + } + if len(frames) == 0 { + t.Fatalf("expected at least one json frame, body=%s", rec.Body.String()) + } + last := frames[len(frames)-1] + resp, _ := last["response"].(map[string]any) + if resp == nil { + t.Fatalf("expected response payload in final frame, got %#v", last) + } + usage, _ := resp["usage"].(map[string]any) + if usage == nil { + t.Fatalf("expected usage in response payload, got %#v", resp) + } + if got, _ := usage["output_tokens"].(float64); int(got) == 190 { + t.Fatalf("expected upstream accumulated token usage to be ignored, got %#v", usage["output_tokens"]) + } +} + +func TestResponsesStreamRetriesThinkingOnlyOutput(t *testing.T) { + ds := &streamStatusDSSeqStub{resps: []*http.Response{ + makeOpenAISSEHTTPResponse(`data: {"response_message_id":77,"p":"response/thinking_content","v":"plan"}`, "data: [DONE]"), + makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"visible"}`, "data: [DONE]"), + }} + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody := `{"model":"deepseek-v4-pro","input":"hi","stream":true}` + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + newOpenAITestRouter(h).ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.payloads) != 2 { + t.Fatalf("expected one synthetic retry call, got %d", len(ds.payloads)) + } + // Verify multi-turn chaining. + if parentID, ok := ds.payloads[1]["parent_message_id"].(int); !ok || parentID != 77 { + t.Fatalf("expected retry parent_message_id=77, got %#v", ds.payloads[1]["parent_message_id"]) + } + body := rec.Body.String() + if strings.Contains(body, "response.failed") { + t.Fatalf("did not expect premature response.failed, body=%s", body) + } + if !strings.Contains(body, "response.reasoning.delta") || !strings.Contains(body, "response.output_text.delta") || !strings.Contains(body, "response.completed") { + t.Fatalf("expected reasoning, text delta, and completed events, body=%s", body) + } + if strings.Count(body, "data: [DONE]") != 1 { + t.Fatalf("expected one [DONE], body=%s", body) + } +} + +func TestResponsesNonStreamRetriesThinkingOnlyOutput(t *testing.T) { + ds := &streamStatusDSSeqStub{resps: []*http.Response{ + makeOpenAISSEHTTPResponse(`data: {"response_message_id":88,"p":"response/thinking_content","v":"plan"}`, "data: [DONE]"), + makeOpenAISSEHTTPResponse(`data: {"p":"response/content","v":"visible"}`, "data: [DONE]"), + }} + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: ds, + } + reqBody := `{"model":"deepseek-v4-pro","input":"hi","stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + newOpenAITestRouter(h).ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200 after retry, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(ds.payloads) != 2 { + t.Fatalf("expected one synthetic retry call, got %d", len(ds.payloads)) + } + // Verify multi-turn chaining. + if parentID, ok := ds.payloads[1]["parent_message_id"].(int); !ok || parentID != 88 { + t.Fatalf("expected retry parent_message_id=88, got %#v", ds.payloads[1]["parent_message_id"]) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + if asString(out["output_text"]) != "visible" { + t.Fatalf("expected retry visible output_text, got %#v", out["output_text"]) + } + output, _ := out["output"].([]any) + if len(output) == 0 { + t.Fatalf("expected output items, got %#v", out) + } + item, _ := output[0].(map[string]any) + content, _ := item["content"].([]any) + if len(content) == 0 { + t.Fatalf("expected content entries, got %#v", item) + } + var textEntry map[string]any + for _, entry := range content { + obj, _ := entry.(map[string]any) + if asString(obj["type"]) == "output_text" { + textEntry = obj + break + } + } + if asString(textEntry["text"]) != "visible" { + t.Fatalf("expected visible text entry, got %#v", content) + } +} + +func TestResponsesNonStreamUsageIgnoresPromptAndOutputTokenUsage(t *testing.T) { + statuses := make([]int, 0, 1) + h := &openAITestSurface{ + Store: mockOpenAIConfig{}, + Auth: streamStatusAuthStub{}, + DS: streamStatusDSStub{resp: makeOpenAISSEHTTPResponse( + `data: {"p":"response/content","v":"ok"}`, + `data: {"p":"response","o":"BATCH","v":[{"p":"token_usage","v":{"prompt_tokens":11,"completion_tokens":29}},{"p":"quasi_status","v":"FINISHED"}]}`, + )}, + } + r := chi.NewRouter() + r.Use(captureStatusMiddleware(&statuses)) + registerOpenAITestRoutes(r, h) + + reqBody := `{"model":"deepseek-v4-flash","input":"hi","stream":false}` + req := httptest.NewRequest(http.MethodPost, "/v1/responses", strings.NewReader(reqBody)) + req.Header.Set("Authorization", "Bearer direct-token") + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + r.ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String()) + } + if len(statuses) != 1 || statuses[0] != http.StatusOK { + t.Fatalf("expected captured status 200, got %#v", statuses) + } + var out map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { + t.Fatalf("decode response failed: %v body=%s", err, rec.Body.String()) + } + usage, _ := out["usage"].(map[string]any) + if usage == nil { + t.Fatalf("expected usage object, got %#v", out) + } + input, _ := usage["input_tokens"].(float64) + output, _ := usage["output_tokens"].(float64) + total, _ := usage["total_tokens"].(float64) + if int(output) == 29 { + t.Fatalf("expected upstream completion token usage to be ignored, got %#v", usage["output_tokens"]) + } + if int(total) != int(input)+int(output) { + t.Fatalf("expected total_tokens=input_tokens+output_tokens, usage=%#v", usage) + } +} diff --git a/internal/httpapi/openai/test_bridge_test.go b/internal/httpapi/openai/test_bridge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f0e62051dcc900dae9703d18540fd19d93823867 --- /dev/null +++ b/internal/httpapi/openai/test_bridge_test.go @@ -0,0 +1,160 @@ +package openai + +import ( + "context" + "encoding/json" + "net/http" + "strings" + "testing" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/httpapi/openai/chat" + "ds2api/internal/httpapi/openai/embeddings" + "ds2api/internal/httpapi/openai/files" + "ds2api/internal/httpapi/openai/history" + "ds2api/internal/httpapi/openai/responses" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/promptcompat" +) + +type openAITestSurface struct { + Store shared.ConfigReader + Auth shared.AuthResolver + DS shared.DeepSeekCaller + ChatHistory *chathistory.Store + + chat *chat.Handler + responses *responses.Handler + files *files.Handler + embeddings *embeddings.Handler + models *shared.ModelsHandler +} + +func (h *openAITestSurface) deps() shared.Deps { + if h == nil { + return shared.Deps{} + } + return shared.Deps{Store: h.Store, Auth: h.Auth, DS: h.DS, ChatHistory: h.ChatHistory} +} + +func (h *openAITestSurface) chatHandler() *chat.Handler { + if h.chat == nil { + deps := h.deps() + h.chat = &chat.Handler{Store: deps.Store, Auth: deps.Auth, DS: deps.DS, ChatHistory: deps.ChatHistory} + } + return h.chat +} + +func (h *openAITestSurface) responsesHandler() *responses.Handler { + if h.responses == nil { + deps := h.deps() + h.responses = &responses.Handler{Store: deps.Store, Auth: deps.Auth, DS: deps.DS, ChatHistory: deps.ChatHistory} + } + return h.responses +} + +func (h *openAITestSurface) filesHandler() *files.Handler { + if h.files == nil { + deps := h.deps() + h.files = &files.Handler{Store: deps.Store, Auth: deps.Auth, DS: deps.DS, ChatHistory: deps.ChatHistory} + } + return h.files +} + +func (h *openAITestSurface) embeddingsHandler() *embeddings.Handler { + if h.embeddings == nil { + deps := h.deps() + h.embeddings = &embeddings.Handler{Store: deps.Store, Auth: deps.Auth, DS: deps.DS, ChatHistory: deps.ChatHistory} + } + return h.embeddings +} + +func (h *openAITestSurface) modelsHandler() *shared.ModelsHandler { + if h.models == nil { + h.models = &shared.ModelsHandler{Store: h.Store} + } + return h.models +} + +func (h *openAITestSurface) ChatCompletions(w http.ResponseWriter, r *http.Request) { + h.chatHandler().ChatCompletions(w, r) +} + +func (h *openAITestSurface) applyCurrentInputFile(ctx context.Context, a *auth.RequestAuth, stdReq promptcompat.StandardRequest) (promptcompat.StandardRequest, error) { + stdReq = shared.ApplyThinkingInjection(h.Store, stdReq) + svc := history.Service{Store: h.Store, DS: h.DS} + out, err := svc.ApplyCurrentInputFile(ctx, a, stdReq) + if err != nil || out.CurrentInputFileApplied { + return out, err + } + return out, nil +} + +func (h *openAITestSurface) preprocessInlineFileInputs(ctx context.Context, a *auth.RequestAuth, req map[string]any) error { + return h.filesHandler().PreprocessInlineFileInputs(ctx, a, req) +} + +func registerOpenAITestRoutes(r chi.Router, h *openAITestSurface) { + r.Get("/v1/models", h.modelsHandler().ListModels) + r.Get("/v1/models/{model_id}", h.modelsHandler().GetModel) + r.Post("/v1/chat/completions", h.chatHandler().ChatCompletions) + r.Post("/v1/responses", h.responsesHandler().Responses) + r.Get("/v1/responses/{response_id}", h.responsesHandler().GetResponseByID) + r.Post("/v1/files", h.filesHandler().UploadFile) + r.Get("/v1/files/{file_id}", h.filesHandler().RetrieveFile) + r.Post("/v1/embeddings", h.embeddingsHandler().Embeddings) +} + +func buildOpenAICurrentInputContextTranscript(messages []any) string { + return promptcompat.BuildOpenAICurrentInputContextTranscript(messages) +} + +func writeOpenAIError(w http.ResponseWriter, status int, message string) { + shared.WriteOpenAIError(w, status, message) +} + +func replaceCitationMarkersWithLinks(text string, links map[int]string) string { + return shared.ReplaceCitationMarkersWithLinks(text, links) +} + +func sanitizeLeakedOutput(text string) string { + return shared.CleanVisibleOutput(text, false) +} + +func requestTraceID(r *http.Request) string { + return shared.RequestTraceID(r) +} + +func asString(v any) string { + return shared.AsString(v) +} + +func parseSSEDataFrames(t *testing.T, body string) ([]map[string]any, bool) { + t.Helper() + lines := strings.Split(body, "\n") + frames := make([]map[string]any, 0, len(lines)) + done := false + for _, line := range lines { + line = strings.TrimSpace(line) + if !strings.HasPrefix(line, "data:") { + continue + } + payload := strings.TrimSpace(strings.TrimPrefix(line, "data:")) + if payload == "" { + continue + } + if payload == "[DONE]" { + done = true + continue + } + var frame map[string]any + if err := json.Unmarshal([]byte(payload), &frame); err != nil { + t.Fatalf("decode sse frame failed: %v, payload=%s", err, payload) + } + frames = append(frames, frame) + } + return frames, done +} diff --git a/internal/httpapi/openai/trace_test.go b/internal/httpapi/openai/trace_test.go new file mode 100644 index 0000000000000000000000000000000000000000..cbacbf3bf3fc6a2adbb2b7c88edaa45228d040d5 --- /dev/null +++ b/internal/httpapi/openai/trace_test.go @@ -0,0 +1,47 @@ +package openai + +import ( + "net/http" + "net/http/httptest" + "testing" + + "github.com/go-chi/chi/v5/middleware" +) + +func traceIDViaMiddleware(req *http.Request) string { + if req == nil { + return requestTraceID(nil) + } + var got string + h := middleware.RequestID(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) { + got = requestTraceID(r) + })) + h.ServeHTTP(httptest.NewRecorder(), req) + return got +} + +func TestRequestTraceIDPriority(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/chat/completions?__trace_id=query-trace", nil) + req.Header.Set("X-Ds2-Test-Trace", "header-trace") + got := traceIDViaMiddleware(req) + if got != "query-trace" { + t.Fatalf("expected query trace id to win, got %q", got) + } +} + +func TestRequestTraceIDHeaderFallback(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/chat/completions", nil) + req.Header.Set("X-Ds2-Test-Trace", "header-trace") + got := traceIDViaMiddleware(req) + if got != "header-trace" { + t.Fatalf("expected header trace id to win when query missing, got %q", got) + } +} + +func TestRequestTraceIDReqIDFallback(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, "/v1/chat/completions", nil) + got := traceIDViaMiddleware(req) + if got == "" { + t.Fatal("expected middleware request id fallback to be non-empty") + } +} diff --git a/internal/httpapi/requestbody/json_utf8.go b/internal/httpapi/requestbody/json_utf8.go new file mode 100644 index 0000000000000000000000000000000000000000..5a3afe8ecbc90e04039fa4703eb41da551f9ebfe --- /dev/null +++ b/internal/httpapi/requestbody/json_utf8.go @@ -0,0 +1,134 @@ +package requestbody + +import ( + "bytes" + "errors" + "io" + "mime" + "net/http" + "strings" + "unicode/utf8" +) + +var ( + ErrInvalidUTF8Body = errors.New("invalid utf-8 request body") + errRequestBodyTooLarge = errors.New("request body too large") +) + +const maxJSONUTF8ValidationSize = 100 << 20 + +// ValidateJSONUTF8 validates complete JSON request bodies before downstream +// decoders can silently replace malformed UTF-8 or stop before trailing bytes. +func ValidateJSONUTF8(next http.Handler) http.Handler { + if next == nil { + return http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}) + } + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if shouldValidateJSONBody(r) { + r.Body = validateAndReplayBody(r.Body) + } + next.ServeHTTP(w, r) + }) +} + +func shouldValidateJSONBody(r *http.Request) bool { + if r == nil || r.Body == nil { + return false + } + path := "" + if r.URL != nil { + path = r.URL.Path + } + return isJSONContentType(r.Header.Get("Content-Type")) || isKnownJSONRequestPath(r.Method, path) +} + +func isJSONContentType(raw string) bool { + raw = strings.TrimSpace(raw) + if raw == "" { + return false + } + mediaType, _, err := mime.ParseMediaType(raw) + if err != nil { + mediaType = raw + } + mediaType = strings.ToLower(strings.TrimSpace(mediaType)) + return strings.Contains(mediaType, "json") +} + +func isKnownJSONRequestPath(method, path string) bool { + switch strings.ToUpper(strings.TrimSpace(method)) { + case http.MethodPost, http.MethodPut, http.MethodPatch, http.MethodDelete: + default: + return false + } + path = strings.TrimSpace(path) + if path == "" { + return false + } + switch { + case path == "/v1/chat/completions" || path == "/chat/completions": + return true + case path == "/v1/responses" || path == "/responses": + return true + case path == "/v1/embeddings" || path == "/embeddings": + return true + case path == "/anthropic/v1/messages" || path == "/v1/messages" || path == "/messages": + return true + case path == "/anthropic/v1/messages/count_tokens" || path == "/v1/messages/count_tokens" || path == "/messages/count_tokens": + return true + case strings.HasPrefix(path, "/v1beta/models/") || strings.HasPrefix(path, "/v1/models/"): + return strings.Contains(path, ":generateContent") || strings.Contains(path, ":streamGenerateContent") + case strings.HasPrefix(path, "/admin/"): + return true + default: + return false + } +} + +func validateAndReplayBody(body io.ReadCloser) io.ReadCloser { + if body == nil { + return body + } + raw, err := io.ReadAll(io.LimitReader(body, maxJSONUTF8ValidationSize+1)) + if err != nil { + return &errorReadCloser{err: err, closer: body} + } + if len(raw) > maxJSONUTF8ValidationSize { + return &errorReadCloser{err: errRequestBodyTooLarge, closer: body} + } + if !utf8.Valid(raw) { + return &errorReadCloser{err: ErrInvalidUTF8Body, closer: body} + } + return &replayReadCloser{Reader: bytes.NewReader(raw), closer: body} +} + +type replayReadCloser struct { + *bytes.Reader + closer io.Closer +} + +func (r *replayReadCloser) Close() error { + if r == nil || r.closer == nil { + return nil + } + return r.closer.Close() +} + +type errorReadCloser struct { + err error + closer io.Closer +} + +func (r *errorReadCloser) Read([]byte) (int, error) { + if r == nil || r.err == nil { + return 0, io.EOF + } + return 0, r.err +} + +func (r *errorReadCloser) Close() error { + if r == nil || r.closer == nil { + return nil + } + return r.closer.Close() +} diff --git a/internal/httpapi/requestbody/json_utf8_test.go b/internal/httpapi/requestbody/json_utf8_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e46af203a40074981bb08f9818140391a1b15271 --- /dev/null +++ b/internal/httpapi/requestbody/json_utf8_test.go @@ -0,0 +1,158 @@ +package requestbody + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +type singleByteReadCloser struct { + data []byte + pos int +} + +func (r *singleByteReadCloser) Read(p []byte) (int, error) { + if r.pos >= len(r.data) { + return 0, io.EOF + } + p[0] = r.data[r.pos] + r.pos++ + return 1, nil +} + +func (r *singleByteReadCloser) Close() error { + return nil +} + +func TestValidateJSONUTF8AllowsSplitMultibyteRunes(t *testing.T) { + body := []byte(`{"text":"你好"}`) + handler := ValidateJSONUTF8(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + t.Fatalf("unexpected decode error: %v", err) + } + w.WriteHeader(http.StatusNoContent) + })) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", &singleByteReadCloser{data: body}) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusNoContent { + t.Fatalf("expected 204 for valid utf-8 json, got %d body=%q", rec.Code, rec.Body.String()) + } +} + +func TestValidateJSONUTF8RejectsInvalidBytesBeforeJSONDecode(t *testing.T) { + body := append([]byte(`{"text":"`), 0xff) + body = append(body, []byte(`"}`)...) + handler := ValidateJSONUTF8(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(err.Error())) + return + } + w.WriteHeader(http.StatusOK) + })) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json; charset=utf-8") + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400 for invalid utf-8 json, got %d body=%q", rec.Code, rec.Body.String()) + } + if !strings.Contains(strings.ToLower(rec.Body.String()), "invalid utf-8") { + t.Fatalf("expected utf-8 validation error, got %q", rec.Body.String()) + } +} + +func TestValidateJSONUTF8RejectsInvalidBytesWithoutJSONContentTypeOnKnownPath(t *testing.T) { + body := append([]byte(`{"text":"`), 0xff) + body = append(body, []byte(`"}`)...) + handler := ValidateJSONUTF8(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(err.Error())) + return + } + w.WriteHeader(http.StatusOK) + })) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", bytes.NewReader(body)) + req.Header.Set("Content-Type", "text/plain") + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400 for invalid utf-8 json, got %d body=%q", rec.Code, rec.Body.String()) + } + if !strings.Contains(strings.ToLower(rec.Body.String()), "invalid utf-8") { + t.Fatalf("expected utf-8 validation error, got %q", rec.Body.String()) + } +} + +func TestValidateJSONUTF8RejectsTrailingInvalidBytesAfterJSONValue(t *testing.T) { + body := append([]byte(`{"text":"ok"}`), 0xff) + handler := ValidateJSONUTF8(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req map[string]any + if err := json.NewDecoder(r.Body).Decode(&req); err != nil { + w.WriteHeader(http.StatusBadRequest) + _, _ = w.Write([]byte(err.Error())) + return + } + w.WriteHeader(http.StatusOK) + })) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + rec := httptest.NewRecorder() + + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400 for trailing invalid utf-8, got %d body=%q", rec.Code, rec.Body.String()) + } + if !strings.Contains(strings.ToLower(rec.Body.String()), "invalid utf-8") { + t.Fatalf("expected utf-8 validation error, got %q", rec.Body.String()) + } +} + +func TestIsJSONContentType(t *testing.T) { + for _, raw := range []string{ + "application/json", + "application/json; charset=utf-8", + "application/problem+json", + "application/vnd.api+json", + } { + if !isJSONContentType(raw) { + t.Fatalf("expected %q to be recognized as json", raw) + } + } + for _, raw := range []string{ + "multipart/form-data; boundary=abc", + "text/plain", + "application/octet-stream", + } { + if isJSONContentType(raw) { + t.Fatalf("expected %q not to be recognized as json", raw) + } + } +} + +func TestIsKnownJSONRequestPathIncludesGeminiStream(t *testing.T) { + if !isKnownJSONRequestPath(http.MethodPost, "/v1beta/models/gemini-pro:streamGenerateContent") { + t.Fatal("expected Gemini stream generate path to be recognized as json") + } +} diff --git a/internal/js/chat-stream/cors.js b/internal/js/chat-stream/cors.js new file mode 100644 index 0000000000000000000000000000000000000000..f7966395c7ba96a4ec651621105695628b8580ad --- /dev/null +++ b/internal/js/chat-stream/cors.js @@ -0,0 +1,136 @@ +'use strict'; + +const DEFAULT_CORS_ALLOW_HEADERS = [ + 'Content-Type', + 'Authorization', + 'X-API-Key', + 'X-Ds2-Target-Account', + 'X-Ds2-Source', + 'X-Vercel-Protection-Bypass', + 'X-Goog-Api-Key', + 'Anthropic-Version', + 'Anthropic-Beta', +]; + +const BLOCKED_CORS_REQUEST_HEADERS = new Set([ + 'x-ds2-internal-token', +]); + +function setCorsHeaders(res, req) { + const origin = asString(readHeader(req, 'origin')); + res.setHeader('Access-Control-Allow-Origin', origin || '*'); + if (origin) { + addVaryHeader(res, 'Origin'); + } + res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE'); + res.setHeader('Access-Control-Max-Age', '600'); + res.setHeader( + 'Access-Control-Allow-Headers', + buildCORSAllowHeaders(req), + ); + addVaryHeader(res, 'Access-Control-Request-Headers'); + if (asString(readHeader(req, 'access-control-request-private-network')).toLowerCase() === 'true') { + res.setHeader('Access-Control-Allow-Private-Network', 'true'); + addVaryHeader(res, 'Access-Control-Request-Private-Network'); + } +} + +function buildCORSAllowHeaders(req) { + const seen = new Set(); + const headers = []; + for (const name of DEFAULT_CORS_ALLOW_HEADERS) { + appendCORSHeaderName(headers, seen, name); + } + for (const name of splitCORSRequestHeaders(readHeader(req, 'access-control-request-headers'))) { + appendCORSHeaderName(headers, seen, name); + } + return headers.join(', '); +} + +function splitCORSRequestHeaders(raw) { + const text = asString(raw); + if (!text) { + return []; + } + return text + .split(',') + .map((part) => asString(part)) + .filter((name) => isValidCORSHeaderToken(name)) + .filter((name) => !BLOCKED_CORS_REQUEST_HEADERS.has(name.toLowerCase())); +} + +function appendCORSHeaderName(headers, seen, name) { + const text = asString(name); + if (!isValidCORSHeaderToken(text)) { + return; + } + const lower = text.toLowerCase(); + if (BLOCKED_CORS_REQUEST_HEADERS.has(lower) || seen.has(lower)) { + return; + } + seen.add(lower); + headers.push(text); +} + +function isValidCORSHeaderToken(name) { + return /^[A-Za-z0-9!#$%&'*+.^_`|~-]+$/.test(asString(name)); +} + +function addVaryHeader(res, token) { + const text = asString(token); + if (!text || typeof res.setHeader !== 'function') { + return; + } + const current = typeof res.getHeader === 'function' ? res.getHeader('Vary') : ''; + const seen = new Set(); + const merged = []; + const addToken = (value) => { + const trimmed = asString(value); + if (!trimmed) { + return; + } + const lower = trimmed.toLowerCase(); + if (seen.has(lower)) { + return; + } + seen.add(lower); + merged.push(trimmed); + }; + if (Array.isArray(current)) { + for (const value of current) { + for (const part of String(value).split(',')) { + addToken(part); + } + } + } else { + for (const part of String(current || '').split(',')) { + addToken(part); + } + } + addToken(text); + res.setHeader('Vary', merged.join(', ')); +} + +function readHeader(req, key) { + if (!req || !req.headers) { + return ''; + } + return req.headers[String(key).toLowerCase()]; +} + +function asString(v) { + if (typeof v === 'string') { + return v.trim(); + } + if (Array.isArray(v)) { + return asString(v[0]); + } + if (v == null) { + return ''; + } + return String(v).trim(); +} + +module.exports = { + setCorsHeaders, +}; diff --git a/internal/js/chat-stream/dedupe.js b/internal/js/chat-stream/dedupe.js new file mode 100644 index 0000000000000000000000000000000000000000..18f683887268b89b611e1876fdb140f3c7c831c0 --- /dev/null +++ b/internal/js/chat-stream/dedupe.js @@ -0,0 +1,23 @@ +'use strict'; + +const MIN_CONTINUATION_SNAPSHOT_LEN = 32; + +function trimContinuationOverlap(existing, incoming) { + if (!incoming) { + return ''; + } + if (!existing) { + return incoming; + } + if (incoming.length >= MIN_CONTINUATION_SNAPSHOT_LEN && incoming.startsWith(existing)) { + return incoming.slice(existing.length); + } + if (incoming.length >= MIN_CONTINUATION_SNAPSHOT_LEN && existing.startsWith(incoming)) { + return ''; + } + return incoming; +} + +module.exports = { + trimContinuationOverlap, +}; diff --git a/internal/js/chat-stream/error_shape.js b/internal/js/chat-stream/error_shape.js new file mode 100644 index 0000000000000000000000000000000000000000..18aeedb3a0531f56953a055b30a8d7bc4fa7e167 --- /dev/null +++ b/internal/js/chat-stream/error_shape.js @@ -0,0 +1,36 @@ +'use strict'; + +function writeOpenAIError(res, status, message) { + res.statusCode = status; + res.setHeader('Content-Type', 'application/json'); + res.end( + JSON.stringify({ + error: { + message, + type: openAIErrorType(status), + }, + }), + ); +} + +function openAIErrorType(status) { + switch (status) { + case 400: + return 'invalid_request_error'; + case 401: + return 'authentication_error'; + case 403: + return 'permission_error'; + case 429: + return 'rate_limit_error'; + case 503: + return 'service_unavailable_error'; + default: + return status >= 500 ? 'api_error' : 'invalid_request_error'; + } +} + +module.exports = { + writeOpenAIError, + openAIErrorType, +}; diff --git a/internal/js/chat-stream/http_internal.js b/internal/js/chat-stream/http_internal.js new file mode 100644 index 0000000000000000000000000000000000000000..1c94ced59caed24ba04d12532174e866d3e1b09c --- /dev/null +++ b/internal/js/chat-stream/http_internal.js @@ -0,0 +1,264 @@ +'use strict'; + +const { + writeOpenAIError, +} = require('./error_shape'); +const { + setCorsHeaders, +} = require('./cors'); + +function header(req, key) { + if (!req || !req.headers) { + return ''; + } + return asString(req.headers[key.toLowerCase()]); +} + +async function readRawBody(req) { + if (Buffer.isBuffer(req.body)) { + return req.body; + } + if (typeof req.body === 'string') { + return Buffer.from(req.body); + } + if (req.body && typeof req.body === 'object') { + return Buffer.from(JSON.stringify(req.body)); + } + const chunks = []; + for await (const chunk of req) { + chunks.push(Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk)); + } + return Buffer.concat(chunks); +} + +async function fetchStreamPrepare(req, rawBody) { + const url = buildInternalGoURL(req); + url.searchParams.set('__stream_prepare', '1'); + + const upstream = await fetch(url.toString(), { + method: 'POST', + headers: buildInternalGoHeaders(req, { withInternalToken: true, withContentType: true }), + body: rawBody, + }); + + const text = await upstream.text(); + let body = {}; + try { + body = JSON.parse(text || '{}'); + } catch (_err) { + body = {}; + } + + return { + ok: upstream.ok, + status: upstream.status, + contentType: upstream.headers.get('content-type') || 'application/json', + text, + body, + }; +} + +async function fetchStreamPow(req, leaseID) { + const url = buildInternalGoURL(req); + url.searchParams.set('__stream_pow', '1'); + + const upstream = await fetch(url.toString(), { + method: 'POST', + headers: buildInternalGoHeaders(req, { withInternalToken: true, withContentType: true }), + body: Buffer.from(JSON.stringify({ lease_id: leaseID })), + }); + + const text = await upstream.text(); + let body = {}; + try { + body = JSON.parse(text || '{}'); + } catch (_err) { + body = {}; + } + + return { + ok: upstream.ok, + status: upstream.status, + contentType: upstream.headers.get('content-type') || 'application/json', + text, + body, + }; +} + +async function fetchStreamSwitch(req, leaseID) { + const url = buildInternalGoURL(req); + url.searchParams.set('__stream_switch', '1'); + + const upstream = await fetch(url.toString(), { + method: 'POST', + headers: buildInternalGoHeaders(req, { withInternalToken: true, withContentType: true }), + body: Buffer.from(JSON.stringify({ lease_id: leaseID })), + }); + + const text = await upstream.text(); + let body = {}; + try { + body = JSON.parse(text || '{}'); + } catch (_err) { + body = {}; + } + + return { + ok: upstream.ok, + status: upstream.status, + contentType: upstream.headers.get('content-type') || 'application/json', + text, + body, + }; +} + +function relayPreparedFailure(res, prep) { + if (prep.status === 401 && looksLikeVercelAuthPage(prep.text)) { + writeOpenAIError( + res, + 401, + 'Vercel Deployment Protection blocked internal prepare request. Disable protection for this deployment or set VERCEL_AUTOMATION_BYPASS_SECRET.', + ); + return; + } + res.statusCode = prep.status || 500; + res.setHeader('Content-Type', prep.contentType || 'application/json'); + if (prep.text) { + res.end(prep.text); + return; + } + writeOpenAIError(res, prep.status || 500, 'vercel prepare failed'); +} + +async function safeReadText(resp) { + if (!resp) { + return ''; + } + try { + const text = await resp.text(); + return text.trim(); + } catch (_err) { + return ''; + } +} + +function internalSecret() { + return asString(process.env.DS2API_VERCEL_INTERNAL_SECRET) || asString(process.env.DS2API_ADMIN_KEY) || 'admin'; +} + +function buildInternalGoURL(req) { + const proto = asString(header(req, 'x-forwarded-proto')) || 'https'; + const host = asString(header(req, 'host')); + const url = new URL(`${proto}://${host}${req.url || '/v1/chat/completions'}`); + url.searchParams.set('__go', '1'); + const protectionBypass = resolveProtectionBypass(req); + if (protectionBypass) { + url.searchParams.set('x-vercel-protection-bypass', protectionBypass); + } + return url; +} + +function buildInternalGoHeaders(req, opts = {}) { + const headers = { + authorization: asString(header(req, 'authorization')), + 'x-api-key': asString(header(req, 'x-api-key')), + 'x-ds2-target-account': asString(header(req, 'x-ds2-target-account')), + 'x-vercel-protection-bypass': resolveProtectionBypass(req), + }; + if (opts.withInternalToken) { + headers['x-ds2-internal-token'] = internalSecret(); + } + if (opts.withContentType) { + headers['content-type'] = asString(header(req, 'content-type')) || 'application/json'; + } + return headers; +} + +function createLeaseReleaser(req, leaseID) { + let released = false; + return async () => { + if (released || !leaseID) { + return; + } + released = true; + try { + await releaseStreamLease(req, leaseID); + } catch (_err) { + // Ignore release errors. Lease TTL cleanup on Go side still prevents permanent leaks. + } + }; +} + +async function releaseStreamLease(req, leaseID) { + const url = buildInternalGoURL(req); + url.searchParams.set('__stream_release', '1'); + const body = Buffer.from(JSON.stringify({ lease_id: leaseID })); + + const controller = new AbortController(); + const timeout = setTimeout(() => controller.abort(), 1500); + try { + await fetch(url.toString(), { + method: 'POST', + headers: buildInternalGoHeaders(req, { withInternalToken: true, withContentType: true }), + body, + signal: controller.signal, + }); + } finally { + clearTimeout(timeout); + } +} + +function resolveProtectionBypass(req) { + const fromHeader = asString(header(req, 'x-vercel-protection-bypass')); + if (fromHeader) { + return fromHeader; + } + return asString(process.env.VERCEL_AUTOMATION_BYPASS_SECRET) || asString(process.env.DS2API_VERCEL_PROTECTION_BYPASS); +} + +function looksLikeVercelAuthPage(text) { + const body = asString(text).toLowerCase(); + if (!body) { + return false; + } + return body.includes('authentication required') && body.includes('vercel'); +} + +function asString(v) { + if (typeof v === 'string') { + return v.trim(); + } + if (Array.isArray(v)) { + return asString(v[0]); + } + if (v == null) { + return ''; + } + return String(v).trim(); +} + +function isAbortError(err) { + if (!err || typeof err !== 'object') { + return false; + } + return err.name === 'AbortError' || err.code === 'ABORT_ERR'; +} + +module.exports = { + setCorsHeaders, + header, + readRawBody, + fetchStreamPrepare, + fetchStreamPow, + fetchStreamSwitch, + relayPreparedFailure, + safeReadText, + buildInternalGoURL, + buildInternalGoHeaders, + createLeaseReleaser, + releaseStreamLease, + resolveProtectionBypass, + looksLikeVercelAuthPage, + asString, + isAbortError, +}; diff --git a/internal/js/chat-stream/index.js b/internal/js/chat-stream/index.js new file mode 100644 index 0000000000000000000000000000000000000000..af9b264d7267fa8011ce9d32e441c37f7b5b45ef --- /dev/null +++ b/internal/js/chat-stream/index.js @@ -0,0 +1,128 @@ +'use strict'; + +const { + writeOpenAIError, +} = require('./error_shape'); +const { + parseChunkForContent, + extractContentRecursive, + filterLeakedContentFilterParts, + hasContentFilterStatus, + extractAccumulatedTokenUsage, + shouldSkipPath, + stripReferenceMarkers, +} = require('./sse_parse'); +const { + resolveToolcallPolicy, + formatIncrementalToolCallDeltas, + normalizePreparedToolNames, + boolDefaultTrue, + filterIncrementalToolCallDeltasByAllowed, + resetStreamToolCallState, +} = require('./toolcall_policy'); +const { + estimateTokens, + buildUsage, +} = require('./token_usage'); +const { + setCorsHeaders, + readRawBody, + asString, +} = require('./http_internal'); +const { + proxyToGo, +} = require('./proxy_go'); +const { + handleVercelStream, +} = require('./vercel_stream'); +const { + trimContinuationOverlap, +} = require('./dedupe'); + +async function handler(req, res) { + setCorsHeaders(res, req); + if (req.method === 'OPTIONS') { + res.statusCode = 204; + res.end(); + return; + } + if (req.method !== 'POST') { + writeOpenAIError(res, 405, 'method not allowed'); + return; + } + + const rawBody = await readRawBody(req); + + // Hard guard: only use Node data path for streaming on Vercel runtime. + // Any non-Vercel runtime always falls back to Go for full behavior parity. + if (!isVercelRuntime()) { + await proxyToGo(req, res, rawBody); + return; + } + + let payload; + try { + payload = JSON.parse(rawBody.toString('utf8') || '{}'); + } catch (_err) { + writeOpenAIError(res, 400, 'invalid json'); + return; + } + + // Keep all non-stream behavior and non-OpenAI-chat paths on Go side to avoid + // protocol-shape regressions (e.g. Gemini/Claude clients expecting their own formats). + if (!toBool(payload.stream) || !isNodeStreamSupportedPath(req.url || '')) { + await proxyToGo(req, res, rawBody); + return; + } + + await handleVercelStream(req, res, rawBody, payload); +} + +function toBool(v) { + return v === true; +} + +function isVercelRuntime() { + return asString(process.env.VERCEL) !== '' || asString(process.env.NOW_REGION) !== ''; +} + +function isNodeStreamSupportedPath(rawURL) { + const path = extractPathname(rawURL); + return path === '/v1/chat/completions' || path === '/chat/completions'; +} + +function extractPathname(rawURL) { + const text = asString(rawURL); + if (!text) { + return ''; + } + const q = text.indexOf('?'); + if (q >= 0) { + return text.slice(0, q); + } + return text; +} + +module.exports = handler; + +module.exports.__test = { + parseChunkForContent, + extractContentRecursive, + shouldSkipPath, + stripReferenceMarkers, + asString, + resolveToolcallPolicy, + formatIncrementalToolCallDeltas, + normalizePreparedToolNames, + boolDefaultTrue, + filterIncrementalToolCallDeltasByAllowed, + resetStreamToolCallState, + estimateTokens, + buildUsage, + filterLeakedContentFilterParts, + hasContentFilterStatus, + extractAccumulatedTokenUsage, + isNodeStreamSupportedPath, + extractPathname, + trimContinuationOverlap, +}; diff --git a/internal/js/chat-stream/proxy_go.js b/internal/js/chat-stream/proxy_go.js new file mode 100644 index 0000000000000000000000000000000000000000..4e31a9c4c395f707d03c5d9b1c5610f1adaac0b2 --- /dev/null +++ b/internal/js/chat-stream/proxy_go.js @@ -0,0 +1,106 @@ +'use strict'; + +const { + buildInternalGoURL, + buildInternalGoHeaders, + isAbortError, +} = require('./http_internal'); + +async function proxyToGo(req, res, rawBody) { + const url = buildInternalGoURL(req); + const controller = new AbortController(); + let clientClosed = false; + const markClientClosed = () => { + if (clientClosed) { + return; + } + clientClosed = true; + controller.abort(); + }; + const onReqAborted = () => markClientClosed(); + const onResClose = () => { + if (!res.writableEnded) { + markClientClosed(); + } + }; + req.on('aborted', onReqAborted); + res.on('close', onResClose); + + try { + let upstream; + try { + upstream = await fetch(url.toString(), { + method: 'POST', + headers: buildInternalGoHeaders(req, { withContentType: true }), + body: rawBody, + signal: controller.signal, + }); + } catch (err) { + if (clientClosed || isAbortError(err)) { + if (!res.writableEnded) { + res.end(); + } + return; + } + throw err; + } + if (clientClosed) { + if (!res.writableEnded) { + res.end(); + } + return; + } + + res.statusCode = upstream.status; + upstream.headers.forEach((value, key) => { + const lower = key.toLowerCase(); + if (lower === 'content-length' || lower === 'content-encoding') { + return; + } + res.setHeader(key, value); + }); + + if (!upstream.body || typeof upstream.body.getReader !== 'function') { + const bytes = Buffer.from(await upstream.arrayBuffer()); + res.end(bytes); + return; + } + + const reader = upstream.body.getReader(); + try { + // eslint-disable-next-line no-constant-condition + while (true) { + if (clientClosed) { + break; + } + const { value, done } = await reader.read(); + if (done) { + break; + } + if (value && value.length > 0) { + res.write(Buffer.from(value)); + if (typeof res.flush === 'function') { + res.flush(); + } + } + } + if (!res.writableEnded) { + res.end(); + } + } catch (err) { + if (!isAbortError(err) && !res.writableEnded) { + res.end(); + } + } + } finally { + req.removeListener('aborted', onReqAborted); + res.removeListener('close', onResClose); + if (!res.writableEnded) { + res.end(); + } + } +} + +module.exports = { + proxyToGo, +}; diff --git a/internal/js/chat-stream/sse_parse.js b/internal/js/chat-stream/sse_parse.js new file mode 100644 index 0000000000000000000000000000000000000000..bae74264a5a7684f1ba7df63f84068e4f3181f91 --- /dev/null +++ b/internal/js/chat-stream/sse_parse.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('./sse_parse_impl'); diff --git a/internal/js/chat-stream/sse_parse_impl.js b/internal/js/chat-stream/sse_parse_impl.js new file mode 100644 index 0000000000000000000000000000000000000000..910747107b9e57f1d95430d2e8012b0343455b75 --- /dev/null +++ b/internal/js/chat-stream/sse_parse_impl.js @@ -0,0 +1,659 @@ +'use strict'; + +// Implementation moved here to keep the line-gate wrapper tiny. + +const { + SKIP_PATTERNS, + SKIP_EXACT_PATHS, +} = require('../shared/deepseek-constants'); + +const LEAKED_BOS_MARKER_PATTERN = /<[\|\uFF5C]\s*begin[_▁]of[_▁]sentence\s*[\|\uFF5C]>/gi; +const LEAKED_THOUGHT_MARKER_PATTERN = /<[\|\uFF5C]\s*(?:begin[_▁])?[_▁]*of[_▁]thought\s*[\|\uFF5C]>/gi; +const LEAKED_META_MARKER_PATTERN = /<[\|\uFF5C]\s*(?:assistant|tool|end[_▁]of[_▁]sentence|end[_▁]of[_▁]thinking|end[_▁]of[_▁]thought|end[_▁]of[_▁]toolresults|end[_▁]of[_▁]instructions)\s*[\|\uFF5C]>/gi; + + + +function stripThinkTags(text) { + if (typeof text !== 'string' || !text) { + return text; + } + return text.replace(/<\/?\s*think\s*>/gi, ''); +} + +function splitThinkingParts(parts) { + const out = []; + let thinkingDone = false; + for (const p of parts) { + if (!p) continue; + if (thinkingDone && p.type === 'thinking') { + const cleaned = stripThinkTags(p.text); + if (cleaned) { + out.push({ text: cleaned, type: 'text' }); + } + continue; + } + if (p.type !== 'thinking') { + const cleaned = stripThinkTags(p.text); + if (cleaned) { + out.push({ text: cleaned, type: p.type }); + } + continue; + } + const match = /<\/\s*think\s*>/i.exec(p.text); + if (!match) { + out.push(p); + continue; + } + thinkingDone = true; + const before = p.text.substring(0, match.index); + let after = p.text.substring(match.index + match[0].length); + if (before) { + out.push({ text: before, type: 'thinking' }); + } + after = stripThinkTags(after); + if (after) { + out.push({ text: after, type: 'text' }); + } + } + return { parts: out, transitioned: thinkingDone }; +} + +function dropThinkingParts(parts) { + if (!Array.isArray(parts) || parts.length === 0) { + return parts; + } + return parts.filter((p) => p && p.type !== 'thinking'); +} + +function finalizeThinkingParts(parts, thinkingEnabled, newType) { + const splitResult = splitThinkingParts(parts); + let finalType = newType; + let finalParts = splitResult.parts; + if (splitResult.transitioned) { + finalType = 'text'; + } + if (!thinkingEnabled) { + finalParts = dropThinkingParts(finalParts); + } + return { parts: finalParts, newType: finalType }; +} + +function parseChunkForContent(chunk, thinkingEnabled, currentType, stripReferenceMarkers = true) { + if (!chunk || typeof chunk !== 'object') { + return { + parsed: false, + parts: [], + finished: false, + contentFilter: false, + errorMessage: '', + outputTokens: 0, + newType: currentType, + }; + } + + const usage = extractAccumulatedTokenUsage(chunk); + const promptTokens = usage.prompt; + const outputTokens = usage.output; + + if (Object.prototype.hasOwnProperty.call(chunk, 'error')) { + return { + parsed: true, + parts: [], + finished: true, + contentFilter: false, + errorMessage: formatErrorMessage(chunk.error), + promptTokens, + outputTokens, + newType: currentType, + }; + } + + const pathValue = asString(chunk.p); + + if (hasContentFilterStatus(chunk)) { + return { + parsed: true, + parts: [], + finished: true, + contentFilter: true, + errorMessage: '', + promptTokens, + outputTokens, + newType: currentType, + }; + } + + if (shouldSkipPath(pathValue)) { + return { + parsed: true, + parts: [], + finished: false, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType: currentType, + }; + } + if (isStatusPath(pathValue)) { + if (isFinishedStatus(chunk.v)) { + return { + parsed: true, + parts: [], + finished: true, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType: currentType, + }; + } + return { + parsed: true, + parts: [], + finished: false, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType: currentType, + }; + } + + if (!Object.prototype.hasOwnProperty.call(chunk, 'v')) { + return { + parsed: true, + parts: [], + finished: false, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType: currentType, + }; + } + + let newType = currentType; + const parts = []; + + if (pathValue === 'response/fragments' && asString(chunk.o).toUpperCase() === 'APPEND' && Array.isArray(chunk.v)) { + for (const frag of chunk.v) { + if (!frag || typeof frag !== 'object') { + continue; + } + const fragType = asString(frag.type).toUpperCase(); + const content = asContentString(frag.content, stripReferenceMarkers); + if (!content) { + continue; + } + if (fragType === 'THINK' || fragType === 'THINKING') { + newType = 'thinking'; + parts.push({ text: content, type: 'thinking' }); + } else if (fragType === 'RESPONSE') { + newType = 'text'; + parts.push({ text: content, type: 'text' }); + } else { + parts.push({ text: content, type: 'text' }); + } + } + } + + if (pathValue === 'response' && Array.isArray(chunk.v)) { + for (const item of chunk.v) { + if (!item || typeof item !== 'object') { + continue; + } + if (item.p === 'fragments' && item.o === 'APPEND' && Array.isArray(item.v)) { + for (const frag of item.v) { + const fragType = asString(frag && frag.type).toUpperCase(); + if (fragType === 'THINK' || fragType === 'THINKING') { + newType = 'thinking'; + } else if (fragType === 'RESPONSE') { + newType = 'text'; + } + } + } + } + } + + if (pathValue === 'response/content') { + newType = 'text'; + } else if (pathValue === 'response/thinking_content' && (!thinkingEnabled || newType !== 'text')) { + newType = 'thinking'; + } + + let partType = 'text'; + if (pathValue === 'response/thinking_content') { + if (!thinkingEnabled) { + partType = 'thinking'; + } else if (newType === 'text') { + partType = 'text'; + } else { + partType = 'thinking'; + } + } else if (pathValue === 'response/content') { + partType = 'text'; + } else if (pathValue.includes('response/fragments') && pathValue.includes('/content')) { + partType = newType; + } else if (!pathValue) { + partType = newType || 'text'; + } + + const val = chunk.v; + if (typeof val === 'string') { + if (isFinishedStatus(val) && (!pathValue || pathValue === 'status')) { + return { + parsed: true, + parts: [], + finished: true, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType, + }; + } + if (isStatusPath(pathValue)) { + return { + parsed: true, + parts: [], + finished: false, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType, + }; + } + const content = asContentString(val, stripReferenceMarkers); + if (content) { + parts.push({ text: content, type: partType }); + } + + let resolvedParts = filterLeakedContentFilterParts(parts); + const finalized = finalizeThinkingParts(resolvedParts, thinkingEnabled, newType); + + return { + parsed: true, + parts: finalized.parts, + finished: false, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType: finalized.newType, + }; + } + + if (Array.isArray(val)) { + const extracted = extractContentRecursive(val, partType, stripReferenceMarkers); + if (extracted.finished) { + return { + parsed: true, + parts: [], + finished: true, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType, + }; + } + parts.push(...extracted.parts); + + let resolvedParts = filterLeakedContentFilterParts(parts); + const finalized = finalizeThinkingParts(resolvedParts, thinkingEnabled, newType); + + return { + parsed: true, + parts: finalized.parts, + finished: false, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType: finalized.newType, + }; + } + + if (val && typeof val === 'object') { + const directContent = asContentString(val, stripReferenceMarkers); + if (directContent) { + parts.push({ text: directContent, type: partType }); + } + const resp = val.response && typeof val.response === 'object' ? val.response : val; + if (Array.isArray(resp.fragments)) { + for (const frag of resp.fragments) { + if (!frag || typeof frag !== 'object') { + continue; + } + const content = asContentString(frag.content, stripReferenceMarkers); + if (!content) { + continue; + } + const t = asString(frag.type).toUpperCase(); + if (t === 'THINK' || t === 'THINKING') { + newType = 'thinking'; + parts.push({ text: content, type: 'thinking' }); + } else if (t === 'RESPONSE') { + newType = 'text'; + parts.push({ text: content, type: 'text' }); + } else { + parts.push({ text: content, type: partType }); + } + } + } + } + + let resolvedParts = filterLeakedContentFilterParts(parts); + const finalized = finalizeThinkingParts(resolvedParts, thinkingEnabled, newType); + + return { + parsed: true, + parts: finalized.parts, + finished: false, + contentFilter: false, + errorMessage: '', + promptTokens, + outputTokens, + newType: finalized.newType, + }; +} + +function extractContentRecursive(items, defaultType, stripReferenceMarkers = true) { + const parts = []; + for (const it of items) { + if (!it || typeof it !== 'object') { + continue; + } + if (!Object.prototype.hasOwnProperty.call(it, 'v')) { + continue; + } + const itemPath = asString(it.p); + const itemV = it.v; + if (isStatusPath(itemPath)) { + if (isFinishedStatus(itemV)) { + return { parts: [], finished: true }; + } + continue; + } + if (shouldSkipPath(itemPath)) { + continue; + } + const content = asContentString(it.content, stripReferenceMarkers); + if (content) { + const typeName = asString(it.type).toUpperCase(); + if (typeName === 'THINK' || typeName === 'THINKING') { + parts.push({ text: content, type: 'thinking' }); + } else if (typeName === 'RESPONSE') { + parts.push({ text: content, type: 'text' }); + } else { + parts.push({ text: content, type: defaultType }); + } + continue; + } + + let partType = defaultType; + if (itemPath.includes('thinking')) { + partType = 'thinking'; + } else if (itemPath.includes('content') || itemPath === 'response' || itemPath === 'fragments') { + partType = 'text'; + } + + if (typeof itemV === 'string') { + if (isStatusPath(itemPath)) { + continue; + } + if (itemV && itemV !== 'FINISHED') { + const content = asContentString(itemV, stripReferenceMarkers); + if (content) { + parts.push({ text: content, type: partType }); + } + } + continue; + } + + if (!Array.isArray(itemV)) { + continue; + } + for (const inner of itemV) { + if (typeof inner === 'string') { + if (inner) { + const content = asContentString(inner, stripReferenceMarkers); + if (content) { + parts.push({ text: content, type: partType }); + } + } + continue; + } + if (!inner || typeof inner !== 'object') { + continue; + } + const ct = asContentString(inner.content, stripReferenceMarkers); + if (!ct) { + continue; + } + const typeName = asString(inner.type).toUpperCase(); + if (typeName === 'THINK' || typeName === 'THINKING') { + parts.push({ text: ct, type: 'thinking' }); + } else if (typeName === 'RESPONSE') { + parts.push({ text: ct, type: 'text' }); + } else { + parts.push({ text: ct, type: partType }); + } + } + } + return { parts, finished: false }; +} + +function isStatusPath(pathValue) { + return pathValue === 'response/status' || pathValue === 'status'; +} + +function isFinishedStatus(value) { + return asString(value).toUpperCase() === 'FINISHED'; +} + +function filterLeakedContentFilterParts(parts) { + if (!Array.isArray(parts) || parts.length === 0) { + return parts; + } + const out = []; + for (const p of parts) { + if (!p || typeof p !== 'object') { + continue; + } + const { text, stripped } = stripLeakedContentFilterSuffix(p.text); + if (stripped && shouldDropCleanedLeakedChunk(text)) { + continue; + } + if (stripped) { + out.push({ ...p, text }); + continue; + } + out.push(p); + } + return out; +} + +function stripLeakedContentFilterSuffix(text) { + if (typeof text !== 'string' || text === '') { + return { text, stripped: false }; + } + const upperText = text.toUpperCase(); + const idx = upperText.indexOf('CONTENT_FILTER'); + if (idx < 0) { + return { text, stripped: false }; + } + return { + text: text.slice(0, idx).replace(/[ \t\r]+$/g, ''), + stripped: true, + }; +} + +function shouldDropCleanedLeakedChunk(cleaned) { + if (cleaned === '') { + return true; + } + if (typeof cleaned === 'string' && cleaned.includes('\n')) { + return false; + } + return asString(cleaned).trim() === ''; +} + +function hasContentFilterStatus(chunk) { + if (!chunk || typeof chunk !== 'object') { + return false; + } + const code = asString(chunk.code); + if (code && code.toLowerCase() === 'content_filter') { + return true; + } + return hasContentFilterStatusValue(chunk); +} + +function hasContentFilterStatusValue(v) { + if (Array.isArray(v)) { + for (const item of v) { + if (hasContentFilterStatusValue(item)) { + return true; + } + } + return false; + } + if (!v || typeof v !== 'object') { + return false; + } + const pathValue = asString(v.p); + if (pathValue && pathValue.toLowerCase().includes('status')) { + if (asString(v.v).toLowerCase() === 'content_filter') { + return true; + } + } + if (asString(v.code).toLowerCase() === 'content_filter') { + return true; + } + for (const value of Object.values(v)) { + if (hasContentFilterStatusValue(value)) { + return true; + } + } + return false; +} + +function extractAccumulatedTokenUsage(chunk) { + // 临时策略:忽略上游 usage 字段(accumulated_token_usage / token_usage), + // 统一使用内部估算计数,避免上下文累计口径误差。 + void chunk; + return { prompt: 0, output: 0 }; +} + +function formatErrorMessage(v) { + if (typeof v === 'string') { + return v; + } + if (v == null) { + return String(v); + } + try { + return JSON.stringify(v); + } catch (_err) { + return String(v); + } +} + +function shouldSkipPath(pathValue) { + if (isFragmentStatusPath(pathValue)) { + return true; + } + if (SKIP_EXACT_PATHS.has(pathValue)) { + return true; + } + for (const p of SKIP_PATTERNS) { + if (pathValue.includes(p)) { + return true; + } + } + return false; +} + +function isFragmentStatusPath(pathValue) { + if (!pathValue || pathValue === 'response/status') { + return false; + } + return /^response\/fragments\/-?\d+\/status$/i.test(pathValue); +} + +function isCitation(text) { + return asString(text).trim().startsWith('[citation:'); +} + +function asContentString(v, stripReferenceMarkers = true) { + if (typeof v === 'string') { + return stripReferenceMarkers ? stripReferenceMarkersText(v) : v; + } + if (Array.isArray(v)) { + let out = ''; + for (const item of v) { + out += asContentString(item, stripReferenceMarkers); + } + return out; + } + if (v && typeof v === 'object') { + if (Object.prototype.hasOwnProperty.call(v, 'content')) { + return asContentString(v.content, stripReferenceMarkers); + } + if (Object.prototype.hasOwnProperty.call(v, 'v')) { + return asContentString(v.v, stripReferenceMarkers); + } + if (Object.prototype.hasOwnProperty.call(v, 'text')) { + return asContentString(v.text, stripReferenceMarkers); + } + if (Object.prototype.hasOwnProperty.call(v, 'value')) { + return asContentString(v.value, stripReferenceMarkers); + } + return ''; + } + if (v == null) { + return ''; + } + const text = String(v); + return stripReferenceMarkers ? stripReferenceMarkersText(text) : text; +} + +function stripReferenceMarkersText(text) { + if (!text) { + return text; + } + return text + .replace(/\[(?:citation|reference):\s*\d+\]/gi, '') + .replace(LEAKED_BOS_MARKER_PATTERN, '') + .replace(LEAKED_THOUGHT_MARKER_PATTERN, '') + .replace(LEAKED_META_MARKER_PATTERN, ''); +} + +function asString(v) { + if (typeof v === 'string') { + return v.trim(); + } + if (Array.isArray(v)) { + return asString(v[0]); + } + if (v == null) { + return ''; + } + return String(v).trim(); +} + +module.exports = { + parseChunkForContent, + extractContentRecursive, + filterLeakedContentFilterParts, + hasContentFilterStatus, + extractAccumulatedTokenUsage, + shouldSkipPath, + isFragmentStatusPath, + isCitation, + stripReferenceMarkers: stripReferenceMarkersText, + stripThinkTags, +}; diff --git a/internal/js/chat-stream/stream_emitter.js b/internal/js/chat-stream/stream_emitter.js new file mode 100644 index 0000000000000000000000000000000000000000..b3faadf878c5bfd505e9ab74d529fb8b97eb4339 --- /dev/null +++ b/internal/js/chat-stream/stream_emitter.js @@ -0,0 +1,98 @@ +'use strict'; + +const MIN_DELTA_FLUSH_CHARS = 16; +const MAX_DELTA_FLUSH_WAIT_MS = 20; + +function createChatCompletionEmitter({ res, sessionID, created, model, isClosed }) { + let firstChunkSent = false; + + const sendFrame = (obj) => { + if (isClosed() || res.writableEnded || res.destroyed) { + return; + } + res.write(`data: ${JSON.stringify(obj)}\n\n`); + if (typeof res.flush === 'function') { + res.flush(); + } + }; + + const sendDeltaFrame = (delta) => { + const payloadDelta = { ...delta }; + if (!firstChunkSent) { + payloadDelta.role = 'assistant'; + firstChunkSent = true; + } + sendFrame({ + id: sessionID, + object: 'chat.completion.chunk', + created, + model, + choices: [{ delta: payloadDelta, index: 0 }], + }); + }; + + return { + sendFrame, + sendDeltaFrame, + }; +} + +function createDeltaCoalescer({ sendDeltaFrame, minFlushChars = MIN_DELTA_FLUSH_CHARS, maxFlushWaitMS = MAX_DELTA_FLUSH_WAIT_MS }) { + let pendingField = ''; + let pendingText = ''; + let flushTimer = null; + + const clearFlushTimer = () => { + if (flushTimer) { + clearTimeout(flushTimer); + flushTimer = null; + } + }; + + const flush = () => { + clearFlushTimer(); + if (!pendingField || !pendingText) { + return; + } + const delta = { [pendingField]: pendingText }; + pendingField = ''; + pendingText = ''; + sendDeltaFrame(delta); + }; + + const scheduleFlush = () => { + if (flushTimer || maxFlushWaitMS <= 0) { + return; + } + flushTimer = setTimeout(flush, maxFlushWaitMS); + if (typeof flushTimer.unref === 'function') { + flushTimer.unref(); + } + }; + + const append = (field, text) => { + if (!field || !text) { + return; + } + if (pendingField && pendingField !== field) { + flush(); + } + pendingField = field; + pendingText += text; + if ([...pendingText].length >= minFlushChars) { + flush(); + return; + } + scheduleFlush(); + }; + + return { + append, + flush, + }; +} + +module.exports = { + createChatCompletionEmitter, + createDeltaCoalescer, +}; diff --git a/internal/js/chat-stream/token_usage.js b/internal/js/chat-stream/token_usage.js new file mode 100644 index 0000000000000000000000000000000000000000..82e12e89e1dfab06c8d2e3f479326eab80e2925e --- /dev/null +++ b/internal/js/chat-stream/token_usage.js @@ -0,0 +1,55 @@ +'use strict'; + +function buildUsage(prompt, thinking, output, outputTokens = 0, providedPromptTokens = 0) { + const reasoningTokens = estimateTokens(thinking); + const completionTokens = estimateTokens(output); + + const finalPromptTokens = Number.isFinite(providedPromptTokens) && providedPromptTokens > 0 ? Math.trunc(providedPromptTokens) : estimateTokens(prompt); + + const overriddenCompletionTokens = Number.isFinite(outputTokens) && outputTokens > 0 ? Math.trunc(outputTokens) : 0; + const finalCompletionTokens = overriddenCompletionTokens > 0 ? overriddenCompletionTokens : reasoningTokens + completionTokens; + return { + prompt_tokens: finalPromptTokens, + completion_tokens: finalCompletionTokens, + total_tokens: finalPromptTokens + finalCompletionTokens, + completion_tokens_details: { + reasoning_tokens: reasoningTokens, + }, + }; +} + +function estimateTokens(text) { + const t = asTokenString(text); + if (!t) { + return 0; + } + let asciiChars = 0; + let nonASCIIChars = 0; + for (const ch of Array.from(t)) { + if (ch.charCodeAt(0) < 128) { + asciiChars += 1; + } else { + nonASCIIChars += 1; + } + } + const n = Math.floor(asciiChars / 4) + Math.floor((nonASCIIChars * 10 + 7) / 13); + return n < 1 ? 1 : n; +} + +function asTokenString(v) { + if (typeof v === 'string') { + return v; + } + if (Array.isArray(v)) { + return asTokenString(v[0]); + } + if (v == null) { + return ''; + } + return String(v); +} + +module.exports = { + buildUsage, + estimateTokens, +}; diff --git a/internal/js/chat-stream/toolcall_policy.js b/internal/js/chat-stream/toolcall_policy.js new file mode 100644 index 0000000000000000000000000000000000000000..f3fa01e878826d16339e7d276494a46a2e5cbc4b --- /dev/null +++ b/internal/js/chat-stream/toolcall_policy.js @@ -0,0 +1,148 @@ +'use strict'; + +const crypto = require('crypto'); + +const { + extractToolNames, +} = require('../helpers/stream-tool-sieve'); + +function resolveToolcallPolicy(prepBody, payloadTools) { + const preparedToolNames = normalizePreparedToolNames(prepBody && prepBody.tool_names); + let toolNames = preparedToolNames.length > 0 ? preparedToolNames : extractToolNames(payloadTools); + if (toolNames.length === 0 && Array.isArray(payloadTools) && payloadTools.length > 0) { + toolNames = ['__any_tool__']; + } + return { + toolNames, + toolSieveEnabled: toolNames.length > 0, + emitEarlyToolDeltas: true, + }; +} + +function normalizePreparedToolNames(v) { + if (!Array.isArray(v) || v.length === 0) { + return []; + } + const out = []; + for (const item of v) { + const name = asString(item); + if (!name) { + continue; + } + out.push(name); + } + return out; +} + +function boolDefaultTrue(v) { + return v !== false; +} + +function formatIncrementalToolCallDeltas(deltas, idStore) { + if (!Array.isArray(deltas) || deltas.length === 0) { + return []; + } + const out = []; + for (const d of deltas) { + if (!d || typeof d !== 'object') { + continue; + } + const index = Number.isInteger(d.index) ? d.index : 0; + const id = ensureStreamToolCallID(idStore, index); + const item = { + index, + id, + type: 'function', + }; + const fn = {}; + if (asString(d.name)) { + fn.name = asString(d.name); + } + if (typeof d.arguments === 'string' && d.arguments !== '') { + fn.arguments = d.arguments; + } + if (Object.keys(fn).length === 0) { + continue; + } + if (Object.keys(fn).length > 0) { + item.function = fn; + } + out.push(item); + } + return out; +} + +function filterIncrementalToolCallDeltasByAllowed(deltas, allowedNames, seenNames) { + if (!Array.isArray(deltas) || deltas.length === 0) { + return []; + } + const seen = seenNames instanceof Map ? seenNames : new Map(); + const out = []; + for (const d of deltas) { + if (!d || typeof d !== 'object') { + continue; + } + const index = Number.isInteger(d.index) ? d.index : 0; + const name = asString(d.name); + if (name) { + seen.set(index, name); + out.push(d); + continue; + } + const existing = asString(seen.get(index)); + if (!existing) { + continue; + } + out.push(d); + } + return out; +} + +function resetStreamToolCallState(idStore, seenNames) { + if (idStore instanceof Map) { + idStore.clear(); + } + if (seenNames instanceof Map) { + seenNames.clear(); + } +} + +function ensureStreamToolCallID(idStore, index) { + const key = Number.isInteger(index) ? index : 0; + const existing = idStore.get(key); + if (existing) { + return existing; + } + const next = `call_${newCallID()}`; + idStore.set(key, next); + return next; +} + +function newCallID() { + if (typeof crypto.randomUUID === 'function') { + return crypto.randomUUID().replace(/-/g, ''); + } + return `${Date.now()}${Math.floor(Math.random() * 1e9)}`; +} + +function asString(v) { + if (typeof v === 'string') { + return v.trim(); + } + if (Array.isArray(v)) { + return asString(v[0]); + } + if (v == null) { + return ''; + } + return String(v).trim(); +} + +module.exports = { + resolveToolcallPolicy, + normalizePreparedToolNames, + boolDefaultTrue, + formatIncrementalToolCallDeltas, + filterIncrementalToolCallDeltasByAllowed, + resetStreamToolCallState, +}; diff --git a/internal/js/chat-stream/vercel_stream.js b/internal/js/chat-stream/vercel_stream.js new file mode 100644 index 0000000000000000000000000000000000000000..a69f5290bbc6e7ad592a8e89399123b587ba1b37 --- /dev/null +++ b/internal/js/chat-stream/vercel_stream.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('./vercel_stream_impl'); diff --git a/internal/js/chat-stream/vercel_stream_impl.js b/internal/js/chat-stream/vercel_stream_impl.js new file mode 100644 index 0000000000000000000000000000000000000000..ed407c11725548127151678e04f979d6bf5c73ac --- /dev/null +++ b/internal/js/chat-stream/vercel_stream_impl.js @@ -0,0 +1,698 @@ +'use strict'; + +// Implementation moved here to keep the line-gate wrapper tiny. + +const { + createToolSieveState, + processToolSieveChunk, + flushToolSieve, + parseStandaloneToolCalls, + formatOpenAIStreamToolCalls, +} = require('../helpers/stream-tool-sieve'); +const { BASE_HEADERS } = require('../shared/deepseek-constants'); +const { writeOpenAIError, openAIErrorType } = require('./error_shape'); +const { parseChunkForContent, isCitation } = require('./sse_parse'); +const { buildUsage } = require('./token_usage'); +const { + resolveToolcallPolicy, + formatIncrementalToolCallDeltas, + filterIncrementalToolCallDeltasByAllowed, + resetStreamToolCallState, +} = require('./toolcall_policy'); +const { createChatCompletionEmitter, createDeltaCoalescer } = require('./stream_emitter'); +const { + asString, + isAbortError, + fetchStreamPrepare, + fetchStreamPow, + fetchStreamSwitch, + relayPreparedFailure, + createLeaseReleaser, +} = require('./http_internal'); +const { + trimContinuationOverlap, +} = require('./dedupe'); + +const DEEPSEEK_COMPLETION_URL = 'https://chat.deepseek.com/api/v0/chat/completion'; +const DEEPSEEK_CONTINUE_URL = 'https://chat.deepseek.com/api/v0/chat/continue'; +const EMPTY_OUTPUT_RETRY_SUFFIX = 'Please provide a non-empty final answer or tool call.'; +const EMPTY_OUTPUT_RETRY_MAX_ATTEMPTS = 1; +const AUTO_CONTINUE_MAX_ROUNDS = 8; + +async function handleVercelStream(req, res, rawBody, payload) { + const prep = await fetchStreamPrepare(req, rawBody); + if (!prep.ok) { + relayPreparedFailure(res, prep); + return; + } + + const model = asString(prep.body.model) || asString(payload.model); + const responseID = asString(prep.body.session_id) || `chatcmpl-${Date.now()}`; + const leaseID = asString(prep.body.lease_id); + let deepseekToken = asString(prep.body.deepseek_token); + const initialPowHeader = asString(prep.body.pow_header); + let completionPayload = prep.body.payload && typeof prep.body.payload === 'object' ? prep.body.payload : null; + const finalPrompt = asString(prep.body.final_prompt); + const thinkingEnabled = toBool(prep.body.thinking_enabled); + const searchEnabled = toBool(prep.body.search_enabled); + const toolPolicy = resolveToolcallPolicy(prep.body, payload.tools); + const toolNames = toolPolicy.toolNames; + const emitEarlyToolDeltas = toolPolicy.emitEarlyToolDeltas; + const stripReferenceMarkers = true; + + if (!model || !leaseID || !deepseekToken || !initialPowHeader || !completionPayload) { + writeOpenAIError(res, 500, 'invalid vercel prepare response'); + return; + } + + const releaseLease = createLeaseReleaser(req, leaseID); + const upstreamController = new AbortController(); + let clientClosed = false; + let reader = null; + const markClientClosed = () => { + if (clientClosed) { + return; + } + clientClosed = true; + upstreamController.abort(); + if (reader && typeof reader.cancel === 'function') { + Promise.resolve(reader.cancel()).catch(() => {}); + } + }; + const onReqAborted = () => markClientClosed(); + const onResClose = () => { + if (!res.writableEnded) { + markClientClosed(); + } + }; + req.on('aborted', onReqAborted); + res.on('close', onResClose); + + try { + let currentPowHeader = initialPowHeader; + const refreshPowHeader = async (roundType) => { + try { + const pow = await fetchStreamPow(req, leaseID); + const nextPowHeader = asString(pow.body && pow.body.pow_header); + if (pow.ok && nextPowHeader) { + currentPowHeader = nextPowHeader; + return currentPowHeader; + } + console.warn('[vercel_stream_pow] refresh failed, reusing previous PoW', { + round_type: roundType, + status: pow.status || 0, + }); + } catch (err) { + if (clientClosed || isAbortError(err)) { + return ''; + } + console.warn('[vercel_stream_pow] refresh failed, reusing previous PoW', { + round_type: roundType, + error: err, + }); + } + return currentPowHeader; + }; + + const fetchDeepSeekStream = async (url, bodyPayload, powHeader) => { + try { + return await fetch(url, { + method: 'POST', + headers: { + ...BASE_HEADERS, + authorization: `Bearer ${deepseekToken}`, + 'x-ds-pow-response': powHeader, + }, + body: JSON.stringify(bodyPayload), + signal: upstreamController.signal, + }); + } catch (err) { + if (clientClosed || isAbortError(err)) { + return null; + } + throw err; + } + }; + const fetchCompletion = (bodyPayload) => fetchDeepSeekStream(DEEPSEEK_COMPLETION_URL, bodyPayload, currentPowHeader); + let activeDeepSeekSessionID = responseID; + const fetchContinue = async (messageID) => { + const powHeader = await refreshPowHeader('continue'); + if (!powHeader) { + return null; + } + return fetchDeepSeekStream(DEEPSEEK_CONTINUE_URL, { + chat_session_id: activeDeepSeekSessionID, + message_id: messageID, + fallback_to_resume: true, + }, powHeader); + }; + + let completionRes = await fetchCompletion(completionPayload); + if (completionRes === null) { + return; + } + if (clientClosed) { + return; + } + + if (!completionRes.ok || !completionRes.body) { + const detail = completionRes.body ? await completionRes.text() : ''; + const status = completionRes.ok ? 500 : completionRes.status || 500; + writeOpenAIError(res, status, detail); + return; + } + + res.statusCode = 200; + res.setHeader('Content-Type', 'text/event-stream'); + res.setHeader('Cache-Control', 'no-cache, no-transform'); + res.setHeader('Connection', 'keep-alive'); + res.setHeader('X-Accel-Buffering', 'no'); + if (typeof res.flushHeaders === 'function') { + res.flushHeaders(); + } + + const created = Math.floor(Date.now() / 1000); + let currentType = thinkingEnabled ? 'thinking' : 'text'; + let thinkingText = ''; + let outputText = ''; + let usagePrompt = finalPrompt; + const toolSieveEnabled = toolPolicy.toolSieveEnabled; + const toolSieveState = createToolSieveState(); + let toolCallsEmitted = false; + let toolCallsDoneEmitted = false; + const streamToolCallIDs = new Map(); + const streamToolNames = new Map(); + const decoder = new TextDecoder(); + let buffered = ''; + let ended = false; + const { sendFrame, sendDeltaFrame } = createChatCompletionEmitter({ + res, + sessionID: responseID, + created, + model, + isClosed: () => clientClosed, + }); + const deltaCoalescer = createDeltaCoalescer({ sendDeltaFrame }); + + const finish = async (reason, options = {}) => { + if (ended) { + return true; + } + if (clientClosed || res.writableEnded || res.destroyed) { + ended = true; + await releaseLease(); + return true; + } + deltaCoalescer.flush(); + const detected = parseStandaloneToolCalls(outputText, toolNames); + if (detected.length > 0 && !toolCallsDoneEmitted) { + toolCallsEmitted = true; + toolCallsDoneEmitted = true; + sendDeltaFrame({ tool_calls: formatOpenAIStreamToolCalls(detected, streamToolCallIDs, payload.tools) }); + } else if (toolSieveEnabled) { + const tailEvents = flushToolSieve(toolSieveState, toolNames); + for (const evt of tailEvents) { + if (evt.type === 'tool_calls' && Array.isArray(evt.calls) && evt.calls.length > 0) { + deltaCoalescer.flush(); + toolCallsEmitted = true; + toolCallsDoneEmitted = true; + sendDeltaFrame({ tool_calls: formatOpenAIStreamToolCalls(evt.calls, streamToolCallIDs, payload.tools) }); + resetStreamToolCallState(streamToolCallIDs, streamToolNames); + continue; + } + if (evt.text) { + deltaCoalescer.append('content', evt.text); + } + } + deltaCoalescer.flush(); + } + if (detected.length > 0 || toolCallsEmitted) { + reason = 'tool_calls'; + } + if (detected.length === 0 && !toolCallsEmitted && outputText.trim() === '') { + if (options.deferEmpty && reason !== 'content_filter') { + return false; + } + ended = true; + const detail = upstreamEmptyOutputDetail(reason === 'content_filter', outputText, thinkingText); + sendFailedChunk(res, detail.status, detail.message, detail.code); + await releaseLease(); + if (!res.writableEnded && !res.destroyed) { + res.end(); + } + return true; + } + ended = true; + sendFrame({ + id: responseID, + object: 'chat.completion.chunk', + created, + model, + choices: [{ delta: {}, index: 0, finish_reason: reason }], + usage: buildUsage(usagePrompt, thinkingText, outputText), + }); + if (!res.writableEnded && !res.destroyed) { + res.write('data: [DONE]\n\n'); + } + await releaseLease(); + if (!res.writableEnded && !res.destroyed) { + res.end(); + } + return true; + }; + + const processStream = async (initialResponse, allowDeferEmpty) => { + let currentResponse = initialResponse; + let continueState = createContinueState(activeDeepSeekSessionID); + let continueRounds = 0; + // eslint-disable-next-line no-constant-condition + while (true) { + reader = currentResponse.body.getReader(); + buffered = ''; + let streamEnded = false; + try { + // eslint-disable-next-line no-constant-condition + while (true) { + if (clientClosed) { + await finish('stop'); + return { terminal: true, retryable: false }; + } + const { value, done } = await reader.read(); + if (done) { + break; + } + buffered += decoder.decode(value, { stream: true }); + const lines = buffered.split('\n'); + buffered = lines.pop() || ''; + + for (const rawLine of lines) { + const line = rawLine.trim(); + if (!line.startsWith('data:')) { + continue; + } + const dataStr = line.slice(5).trim(); + if (!dataStr) { + continue; + } + if (dataStr === '[DONE]') { + streamEnded = true; + break; + } + let chunk; + try { + chunk = JSON.parse(dataStr); + } catch (_err) { + continue; + } + observeContinueState(continueState, chunk); + const parsed = parseChunkForContent(chunk, thinkingEnabled, currentType, stripReferenceMarkers); + if (!parsed.parsed) { + continue; + } + currentType = parsed.newType; + if (parsed.errorMessage) { + return { terminal: await finish('content_filter'), retryable: false }; + } + if (parsed.contentFilter) { + return { terminal: await finish(outputText.trim() === '' ? 'content_filter' : 'stop'), retryable: false }; + } + if (parsed.finished) { + streamEnded = true; + break; + } + + for (const p of parsed.parts) { + if (!p.text) { + continue; + } + if (p.type === 'thinking') { + if (thinkingEnabled) { + const trimmed = trimContinuationOverlap(thinkingText, p.text); + if (!trimmed) { + continue; + } + thinkingText += trimmed; + deltaCoalescer.append('reasoning_content', trimmed); + } + } else { + const trimmed = trimContinuationOverlap(outputText, p.text); + if (!trimmed) { + continue; + } + if (searchEnabled && isCitation(trimmed)) { + continue; + } + outputText += trimmed; + if (!toolSieveEnabled) { + deltaCoalescer.append('content', trimmed); + continue; + } + const events = processToolSieveChunk(toolSieveState, trimmed, toolNames); + for (const evt of events) { + if (evt.type === 'tool_call_deltas') { + if (!emitEarlyToolDeltas) { + continue; + } + const filtered = filterIncrementalToolCallDeltasByAllowed(evt.deltas, toolNames, streamToolNames); + const formatted = formatIncrementalToolCallDeltas(filtered, streamToolCallIDs); + if (formatted.length > 0) { + toolCallsEmitted = true; + deltaCoalescer.flush(); + sendDeltaFrame({ tool_calls: formatted }); + } + continue; + } + if (evt.type === 'tool_calls') { + toolCallsEmitted = true; + toolCallsDoneEmitted = true; + deltaCoalescer.flush(); + sendDeltaFrame({ tool_calls: formatOpenAIStreamToolCalls(evt.calls, streamToolCallIDs, payload.tools) }); + resetStreamToolCallState(streamToolCallIDs, streamToolNames); + continue; + } + if (evt.text) { + deltaCoalescer.append('content', evt.text); + } + } + } + } + if (streamEnded) { + break; + } + } + if (streamEnded) { + break; + } + } + } catch (err) { + if (clientClosed || isAbortError(err)) { + await finish('stop'); + return { terminal: true, retryable: false }; + } + await finish('stop'); + return { terminal: true, retryable: false }; + } + + if (shouldAutoContinue(continueState) && continueRounds < AUTO_CONTINUE_MAX_ROUNDS) { + continueRounds += 1; + const nextRes = await fetchContinue(continueState.responseMessageID); + if (nextRes === null) { + return { terminal: true, retryable: false }; + } + if (!nextRes.ok || !nextRes.body) { + return { terminal: await finish('stop'), retryable: false }; + } + continueState = prepareContinueStateForNextRound(continueState); + currentResponse = nextRes; + continue; + } + break; + } + + const terminal = await finish('stop', { deferEmpty: allowDeferEmpty }); + return { terminal, retryable: !terminal && allowDeferEmpty, responseMessageID: continueState.responseMessageID }; + }; + + let retryAttempts = 0; + let accountSwitchAttempted = false; + // eslint-disable-next-line no-constant-condition + while (true) { + const allowDeferEmpty = retryAttempts < EMPTY_OUTPUT_RETRY_MAX_ATTEMPTS || !accountSwitchAttempted; + const processed = await processStream(completionRes, allowDeferEmpty); + if (processed.terminal) { + return; + } + if (!processed.retryable) { + await finish('stop'); + return; + } + if (retryAttempts >= EMPTY_OUTPUT_RETRY_MAX_ATTEMPTS) { + if (!accountSwitchAttempted) { + accountSwitchAttempted = true; + const switched = await fetchStreamSwitch(req, leaseID); + if (switched.ok && switched.body && switched.body.payload && typeof switched.body.payload === 'object') { + completionPayload = switched.body.payload; + deepseekToken = asString(switched.body.deepseek_token) || deepseekToken; + currentPowHeader = asString(switched.body.pow_header) || currentPowHeader; + activeDeepSeekSessionID = asString(switched.body.session_id) || activeDeepSeekSessionID; + usagePrompt = finalPrompt; + completionRes = await fetchCompletion(completionPayload); + if (completionRes === null) { + return; + } + if (!completionRes.ok || !completionRes.body) { + await finish('stop'); + return; + } + continue; + } + } + await finish('stop'); + return; + } + retryAttempts += 1; + console.info('[openai_empty_retry] attempting synthetic retry', { + surface: 'chat.completions', + stream: true, + retry_attempt: retryAttempts, + parent_message_id: processed.responseMessageID || 0, + }); + usagePrompt = usagePromptWithEmptyOutputRetry(finalPrompt, retryAttempts); + const retryPowHeader = await refreshPowHeader('retry'); + if (!retryPowHeader) { + return; + } + completionRes = await fetchDeepSeekStream( + DEEPSEEK_COMPLETION_URL, + clonePayloadForEmptyOutputRetry(completionPayload, processed.responseMessageID), + retryPowHeader, + ); + if (completionRes === null) { + return; + } + if (!completionRes.ok || !completionRes.body) { + await finish('stop'); + return; + } + } + } finally { + req.removeListener('aborted', onReqAborted); + res.removeListener('close', onResClose); + await releaseLease(); + } +} + +function toBool(v) { + return v === true; +} + +function clonePayloadForEmptyOutputRetry(payload, parentMessageID) { + const clone = { + ...(payload || {}), + prompt: appendEmptyOutputRetrySuffix(asString(payload && payload.prompt)), + }; + if (parentMessageID && parentMessageID > 0) { + clone.parent_message_id = parentMessageID; + } + return clone; +} + +function appendEmptyOutputRetrySuffix(prompt) { + const base = asString(prompt).trimEnd(); + if (!base) { + return EMPTY_OUTPUT_RETRY_SUFFIX; + } + return `${base}\n\n${EMPTY_OUTPUT_RETRY_SUFFIX}`; +} + +function usagePromptWithEmptyOutputRetry(originalPrompt, attempts) { + if (!attempts || attempts <= 0) { + return originalPrompt; + } + const parts = [originalPrompt]; + let next = originalPrompt; + for (let i = 0; i < attempts; i += 1) { + next = appendEmptyOutputRetrySuffix(next); + parts.push(next); + } + return parts.join('\n'); +} + +function createContinueState(sessionID) { + return { + sessionID: asString(sessionID), + responseMessageID: 0, + lastStatus: '', + finished: false, + }; +} + +function prepareContinueStateForNextRound(state) { + return { + ...state, + lastStatus: '', + finished: false, + }; +} + +function observeContinueState(state, chunk) { + if (!state || !chunk || typeof chunk !== 'object') { + return; + } + const topID = numberValue(chunk.response_message_id); + if (topID > 0) { + state.responseMessageID = topID; + } + observeContinueDirectPatch(state, chunk.p, chunk.v); + if (chunk.p === 'response') { + observeContinueBatchPatches(state, 'response', chunk.v); + } else { + observeContinueBatchPatches(state, '', chunk.v); + } + const response = chunk.v && typeof chunk.v === 'object' ? chunk.v.response : null; + observeContinueResponseObject(state, response); + const messageResponse = chunk.message && typeof chunk.message === 'object' && chunk.message.response; + observeContinueResponseObject(state, messageResponse); +} + +function observeContinueDirectPatch(state, path, value) { + if (!state) { + return; + } + switch (asString(path).trim().replace(/^\/+|\/+$/g, '')) { + case 'response/status': + case 'status': + case 'response/quasi_status': + case 'quasi_status': + setContinueStatus(state, asString(value)); + break; + case 'response/auto_continue': + case 'auto_continue': + if (value === true) { + state.lastStatus = 'AUTO_CONTINUE'; + } + break; + default: + break; + } +} + +function observeContinueResponseObject(state, response) { + if (!state || !response || typeof response !== 'object') { + return; + } + const id = numberValue(response.message_id); + if (id > 0) { + state.responseMessageID = id; + } + setContinueStatus(state, asString(response.status)); + if (response.auto_continue === true) { + state.lastStatus = 'AUTO_CONTINUE'; + } +} + +function observeContinueBatchPatches(state, parentPath, raw) { + if (!state || !Array.isArray(raw)) { + return; + } + for (const patch of raw) { + if (!patch || typeof patch !== 'object') { + continue; + } + const path = asString(patch.p).trim(); + if (!path) { + continue; + } + let fullPath = path; + const parent = asString(parentPath).trim().replace(/^\/+|\/+$/g, ''); + if (parent && !path.includes('/')) { + fullPath = `${parent}/${path}`; + } + switch (fullPath.replace(/^\/+|\/+$/g, '')) { + case 'response/status': + case 'status': + case 'response/quasi_status': + case 'quasi_status': + setContinueStatus(state, asString(patch.v)); + break; + case 'response/auto_continue': + case 'auto_continue': + if (patch.v === true) { + state.lastStatus = 'AUTO_CONTINUE'; + } + break; + default: + break; + } + } +} + +function setContinueStatus(state, status) { + const normalized = asString(status).trim(); + if (!normalized) { + return; + } + state.lastStatus = normalized; + if (['FINISHED', 'CONTENT_FILTER'].includes(normalized.toUpperCase())) { + state.finished = true; + } +} + +function shouldAutoContinue(state) { + if (!state || state.finished || !state.sessionID || state.responseMessageID <= 0) { + return false; + } + return ['INCOMPLETE', 'AUTO_CONTINUE'].includes(asString(state.lastStatus).trim().toUpperCase()); +} + +function numberValue(v) { + if (typeof v === 'number' && Number.isFinite(v)) { + return Math.trunc(v); + } + const parsed = Number.parseInt(asString(v), 10); + return Number.isFinite(parsed) ? parsed : 0; +} + +function upstreamEmptyOutputDetail(contentFilter, _text, thinking) { + if (contentFilter) { + return { + status: 400, + message: 'Upstream content filtered the response and returned no output.', + code: 'content_filter', + }; + } + if (thinking !== '') { + return { + status: 429, + message: 'Upstream account hit a rate limit and returned reasoning without visible output.', + code: 'upstream_empty_output', + }; + } + return { + status: 503, + message: 'Upstream service is unavailable and returned no output.', + code: 'upstream_unavailable', + }; +} + +function sendFailedChunk(res, status, message, code) { + res.write(`data: ${JSON.stringify({ + status_code: status, + error: { + message, + type: openAIErrorType(status), + code, + param: null, + }, + })}\n\n`); + if (!res.writableEnded && !res.destroyed) { + res.write('data: [DONE]\n\n'); + } + if (typeof res.flush === 'function') { + res.flush(); + } +} + +module.exports = { + handleVercelStream, +}; diff --git a/internal/js/helpers/stream-tool-sieve.js b/internal/js/helpers/stream-tool-sieve.js new file mode 100644 index 0000000000000000000000000000000000000000..89854780cfea152392b5cbba6edc86377e3bbe6d --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve.js @@ -0,0 +1,3 @@ +'use strict'; + +module.exports = require('./stream-tool-sieve/index.js'); diff --git a/internal/js/helpers/stream-tool-sieve/format.js b/internal/js/helpers/stream-tool-sieve/format.js new file mode 100644 index 0000000000000000000000000000000000000000..88d7271db91394f015678c904c82e1c7056fc3d0 --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/format.js @@ -0,0 +1,232 @@ +'use strict'; + +const crypto = require('crypto'); + +function formatOpenAIStreamToolCalls(calls, idStore, toolsRaw) { + if (!Array.isArray(calls) || calls.length === 0) { + return []; + } + const normalized = normalizeParsedToolCallsForSchemas(calls, toolsRaw); + return normalized.map((c, idx) => ({ + index: idx, + id: ensureStreamToolCallID(idStore, idx), + type: 'function', + function: { + name: c.name, + arguments: JSON.stringify(c.input || {}), + }, + })); +} + +function normalizeParsedToolCallsForSchemas(calls, toolsRaw) { + if (!Array.isArray(calls) || calls.length === 0) { + return calls; + } + const schemas = buildToolSchemaIndex(toolsRaw); + if (!schemas) { + return calls; + } + let changedAny = false; + const out = calls.map((call) => { + const name = String(call && call.name || '').trim().toLowerCase(); + const schema = schemas[name]; + if (!schema || !call || !call.input || typeof call.input !== 'object' || Array.isArray(call.input)) { + return call; + } + const [normalized, changed] = normalizeToolValueWithSchema(call.input, schema); + if (!changed || !normalized || typeof normalized !== 'object' || Array.isArray(normalized)) { + return call; + } + changedAny = true; + return { ...call, input: normalized }; + }); + return changedAny ? out : calls; +} + +function buildToolSchemaIndex(toolsRaw) { + if (!Array.isArray(toolsRaw) || toolsRaw.length === 0) { + return null; + } + const out = {}; + for (const item of toolsRaw) { + if (!item || typeof item !== 'object' || Array.isArray(item)) { + continue; + } + const [name, schema] = extractToolNameAndSchema(item); + if (!name || !schema || typeof schema !== 'object' || Array.isArray(schema)) { + continue; + } + out[name.toLowerCase()] = schema; + } + return Object.keys(out).length > 0 ? out : null; +} + +function extractToolNameAndSchema(tool) { + const fn = tool && typeof tool.function === 'object' && !Array.isArray(tool.function) ? tool.function : null; + const name = firstNonEmptyString(tool.name, fn && fn.name); + const schema = firstNonNil( + tool.parameters, + tool.input_schema, + tool.inputSchema, + tool.schema, + fn && fn.parameters, + fn && fn.input_schema, + fn && fn.inputSchema, + fn && fn.schema, + ); + return [name, schema]; +} + +function normalizeToolValueWithSchema(value, schema) { + if (value == null || !schema || typeof schema !== 'object' || Array.isArray(schema)) { + return [value, false]; + } + if (shouldCoerceSchemaToString(schema)) { + return stringifySchemaValue(value); + } + if (looksLikeObjectSchema(schema)) { + if (!value || typeof value !== 'object' || Array.isArray(value)) { + return [value, false]; + } + const properties = schema.properties && typeof schema.properties === 'object' && !Array.isArray(schema.properties) ? schema.properties : null; + const additional = schema.additionalProperties; + let changed = false; + const out = {}; + for (const [key, current] of Object.entries(value)) { + let next = current; + let fieldChanged = false; + if (properties && Object.prototype.hasOwnProperty.call(properties, key)) { + [next, fieldChanged] = normalizeToolValueWithSchema(current, properties[key]); + } else if (additional != null) { + [next, fieldChanged] = normalizeToolValueWithSchema(current, additional); + } + out[key] = next; + changed = changed || fieldChanged; + } + return changed ? [out, true] : [value, false]; + } + if (looksLikeArraySchema(schema)) { + if (!Array.isArray(value) || value.length === 0 || schema.items == null) { + return [value, false]; + } + let changed = false; + const out = value.map((item, idx) => { + const itemSchema = Array.isArray(schema.items) ? schema.items[idx] : schema.items; + if (itemSchema == null) { + return item; + } + const [next, itemChanged] = normalizeToolValueWithSchema(item, itemSchema); + changed = changed || itemChanged; + return next; + }); + return changed ? [out, true] : [value, false]; + } + return [value, false]; +} + +function shouldCoerceSchemaToString(schema) { + if (!schema || typeof schema !== 'object' || Array.isArray(schema)) { + return false; + } + if (typeof schema.const === 'string') { + return true; + } + if (Array.isArray(schema.enum) && schema.enum.length > 0 && schema.enum.every((item) => typeof item === 'string')) { + return true; + } + if (typeof schema.type === 'string') { + return schema.type.trim().toLowerCase() === 'string'; + } + if (Array.isArray(schema.type) && schema.type.length > 0) { + let hasString = false; + for (const item of schema.type) { + if (typeof item !== 'string') { + return false; + } + const typ = item.trim().toLowerCase(); + if (typ === 'string') { + hasString = true; + } else if (typ !== 'null') { + return false; + } + } + return hasString; + } + return false; +} + +function looksLikeObjectSchema(schema) { + return !!schema && typeof schema === 'object' && !Array.isArray(schema) && ( + (typeof schema.type === 'string' && schema.type.trim().toLowerCase() === 'object') || + (schema.properties && typeof schema.properties === 'object' && !Array.isArray(schema.properties)) || + schema.additionalProperties != null + ); +} + +function looksLikeArraySchema(schema) { + return !!schema && typeof schema === 'object' && !Array.isArray(schema) && ( + (typeof schema.type === 'string' && schema.type.trim().toLowerCase() === 'array') || + schema.items != null + ); +} + +function stringifySchemaValue(value) { + if (value == null) { + return [value, false]; + } + if (typeof value === 'string') { + return [value, false]; + } + try { + return [JSON.stringify(value), true]; + } catch { + return [value, false]; + } +} + +function firstNonNil(...values) { + for (const value of values) { + if (value != null) { + return value; + } + } + return null; +} + +function firstNonEmptyString(...values) { + for (const value of values) { + if (typeof value !== 'string') { + continue; + } + const trimmed = value.trim(); + if (trimmed) { + return trimmed; + } + } + return ''; +} + +function ensureStreamToolCallID(idStore, index) { + if (!(idStore instanceof Map)) { + return `call_${newCallID()}`; + } + const key = Number.isInteger(index) ? index : 0; + const existing = idStore.get(key); + if (existing) { + return existing; + } + const next = `call_${newCallID()}`; + idStore.set(key, next); + return next; +} + +function newCallID() { + if (typeof crypto.randomUUID === 'function') { + return crypto.randomUUID().replace(/-/g, ''); + } + return `${Date.now()}${Math.floor(Math.random() * 1e9)}`; +} + +module.exports = { + formatOpenAIStreamToolCalls, +}; diff --git a/internal/js/helpers/stream-tool-sieve/index.js b/internal/js/helpers/stream-tool-sieve/index.js new file mode 100644 index 0000000000000000000000000000000000000000..6333d8c9708ae7e58c5cd309e80a260a3f6b4a6f --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/index.js @@ -0,0 +1,31 @@ +'use strict'; + +const { + createToolSieveState, +} = require('./state'); +const { + processToolSieveChunk, + flushToolSieve, +} = require('./sieve'); +const { + extractToolNames, + parseToolCalls, + parseToolCallsDetailed, + parseStandaloneToolCalls, + parseStandaloneToolCallsDetailed, +} = require('./parse'); +const { + formatOpenAIStreamToolCalls, +} = require('./format'); + +module.exports = { + extractToolNames, + createToolSieveState, + processToolSieveChunk, + flushToolSieve, + parseToolCalls, + parseToolCallsDetailed, + parseStandaloneToolCalls, + parseStandaloneToolCallsDetailed, + formatOpenAIStreamToolCalls, +}; diff --git a/internal/js/helpers/stream-tool-sieve/jsonscan.js b/internal/js/helpers/stream-tool-sieve/jsonscan.js new file mode 100644 index 0000000000000000000000000000000000000000..114177375b7b7e24dcc952cbf696b462e64892fc --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/jsonscan.js @@ -0,0 +1,172 @@ +'use strict'; + +function findObjectFieldValueStart(text, objStart, keys) { + if (!text || objStart < 0 || objStart >= text.length || text[objStart] !== '{') { + return -1; + } + let depth = 0; + let quote = ''; + let escaped = false; + for (let i = objStart; i < text.length; i += 1) { + const ch = text[i]; + if (quote) { + if (escaped) { + escaped = false; + continue; + } + if (ch === '\\') { + escaped = true; + continue; + } + if (ch === quote) { + quote = ''; + } + continue; + } + if (ch === '"' || ch === "'") { + if (depth === 1) { + const parsed = parseJSONStringLiteral(text, i); + if (!parsed) { + return -1; + } + let j = skipSpaces(text, parsed.end); + if (j >= text.length || text[j] !== ':') { + i = parsed.end - 1; + continue; + } + j = skipSpaces(text, j + 1); + if (j >= text.length) { + return -1; + } + if (keys.includes(parsed.value)) { + return j; + } + i = j - 1; + continue; + } + quote = ch; + continue; + } + if (ch === '{') { + depth += 1; + continue; + } + if (ch === '}') { + depth -= 1; + if (depth === 0) { + break; + } + } + } + return -1; +} + +function parseJSONStringLiteral(text, start) { + if (!text || start < 0 || start >= text.length || text[start] !== '"') { + return null; + } + let out = ''; + let escaped = false; + for (let i = start + 1; i < text.length; i += 1) { + const ch = text[i]; + if (escaped) { + out += ch; + escaped = false; + continue; + } + if (ch === '\\') { + escaped = true; + continue; + } + if (ch === '"') { + return { value: out, end: i + 1 }; + } + out += ch; + } + return null; +} + +function skipSpaces(text, i) { + let idx = i; + while (idx < text.length) { + const ch = text[idx]; + if (ch === ' ' || ch === '\t' || ch === '\n' || ch === '\r') { + idx += 1; + continue; + } + break; + } + return idx; +} + +function extractJSONObjectFrom(text, start) { + if (!text || start < 0 || start >= text.length || text[start] !== '{') { + return { ok: false, end: 0 }; + } + let depth = 0; + let quote = ''; + let escaped = false; + for (let i = start; i < text.length; i += 1) { + const ch = text[i]; + if (quote) { + if (escaped) { + escaped = false; + continue; + } + if (ch === '\\') { + escaped = true; + continue; + } + if (ch === quote) { + quote = ''; + } + continue; + } + if (ch === '"' || ch === "'") { + quote = ch; + continue; + } + if (ch === '{') { + depth += 1; + continue; + } + if (ch === '}') { + depth -= 1; + if (depth === 0) { + return { ok: true, end: i + 1 }; + } + } + } + return { ok: false, end: 0 }; +} + +function trimWrappingJSONFence(prefix, suffix) { + const rightTrimmedPrefix = (prefix || '').replace(/[ \t\r\n]+$/g, ''); + const fenceIdx = rightTrimmedPrefix.lastIndexOf('```'); + if (fenceIdx < 0) return { prefix, suffix }; + const fenceCount = (rightTrimmedPrefix.slice(0, fenceIdx + 3).match(/```/g) || []).length; + if (fenceCount % 2 === 0) { + return { prefix, suffix }; + } + const header = rightTrimmedPrefix.slice(fenceIdx + 3).trim().toLowerCase(); + if (header && header !== 'json') { + return { prefix, suffix }; + } + const leftTrimmedSuffix = (suffix || '').replace(/^[ \t\r\n]+/g, ''); + if (!leftTrimmedSuffix.startsWith('```')) { + return { prefix, suffix }; + } + const consumed = (suffix || '').length - leftTrimmedSuffix.length; + return { + prefix: rightTrimmedPrefix.slice(0, fenceIdx), + suffix: (suffix || '').slice(consumed + 3), + }; +} + +module.exports = { + findObjectFieldValueStart, + parseJSONStringLiteral, + skipSpaces, + extractJSONObjectFrom, + trimWrappingJSONFence, +}; diff --git a/internal/js/helpers/stream-tool-sieve/parse.js b/internal/js/helpers/stream-tool-sieve/parse.js new file mode 100644 index 0000000000000000000000000000000000000000..7a707695a2675f11882176bead1a190f6cfea4b3 --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/parse.js @@ -0,0 +1,155 @@ +'use strict'; + +const { + toStringSafe, +} = require('./state'); +const { + parseMarkupToolCalls, + stripFencedCodeBlocks, + containsToolCallWrapperSyntaxOutsideIgnored, + normalizeDSMLToolCallMarkup, + hasRepairableXMLToolCallsWrapper, + indexToolCDATAOpen, + sanitizeLooseCDATA, +} = require('./parse_payload'); + +function extractToolNames(tools) { + if (!Array.isArray(tools) || tools.length === 0) { + return []; + } + const out = []; + const seen = new Set(); + for (const t of tools) { + if (!t || typeof t !== 'object') { + continue; + } + const fn = t.function && typeof t.function === 'object' ? t.function : t; + const name = toStringSafe(fn.name); + if (!name || seen.has(name)) { + continue; + } + seen.add(name); + out.push(name); + } + return out; +} + +function parseToolCalls(text, toolNames) { + return parseToolCallsDetailed(text, toolNames).calls; +} + +function parseToolCallsDetailed(text, toolNames) { + const result = emptyParseResult(); + const raw = toStringSafe(text); + if (!raw) { + return result; + } + if (shouldSkipToolCallParsingForCodeFenceExample(raw)) { + return result; + } + const normalized = normalizeDSMLToolCallMarkup(stripFencedCodeBlocks(raw).trim()); + if (!normalized.ok || !normalized.text) { + return result; + } + result.sawToolCallSyntax = looksLikeToolCallSyntax(normalized.text) || hasRepairableXMLToolCallsWrapper(normalized.text); + // XML markup parsing only. + let parsed = parseMarkupToolCalls(normalized.text); + if (parsed.length === 0 && indexToolCDATAOpen(normalized.text, 0) >= 0) { + const recovered = sanitizeLooseCDATA(normalized.text); + if (recovered !== normalized.text) { + parsed = parseMarkupToolCalls(recovered); + } + } + if (parsed.length === 0) { + return result; + } + result.sawToolCallSyntax = true; + const filtered = filterToolCallsDetailed(parsed, toolNames); + result.calls = filtered.calls; + result.rejectedToolNames = filtered.rejectedToolNames; + result.rejectedByPolicy = filtered.rejectedToolNames.length > 0 && filtered.calls.length === 0; + return result; +} + +function parseStandaloneToolCalls(text, toolNames) { + return parseStandaloneToolCallsDetailed(text, toolNames).calls; +} + +function parseStandaloneToolCallsDetailed(text, toolNames) { + const result = emptyParseResult(); + const raw = toStringSafe(text); + if (!raw) { + return result; + } + if (shouldSkipToolCallParsingForCodeFenceExample(raw)) { + return result; + } + const normalized = normalizeDSMLToolCallMarkup(stripFencedCodeBlocks(raw).trim()); + if (!normalized.ok || !normalized.text) { + return result; + } + result.sawToolCallSyntax = looksLikeToolCallSyntax(normalized.text) || hasRepairableXMLToolCallsWrapper(normalized.text); + // XML markup parsing only. + let parsed = parseMarkupToolCalls(normalized.text); + if (parsed.length === 0 && indexToolCDATAOpen(normalized.text, 0) >= 0) { + const recovered = sanitizeLooseCDATA(normalized.text); + if (recovered !== normalized.text) { + parsed = parseMarkupToolCalls(recovered); + } + } + if (parsed.length === 0) { + return result; + } + + result.sawToolCallSyntax = true; + const filtered = filterToolCallsDetailed(parsed, toolNames); + result.calls = filtered.calls; + result.rejectedToolNames = filtered.rejectedToolNames; + result.rejectedByPolicy = filtered.rejectedToolNames.length > 0 && filtered.calls.length === 0; + return result; +} + +function emptyParseResult() { + return { + calls: [], + sawToolCallSyntax: false, + rejectedByPolicy: false, + rejectedToolNames: [], + }; +} + +function filterToolCallsDetailed(parsed, toolNames) { + const calls = []; + for (const tc of parsed) { + if (!tc || !tc.name) { + continue; + } + const input = tc.input && typeof tc.input === 'object' && !Array.isArray(tc.input) ? tc.input : {}; + calls.push({ + name: tc.name, + input, + }); + } + return { calls, rejectedToolNames: [] }; +} + +function looksLikeToolCallSyntax(text) { + const styles = containsToolCallWrapperSyntaxOutsideIgnored(text); + return styles.dsml || styles.canonical; +} + +function shouldSkipToolCallParsingForCodeFenceExample(text) { + if (!looksLikeToolCallSyntax(text)) { + return false; + } + const stripped = stripFencedCodeBlocks(text); + return !looksLikeToolCallSyntax(stripped); +} + +module.exports = { + extractToolNames, + parseToolCalls, + parseToolCallsDetailed, + parseStandaloneToolCalls, + parseStandaloneToolCallsDetailed, +}; diff --git a/internal/js/helpers/stream-tool-sieve/parse_payload.js b/internal/js/helpers/stream-tool-sieve/parse_payload.js new file mode 100644 index 0000000000000000000000000000000000000000..380a0f3f5fc9d04e7f3d056b8879cd957f41a247 --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/parse_payload.js @@ -0,0 +1,2655 @@ +'use strict'; + +const CDATA_PATTERN = /^(?:<|〈)(?:!|!)\[CDATA\[([\s\S]*?)]](?:>|>|〉)$/i; +const XML_ATTR_PATTERN = /\b([a-z0-9_:-]+)\s*=\s*("([^"]*)"|'([^']*)')/gi; +const TOOL_MARKUP_NAMES = [ + { raw: 'tool_calls', canonical: 'tool_calls' }, + { raw: 'tool-calls', canonical: 'tool_calls', dsmlOnly: true }, + { raw: 'toolcalls', canonical: 'tool_calls', dsmlOnly: true }, + { raw: 'invoke', canonical: 'invoke' }, + { raw: 'parameter', canonical: 'parameter' }, +]; + +const { + toStringSafe, +} = require('./state'); + +function stripFencedCodeBlocks(text) { + const t = typeof text === 'string' ? text : ''; + if (!t) { + return ''; + } + const lines = t.split('\n'); + const out = []; + let inFence = false; + let fenceChar = ''; + let fenceLen = 0; + let inCDATA = false; + let beforeFenceIdx = 0; + + for (let li = 0; li < lines.length; li += 1) { + const line = lines[li]; + const lineWithNL = li < lines.length - 1 ? line + '\n' : line; + + // CDATA protection + if (inCDATA || cdataStartsBeforeFence(line)) { + out.push(lineWithNL); + inCDATA = updateCDATAStateLine(inCDATA, line); + continue; + } + + const trimmed = line.replace(/^[ \t]+/, ''); + if (!inFence) { + const fence = parseFenceOpenLine(trimmed); + if (fence) { + inFence = true; + fenceChar = fence.ch; + fenceLen = fence.count; + beforeFenceIdx = out.length; + continue; + } + out.push(lineWithNL); + continue; + } + + if (isFenceCloseLine(trimmed, fenceChar, fenceLen)) { + inFence = false; + fenceChar = ''; + fenceLen = 0; + } + } + + if (inFence) { + // Unclosed fence: keep content before the fence started. + if (beforeFenceIdx > 0) { + return out.slice(0, beforeFenceIdx).join(''); + } + return ''; + } + return out.join(''); +} + +function stripMarkdownCodeSpans(text) { + const raw = toStringSafe(text); + if (!raw) { + return ''; + } + let out = ''; + for (let i = 0; i < raw.length;) { + const skipped = skipXmlIgnoredSection(raw, i); + if (skipped.blocked) { + out += raw.slice(i); + break; + } + if (skipped.advanced) { + out += raw.slice(i, skipped.next); + i = skipped.next; + continue; + } + const spanEnd = markdownCodeSpanEnd(raw, i); + if (spanEnd.ok) { + i = spanEnd.end; + continue; + } + out += raw[i]; + i += 1; + } + return out; +} + +function markdownCodeSpanEnd(text, start) { + const raw = toStringSafe(text); + if (start < 0 || start >= raw.length || raw[start] !== '`') { + return { ok: false, end: start }; + } + const count = countLeadingChars(raw, start, '`'); + if (!count) { + return { ok: false, end: start }; + } + let search = start + count; + while (search < raw.length) { + if (raw[search] !== '`') { + search += 1; + continue; + } + const run = countLeadingChars(raw, search, '`'); + if (run === count) { + return { ok: true, end: search + run }; + } + search += run; + } + return { ok: false, end: start }; +} + +function countLeadingChars(text, start, ch) { + let count = 0; + while (start + count < text.length && text[start + count] === ch) { + count += 1; + } + return count; +} + +function parseFenceOpenLine(trimmed) { + if (trimmed.length < 3) return null; + const ch = trimmed[0]; + if (ch !== '`' && ch !== '~') return null; + let count = 0; + while (count < trimmed.length && trimmed[count] === ch) count++; + if (count < 3) return null; + return { ch, count }; +} + +function isFenceCloseLine(trimmed, fenceChar, fenceLen) { + if (!fenceChar || !trimmed || trimmed[0] !== fenceChar) return false; + let count = 0; + while (count < trimmed.length && trimmed[count] === fenceChar) count++; + if (count < fenceLen) return false; + return trimmed.slice(count).trim() === ''; +} + +function cdataStartsBeforeFence(line) { + const cdataIdx = indexToolCDATAOpen(line, 0); + if (cdataIdx < 0) return false; + const fenceIdx = Math.min( + line.indexOf('```') >= 0 ? line.indexOf('```') : Infinity, + line.indexOf('~~~') >= 0 ? line.indexOf('~~~') : Infinity, + ); + return fenceIdx === Infinity || cdataIdx < fenceIdx; +} + +function updateCDATAStateLine(inCDATA, line) { + let pos = 0; + let state = inCDATA; + while (pos < line.length) { + if (state) { + let end = -1; + let closeLen = 0; + for (let i = pos; i < line.length; i += 1) { + const foundLen = toolCDATACloseLenAt(line, i); + if (foundLen > 0) { + end = i; + closeLen = foundLen; + break; + } + } + if (end < 0) return true; + pos = end + closeLen; + state = false; + continue; + } + const start = indexToolCDATAOpen(line, pos); + if (start < 0) return false; + pos = start + toolCDATAOpenLenAt(line, start); + state = true; + } + return state; +} + +function parseMarkupToolCalls(text) { + const normalized = normalizeDSMLToolCallMarkup(toStringSafe(text)); + if (!normalized.ok) { + return []; + } + let raw = normalized.text.trim(); + if (!raw) { + return []; + } + let wrappers = findToolCallElementBlocksOutsideIgnored(raw); + if (wrappers.length === 0 && hasRepairableXMLToolCallsWrapper(raw)) { + const repaired = repairMissingXMLToolCallsOpeningWrapper(raw); + if (repaired !== raw) { + raw = repaired; + wrappers = findToolCallElementBlocksOutsideIgnored(raw); + } + } + const out = []; + for (const wrapper of wrappers) { + const body = toStringSafe(wrapper.body); + for (const block of findXmlElementBlocks(body, 'invoke')) { + const parsed = parseMarkupSingleToolCall(block); + if (parsed) { + out.push(parsed); + } + } + } + return out; +} + +function findToolCallElementBlocksOutsideIgnored(text) { + const raw = toStringSafe(text); + const out = []; + for (let searchFrom = 0; searchFrom < raw.length;) { + const tag = findToolMarkupTagOutsideIgnored(raw, searchFrom); + if (!tag) { + break; + } + if (tag.closing || tag.name !== 'tool_calls') { + searchFrom = tag.end + 1; + continue; + } + const closeTag = findMatchingToolMarkupClose(raw, tag); + if (!closeTag) { + searchFrom = tag.end + 1; + continue; + } + const endDelim = xmlTagEndDelimiterLenEndingAt(raw, tag.end); + const attrsEnd = endDelim > 0 ? tag.end + 1 - endDelim : tag.end + 1; + out.push({ + attrs: raw.slice(tag.nameEnd, attrsEnd), + body: raw.slice(tag.end + 1, closeTag.start), + start: tag.start, + end: closeTag.end + 1, + }); + searchFrom = closeTag.end + 1; + } + return out; +} + +function normalizeDSMLToolCallMarkup(text) { + const raw = toStringSafe(text); + if (!raw) { + return { text: '', ok: true }; + } + const canonicalized = canonicalizeToolCallCandidateSpans(raw); + const styles = containsToolMarkupSyntaxOutsideIgnored(canonicalized); + if (!styles.dsml && !styles.canonical) { + return { text: canonicalized, ok: true }; + } + return { + text: replaceDSMLToolMarkupOutsideIgnored(canonicalized), + ok: true, + }; +} + +function containsDSMLToolMarkup(text) { + return containsToolMarkupSyntaxOutsideIgnored(text).dsml; +} + +function containsCanonicalToolMarkup(text) { + return containsToolMarkupSyntaxOutsideIgnored(text).canonical; +} + +function containsToolCallWrapperSyntaxOutsideIgnored(text) { + const raw = toStringSafe(text); + const styles = { dsml: false, canonical: false }; + if (!raw) { + return styles; + } + for (let i = 0; i < raw.length;) { + const skipped = skipXmlIgnoredSection(raw, i); + if (skipped.blocked) { + return styles; + } + if (skipped.advanced) { + i = skipped.next; + continue; + } + const spanEnd = markdownCodeSpanEnd(raw, i); + if (spanEnd.ok) { + i = spanEnd.end; + continue; + } + const tag = scanToolMarkupTagAt(raw, i); + if (tag) { + if (tag.name !== 'tool_calls') { + i = tag.end + 1; + continue; + } + if (tag.dsmlLike) { + styles.dsml = true; + } else { + styles.canonical = true; + } + if (styles.dsml && styles.canonical) { + return styles; + } + i = tag.end + 1; + continue; + } + i += 1; + } + return styles; +} +function containsToolMarkupSyntaxOutsideIgnored(text) { + const raw = toStringSafe(text); + const styles = { dsml: false, canonical: false }; + if (!raw) { + return styles; + } + for (let i = 0; i < raw.length;) { + const skipped = skipXmlIgnoredSection(raw, i); + if (skipped.blocked) { + return styles; + } + if (skipped.advanced) { + i = skipped.next; + continue; + } + const spanEnd = markdownCodeSpanEnd(raw, i); + if (spanEnd.ok) { + i = spanEnd.end; + continue; + } + const tag = scanToolMarkupTagAt(raw, i); + if (tag) { + if (tag.dsmlLike) { + styles.dsml = true; + } else { + styles.canonical = true; + } + if (styles.dsml && styles.canonical) { + return styles; + } + i = tag.end + 1; + continue; + } + i += 1; + } + return styles; +} + +function replaceDSMLToolMarkupOutsideIgnored(text) { + const raw = toStringSafe(text); + if (!raw) { + return ''; + } + let out = ''; + for (let i = 0; i < raw.length;) { + const skipped = skipXmlIgnoredSection(raw, i); + if (skipped.blocked) { + out += raw.slice(i); + break; + } + if (skipped.advanced) { + out += raw.slice(i, skipped.next); + i = skipped.next; + continue; + } + const spanEnd = markdownCodeSpanEnd(raw, i); + if (spanEnd.ok) { + out += raw.slice(i, spanEnd.end); + i = spanEnd.end; + continue; + } + const tag = scanToolMarkupTagAt(raw, i); + if (tag) { + out += `<${tag.closing ? '/' : ''}${tag.name}${raw.slice(tag.nameEnd, tag.end)}>`; + i = tag.end + 1; + continue; + } + out += raw[i]; + i += 1; + } + return out; +} + +function parseMarkupSingleToolCall(block) { + const attrs = parseTagAttributes(block.attrs); + const name = toStringSafe(attrs.name).trim(); + if (!name) { + return null; + } + const inner = toStringSafe(block.body).trim(); + + if (inner) { + try { + const decoded = JSON.parse(inner); + if (decoded && typeof decoded === 'object' && !Array.isArray(decoded)) { + return { + name, + input: decoded.input && typeof decoded.input === 'object' && !Array.isArray(decoded.input) + ? decoded.input + : decoded.parameters && typeof decoded.parameters === 'object' && !Array.isArray(decoded.parameters) + ? decoded.parameters + : {}, + }; + } + } catch (_err) { + // Not JSON, continue with markup parsing. + } + } + const input = {}; + for (const match of findXmlElementBlocks(inner, 'parameter')) { + const parameterAttrs = parseTagAttributes(match.attrs); + const paramName = toStringSafe(parameterAttrs.name).trim(); + if (!paramName) { + continue; + } + appendMarkupValue(input, paramName, parseMarkupValue(match.body, paramName)); + } + if (Object.keys(input).length === 0 && inner.trim() !== '') { + return null; + } + return { name, input }; +} + +function findXmlElementBlocks(text, tag) { + const source = toStringSafe(text); + const name = toStringSafe(tag).toLowerCase(); + if (!source || !name) { + return []; + } + const out = []; + let pos = 0; + while (pos < source.length) { + const start = findXmlStartTagOutsideCDATA(source, name, pos); + if (!start) { + break; + } + const end = findMatchingXmlEndTagOutsideCDATA(source, name, start.bodyStart); + if (!end) { + pos = start.bodyStart; + continue; + } + out.push({ + attrs: start.attrs, + body: source.slice(start.bodyStart, end.closeStart), + start: start.start, + end: end.closeEnd, + }); + pos = end.closeEnd; + } + return out; +} + +function findXmlStartTagOutsideCDATA(text, tag, from) { + const lower = text.toLowerCase(); + const target = `<${tag}`; + for (let i = Math.max(0, from || 0); i < text.length;) { + const skipped = skipXmlIgnoredSection(text, i); + if (skipped.blocked) { + return null; + } + if (skipped.advanced) { + i = skipped.next; + continue; + } + if (lower.startsWith(target, i) && hasXmlTagBoundary(text, i + target.length)) { + const tagEnd = findXmlTagEnd(text, i + target.length); + if (tagEnd < 0) { + return null; + } + return { + start: i, + bodyStart: tagEnd + 1, + attrs: text.slice(i + target.length, tagEnd), + }; + } + i += 1; + } + return null; +} + +function findMatchingXmlEndTagOutsideCDATA(text, tag, from) { + const lower = text.toLowerCase(); + const openTarget = `<${tag}`; + const closeTarget = ` 0) { + const end = findToolCDATAEnd(raw, i + openLen); + if (end < 0) { + return { advanced: false, blocked: true, next: i }; + } + return { advanced: true, blocked: false, next: end + toolCDATACloseLenAt(raw, end) }; + } + if (raw.startsWith('', i + ''.length }; + } + return { advanced: false, blocked: false, next: i }; +} + +function findNextCDATAOpen(text, from) { + const raw = toStringSafe(text); + const start = indexToolCDATAOpen(raw, from || 0); + if (start < 0) { + return { ok: false, start: -1, bodyStart: -1 }; + } + return { ok: true, start, bodyStart: start + toolCDATAOpenLenAt(raw, start) }; +} + +function matchCDATAOpenAt(text, start) { + const raw = toStringSafe(text); + const openLen = toolCDATAOpenLenAt(raw, start); + return openLen > 0 ? { ok: true, bodyStart: start + openLen } : { ok: false, bodyStart: start }; +} + +function isCDATAOpenSeparator(ch) { + return isToolMarkupSeparator(ch); +} + +function findCDATAEnd(text, from) { + const raw = toStringSafe(text); + const index = findToolCDATAEnd(raw, from); + return { index, len: index >= 0 ? toolCDATACloseLenAt(raw, index) : 0 }; +} + +function scanToolMarkupTagAt(text, start) { + const raw = toStringSafe(text); + const startDelimLen = xmlTagStartDelimiterLenAt(raw, start); + if (!raw || start < 0 || start >= raw.length || !startDelimLen) { + return null; + } + const lower = raw.toLowerCase(); + let i = start + startDelimLen; + while (i < raw.length) { + i = skipToolMarkupIgnorables(raw, i); + const delimLen = xmlTagStartDelimiterLenAt(raw, i); + if (!delimLen) { + break; + } + i += delimLen; + } + const slash = consumeToolMarkupClosingSlash(raw, i); + let closing = slash.closing; + i = slash.next; + const prefix = consumeToolMarkupNamePrefix(raw, lower, i); + const prefixStart = i; + i = prefix.next; + let dsmlLike = prefix.dsmlLike; + let { name, len } = matchToolMarkupName(raw, i, dsmlLike); + if (!name) { + const fallback = matchToolMarkupNameAfterArbitraryPrefix(raw, prefixStart); + if (!fallback.ok) { + return null; + } + if (!closing && toolMarkupPrefixContainsSlash(raw.slice(prefixStart, fallback.start))) { + closing = true; + } + name = fallback.name; + i = fallback.start; + len = fallback.len; + dsmlLike = true; + } + const originalNameEnd = i + len; + let nameEnd = originalNameEnd; + while (true) { + const nextPipe = consumeToolMarkupSeparator(raw, nameEnd); + if (!nextPipe.ok) { + break; + } + nameEnd = nextPipe.next; + } + const hasTrailingSeparator = nameEnd > originalNameEnd; + if (!hasXmlTagBoundary(raw, nameEnd)) { + return null; + } + let end = findXmlTagEnd(raw, nameEnd); + if (end < 0) { + if (!hasTrailingSeparator) { + return null; + } + end = nameEnd - 1; + } + if (hasTrailingSeparator) { + const nextLT = raw.indexOf('<', nameEnd); + if (nextLT >= 0 && end >= nextLT) { + end = nameEnd - 1; + } + } + if (end < 0) { + return null; + } + return { + start, + end, + nameStart: i, + nameEnd, + name, + closing, + selfClosing: isSelfClosingXmlTag(raw.slice(start, end)), + dsmlLike, + canonical: !dsmlLike, + }; +} + +function findToolMarkupTagOutsideIgnored(text, from) { + const raw = toStringSafe(text); + for (let i = Math.max(0, from || 0); i < raw.length;) { + const skipped = skipXmlIgnoredSection(raw, i); + if (skipped.blocked) { + return null; + } + if (skipped.advanced) { + i = skipped.next; + continue; + } + const spanEnd = markdownCodeSpanEnd(raw, i); + if (spanEnd.ok) { + i = spanEnd.end; + continue; + } + const tag = scanToolMarkupTagAt(raw, i); + if (tag) { + return tag; + } + i += 1; + } + return null; +} + +function findMatchingToolMarkupClose(text, openTag) { + const raw = toStringSafe(text); + if (!raw || !openTag || !openTag.name || openTag.closing) { + return null; + } + let depth = 1; + for (let pos = openTag.end + 1; pos < raw.length;) { + const tag = findToolMarkupTagOutsideIgnored(raw, pos); + if (!tag) { + return null; + } + if (tag.name !== openTag.name) { + pos = tag.end + 1; + continue; + } + if (tag.closing) { + depth -= 1; + if (depth === 0) { + return tag; + } + } else if (!tag.selfClosing) { + depth += 1; + } + pos = tag.end + 1; + } + return null; +} + +function findPartialToolMarkupStart(text) { + const raw = toStringSafe(text); + const lastLT = lastIndexOfToolMarkupStartDelimiter(raw); + if (lastLT < 0) { + return -1; + } + const start = includeDuplicateLeadingLessThan(raw, lastLT); + const tail = raw.slice(start); + if (containsXmlTagTerminator(tail)) { + return -1; + } + return isPartialToolMarkupTagPrefix(tail) ? start : -1; +} + +function includeDuplicateLeadingLessThan(text, idx) { + let out = idx; + while (out > 0 && isXmlTagStartDelimiter(text[out - 1])) { + out -= 1; + } + return out; +} + +function isXmlTagStartDelimiter(ch) { + return ['<', '<', '﹤', '〈'].includes(ch); +} + +function isToolMarkupSeparator(ch) { + if (isToolMarkupWhitespaceLike(ch)) { + return false; + } + const normalized = normalizeFullwidthASCIIChar(ch || ''); + if (!normalized || ['<', '>', '/', '=', '"', "'", '['].includes(normalized)) { + return false; + } + if ([' ', '\t', '\n', '\r'].includes(normalized)) { + return false; + } + return !/^[A-Za-z0-9]$/.test(normalized); +} + +function isToolMarkupWhitespaceLike(ch) { + return !!ch && (/\s/u.test(ch) || ch === '▁'); +} + +function isPartialToolMarkupTagPrefix(text) { + const raw = toStringSafe(text); + if (!raw || !isXmlTagStartDelimiter(raw[0]) || containsXmlTagTerminator(raw)) { + return false; + } + const lower = raw.toLowerCase(); + let i = 1; + while (i < raw.length && isXmlTagStartDelimiter(raw[i])) { + i += 1; + } + if (i >= raw.length) { + return true; + } + const slash = consumeToolMarkupClosingSlash(raw, i); + if (slash.closing) { + i = slash.next; + } + while (i <= raw.length) { + if (i === raw.length) { + return true; + } + if (hasToolMarkupNamePrefix(raw, i)) { + return true; + } + if (hasDSMLNamePrefixOrPartial(raw, i)) { + return true; + } + if (hasPartialToolMarkupNameAfterArbitraryPrefix(raw, i)) { + return true; + } + const next = consumeToolMarkupNamePrefixOnce(raw, lower, i); + if (!next.ok) { + return false; + } + i = next.next; + } + return false; +} + +function consumeToolMarkupNamePrefix(raw, lower, idx) { + let next = idx; + let dsmlLike = false; + while (true) { + const consumed = consumeToolMarkupNamePrefixOnce(raw, lower, next); + if (!consumed.ok) { + return { next, dsmlLike }; + } + next = consumed.next; + dsmlLike = true; + } +} + +function matchToolMarkupNameAfterArbitraryPrefix(raw, start) { + for (let idx = start; idx < raw.length;) { + if (isToolMarkupTagTerminator(raw, idx)) { + return { ok: false }; + } + for (const name of TOOL_MARKUP_NAMES) { + const matched = consumeToolKeyword(raw, idx, name.raw); + if (!matched.ok) { + continue; + } + if (!toolMarkupPrefixAllowsLocalNameAt(raw, start, idx)) { + continue; + } + return { ok: true, name: name.canonical, start: idx, len: matched.next - idx }; + } + idx += 1; + } + return { ok: false }; +} + +function hasPartialToolMarkupNameAfterArbitraryPrefix(raw, start) { + for (let idx = start; idx < raw.length;) { + if (isToolMarkupTagTerminator(raw, idx)) { + return false; + } + if (toolMarkupPrefixAllowsLocalNameAt(raw, start, idx) && hasToolMarkupNamePrefix(raw, idx)) { + return true; + } + if (toolMarkupPrefixAllowsLocalNameAt(raw, start, idx) && hasDSMLNamePrefixOrPartial(raw, idx)) { + return true; + } + idx += 1; + } + return toolMarkupPrefixAllowsLocalName(raw.slice(start)); +} + +function hasDSMLNamePrefixOrPartial(raw, start) { + const tail = normalizedASCIITailAt(raw, start); + return tail.startsWith('dsml') || 'dsml'.startsWith(tail) || hasConfusablePartialKeywordPrefix(raw, start, 'dsml'); +} + +function toolMarkupPrefixAllowsLocalName(prefix) { + if (!prefix) { + return false; + } + if (normalizedASCIITailAt(prefix, 0).includes('dsml')) { + return true; + } + if (/[="']/u.test(prefix)) { + return false; + } + const previous = normalizeFullwidthASCIIChar(prefix[prefix.length - 1] || ''); + return !/^[A-Za-z0-9]$/.test(previous); +} + +function toolMarkupPrefixAllowsLocalNameAt(raw, start, localStart) { + if (start < 0 || localStart <= start || localStart > raw.length) { + return false; + } + const prefix = raw.slice(start, localStart); + if (toolMarkupPrefixAllowsLocalName(prefix)) { + return true; + } + if (/[="']/u.test(prefix)) { + return false; + } + const previous = normalizeFullwidthASCIIChar(prefix[prefix.length - 1] || ''); + const next = normalizeFullwidthASCIIChar(raw[localStart] || ''); + return /^[A-Za-z0-9]$/.test(previous) && /^[A-Z]$/.test(next); +} + +function toolMarkupPrefixContainsSlash(prefix) { + for (const ch of toStringSafe(prefix)) { + if (normalizeFullwidthASCIIChar(ch) === '/') { + return true; + } + } + return false; +} + +function isToolMarkupTagTerminator(raw, idx) { + return raw[idx] === '>' || normalizeFullwidthASCIIChar(raw[idx] || '') === '>'; +} + +function consumeToolMarkupNamePrefixOnce(raw, lower, idx) { + idx = skipToolMarkupIgnorables(raw, idx); + const sep = consumeToolMarkupSeparator(raw, idx); + if (sep.ok) { + return sep; + } + const spacingLen = toolMarkupWhitespaceLikeLenAt(raw, idx); + if (spacingLen > 0) { + return { next: idx + spacingLen, ok: true }; + } + const dsml = consumeToolKeyword(raw, idx, 'dsml'); + if (dsml.ok) { + let next = dsml.next; + const dashLen = toolMarkupDashLenAt(raw, next); + const underscoreLen = toolMarkupUnderscoreLenAt(raw, next); + if (dashLen) { + next += dashLen; + } else if (underscoreLen) { + next += underscoreLen; + } + return { next, ok: true }; + } + const arbitrary = consumeArbitraryToolMarkupNamePrefix(raw, lower, idx); + if (arbitrary.ok) { + return arbitrary; + } + return { next: idx, ok: false }; +} + +function consumeArbitraryToolMarkupNamePrefix(raw, _lower, idx) { + const first = consumeToolMarkupPrefixSegment(raw, idx); + if (!first.ok) { + return { next: idx, ok: false }; + } + let j = first.next; + while (j < raw.length) { + const segment = consumeToolMarkupPrefixSegment(raw, j); + if (!segment.ok) { + break; + } + j = segment.next; + } + let k = j; + while (true) { + const spacingLen = toolMarkupWhitespaceLikeLenAt(raw, k); + if (!spacingLen) { + break; + } + k += spacingLen; + } + let next = k; + let ok = false; + const sep = consumeToolMarkupSeparator(raw, next); + if (sep.ok) { + next = sep.next; + ok = true; + } else { + const dashLen = toolMarkupDashLenAt(raw, next); + const underscoreLen = toolMarkupUnderscoreLenAt(raw, next); + if (dashLen) { + next += dashLen; + ok = true; + } else if (underscoreLen) { + next += underscoreLen; + ok = true; + } + } + if (!ok) { + return { next: idx, ok: false }; + } + while (true) { + const spacingLen = toolMarkupWhitespaceLikeLenAt(raw, next); + if (!spacingLen) { + break; + } + next += spacingLen; + } + if (!hasToolMarkupNamePrefix(raw, next)) { + return { next: idx, ok: false }; + } + return { next, ok: true }; +} + +function consumeToolMarkupPrefixSegment(raw, idx) { + if (idx < 0 || idx >= raw.length) { + return { next: idx, ok: false }; + } + const normalized = normalizeFullwidthASCIIChar(raw[idx]); + if (/^[A-Za-z0-9]$/.test(normalized)) { + return { next: idx + 1, ok: true }; + } + return { next: idx, ok: false }; +} + +function hasToolMarkupNamePrefix(raw, start) { + for (const name of TOOL_MARKUP_NAMES) { + if (consumeToolKeyword(raw, start, name.raw).ok) { + return true; + } + if (hasConfusablePartialKeywordPrefix(raw, start, name.raw)) { + return true; + } + } + return false; +} + +function hasConfusablePartialKeywordPrefix(raw, start, keyword) { + if (start < 0 || start >= raw.length) { + return false; + } + let idx = start; + let matched = 0; + while (matched < keyword.length && idx < raw.length) { + idx = skipToolMarkupIgnorables(raw, idx); + if (idx >= raw.length) { + break; + } + const expected = keyword[matched]; + if (expected === '_') { + const underscoreLen = toolMarkupUnderscoreLenAt(raw, idx); + if (!underscoreLen) { + return false; + } + idx += underscoreLen; + matched += 1; + continue; + } + if (expected === '-') { + const dashLen = toolMarkupDashLenAt(raw, idx); + if (!dashLen) { + return false; + } + idx += dashLen; + matched += 1; + continue; + } + const cp = raw.codePointAt(idx); + const ch = String.fromCodePoint(cp); + const folded = foldToolKeywordRune(ch); + if (!folded || folded !== expected.toLowerCase()) { + return false; + } + idx += ch.length; + matched += 1; + } + return matched > 0 && matched < keyword.length && idx === raw.length; +} + +function matchToolMarkupName(raw, start, dsmlLike) { + for (const name of TOOL_MARKUP_NAMES) { + if (name.dsmlOnly && !dsmlLike) { + continue; + } + const matched = consumeToolKeyword(raw, start, name.raw); + if (matched.ok) { + return { name: name.canonical, len: matched.next - start }; + } + } + return { name: '', len: 0 }; +} + +function consumeToolMarkupSeparator(raw, idx) { + idx = skipToolMarkupIgnorables(raw, idx); + if (idx >= raw.length) { + return { next: idx, ok: false }; + } + const cp = raw.codePointAt(idx); + const ch = String.fromCodePoint(cp); + if (!isToolMarkupSeparator(ch)) { + return { next: idx, ok: false }; + } + return { next: idx + ch.length, ok: true }; +} + +function hasToolMarkupBoundary(text, idx) { + idx = skipToolMarkupIgnorables(text, idx); + if (idx >= text.length) { + return true; + } + if (toolMarkupWhitespaceLikeLenAt(text, idx) > 0) { + return true; + } + if (consumeToolMarkupClosingSlash(text, idx).closing) { + return true; + } + return xmlTagEndDelimiterLenAt(text, idx) > 0; +} + +function consumeToolMarkupLessThan(raw, idx) { + idx = skipToolMarkupIgnorables(raw, idx); + if (idx < 0 || idx >= raw.length) { + return { next: idx, ok: false }; + } + const delimLen = xmlTagStartDelimiterLenAt(raw, idx); + if (!delimLen) { + return { next: idx, ok: false }; + } + return { next: idx + delimLen, ok: true }; +} + +function canonicalizeToolCallCandidateSpans(text) { + const raw = toStringSafe(text); + if (!raw) { + return ''; + } + let out = ''; + for (let i = 0; i < raw.length;) { + const skipped = skipXmlIgnoredSection(raw, i); + if (skipped.blocked) { + out += raw.slice(i); + break; + } + if (skipped.advanced) { + out += raw.slice(i, skipped.next); + i = skipped.next; + continue; + } + const spanEnd = markdownCodeSpanEnd(raw, i); + if (spanEnd.ok) { + out += raw.slice(i, spanEnd.end); + i = spanEnd.end; + continue; + } + const tag = scanToolMarkupTagAt(raw, i); + if (!tag) { + out += raw[i]; + i += 1; + continue; + } + out += canonicalizeRecognizedToolMarkupTag(raw.slice(tag.start, tag.end + 1), tag); + i = tag.end + 1; + } + return out; +} + +function canonicalizeRecognizedToolMarkupTag(rawTag, tag) { + const raw = toStringSafe(rawTag); + if (!raw || !tag) { + return raw; + } + let idx = 0; + const startLen = xmlTagStartDelimiterLenAt(raw, idx); + if (startLen > 0) { + idx += startLen; + } + while (idx < raw.length) { + idx = skipToolMarkupIgnorables(raw, idx); + const delimLen = xmlTagStartDelimiterLenAt(raw, idx); + if (!delimLen) { + break; + } + idx += delimLen; + } + idx = skipToolMarkupIgnorables(raw, idx); + if (tag.closing) { + const slash = consumeToolMarkupClosingSlash(raw, idx); + if (slash.closing) { + idx = slash.next; + } + } + const prefix = consumeToolMarkupNamePrefix(raw, raw.toLowerCase(), idx); + idx = prefix.next; + const nameMatch = consumeToolKeyword(raw, idx, rawNameForTag(tag)); + const afterName = nameMatch.ok ? nameMatch.next : idx; + const attrs = parseCanonicalToolMarkupAttrs(raw, afterName); + + let out = '<'; + if (tag.closing) { + out += '/'; + } + if (tag.dsmlLike) { + out += '|DSML|'; + } + out += tag.name; + for (const attr of attrs) { + if (!attr || !attr.key) { + continue; + } + out += ` ${attr.key}="${quoteCanonicalXMLAttrValue(attr.value)}"`; + } + if (tag.selfClosing) { + out += '/'; + } + out += '>'; + return out; +} + +function parseCanonicalToolMarkupAttrs(rawTag, startIdx) { + const raw = toStringSafe(rawTag); + let idx = Math.max(0, startIdx || 0); + const out = []; + while (idx < raw.length) { + idx = skipToolMarkupIgnorables(raw, idx); + if (idx >= raw.length) { + break; + } + const spacingLen = toolMarkupWhitespaceLikeLenAt(raw, idx); + if (spacingLen > 0) { + idx += spacingLen; + continue; + } + if (xmlTagEndDelimiterLenAt(raw, idx) > 0) { + break; + } + if (consumeToolMarkupPipe(raw, idx).ok) { + idx = consumeToolMarkupPipe(raw, idx).next; + continue; + } + if (consumeToolMarkupClosingSlash(raw, idx).closing) { + idx = consumeToolMarkupClosingSlash(raw, idx).next; + continue; + } + + const keyStart = idx; + while (idx < raw.length) { + idx = skipToolMarkupIgnorables(raw, idx); + if (idx >= raw.length) { + break; + } + if (toolMarkupWhitespaceLikeLenAt(raw, idx) > 0) { + break; + } + if (toolMarkupEqualsLenAt(raw, idx) > 0 || xmlTagEndDelimiterLenAt(raw, idx) > 0) { + break; + } + if (consumeToolMarkupPipe(raw, idx).ok || consumeToolMarkupClosingSlash(raw, idx).closing) { + break; + } + const cp = raw.codePointAt(idx); + idx += cp > 0xFFFF ? 2 : 1; + } + const key = normalizeCanonicalToolAttrKey(raw.slice(keyStart, idx)); + + idx = skipToolMarkupIgnorables(raw, idx); + while (idx < raw.length) { + const wsLen = toolMarkupWhitespaceLikeLenAt(raw, idx); + if (!wsLen) { + break; + } + idx += wsLen; + idx = skipToolMarkupIgnorables(raw, idx); + } + const equalsLen = toolMarkupEqualsLenAt(raw, idx); + if (!equalsLen) { + continue; + } + idx += equalsLen; + idx = skipToolMarkupIgnorables(raw, idx); + while (idx < raw.length) { + const wsLen = toolMarkupWhitespaceLikeLenAt(raw, idx); + if (!wsLen) { + break; + } + idx += wsLen; + idx = skipToolMarkupIgnorables(raw, idx); + } + if (!key) { + if (idx < raw.length) { + const cp = raw.codePointAt(idx); + idx += cp > 0xFFFF ? 2 : 1; + } + continue; + } + + let value = ''; + const quote = xmlQuotePairAt(raw, idx); + if (quote.len) { + const valueStart = idx + quote.len; + idx = valueStart; + while (idx < raw.length) { + const closeLen = xmlQuoteCloseDelimiterLenAt(raw, idx, quote.close); + if (closeLen) { + value = raw.slice(valueStart, idx); + idx += closeLen; + break; + } + const cp = raw.codePointAt(idx); + idx += cp > 0xFFFF ? 2 : 1; + } + } else { + const valueStart = idx; + while (idx < raw.length) { + if (toolMarkupWhitespaceLikeLenAt(raw, idx) > 0 || xmlTagEndDelimiterLenAt(raw, idx) > 0 || toolMarkupEqualsLenAt(raw, idx) > 0) { + break; + } + if (consumeToolMarkupPipe(raw, idx).ok || consumeToolMarkupClosingSlash(raw, idx).closing) { + break; + } + const cp = raw.codePointAt(idx); + idx += cp > 0xFFFF ? 2 : 1; + } + value = raw.slice(valueStart, idx); + } + out.push({ key, value }); + } + return out; +} + +function normalizeCanonicalToolAttrKey(rawKey) { + const trimmed = toStringSafe(removeToolMarkupIgnorables(rawKey)).trim(); + if (!trimmed) { + return ''; + } + const matched = consumeToolKeyword(trimmed, 0, 'name'); + return matched.ok && skipToolMarkupIgnorables(trimmed, matched.next) === trimmed.length ? 'name' : ''; +} + +function quoteCanonicalXMLAttrValue(rawValue) { + return toStringSafe(rawValue).replace(/"/g, '"'); +} + +function removeToolMarkupIgnorables(rawValue) { + const raw = toStringSafe(rawValue); + let out = ''; + for (let i = 0; i < raw.length;) { + const ignorableLen = toolMarkupIgnorableLenAt(raw, i); + if (ignorableLen) { + i += ignorableLen; + continue; + } + const cp = raw.codePointAt(i); + const ch = String.fromCodePoint(cp); + out += ch; + i += ch.length; + } + return out; +} + +function skipToolMarkupIgnorables(text, idx) { + const raw = toStringSafe(text); + let pos = Math.max(0, idx || 0); + while (pos < raw.length) { + const next = toolMarkupIgnorableLenAt(raw, pos); + if (!next) { + break; + } + pos += next; + } + return pos; +} + +function toolMarkupIgnorableLenAt(text, idx) { + const raw = toStringSafe(text); + if (idx < 0 || idx >= raw.length) { + return 0; + } + const cp = raw.codePointAt(idx); + if (cp === undefined) { + return 0; + } + const ch = String.fromCodePoint(cp); + const isFormat = /[\u00AD\u200B-\u200F\u202A-\u202E\u2060-\u206F\uFE00-\uFE0F\uFEFF]/u.test(ch); + const isControl = /[\u0000-\u0008\u000B\u000C\u000E-\u001F\u007F-\u009F]/u.test(ch); + return isFormat || isControl ? ch.length : 0; +} + +function toolMarkupEqualsLenAt(text, idx) { + const raw = toStringSafe(text); + const pos = skipToolMarkupIgnorables(raw, idx); + for (const variant of ['=', '=', '﹦', '꞊']) { + if (raw.startsWith(variant, pos)) { + return (pos + variant.length) - idx; + } + } + return 0; +} + +function toolMarkupDashLenAt(text, idx) { + const raw = toStringSafe(text); + const pos = skipToolMarkupIgnorables(raw, idx); + for (const variant of ['-', '‐', '‑', '‒', '–', '—', '―', '−', '﹣', '-']) { + if (raw.startsWith(variant, pos)) { + return (pos + variant.length) - idx; + } + } + return 0; +} + +function toolMarkupUnderscoreLenAt(text, idx) { + const raw = toStringSafe(text); + const pos = skipToolMarkupIgnorables(raw, idx); + for (const variant of ['_', '_', '﹍', '﹎', '﹏']) { + if (raw.startsWith(variant, pos)) { + return (pos + variant.length) - idx; + } + } + return 0; +} + +function consumeToolKeyword(text, idx, keyword) { + const raw = toStringSafe(text); + let next = idx; + for (const ch of keyword.toLowerCase()) { + next = skipToolMarkupIgnorables(raw, next); + if (next >= raw.length) { + return { next: idx, ok: false }; + } + if (ch === '_') { + const len = toolMarkupUnderscoreLenAt(raw, next); + if (!len) { + return { next: idx, ok: false }; + } + next += len; + continue; + } + if (ch === '-') { + const len = toolMarkupDashLenAt(raw, next); + if (!len) { + return { next: idx, ok: false }; + } + next += len; + continue; + } + const cp = raw.codePointAt(next); + const folded = foldToolKeywordRune(String.fromCodePoint(cp)); + if (!folded || folded !== ch) { + return { next: idx, ok: false }; + } + next += cp > 0xFFFF ? 2 : 1; + } + return { next, ok: true }; +} + +function foldToolKeywordRune(ch) { + if (!ch) { + return ''; + } + const cp = ch.codePointAt(0); + if (cp >= 0xFF21 && cp <= 0xFF3A) { + return String.fromCharCode(cp - 0xFEE0).toLowerCase(); + } + if (cp >= 0xFF41 && cp <= 0xFF5A) { + return String.fromCharCode(cp - 0xFEE0); + } + const lower = ch.toLowerCase(); + if ('acdeiklmnoprstv'.includes(lower)) { + return lower; + } + const mapped = { + 'а': 'a', + 'α': 'a', + 'с': 'c', + 'ϲ': 'c', + 'ԁ': 'd', + 'ⅾ': 'd', + 'е': 'e', + 'ε': 'e', + 'і': 'i', + 'ι': 'i', + 'ı': 'i', + 'к': 'k', + 'κ': 'k', + 'ⅼ': 'l', + 'м': 'm', + 'μ': 'm', + 'ո': 'n', + 'о': 'o', + 'ο': 'o', + 'р': 'p', + 'ρ': 'p', + 'ѕ': 's', + 'т': 't', + 'τ': 't', + 'ν': 'v', + 'ѵ': 'v', + 'ⅴ': 'v', + }; + return mapped[lower] || ''; +} + +function toolMarkupWhitespaceLikeLenAt(text, idx) { + const raw = toStringSafe(text); + const pos = skipToolMarkupIgnorables(raw, idx); + if (pos < 0 || pos >= raw.length) { + return 0; + } + if ([' ', '\t', '\n', '\r'].includes(raw[pos])) { + return (pos + 1) - idx; + } + if (raw.startsWith('▁', pos)) { + return (pos + '▁'.length) - idx; + } + const cp = raw.codePointAt(pos); + const ch = String.fromCodePoint(cp); + return /\s/u.test(ch) ? (pos + ch.length) - idx : 0; +} + +function consumeToolMarkupPipe(raw, idx) { + const pos = skipToolMarkupIgnorables(raw, idx); + if (pos >= raw.length) { + return { next: idx, ok: false }; + } + for (const variant of ['|', '│', '∣', '❘', 'ǀ', '│']) { + if (raw.startsWith(variant, pos)) { + return { next: pos + variant.length, ok: true }; + } + } + return { next: idx, ok: false }; +} + +function consumeToolMarkupClosingSlash(raw, idx) { + const pos = skipToolMarkupIgnorables(raw, idx); + if (pos >= raw.length) { + return { next: idx, closing: false }; + } + for (const variant of ['/', '/', '∕', '⁄', '⧸']) { + if (raw.startsWith(variant, pos)) { + return { next: pos + variant.length, closing: true }; + } + } + return { next: idx, closing: false }; +} + +function xmlTagStartDelimiterLenAt(text, idx) { + const raw = toStringSafe(text); + const pos = skipToolMarkupIgnorables(raw, idx); + if (pos < 0 || pos >= raw.length) { + return 0; + } + for (const variant of ['<', '<', '﹤', '〈']) { + if (raw.startsWith(variant, pos)) { + return (pos + variant.length) - idx; + } + } + return 0; +} + +function xmlTagEndDelimiterLenAt(text, idx) { + const raw = toStringSafe(text); + const pos = skipToolMarkupIgnorables(raw, idx); + if (pos < 0 || pos >= raw.length) { + return 0; + } + for (const variant of ['>', '>', '﹥', '〉']) { + if (raw.startsWith(variant, pos)) { + return (pos + variant.length) - idx; + } + } + return 0; +} + +function xmlTagEndDelimiterLenEndingAt(text, end) { + const raw = toStringSafe(text); + if (end < 0 || end >= raw.length) { + return 0; + } + for (const variant of ['>', '>', '﹥', '〉']) { + if (end + 1 >= variant.length && raw.slice(end + 1 - variant.length, end + 1) === variant) { + return variant.length; + } + } + return 0; +} + +function xmlQuotePairAt(text, idx) { + const raw = toStringSafe(text); + const pos = skipToolMarkupIgnorables(raw, idx); + if (pos < 0 || pos >= raw.length) { + return { close: '', len: 0 }; + } + if (raw[pos] === '"') { + return { close: '"', len: (pos + 1) - idx }; + } + if (raw[pos] === "'") { + return { close: "'", len: (pos + 1) - idx }; + } + if (raw.startsWith('“', pos)) { + return { close: '”', len: (pos + '“'.length) - idx }; + } + if (raw.startsWith('‘', pos)) { + return { close: '’', len: (pos + '‘'.length) - idx }; + } + if (raw.startsWith('"', pos)) { + return { close: '"', len: (pos + '"'.length) - idx }; + } + if (raw.startsWith(''', pos)) { + return { close: ''', len: (pos + '''.length) - idx }; + } + if (raw.startsWith('„', pos)) { + return { close: '”', len: (pos + '„'.length) - idx }; + } + if (raw.startsWith('‟', pos)) { + return { close: '”', len: (pos + '‟'.length) - idx }; + } + return { close: '', len: 0 }; +} + +function xmlQuoteCloseDelimiterLenAt(text, idx, close) { + const raw = toStringSafe(text); + if (!close) { + return 0; + } + return raw.startsWith(close, idx) ? close.length : 0; +} + +function lastIndexOfToolMarkupStartDelimiter(raw) { + const text = toStringSafe(raw); + let best = -1; + for (const variant of ['<', '<', '﹤', '〈']) { + const idx = text.lastIndexOf(variant); + if (idx > best) { + best = idx; + } + } + return best; +} + +function containsXmlTagTerminator(raw) { + const text = toStringSafe(raw); + return text.includes('>') || text.includes('>') || text.includes('﹥') || text.includes('〉'); +} + +function findXmlTagEnd(text, from) { + const raw = toStringSafe(text); + let quote = ''; + for (let i = Math.max(0, from || 0); i < raw.length;) { + if (quote) { + const closeLen = xmlQuoteCloseDelimiterLenAt(raw, i, quote); + if (closeLen) { + quote = ''; + i += closeLen; + continue; + } + const cp = raw.codePointAt(i); + i += cp > 0xFFFF ? 2 : 1; + continue; + } + const nextQuote = xmlQuotePairAt(raw, i); + if (nextQuote.len) { + quote = nextQuote.close; + i += nextQuote.len; + continue; + } + const endLen = xmlTagEndDelimiterLenAt(raw, i); + if (endLen > 0) { + return i + endLen - 1; + } + const cp = raw.codePointAt(i); + i += cp > 0xFFFF ? 2 : 1; + } + return -1; +} + +function hasXmlTagBoundary(text, idx) { + const pos = skipToolMarkupIgnorables(text, idx); + if (pos >= text.length) { + return true; + } + return toolMarkupWhitespaceLikeLenAt(text, pos) > 0 + || consumeToolMarkupClosingSlash(text, pos).closing + || xmlTagEndDelimiterLenAt(text, pos) > 0; +} + +function isSelfClosingXmlTag(startTag) { + const trimmed = toStringSafe(startTag).trim(); + return trimmed.endsWith('/') || trimmed.endsWith('/'); +} + +function normalizeFullwidthASCIIChar(ch) { + if (!ch) { + return ch; + } + if (ch === '〈') { + return '<'; + } + if (ch === '〉') { + return '>'; + } + if (ch === '“' || ch === '”') { + return '"'; + } + if (ch === '‘' || ch === '’') { + return "'"; + } + const code = ch.charCodeAt(0); + if (code >= 0xff01 && code <= 0xff5e) { + return String.fromCharCode(code - 0xfee0); + } + return ch; +} + +function normalizedASCIITailAt(raw, start) { + let out = ''; + for (let i = Math.max(0, start || 0); i < raw.length; i += 1) { + const ch = normalizeFullwidthASCIIChar(raw[i]).toLowerCase(); + if (ch.charCodeAt(0) > 0x7f) { + break; + } + out += ch; + } + return out; +} + +function matchNormalizedASCII(raw, start, expected) { + let idx = start; + for (let j = 0; j < expected.length; j += 1) { + if (idx >= raw.length) { + return { ok: false, len: 0 }; + } + const ch = normalizeFullwidthASCIIChar(raw[idx]).toLowerCase(); + if (ch !== expected[j].toLowerCase()) { + return { ok: false, len: 0 }; + } + idx += 1; + } + return { ok: true, len: idx - start }; +} + +function normalizeToolMarkupTagTailForXML(tail) { + let out = ''; + const raw = typeof tail === 'string' ? tail : String(tail || ''); + let quote = ''; + for (let i = 0; i < raw.length; i += 1) { + const ch = raw[i]; + const normalized = normalizeFullwidthASCIIChar(ch); + if (quote) { + out += normalized; + if (normalized === quote) { + quote = ''; + } + } else if (normalized === '"' || normalized === "'") { + quote = normalized; + out += normalized; + } else if (normalized === '|' || normalized === '!') { + let j = i + 1; + while (j < raw.length && [' ', '\t', '\r', '\n'].includes(raw[j])) { + j += 1; + } + if (normalizeFullwidthASCIIChar(raw[j] || '') !== '>') { + out += normalized; + } + } else if (['>', '/', '='].includes(normalized)) { + out += normalized; + } else { + out += ch; + } + } + return out; +} + +function parseMarkupInput(raw) { + const s = toStringSafe(raw).trim(); + if (!s) { + return {}; + } + // Prioritize XML-style KV tags (e.g., val) + const kv = unwrapItemOnlyMarkupValue(parseMarkupKVObject(s)); + if (Array.isArray(kv)) { + return kv; + } + if (kv && typeof kv === 'object' && Object.keys(kv).length > 0) { + return kv; + } + + // Fallback to JSON parsing + const parsed = parseToolCallInput(s); + if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { + if (Object.keys(parsed).length > 0) { + return parsed; + } + } + + return { _raw: extractRawTagValue(s) }; +} + +function parseMarkupKVObject(text) { + const raw = toStringSafe(text).trim(); + if (!raw) { + return {}; + } + const out = {}; + for (const block of findGenericXmlElementBlocks(raw)) { + const key = toStringSafe(block.localName).trim(); + if (!key) { + continue; + } + const value = parseMarkupValue(block.body, key); + if (value === undefined || value === null) { + continue; + } + appendMarkupValue(out, key, value); + } + return out; +} + +function findGenericXmlElementBlocks(text) { + const source = toStringSafe(text); + if (!source) { + return []; + } + const out = []; + let pos = 0; + while (pos < source.length) { + const start = findGenericXmlStartTagOutsideCDATA(source, pos); + if (!start) { + break; + } + if (start.selfClosing) { + out.push({ + name: start.name, + localName: start.localName, + attrs: start.attrs, + body: '', + start: start.start, + end: start.end + 1, + }); + pos = start.end + 1; + continue; + } + const end = findMatchingGenericXmlEndTagOutsideCDATA(source, start.name, start.bodyStart); + if (!end) { + pos = start.bodyStart; + continue; + } + out.push({ + name: start.name, + localName: start.localName, + attrs: start.attrs, + body: source.slice(start.bodyStart, end.closeStart), + start: start.start, + end: end.closeEnd, + }); + pos = end.closeEnd; + } + return out; +} + +function findGenericXmlStartTagOutsideCDATA(text, from) { + const lower = text.toLowerCase(); + for (let i = Math.max(0, from || 0); i < text.length;) { + const skipped = skipXmlIgnoredSection(text, i); + if (skipped.blocked) { + return null; + } + if (skipped.advanced) { + i = skipped.next; + continue; + } + if (text[i] !== '<' || text[i + 1] === '/' || text[i + 1] === '!' || text[i + 1] === '?') { + i += 1; + continue; + } + const match = text.slice(i + 1).match(/^([A-Za-z_][A-Za-z0-9_.:-]*)/); + if (!match) { + i += 1; + continue; + } + const name = match[1]; + const nameEnd = i + 1 + name.length; + if (!hasXmlTagBoundary(text, nameEnd)) { + i += 1; + continue; + } + const tagEnd = findXmlTagEnd(text, nameEnd); + if (tagEnd < 0) { + return null; + } + return { + start: i, + end: tagEnd, + bodyStart: tagEnd + 1, + name, + localName: name.includes(':') ? name.slice(name.lastIndexOf(':') + 1) : name, + attrs: text.slice(nameEnd, tagEnd), + selfClosing: isSelfClosingXmlTag(text.slice(i, tagEnd)), + }; + } + return null; +} + +function findMatchingGenericXmlEndTagOutsideCDATA(text, name, from) { + const lower = text.toLowerCase(); + const needle = toStringSafe(name).toLowerCase(); + if (!needle) { + return null; + } + const openTarget = `<${needle}`; + const closeTarget = `')) { + const nested = unwrapItemOnlyMarkupValue(parseMarkupInput(s)); + if (Array.isArray(nested)) { + return nested; + } + if (nested && typeof nested === 'object') { + const nestedArray = coerceArrayValue(nested, paramName); + if (nestedArray.ok) { + return nestedArray.value; + } + if (isOnlyRawValue(nested)) { + const rawValue = toStringSafe(nested._raw); + const looseArray = parseLooseJSONArrayValue(rawValue, paramName); + return looseArray.ok ? looseArray.value : rawValue; + } + return nested; + } + } + + const literal = parseJSONLiteralValue(s); + if (literal.ok) { + const literalArray = coerceArrayValue(literal.value, paramName); + if (literalArray.ok) { + return literalArray.value; + } + return literal.value; + } + const looseArray = parseLooseJSONArrayValue(s, paramName); + if (looseArray.ok) { + return looseArray.value; + } + return s; +} + +function parseStructuredCDATAParameterValue(paramName, raw) { + if (preservesCDATAStringParameter(paramName)) { + return { ok: false, value: null }; + } + const normalized = normalizeCDATAForStructuredParse(raw); + if (!normalized.includes('<') || !normalized.includes('>')) { + return { ok: false, value: null }; + } + if (!cdataFragmentLooksExplicitlyStructured(normalized)) { + return { ok: false, value: null }; + } + const parsed = parseMarkupInput(normalized); + if (Array.isArray(parsed)) { + return { ok: true, value: parsed }; + } + if (parsed && typeof parsed === 'object' && !isOnlyRawValue(parsed) && Object.keys(parsed).length > 0) { + return { ok: true, value: parsed }; + } + return { ok: false, value: null }; +} + +function normalizeCDATAForStructuredParse(raw) { + return unescapeHtml(toStringSafe(raw).replace(//gi, '\n').trim()); +} + +function cdataFragmentLooksExplicitlyStructured(raw) { + const blocks = findGenericXmlElementBlocks(raw); + if (blocks.length === 0) { + return false; + } + if (blocks.length > 1) { + return true; + } + const block = blocks[0]; + if (toStringSafe(block.localName).trim().toLowerCase() === 'item') { + return true; + } + return findGenericXmlElementBlocks(block.body).length > 0; +} + +function preservesCDATAStringParameter(name) { + return new Set([ + 'content', + 'file_content', + 'text', + 'prompt', + 'query', + 'command', + 'cmd', + 'script', + 'code', + 'old_string', + 'new_string', + 'pattern', + 'path', + 'file_path', + ]).has(toStringSafe(name).trim().toLowerCase()); +} + +function unwrapItemOnlyMarkupValue(value) { + if (Array.isArray(value)) { + return value.map(unwrapItemOnlyMarkupValue); + } + if (!value || typeof value !== 'object') { + return value; + } + const keys = Object.keys(value); + if (keys.length === 1 && keys[0] === 'item') { + const items = unwrapItemOnlyMarkupValue(value.item); + return Array.isArray(items) ? items : [items]; + } + const out = {}; + for (const key of keys) { + out[key] = unwrapItemOnlyMarkupValue(value[key]); + } + return out; +} + +function extractRawTagValue(inner) { + const s = toStringSafe(inner).trim(); + if (!s) { + return ''; + } + + // 1. Check for CDATA + const cdata = extractStandaloneCDATA(s); + if (cdata.ok) { + return cdata.value; + } + + // 2. Fallback to unescaping standard HTML entities + // Note: we avoid broad tag stripping here to preserve user content (like < symbols in code) + return unescapeHtml(inner); +} + +function unescapeHtml(safe) { + if (!safe) return ''; + return safe.replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/'/g, "'") + .replace(/'/g, "'"); +} + +function extractStandaloneCDATA(inner) { + const s = toStringSafe(inner).trim(); + const openLen = toolCDATAOpenLenAt(s, 0); + if (!openLen) { + return { ok: false, value: '' }; + } + const closeStart = findTrailingToolCDATACloseStart(s); + if (closeStart >= openLen) { + return { ok: true, value: s.slice(openLen, closeStart) }; + } + const end = findToolCDATAEnd(s, openLen); + if (end >= 0) { + return { ok: true, value: s.slice(openLen, end) }; + } + return { ok: true, value: s.slice(openLen) }; +} + +function findStandaloneCDATAEnd(text, from) { + const raw = toStringSafe(text); + let best = { index: -1, len: 0 }; + for (let searchFrom = Math.max(0, from || 0); searchFrom < raw.length;) { + const index = findToolCDATAEnd(raw, searchFrom); + if (index < 0) { + break; + } + const len = toolCDATACloseLenAt(raw, index); + const closeEnd = index + len; + if (!raw.slice(closeEnd).trim()) { + best = { index, len }; + } + searchFrom = closeEnd; + } + return best; +} + +function parseJSONLiteralValue(raw) { + const s = toStringSafe(raw).trim(); + if (!s) { + return { ok: false, value: null }; + } + if (!['{', '[', '"', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 't', 'f', 'n'].includes(s[0])) { + return { ok: false, value: null }; + } + try { + return { ok: true, value: JSON.parse(s) }; + } catch (_err) { + return { ok: false, value: null }; + } +} + +function parseLooseJSONArrayValue(raw, paramName = '') { + if (preservesCDATAStringParameter(paramName)) { + return { ok: false, value: null }; + } + const s = toStringSafe(raw).trim(); + if (!s) { + return { ok: false, value: null }; + } + const candidate = parseLooseJSONArrayCandidate(s, paramName); + if (candidate.ok) { + return candidate; + } + + const segments = splitTopLevelJSONValues(s); + if (segments.length < 2) { + return { ok: false, value: null }; + } + + const out = []; + for (const segment of segments) { + const parsed = parseLooseArrayElementValue(segment); + if (!parsed.ok) { + return { ok: false, value: null }; + } + out.push(parsed.value); + } + return { ok: true, value: out }; +} + +function parseLooseJSONArrayCandidate(raw, paramName = '') { + const parsed = parseLooseArrayElementValue(raw); + if (!parsed.ok) { + return { ok: false, value: null }; + } + return coerceArrayValue(parsed.value, paramName); +} + +function parseLooseArrayElementValue(raw) { + const s = toStringSafe(raw).trim(); + if (!s) { + return { ok: false, value: null }; + } + + const literal = parseJSONLiteralValue(s); + if (literal.ok) { + return literal; + } + + const repairedBackslashes = repairInvalidJSONBackslashes(s); + if (repairedBackslashes !== s) { + try { + const parsed = JSON.parse(repairedBackslashes); + return { ok: true, value: parsed }; + } catch (_err) { + // Fall through. + } + } + + const repairedLoose = repairLooseJSON(s); + if (repairedLoose !== s) { + try { + const parsed = JSON.parse(repairedLoose); + return { ok: true, value: parsed }; + } catch (_err) { + // Fall through. + } + } + + if (s.includes('<') && s.includes('>')) { + const parsed = parseMarkupInput(s); + if (Array.isArray(parsed)) { + return { ok: true, value: parsed }; + } + if (parsed && typeof parsed === 'object') { + return { ok: true, value: parsed }; + } + } + + return { ok: false, value: null }; +} + +function coerceArrayValue(value, paramName = '') { + if (Array.isArray(value)) { + return { ok: true, value }; + } + if (!value || typeof value !== 'object') { + return { ok: false, value: null }; + } + + const keys = Object.keys(value); + if (keys.length !== 1) { + return { ok: false, value: null }; + } + + if (Object.prototype.hasOwnProperty.call(value, 'item')) { + const items = value.item; + const nested = coerceArrayValue(items, ''); + return nested.ok ? nested : { ok: true, value: [items] }; + } + + if (paramName && Object.prototype.hasOwnProperty.call(value, paramName)) { + const nested = coerceArrayValue(value[paramName], ''); + if (nested.ok) { + return nested; + } + } + + return { ok: false, value: null }; +} + +function splitTopLevelJSONValues(raw) { + const s = toStringSafe(raw).trim(); + if (!s) { + return []; + } + + const values = []; + let start = 0; + let depth = 0; + let inString = false; + let escaped = false; + + for (let i = 0; i < s.length; i += 1) { + const ch = s[i]; + if (inString) { + if (escaped) { + escaped = false; + continue; + } + if (ch === '\\') { + escaped = true; + continue; + } + if (ch === '"') { + inString = false; + } + continue; + } + if (ch === '"') { + inString = true; + continue; + } + if (ch === '{' || ch === '[') { + depth += 1; + continue; + } + if (ch === '}' || ch === ']') { + if (depth > 0) { + depth -= 1; + } + continue; + } + if (ch === ',' && depth === 0) { + const segment = s.slice(start, i).trim(); + if (!segment) { + return []; + } + values.push(segment); + start = i + 1; + } + } + + const last = s.slice(start).trim(); + if (!last) { + return []; + } + values.push(last); + return values.length > 1 ? values : []; +} + +function repairInvalidJSONBackslashes(s) { + if (!s || !s.includes('\\')) { + return s; + } + + let out = ''; + for (let i = 0; i < s.length; i += 1) { + const ch = s[i]; + if (ch !== '\\') { + out += ch; + continue; + } + if (i + 1 < s.length) { + const next = s[i + 1]; + if ('"\\/bfnrt'.includes(next)) { + out += `\\${next}`; + i += 1; + continue; + } + if (next === 'u' && i + 5 < s.length) { + let isHex = true; + for (let j = 1; j <= 4; j += 1) { + const r = s[i + 1 + j]; + if (!/[0-9a-fA-F]/.test(r)) { + isHex = false; + break; + } + } + if (isHex) { + out += `\\u${s.slice(i + 2, i + 6)}`; + i += 5; + continue; + } + } + } + out += '\\\\'; + } + return out; +} + +function repairLooseJSON(s) { + const raw = toStringSafe(s).trim(); + if (!raw) { + return raw; + } + let out = raw.replace(/([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:/g, '$1"$2":'); + out = out.replace(/(:\s*)(\{(?:[^{}]|\{[^{}]*\})*\}(?:\s*,\s*\{(?:[^{}]|\{[^{}]*\})*\})+)/g, '$1[$2]'); + return out; +} + +function sanitizeLooseCDATA(text) { + const raw = toStringSafe(text); + if (!raw) { + return ''; + } + + let out = ''; + let pos = 0; + let changed = false; + while (pos < raw.length) { + const start = indexToolCDATAOpen(raw, pos); + if (start < 0) { + out += raw.slice(pos); + break; + } + const openLen = toolCDATAOpenLenAt(raw, start); + const contentStart = start + openLen; + out += raw.slice(pos, start); + + const endRel = findToolCDATAEnd(raw, contentStart); + if (endRel >= 0) { + const end = endRel + toolCDATACloseLenAt(raw, endRel); + out += raw.slice(start, end); + pos = end; + continue; + } + + changed = true; + out += raw.slice(contentStart); + pos = raw.length; + } + + return changed ? out : raw; +} + +function hasRepairableXMLToolCallsWrapper(text) { + const raw = toStringSafe(text).trim(); + if (!raw || firstToolMarkupTagByName(raw, 'tool_calls', false)) { + return false; + } + const invoke = firstToolMarkupTagByName(raw, 'invoke', false); + if (!invoke) { + return false; + } + const close = lastToolMarkupTagByName(raw, 'tool_calls', true); + if (!close) { + return false; + } + return invoke.start < close.start; +} + +function repairMissingXMLToolCallsOpeningWrapper(text) { + const raw = toStringSafe(text); + if (firstToolMarkupTagByName(raw, 'tool_calls', false)) { + return raw; + } + const invoke = firstToolMarkupTagByName(raw, 'invoke', false); + const close = lastToolMarkupTagByName(raw, 'tool_calls', true); + if (!invoke || !close || invoke.start >= close.start) { + return raw; + } + return `${raw.slice(0, invoke.start)}${raw.slice(invoke.start, close.start)}${raw.slice(close.end + 1)}`; +} + +function firstToolMarkupTagByName(text, name, closing) { + const raw = toStringSafe(text); + for (let searchFrom = 0; searchFrom < raw.length;) { + const tag = findToolMarkupTagOutsideIgnored(raw, searchFrom); + if (!tag) { + break; + } + if (tag.name === name && tag.closing === closing) { + return tag; + } + searchFrom = tag.end + 1; + } + return null; +} + +function lastToolMarkupTagByName(text, name, closing) { + const raw = toStringSafe(text); + let last = null; + for (let searchFrom = 0; searchFrom < raw.length;) { + const tag = findToolMarkupTagOutsideIgnored(raw, searchFrom); + if (!tag) { + break; + } + if (tag.name === name && tag.closing === closing) { + last = tag; + } + searchFrom = tag.end + 1; + } + return last; +} + +function rawNameForTag(tag) { + for (const candidate of TOOL_MARKUP_NAMES) { + if (candidate.canonical === tag.name) { + return candidate.raw; + } + } + return tag.name || ''; +} + +function toolCDATAOpenLenAt(text, idx) { + const raw = toStringSafe(text); + const start = skipToolMarkupIgnorables(raw, idx); + const ltLen = xmlTagStartDelimiterLenAt(raw, start); + if (!ltLen) { + return 0; + } + let pos = start + ltLen; + for (let skipped = 0; skipped <= 4 && pos < raw.length; skipped += 1) { + pos = skipToolMarkupIgnorables(raw, pos); + if (raw[pos] === '[') { + pos += 1; + const keyword = consumeToolKeyword(raw, pos, 'cdata'); + if (!keyword.ok) { + return 0; + } + pos = skipToolMarkupIgnorables(raw, keyword.next); + if (raw[pos] !== '[') { + return 0; + } + pos += 1; + return pos - idx; + } + const cp = raw.codePointAt(pos); + if (cp === undefined) { + return 0; + } + const ch = String.fromCodePoint(cp); + if (!isToolMarkupSeparator(ch)) { + return 0; + } + pos += ch.length; + } + return 0; +} + +function toolCDATACloseLenAt(text, idx) { + const raw = toStringSafe(text); + const start = skipToolMarkupIgnorables(raw, idx); + if (raw[start] !== ']') { + return 0; + } + let pos = start + 1; + pos = skipToolMarkupIgnorables(raw, pos); + if (raw[pos] !== ']') { + return 0; + } + pos += 1; + const gtLen = xmlTagEndDelimiterLenAt(raw, pos); + return gtLen ? (pos + gtLen) - idx : 0; +} + +function findToolCDATAEnd(text, from) { + const raw = toStringSafe(text); + if (from < 0 || from >= raw.length) { + return -1; + } + let firstNonFenceEnd = -1; + for (let i = from; i < raw.length; i += 1) { + const closeLen = toolCDATACloseLenAt(raw, i); + if (!closeLen) { + continue; + } + const end = i; + if (cdataOffsetIsInsideMarkdownFence(raw.slice(from, end))) { + continue; + } + if (cdataEndLooksStructural(raw, end + closeLen)) { + return end; + } + if (firstNonFenceEnd < 0) { + firstNonFenceEnd = end; + } + i = end + closeLen - 1; + } + return firstNonFenceEnd; +} + +function indexToolCDATAOpen(text, from = 0) { + const raw = toStringSafe(text); + for (let i = Math.max(0, from || 0); i < raw.length; i += 1) { + if (toolCDATAOpenLenAt(raw, i)) { + return i; + } + } + return -1; +} + +function findTrailingToolCDATACloseStart(text) { + const raw = toStringSafe(text); + for (let i = raw.length - 1; i >= 0; i -= 1) { + const closeLen = toolCDATACloseLenAt(raw, i); + if (closeLen && i + closeLen === raw.length) { + return i; + } + } + return -1; +} + +function cdataOffsetIsInsideMarkdownFence(fragment) { + const lines = toStringSafe(fragment).split('\n'); + let inFence = false; + let fenceChar = ''; + let fenceLen = 0; + for (const line of lines) { + const trimmed = line.replace(/^[ \t]+/, ''); + if (!inFence) { + const fence = parseFenceOpenLine(trimmed); + if (fence) { + inFence = true; + fenceChar = fence.ch; + fenceLen = fence.count; + } + continue; + } + if (isFenceCloseLine(trimmed, fenceChar, fenceLen)) { + inFence = false; + fenceChar = ''; + fenceLen = 0; + } + } + return inFence; +} + +function cdataEndLooksStructural(text, after) { + const raw = toStringSafe(text); + let pos = after; + while (pos < raw.length) { + const ch = raw[pos]; + if ([' ', '\t', '\r', '\n'].includes(ch)) { + pos += 1; + continue; + } + return raw.startsWith(' value !== undefined && value !== '') || ''; + } + return out; +} + +function parseToolCallInput(v) { + if (v == null) { + return {}; + } + if (typeof v === 'string') { + const raw = toStringSafe(v); + if (!raw) { + return {}; + } + try { + const parsed = JSON.parse(raw); + if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { + return parsed; + } + return { _raw: raw }; + } catch (_err) { + return { _raw: raw }; + } + } + if (typeof v === 'object' && !Array.isArray(v)) { + return v; + } + try { + const parsed = JSON.parse(JSON.stringify(v)); + if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) { + return parsed; + } + } catch (_err) { + return {}; + } + return {}; +} + +function appendMarkupValue(out, key, value) { + if (Object.prototype.hasOwnProperty.call(out, key)) { + const current = out[key]; + if (Array.isArray(current)) { + current.push(value); + return; + } + out[key] = [current, value]; + return; + } + out[key] = value; +} + +function isOnlyRawValue(obj) { + if (!obj || typeof obj !== 'object' || Array.isArray(obj)) { + return false; + } + const keys = Object.keys(obj); + return keys.length === 1 && keys[0] === '_raw'; +} + +module.exports = { + stripFencedCodeBlocks, + stripMarkdownCodeSpans, + parseMarkupToolCalls, + normalizeDSMLToolCallMarkup, + containsToolMarkupSyntaxOutsideIgnored, + containsToolCallWrapperSyntaxOutsideIgnored, + hasRepairableXMLToolCallsWrapper, + findToolMarkupTagOutsideIgnored, + findMatchingToolMarkupClose, + findPartialToolMarkupStart, + indexToolCDATAOpen, + sanitizeLooseCDATA, +}; diff --git a/internal/js/helpers/stream-tool-sieve/sieve-xml.js b/internal/js/helpers/stream-tool-sieve/sieve-xml.js new file mode 100644 index 0000000000000000000000000000000000000000..6e2b1ed76e5f01aac606a11f973c52d71a22662e --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/sieve-xml.js @@ -0,0 +1,169 @@ +'use strict'; +const { parseToolCallsDetailed } = require('./parse'); +const { + findToolMarkupTagOutsideIgnored, + findMatchingToolMarkupClose, + findPartialToolMarkupStart, +} = require('./parse_payload'); + +function consumeXMLToolCapture(captured, toolNames, trimWrappingJSONFence) { + let anyOpenFound = false; + let best = null; + let rejected = null; + + // Scan every recognized wrapper occurrence. Prose can mention a wrapper tag + // before the actual tool block, including the same variant as the real block. + for (let searchFrom = 0; searchFrom < captured.length;) { + const openTag = findFirstToolTag(captured, searchFrom, 'tool_calls', false); + if (!openTag) { + break; + } + const closeTag = findMatchingToolMarkupClose(captured, openTag); + if (!closeTag) { + anyOpenFound = true; + searchFrom = openTag.end + 1; + continue; + } + const xmlBlock = captured.slice(openTag.start, closeTag.end + 1); + const prefixPart = captured.slice(0, openTag.start); + const suffixPart = captured.slice(closeTag.end + 1); + const parsed = parseToolCallsDetailed(xmlBlock, toolNames); + if (Array.isArray(parsed.calls) && parsed.calls.length > 0) { + const trimmedFence = trimWrappingJSONFence(prefixPart, suffixPart); + if (!best || openTag.start < best.start) { + best = { + start: openTag.start, + prefix: trimmedFence.prefix, + calls: parsed.calls, + suffix: trimmedFence.suffix, + }; + } + break; + } + if (parsed.sawToolCallSyntax) { + if (!rejected || openTag.start < rejected.start) { + rejected = { + start: openTag.start, + prefix: prefixPart + xmlBlock, + suffix: suffixPart, + }; + } + searchFrom = openTag.end + 1; + continue; + } + if (!rejected || openTag.start < rejected.start) { + rejected = { + start: openTag.start, + prefix: prefixPart + xmlBlock, + suffix: suffixPart, + }; + } + searchFrom = openTag.end + 1; + } + if (best) { + return { ready: true, prefix: best.prefix, calls: best.calls, suffix: best.suffix }; + } + if (anyOpenFound) { + // At least one opening tag was found but none had a matching close tag. + return { ready: false, prefix: '', calls: [], suffix: '' }; + } + if (rejected) { + // If this block failed to become a tool call, pass it through as text. + return { ready: true, prefix: rejected.prefix, calls: [], suffix: rejected.suffix }; + } + const invokeTag = findFirstToolTag(captured, 0, 'invoke', false); + if (invokeTag) { + const wrapperOpen = findFirstToolTag(captured, 0, 'tool_calls', false); + if (!wrapperOpen || wrapperOpen.start > invokeTag.start) { + const closeTag = findFirstToolTag(captured, invokeTag.start + 1, 'tool_calls', true); + if (closeTag && closeTag.start > invokeTag.start) { + const xmlBlock = '' + captured.slice(invokeTag.start, closeTag.end + 1); + const prefixPart = captured.slice(0, invokeTag.start); + const suffixPart = captured.slice(closeTag.end + 1); + const parsed = parseToolCallsDetailed(xmlBlock, toolNames); + if (Array.isArray(parsed.calls) && parsed.calls.length > 0) { + const trimmedFence = trimWrappingJSONFence(prefixPart, suffixPart); + return { + ready: true, + prefix: trimmedFence.prefix, + calls: parsed.calls, + suffix: trimmedFence.suffix, + }; + } + if (parsed.sawToolCallSyntax) { + return { ready: true, prefix: prefixPart + captured.slice(invokeTag.start, closeTag.end + 1), calls: [], suffix: suffixPart }; + } + return { ready: true, prefix: prefixPart + captured.slice(invokeTag.start, closeTag.end + 1), calls: [], suffix: suffixPart }; + } + } + } + return { ready: false, prefix: '', calls: [], suffix: '' }; +} + +function hasOpenXMLToolTag(captured) { + for (let pos = 0; pos < captured.length;) { + const tag = findFirstToolTag(captured, pos, 'tool_calls', false); + if (!tag) { + return false; + } + if (!findMatchingToolMarkupClose(captured, tag)) { + return true; + } + pos = tag.end + 1; + } + return false; +} + +function shouldKeepBareInvokeCapture(captured) { + const invokeTag = findFirstToolTag(captured, 0, 'invoke', false); + if (!invokeTag) { + return false; + } + const wrapperOpen = findFirstToolTag(captured, 0, 'tool_calls', false); + if (wrapperOpen && wrapperOpen.start <= invokeTag.start) { + return false; + } + const closeTag = findFirstToolTag(captured, invokeTag.start + 1, 'tool_calls', true); + if (closeTag && closeTag.start > invokeTag.start) { + return true; + } + const startEnd = invokeTag.end; + if (startEnd < 0) { + return true; + } + const body = captured.slice(startEnd + 1); + const trimmedBody = body.replace(/^[ \t\r\n]+/, ''); + if (!trimmedBody) { + return true; + } + const invokeCloseTag = findFirstToolTag(captured, startEnd + 1, 'invoke', true); + if (invokeCloseTag) { + return captured.slice(invokeCloseTag.end + 1).trim() === ''; + } + const paramTag = findFirstToolTag(body, 0, 'parameter', false); + if (paramTag && body.slice(0, paramTag.start).trim() === '') { + return true; + } + return trimmedBody.startsWith('{') || trimmedBody.startsWith('['); +} + +function findFirstToolTag(text, from, name, closing) { + for (let pos = Math.max(0, from || 0); pos < text.length;) { + const tag = findToolMarkupTagOutsideIgnored(text, pos); + if (!tag) { + return null; + } + if (tag.name === name && tag.closing === closing) { + return tag; + } + pos = tag.end + 1; + } + return null; +} + +module.exports = { + consumeXMLToolCapture, + hasOpenXMLToolTag, + shouldKeepBareInvokeCapture, + findPartialXMLToolTagStart: findPartialToolMarkupStart, +}; diff --git a/internal/js/helpers/stream-tool-sieve/sieve.js b/internal/js/helpers/stream-tool-sieve/sieve.js new file mode 100644 index 0000000000000000000000000000000000000000..961211c687f7dab3afb34ee1d3d4fa9f314864d4 --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/sieve.js @@ -0,0 +1,330 @@ +'use strict'; +const { + resetIncrementalToolState, + noteText, + insideCodeFenceWithState, +} = require('./state'); +const { trimWrappingJSONFence } = require('./jsonscan'); +const { + findToolMarkupTagOutsideIgnored, + sanitizeLooseCDATA, +} = require('./parse_payload'); +const { + consumeXMLToolCapture: consumeXMLToolCaptureImpl, + hasOpenXMLToolTag, + shouldKeepBareInvokeCapture, + findPartialXMLToolTagStart, +} = require('./sieve-xml'); +function processToolSieveChunk(state, chunk, toolNames) { + if (!state) { + return []; + } + if (chunk) { + state.pending += chunk; + } + const events = []; + while (true) { + if (Array.isArray(state.pendingToolCalls) && state.pendingToolCalls.length > 0) { + events.push({ type: 'tool_calls', calls: state.pendingToolCalls }); + state.pendingToolRaw = ''; + state.pendingToolCalls = []; + continue; + } + if (state.capturing) { + if (state.pending) { + state.capture += state.pending; + state.pending = ''; + } + const consumed = consumeToolCapture(state, toolNames); + if (!consumed.ready) { + break; + } + const captured = state.capture; + state.capture = ''; + state.capturing = false; + resetIncrementalToolState(state); + + if (Array.isArray(consumed.calls) && consumed.calls.length > 0) { + if (consumed.prefix) { + noteText(state, consumed.prefix); + events.push({ type: 'text', text: consumed.prefix }); + } + state.pendingToolRaw = captured; + state.pendingToolCalls = consumed.calls; + if (consumed.suffix) { + state.pending = consumed.suffix + state.pending; + } + continue; + } + if (consumed.prefix) { + noteText(state, consumed.prefix); + events.push({ type: 'text', text: consumed.prefix }); + } + if (consumed.suffix) { + state.pending += consumed.suffix; + } + continue; + } + const pending = state.pending || ''; + if (!pending) { + break; + } + const start = findToolSegmentStart(state, pending); + if (start === HOLD_TOOL_SEGMENT_START) { + break; + } + if (start >= 0) { + const prefix = pending.slice(0, start); + if (prefix) { + const resetMarkdownSpan = shouldResetUnclosedMarkdownPrefix(state, prefix, pending.slice(start)); + noteText(state, prefix); + if (resetMarkdownSpan) { + state.markdownCodeSpanTicks = 0; + } + events.push({ type: 'text', text: prefix }); + } + state.pending = ''; + state.capture += pending.slice(start); + state.capturing = true; + resetIncrementalToolState(state); + continue; + } + const [safe, hold] = splitSafeContentForToolDetection(state, pending); + if (!safe) { + break; + } + state.pending = hold; + noteText(state, safe); + events.push({ type: 'text', text: safe }); + } + return events; +} + +function flushToolSieve(state, toolNames) { + if (!state) { + return []; + } + const events = processToolSieveChunk(state, '', toolNames); + if (state.pending && Number.isInteger(state.markdownCodeSpanTicks) && state.markdownCodeSpanTicks > 0) { + state.markdownCodeSpanTicks = 0; + events.push(...processToolSieveChunk(state, '', toolNames)); + } + if (Array.isArray(state.pendingToolCalls) && state.pendingToolCalls.length > 0) { + events.push({ type: 'tool_calls', calls: state.pendingToolCalls }); + state.pendingToolRaw = ''; + state.pendingToolCalls = []; + } + if (state.capturing) { + const consumed = consumeToolCapture(state, toolNames); + if (consumed.ready) { + if (consumed.prefix) { + noteText(state, consumed.prefix); + events.push({ type: 'text', text: consumed.prefix }); + } + if (Array.isArray(consumed.calls) && consumed.calls.length > 0) { + events.push({ type: 'tool_calls', calls: consumed.calls }); + } + if (consumed.suffix) { + noteText(state, consumed.suffix); + events.push({ type: 'text', text: consumed.suffix }); + } + } else if (state.capture) { + const content = state.capture; + const recovered = sanitizeLooseCDATA(content); + if (recovered !== content) { + const recoveredResult = consumeXMLToolCaptureImpl(recovered, toolNames, trimWrappingJSONFence); + if (recoveredResult.ready && Array.isArray(recoveredResult.calls) && recoveredResult.calls.length > 0) { + if (recoveredResult.prefix) { + noteText(state, recoveredResult.prefix); + events.push({ type: 'text', text: recoveredResult.prefix }); + } + events.push({ type: 'tool_calls', calls: recoveredResult.calls }); + if (recoveredResult.suffix) { + noteText(state, recoveredResult.suffix); + events.push({ type: 'text', text: recoveredResult.suffix }); + } + } else { + noteText(state, content); + events.push({ type: 'text', text: content }); + } + } else { + noteText(state, content); + events.push({ type: 'text', text: content }); + } + } + state.capture = ''; + state.capturing = false; + resetIncrementalToolState(state); + } + if (state.pending) { + noteText(state, state.pending); + events.push({ type: 'text', text: state.pending }); + state.pending = ''; + } + return events; +} + +function splitSafeContentForToolDetection(state, s) { + const text = s || ''; + if (!text) { + return ['', '']; + } + // Only hold back partial XML tool tags. + const xmlIdx = findPartialXMLToolTagStart(text); + if (xmlIdx >= 0) { + if (insideCodeFenceWithState(state, text.slice(0, xmlIdx))) { + return [text, '']; + } + const markdown = markdownCodeSpanStateAt(state, text.slice(0, xmlIdx)); + if (markdown.ticks > 0) { + if (markdownCodeSpanCloses(text.slice(xmlIdx), markdown.ticks)) { + return [text, '']; + } + if (markdown.fromPrior) { + return ['', text]; + } + } + if (xmlIdx > 0) { + return [text.slice(0, xmlIdx), text.slice(xmlIdx)]; + } + return ['', text]; + } + return [text, '']; +} + +const HOLD_TOOL_SEGMENT_START = -2; + +function findToolSegmentStart(state, s) { + if (!s) { + return -1; + } + let offset = 0; + while (true) { + const tag = findToolMarkupTagOutsideIgnored(s, offset); + if (!tag) { + return -1; + } + if (insideCodeFenceWithState(state, s.slice(0, tag.start))) { + offset = tag.end + 1; + continue; + } + const markdown = markdownCodeSpanStateAt(state, s.slice(0, tag.start)); + if (markdown.ticks === 0) { + return tag.start; + } + if (markdownCodeSpanCloses(s.slice(tag.start), markdown.ticks)) { + offset = tag.end + 1; + continue; + } + if (markdown.fromPrior) { + return HOLD_TOOL_SEGMENT_START; + } + return tag.start; + } +} + +function markdownCodeSpanStateAt(state, text) { + const raw = typeof text === 'string' ? text : ''; + let ticks = state && Number.isInteger(state.markdownCodeSpanTicks) ? state.markdownCodeSpanTicks : 0; + let fromPrior = ticks > 0; + for (let i = 0; i < raw.length;) { + if (raw[i] !== '`') { + i += 1; + continue; + } + const run = countBacktickRun(raw, i); + if (ticks === 0) { + if (run >= 3 && atMarkdownFenceLineStart(raw, i)) { + i += run; + continue; + } + if (state && insideCodeFenceWithState(state, raw.slice(0, i))) { + i += run; + continue; + } + ticks = run; + fromPrior = false; + } else if (run === ticks) { + ticks = 0; + fromPrior = false; + } + i += run; + } + return { ticks, fromPrior }; +} + +function markdownCodeSpanCloses(text, ticks) { + const raw = typeof text === 'string' ? text : ''; + if (!Number.isInteger(ticks) || ticks <= 0) { + return false; + } + for (let i = 0; i < raw.length;) { + if (raw[i] !== '`') { + i += 1; + continue; + } + const run = countBacktickRun(raw, i); + if (run === ticks) { + return true; + } + i += run; + } + return false; +} + +function shouldResetUnclosedMarkdownPrefix(state, prefix, suffix) { + const markdown = markdownCodeSpanStateAt(state, prefix); + return markdown.ticks > 0 && !markdown.fromPrior && !markdownCodeSpanCloses(suffix, markdown.ticks); +} + +function countBacktickRun(text, start) { + let count = 0; + while (start + count < text.length && text[start + count] === '`') { + count += 1; + } + return count; +} + +function atMarkdownFenceLineStart(text, idx) { + for (let i = idx - 1; i >= 0; i -= 1) { + const ch = text[i]; + if (ch === ' ' || ch === '\t') { + continue; + } + return ch === '\n' || ch === '\r'; + } + return true; +} + +function consumeToolCapture(state, toolNames) { + const captured = state.capture || ''; + if (!captured) { + return { ready: false, prefix: '', calls: [], suffix: '' }; + } + + // XML-only tool call extraction. + const xmlResult = consumeXMLToolCaptureImpl(captured, toolNames, trimWrappingJSONFence); + if (xmlResult.ready) { + return xmlResult; + } + // If XML tags are present but block is incomplete, keep buffering. + if (hasOpenXMLToolTag(captured)) { + return { ready: false, prefix: '', calls: [], suffix: '' }; + } + if (shouldKeepBareInvokeCapture(captured)) { + return { ready: false, prefix: '', calls: [], suffix: '' }; + } + + // No XML tool tags detected — release captured content as text. + return { + ready: true, + prefix: captured, + calls: [], + suffix: '', + }; +} + +module.exports = { + processToolSieveChunk, + flushToolSieve, +}; diff --git a/internal/js/helpers/stream-tool-sieve/state.js b/internal/js/helpers/stream-tool-sieve/state.js new file mode 100644 index 0000000000000000000000000000000000000000..8282f2e03f5575154b518abdc5ce0ea885ef03c3 --- /dev/null +++ b/internal/js/helpers/stream-tool-sieve/state.js @@ -0,0 +1,260 @@ +'use strict'; + +function createToolSieveState() { + return { + pending: '', + capture: '', + capturing: false, + codeFenceStack: [], + codeFencePendingTicks: 0, + codeFencePendingTildes: 0, + codeFenceLineStart: true, + markdownCodeSpanTicks: 0, + pendingToolRaw: '', + pendingToolCalls: [], + disableDeltas: false, + toolNameSent: false, + toolName: '', + toolArgsStart: -1, + toolArgsSent: -1, + toolArgsString: false, + toolArgsDone: false, + }; +} + +function resetIncrementalToolState(state) { + state.disableDeltas = false; + state.toolNameSent = false; + state.toolName = ''; + state.toolArgsStart = -1; + state.toolArgsSent = -1; + state.toolArgsString = false; + state.toolArgsDone = false; +} + +function noteText(state, text) { + if (!state || !hasMeaningfulText(text)) { + return; + } + updateMarkdownCodeSpanState(state, text); + updateCodeFenceState(state, text); +} + +function looksLikeToolExampleContext(text) { + return insideCodeFence(text); +} + +function insideCodeFence(text) { + const t = typeof text === 'string' ? text : ''; + if (!t) { + return false; + } + return simulateCodeFenceState([], 0, 0, true, t).stack.length > 0; +} + +function insideCodeFenceWithState(state, text) { + if (!state) { + return insideCodeFence(text); + } + const simulated = simulateCodeFenceState( + Array.isArray(state.codeFenceStack) ? state.codeFenceStack : [], + Number.isInteger(state.codeFencePendingTicks) ? state.codeFencePendingTicks : 0, + Number.isInteger(state.codeFencePendingTildes) ? state.codeFencePendingTildes : 0, + state.codeFenceLineStart !== false, + text, + ); + return simulated.stack.length > 0; +} + +function insideMarkdownCodeSpanWithState(state, text) { + if (!state) { + return simulateMarkdownCodeSpanTicks(null, 0, text) > 0; + } + const ticks = Number.isInteger(state.markdownCodeSpanTicks) ? state.markdownCodeSpanTicks : 0; + return simulateMarkdownCodeSpanTicks(state, ticks, text) > 0; +} + +function updateMarkdownCodeSpanState(state, text) { + if (!state || !hasMeaningfulText(text)) { + return; + } + const ticks = Number.isInteger(state.markdownCodeSpanTicks) ? state.markdownCodeSpanTicks : 0; + state.markdownCodeSpanTicks = simulateMarkdownCodeSpanTicks(state, ticks, text); +} + +function simulateMarkdownCodeSpanTicks(state, initialTicks, text) { + const raw = typeof text === 'string' ? text : ''; + let ticks = Number.isInteger(initialTicks) ? initialTicks : 0; + for (let i = 0; i < raw.length;) { + if (raw[i] !== '`') { + i += 1; + continue; + } + const run = countBacktickRun(raw, i); + if (ticks === 0) { + if (run >= 3 && atMarkdownFenceLineStart(raw, i)) { + i += run; + continue; + } + if (state && insideCodeFenceWithState(state, raw.slice(0, i))) { + i += run; + continue; + } + ticks = run; + } else if (run === ticks) { + ticks = 0; + } + i += run; + } + return ticks; +} + +function countBacktickRun(text, start) { + let count = 0; + while (start + count < text.length && text[start + count] === '`') { + count += 1; + } + return count; +} + +function atMarkdownFenceLineStart(text, idx) { + for (let i = idx - 1; i >= 0; i -= 1) { + const ch = text[i]; + if (ch === ' ' || ch === '\t') { + continue; + } + return ch === '\n' || ch === '\r'; + } + return true; +} + +function updateCodeFenceState(state, text) { + if (!state) { + return; + } + const next = simulateCodeFenceState( + Array.isArray(state.codeFenceStack) ? state.codeFenceStack : [], + Number.isInteger(state.codeFencePendingTicks) ? state.codeFencePendingTicks : 0, + Number.isInteger(state.codeFencePendingTildes) ? state.codeFencePendingTildes : 0, + state.codeFenceLineStart !== false, + text, + ); + state.codeFenceStack = next.stack; + state.codeFencePendingTicks = next.pendingTicks; + state.codeFencePendingTildes = next.pendingTildes; + state.codeFenceLineStart = next.lineStart; +} + +function simulateCodeFenceState(stack, pendingTicks, pendingTildes, lineStart, text) { + const chunk = typeof text === 'string' ? text : ''; + const nextStack = Array.isArray(stack) ? [...stack] : []; + let ticks = Number.isInteger(pendingTicks) ? pendingTicks : 0; + let tildes = Number.isInteger(pendingTildes) ? pendingTildes : 0; + let atLineStart = lineStart !== false; + + const flushPending = () => { + if (ticks > 0) { + if (atLineStart && ticks >= 3) { + applyFenceMarker(nextStack, ticks); // positive = backtick + } + atLineStart = false; + ticks = 0; + } + if (tildes > 0) { + if (atLineStart && tildes >= 3) { + applyFenceMarker(nextStack, -tildes); // negative = tilde + } + atLineStart = false; + tildes = 0; + } + }; + + for (let i = 0; i < chunk.length; i += 1) { + const ch = chunk[i]; + if (ch === '`') { + if (tildes > 0) { + flushPending(); + } + ticks += 1; + continue; + } + if (ch === '~') { + if (ticks > 0) { + flushPending(); + } + tildes += 1; + continue; + } + flushPending(); + if (ch === '\n' || ch === '\r') { + atLineStart = true; + continue; + } + if ((ch === ' ' || ch === '\t') && atLineStart) { + continue; + } + atLineStart = false; + } + return { + stack: nextStack, + pendingTicks: ticks, + pendingTildes: tildes, + lineStart: atLineStart, + }; +} + +// Positive values = backtick fences, negative = tilde fences. +// Closing must match fence type. +function applyFenceMarker(stack, marker) { + if (!Array.isArray(stack)) { + return; + } + if (stack.length === 0) { + stack.push(marker); + return; + } + const top = stack[stack.length - 1]; + const sameType = (top > 0 && marker > 0) || (top < 0 && marker < 0); + if (!sameType) { + stack.push(marker); + return; + } + const absMarker = Math.abs(marker); + const absTop = Math.abs(top); + if (absMarker >= absTop) { + stack.pop(); + return; + } + stack.push(marker); +} + +function hasMeaningfulText(text) { + return toStringSafe(text) !== ''; +} + +function toStringSafe(v) { + if (typeof v === 'string') { + return v.trim(); + } + if (Array.isArray(v)) { + return toStringSafe(v[0]); + } + if (v == null) { + return ''; + } + return String(v).trim(); +} + +module.exports = { + createToolSieveState, + resetIncrementalToolState, + noteText, + looksLikeToolExampleContext, + insideCodeFence, + insideCodeFenceWithState, + insideMarkdownCodeSpanWithState, + updateCodeFenceState, + updateMarkdownCodeSpanState, + hasMeaningfulText, + toStringSafe, +}; diff --git a/internal/js/shared/deepseek-constants.js b/internal/js/shared/deepseek-constants.js new file mode 100644 index 0000000000000000000000000000000000000000..b142c9e5e8a1117e2766088393ffda72363daec8 --- /dev/null +++ b/internal/js/shared/deepseek-constants.js @@ -0,0 +1,127 @@ +'use strict'; + +const fs = require('fs'); +const path = require('path'); + +const DEFAULT_CLIENT = Object.freeze({ + name: 'DeepSeek', + platform: 'android', + androidApiLevel: '35', + locale: 'zh_CN', +}); + +const DEFAULT_BASE_HEADERS = Object.freeze({ + Host: 'chat.deepseek.com', + Accept: 'application/json', + 'Content-Type': 'application/json', + 'accept-charset': 'UTF-8', +}); + +const DEFAULT_SKIP_PATTERNS = Object.freeze([ + 'quasi_status', + 'elapsed_secs', + 'token_usage', + 'pending_fragment', + 'conversation_mode', + 'fragments/-1/status', + 'fragments/-2/status', + 'fragments/-3/status', +]); + +const DEFAULT_SKIP_EXACT_PATHS = Object.freeze([ + 'response/search_status', +]); + +function asNonEmptyString(value) { + return typeof value === 'string' && value !== '' ? value : ''; +} + +function normalizeClient(raw) { + const client = raw && typeof raw === 'object' && !Array.isArray(raw) ? raw : {}; + return { + name: asNonEmptyString(client.name) || DEFAULT_CLIENT.name, + platform: asNonEmptyString(client.platform) || DEFAULT_CLIENT.platform, + version: asNonEmptyString(client.version), + androidApiLevel: asNonEmptyString(client.android_api_level) || DEFAULT_CLIENT.androidApiLevel, + locale: asNonEmptyString(client.locale) || DEFAULT_CLIENT.locale, + }; +} + +function buildBaseHeaders(parsed, client) { + const rawBaseHeaders = parsed && typeof parsed.base_headers === 'object' && !Array.isArray(parsed.base_headers) + ? parsed.base_headers + : {}; + const baseHeaders = { ...DEFAULT_BASE_HEADERS, ...rawBaseHeaders }; + if (client.name && client.version) { + const androidSuffix = client.platform === 'android' && client.androidApiLevel + ? ` Android/${client.androidApiLevel}` + : ''; + baseHeaders['User-Agent'] = `${client.name}/${client.version}${androidSuffix}`; + } + if (client.platform) { + baseHeaders['x-client-platform'] = client.platform; + } + if (client.version) { + baseHeaders['x-client-version'] = client.version; + } + if (client.locale) { + baseHeaders['x-client-locale'] = client.locale; + } + return baseHeaders; +} + +function sharedConstantsPaths() { + return [ + path.resolve(__dirname, '../../deepseek/protocol/constants_shared.json'), + path.resolve(process.cwd(), 'internal/deepseek/protocol/constants_shared.json'), + ]; +} + +function readSharedConstants() { + try { + return require('../../deepseek/protocol/constants_shared.json'); + } catch (_err) { + // Fall through to filesystem candidates for test and local execution variants. + } + for (const sharedPath of sharedConstantsPaths()) { + try { + const raw = fs.readFileSync(sharedPath, 'utf8'); + return JSON.parse(raw); + } catch (_err) { + // Try the next candidate path; fall back to in-file structural defaults below. + } + } + return {}; +} + +function loadSharedConstants() { + const parsed = readSharedConstants(); + const client = normalizeClient(parsed && parsed.client); + const skipPatterns = Array.isArray(parsed && parsed.skip_contains_patterns) + ? parsed.skip_contains_patterns.filter((v) => typeof v === 'string' && v !== '') + : [...DEFAULT_SKIP_PATTERNS]; + const skipExactPaths = Array.isArray(parsed && parsed.skip_exact_paths) + ? parsed.skip_exact_paths.filter((v) => typeof v === 'string' && v !== '') + : [...DEFAULT_SKIP_EXACT_PATHS]; + return { + client, + baseHeaders: buildBaseHeaders(parsed, client), + skipPatterns, + skipExactPaths, + }; +} + +const shared = loadSharedConstants(); + +module.exports = { + CLIENT: Object.freeze({ ...shared.client }), + CLIENT_VERSION: shared.client.version, + BASE_HEADERS: Object.freeze(shared.baseHeaders), + SKIP_PATTERNS: Object.freeze(shared.skipPatterns), + SKIP_EXACT_PATHS: new Set(shared.skipExactPaths), + __test: { + buildBaseHeaders, + normalizeClient, + sharedConstantsPaths, + }, +}; diff --git a/internal/prompt/messages.go b/internal/prompt/messages.go new file mode 100644 index 0000000000000000000000000000000000000000..caec9ace67918047447f2c350f7cb5491e61d018 --- /dev/null +++ b/internal/prompt/messages.go @@ -0,0 +1,125 @@ +package prompt + +import ( + "encoding/json" + "fmt" + "regexp" + "strings" +) + +var markdownImagePattern = regexp.MustCompile(`!\[(.*?)\]\((.*?)\)`) + +const ( + beginSentenceMarker = "" + systemMarker = "System: " + userMarker = "User: " + assistantMarker = "Assistant: " + toolMarker = "Tool: " + endSentenceMarker = "\n" + endToolResultsMarker = "\n" + endInstructionsMarker = "\n" +) + +func MessagesPrepare(messages []map[string]any) string { + return MessagesPrepareWithThinking(messages, false) +} + +func MessagesPrepareWithThinking(messages []map[string]any, _ bool) string { + type block struct { + Role string + Text string + } + processed := make([]block, 0, len(messages)) + for _, m := range messages { + role, _ := m["role"].(string) + text := NormalizeContent(m["content"]) + processed = append(processed, block{Role: role, Text: text}) + } + if len(processed) == 0 { + return "" + } + merged := make([]block, 0, len(processed)) + for _, msg := range processed { + if len(merged) > 0 && merged[len(merged)-1].Role == msg.Role { + merged[len(merged)-1].Text += "\n\n" + msg.Text + continue + } + merged = append(merged, msg) + } + parts := make([]string, 0, len(merged)+2) + parts = append(parts, beginSentenceMarker) + lastRole := "" + for _, m := range merged { + lastRole = m.Role + switch m.Role { + case "assistant": + parts = append(parts, formatRoleBlock(assistantMarker, m.Text, endSentenceMarker)) + case "tool": + if strings.TrimSpace(m.Text) != "" { + parts = append(parts, formatRoleBlock(toolMarker, m.Text, endToolResultsMarker)) + } + case "system": + if text := strings.TrimSpace(m.Text); text != "" { + parts = append(parts, formatRoleBlock(systemMarker, text, endInstructionsMarker)) + } + case "user": + parts = append(parts, formatRoleBlock(userMarker, m.Text, "")) + default: + if strings.TrimSpace(m.Text) != "" { + parts = append(parts, m.Text) + } + } + } + if lastRole != "assistant" { + parts = append(parts, assistantMarker) + } + out := strings.Join(parts, "") + return markdownImagePattern.ReplaceAllString(out, `[${1}](${2})`) +} + +// formatRoleBlock produces a single concatenated block: marker + text + endMarker. +// No whitespace is inserted between marker and text so role boundaries stay +// compact and predictable for downstream parsers. +func formatRoleBlock(marker, text, endMarker string) string { + out := marker + text + if strings.TrimSpace(endMarker) != "" { + out += endMarker + } + return out +} + +func NormalizeContent(v any) string { + if v == nil { + return "" + } + switch x := v.(type) { + case string: + return x + case []any: + parts := make([]string, 0, len(x)) + for _, item := range x { + m, ok := item.(map[string]any) + if !ok { + continue + } + typeStr, _ := m["type"].(string) + typeStr = strings.ToLower(strings.TrimSpace(typeStr)) + if typeStr == "text" || typeStr == "output_text" || typeStr == "input_text" { + if txt, ok := m["text"].(string); ok && txt != "" { + parts = append(parts, txt) + continue + } + if txt, ok := m["content"].(string); ok && txt != "" { + parts = append(parts, txt) + } + } + } + return strings.Join(parts, "\n") + default: + b, err := json.Marshal(v) + if err != nil { + return fmt.Sprintf("%v", v) + } + return string(b) + } +} diff --git a/internal/prompt/messages_test.go b/internal/prompt/messages_test.go new file mode 100644 index 0000000000000000000000000000000000000000..40852134ba0d3a5d2a75bb00b3f8c48c461639c0 --- /dev/null +++ b/internal/prompt/messages_test.go @@ -0,0 +1,68 @@ +package prompt + +import ( + "strings" + "testing" +) + +func TestNormalizeContentNilReturnsEmpty(t *testing.T) { + if got := NormalizeContent(nil); got != "" { + t.Fatalf("expected empty string for nil content, got %q", got) + } +} + +func TestMessagesPrepareNilContentNoNullLiteral(t *testing.T) { + messages := []map[string]any{ + {"role": "assistant", "content": nil}, + {"role": "user", "content": "ok"}, + } + got := MessagesPrepare(messages) + if got == "" { + t.Fatalf("expected non-empty output") + } + if got == "null" { + t.Fatalf("expected no null literal output, got %q", got) + } +} + +func TestMessagesPrepareUsesTurnSuffixes(t *testing.T) { + messages := []map[string]any{ + {"role": "system", "content": "System rule"}, + {"role": "user", "content": "Question"}, + {"role": "assistant", "content": "Answer"}, + } + got := MessagesPrepare(messages) + if !strings.Contains(got, "System: ") || !strings.Contains(got, "System rule") { + t.Fatalf("expected system instructions to remain present, got %q", got) + } + if !strings.Contains(got, "User: Question") { + t.Fatalf("expected user question, got %q", got) + } + if !strings.Contains(got, "Assistant: Answer\n") { + t.Fatalf("expected assistant sentence suffix, got %q", got) + } + if strings.Contains(got, "") || strings.Contains(got, "") { + t.Fatalf("did not expect think tags in prompt, got %q", got) + } +} + +func TestNormalizeContentArrayFallsBackToContentWhenTextEmpty(t *testing.T) { + got := NormalizeContent([]any{ + map[string]any{"type": "text", "text": "", "content": "from-content"}, + }) + if got != "from-content" { + t.Fatalf("expected fallback to content when text is empty, got %q", got) + } +} + +func TestMessagesPrepareWithThinkingPreservesPromptShape(t *testing.T) { + messages := []map[string]any{{"role": "user", "content": "Question"}} + gotThinking := MessagesPrepareWithThinking(messages, true) + gotPlain := MessagesPrepareWithThinking(messages, false) + if gotThinking != gotPlain { + t.Fatalf("expected thinking flag not to add extra continuity instructions, got thinking=%q plain=%q", gotThinking, gotPlain) + } + if !strings.HasSuffix(gotThinking, "Assistant: ") { + t.Fatalf("expected assistant suffix, got %q", gotThinking) + } +} diff --git a/internal/prompt/tool_calls.go b/internal/prompt/tool_calls.go new file mode 100644 index 0000000000000000000000000000000000000000..050a2a91f8d533557bf9a82379ba407b322ee50a --- /dev/null +++ b/internal/prompt/tool_calls.go @@ -0,0 +1,382 @@ +package prompt + +import ( + "encoding/json" + "fmt" + "regexp" + "sort" + "strings" +) + +var promptXMLTextEscaper = strings.NewReplacer( + "&", "&", + "<", "<", + ">", ">", +) + +var promptXMLNamePattern = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_.:-]*$`) + +const ( + promptDSMLToolCallsOpen = "<|DSML|tool_calls>" + promptDSMLToolCallsClose = "" + promptDSMLInvokeOpen = "<|DSML|invoke" + promptDSMLInvokeClose = "" + promptDSMLParameterOpen = "<|DSML|parameter" + promptDSMLParameterClose = "" +) + +// FormatToolCallsForPrompt renders a tool_calls slice into the prompt-visible +// invoke/parameter history block used across adapters. +func FormatToolCallsForPrompt(raw any) string { + calls, ok := raw.([]any) + if !ok || len(calls) == 0 { + return "" + } + + blocks := make([]string, 0, len(calls)) + for _, item := range calls { + call, ok := item.(map[string]any) + if !ok { + continue + } + block := formatToolCallForPrompt(call) + if block != "" { + blocks = append(blocks, block) + } + } + if len(blocks) == 0 { + return "" + } + return promptDSMLToolCallsOpen + "\n" + strings.Join(blocks, "\n") + "\n" + promptDSMLToolCallsClose +} + +// StringifyToolCallArguments normalizes tool arguments into a compact string +// while preserving raw concatenated payloads when they already look like model +// output rather than a single JSON object. +func StringifyToolCallArguments(v any) string { + switch x := v.(type) { + case nil: + return "{}" + case string: + s := strings.TrimSpace(x) + if s == "" { + return "{}" + } + s = normalizeToolArgumentString(s) + if s == "" { + return "{}" + } + return s + default: + b, err := json.Marshal(x) + if err != nil || len(b) == 0 { + return "{}" + } + return string(b) + } +} + +func formatToolCallForPrompt(call map[string]any) string { + if call == nil { + return "" + } + + name := strings.TrimSpace(asString(call["name"])) + fn, _ := call["function"].(map[string]any) + if name == "" && fn != nil { + name = strings.TrimSpace(asString(fn["name"])) + } + if name == "" { + return "" + } + + argsRaw := call["arguments"] + if argsRaw == nil { + argsRaw = call["input"] + } + if argsRaw == nil && fn != nil { + argsRaw = fn["arguments"] + if argsRaw == nil { + argsRaw = fn["input"] + } + } + + parameters := formatToolCallParametersForPrompt(argsRaw) + if parameters == "" { + return ` ` + promptDSMLInvokeOpen + ` name="` + escapeXMLAttribute(name) + `">` + promptDSMLInvokeClose + } + + return " " + promptDSMLInvokeOpen + " name=\"" + escapeXMLAttribute(name) + "\">\n" + + parameters + "\n" + + " " + promptDSMLInvokeClose +} + +func formatToolCallParametersForPrompt(raw any) string { + value := normalizePromptToolCallValue(raw) + body, ok := renderPromptToolParameters(value, " ") + if ok && strings.TrimSpace(body) != "" { + return body + } + + fallback := StringifyToolCallArguments(raw) + if strings.TrimSpace(fallback) == "" { + return "" + } + return " " + promptDSMLParameterOpen + " name=\"content\">" + renderPromptXMLText(fallback) + promptDSMLParameterClose +} + +func renderPromptToolParameters(value any, indent string) (string, bool) { + switch v := value.(type) { + case nil: + return "", true + case map[string]any: + if len(v) == 0 { + return "", true + } + keys := make([]string, 0, len(v)) + for k := range v { + keys = append(keys, k) + } + sort.Strings(keys) + lines := make([]string, 0, len(keys)) + for _, key := range keys { + rendered, ok := renderPromptParameterNode(key, v[key], indent) + if !ok { + return "", false + } + lines = append(lines, rendered) + } + return strings.Join(lines, "\n"), true + case []any: + lines := make([]string, 0, len(v)) + for _, item := range v { + rendered, ok := renderPromptParameterNode("item", item, indent) + if !ok { + return "", false + } + lines = append(lines, rendered) + } + return strings.Join(lines, "\n"), true + case string: + return indent + promptDSMLParameterOpen + ` name="content">` + renderPromptXMLText(v) + promptDSMLParameterClose, true + default: + return indent + promptDSMLParameterOpen + ` name="value">` + renderPromptXMLText(fmt.Sprint(v)) + promptDSMLParameterClose, true + } +} + +func renderPromptParameterNode(name string, value any, indent string) (string, bool) { + trimmedName := strings.TrimSpace(name) + if trimmedName == "" { + return "", false + } + switch v := value.(type) { + case nil: + return indent + promptDSMLParameterOpen + ` name="` + escapeXMLAttribute(trimmedName) + `">` + promptDSMLParameterClose, true + case map[string]any: + body, ok := renderPromptToolXMLBody(v, indent+" ") + if !ok { + return "", false + } + if strings.TrimSpace(body) == "" { + return indent + promptDSMLParameterOpen + ` name="` + escapeXMLAttribute(trimmedName) + `">` + promptDSMLParameterClose, true + } + return indent + promptDSMLParameterOpen + ` name="` + escapeXMLAttribute(trimmedName) + "\">\n" + body + "\n" + indent + promptDSMLParameterClose, true + case []any: + body, ok := renderPromptToolXMLArray(v, indent+" ") + if !ok { + return "", false + } + if strings.TrimSpace(body) == "" { + return indent + promptDSMLParameterOpen + ` name="` + escapeXMLAttribute(trimmedName) + `">` + promptDSMLParameterClose, true + } + return indent + promptDSMLParameterOpen + ` name="` + escapeXMLAttribute(trimmedName) + "\">\n" + body + "\n" + indent + promptDSMLParameterClose, true + case string: + return indent + promptDSMLParameterOpen + ` name="` + escapeXMLAttribute(trimmedName) + `">` + renderPromptXMLText(v) + promptDSMLParameterClose, true + default: + return indent + promptDSMLParameterOpen + ` name="` + escapeXMLAttribute(trimmedName) + `">` + renderPromptXMLText(fmt.Sprint(v)) + promptDSMLParameterClose, true + } +} + +func normalizePromptToolCallValue(raw any) any { + switch x := raw.(type) { + case nil: + return nil + case string: + s := strings.TrimSpace(x) + if s == "" { + return "" + } + var parsed any + if err := json.Unmarshal([]byte(s), &parsed); err == nil { + return parsed + } + return x + default: + return x + } +} + +func renderPromptToolXMLBody(value any, indent string) (string, bool) { + switch v := value.(type) { + case nil: + return "", true + case map[string]any: + return renderPromptToolXMLMap(v, indent) + case []any: + return renderPromptToolXMLArray(v, indent) + case string: + return indent + "" + renderPromptXMLText(v) + "", true + case bool, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: + return indent + "" + escapeXMLText(fmt.Sprint(v)) + "", true + default: + return indent + "" + renderPromptXMLText(fmt.Sprint(v)) + "", true + } +} + +func renderPromptToolXMLMap(m map[string]any, indent string) (string, bool) { + if len(m) == 0 { + return "", true + } + keys := make([]string, 0, len(m)) + for k := range m { + if !isValidPromptXMLName(k) { + return "", false + } + keys = append(keys, k) + } + sort.Strings(keys) + + lines := make([]string, 0, len(keys)) + for _, key := range keys { + rendered, ok := renderPromptToolXMLNode(key, m[key], indent) + if !ok { + return "", false + } + lines = append(lines, rendered) + } + return strings.Join(lines, "\n"), true +} + +func renderPromptToolXMLArray(items []any, indent string) (string, bool) { + if len(items) == 0 { + return "", true + } + lines := make([]string, 0, len(items)) + for _, item := range items { + rendered, ok := renderPromptToolXMLNode("item", item, indent) + if !ok { + return "", false + } + lines = append(lines, rendered) + } + return strings.Join(lines, "\n"), true +} + +func renderPromptToolXMLNode(name string, value any, indent string) (string, bool) { + if !isValidPromptXMLName(name) { + return "", false + } + switch v := value.(type) { + case nil: + return indent + "<" + name + ">", true + case map[string]any: + inner, ok := renderPromptToolXMLMap(v, indent+" ") + if !ok { + return "", false + } + if strings.TrimSpace(inner) == "" { + return indent + "<" + name + ">", true + } + return indent + "<" + name + ">\n" + inner + "\n" + indent + "", true + case []any: + if len(v) == 0 { + return indent + "<" + name + ">", true + } + lines := make([]string, 0, len(v)) + for _, item := range v { + rendered, ok := renderPromptToolXMLNode(name, item, indent) + if !ok { + return "", false + } + lines = append(lines, rendered) + } + return strings.Join(lines, "\n"), true + case string: + return indent + "<" + name + ">" + renderPromptXMLText(v) + "", true + case bool, float32, float64, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: + return indent + "<" + name + ">" + escapeXMLText(fmt.Sprint(v)) + "", true + default: + return indent + "<" + name + ">" + renderPromptXMLText(fmt.Sprint(v)) + "", true + } +} + +// renderPromptXMLText emits CDATA for every string so prompt-visible tool +// history stays uniform and does not drift back toward ad-hoc escaping. +func renderPromptXMLText(text string) string { + if text == "" { + return "" + } + if strings.Contains(text, "]]>") { + return "", "]]]]>") + "]]>" + } + return "" +} + +func isValidPromptXMLName(name string) bool { + return promptXMLNamePattern.MatchString(strings.TrimSpace(name)) +} + +func escapeXMLAttribute(text string) string { + if text == "" { + return "" + } + return strings.NewReplacer( + "&", "&", + `"`, """, + "<", "<", + ">", ">", + ).Replace(text) +} + +func normalizeToolArgumentString(raw string) string { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return "" + } + if looksLikeConcatenatedJSON(trimmed) { + // Keep the original payload to avoid silently rewriting model output. + return raw + } + return trimmed +} + +func looksLikeConcatenatedJSON(raw string) bool { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return false + } + if strings.Contains(trimmed, "}{") || strings.Contains(trimmed, "][") { + return true + } + dec := json.NewDecoder(strings.NewReader(trimmed)) + var first any + if err := dec.Decode(&first); err != nil { + return false + } + var second any + return dec.Decode(&second) == nil +} + +func asString(v any) string { + if s, ok := v.(string); ok { + return s + } + return "" +} + +func escapeXMLText(v string) string { + if v == "" { + return "" + } + return promptXMLTextEscaper.Replace(v) +} diff --git a/internal/prompt/tool_calls_test.go b/internal/prompt/tool_calls_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8a5a36934412408df620f20b16c1df7ecd577271 --- /dev/null +++ b/internal/prompt/tool_calls_test.go @@ -0,0 +1,57 @@ +package prompt + +import "testing" + +func TestStringifyToolCallArgumentsPreservesConcatenatedJSON(t *testing.T) { + got := StringifyToolCallArguments(`{}{"query":"测试工具调用"}`) + if got != `{}{"query":"测试工具调用"}` { + t.Fatalf("expected raw concatenated JSON to be preserved, got %q", got) + } +} + +func TestFormatToolCallsForPromptDSML(t *testing.T) { + got := FormatToolCallsForPrompt([]any{ + map[string]any{ + "id": "call_1", + "function": map[string]any{ + "name": "search_web", + "arguments": map[string]any{"query": "latest"}, + }, + }, + }) + if got == "" { + t.Fatal("expected non-empty formatted tool calls") + } + if got != "<|DSML|tool_calls>\n <|DSML|invoke name=\"search_web\">\n <|DSML|parameter name=\"query\">\n \n" { + t.Fatalf("unexpected formatted tool call DSML: %q", got) + } +} + +func TestFormatToolCallsForPromptEscapesXMLEntities(t *testing.T) { + got := FormatToolCallsForPrompt([]any{ + map[string]any{ + "name": "search<&>", + "arguments": `{"q":"a < b && c > d"}`, + }, + }) + want := "<|DSML|tool_calls>\n <|DSML|invoke name=\"search<&>\">\n <|DSML|parameter name=\"q\"> d]]>\n \n" + if got != want { + t.Fatalf("unexpected escaped tool call XML: %q", got) + } +} + +func TestFormatToolCallsForPromptUsesCDATAForMultilineContent(t *testing.T) { + got := FormatToolCallsForPrompt([]any{ + map[string]any{ + "name": "write_file", + "arguments": map[string]any{ + "path": "script.sh", + "content": "#!/bin/bash\nprintf \"hello\"\n", + }, + }, + }) + want := "<|DSML|tool_calls>\n <|DSML|invoke name=\"write_file\">\n <|DSML|parameter name=\"content\">\n <|DSML|parameter name=\"path\">\n \n" + if got != want { + t.Fatalf("unexpected multiline cdata tool call XML: %q", got) + } +} diff --git a/internal/promptcompat/file_refs.go b/internal/promptcompat/file_refs.go new file mode 100644 index 0000000000000000000000000000000000000000..86006b69cb3731e22d2fa77b1c241a5e0ed51104 --- /dev/null +++ b/internal/promptcompat/file_refs.go @@ -0,0 +1,94 @@ +package promptcompat + +import "strings" + +func CollectOpenAIRefFileIDs(req map[string]any) []string { + if len(req) == 0 { + return nil + } + out := make([]string, 0, 4) + seen := map[string]struct{}{} + for _, key := range []string{ + "ref_file_ids", + "file_ids", + "attachments", + "messages", + "input", + } { + raw := req[key] + if raw == nil { + continue + } + // Skip top-level strings for 'messages' and 'input' as they are likely plain text content, + // not file IDs. String file IDs are expected in 'ref_file_ids' or 'file_ids'. + if key == "messages" || key == "input" { + if _, ok := raw.(string); ok { + continue + } + } + appendOpenAIRefFileIDs(&out, seen, raw) + } + if len(out) == 0 { + return nil + } + return out +} + +func appendOpenAIRefFileIDs(out *[]string, seen map[string]struct{}, raw any) { + switch x := raw.(type) { + case string: + addOpenAIRefFileID(out, seen, x) + case []string: + for _, item := range x { + addOpenAIRefFileID(out, seen, item) + } + case []any: + for _, item := range x { + appendOpenAIRefFileIDs(out, seen, item) + } + case map[string]any: + if fileID := strings.TrimSpace(asString(x["file_id"])); fileID != "" { + addOpenAIRefFileID(out, seen, fileID) + } + if strings.Contains(strings.ToLower(strings.TrimSpace(asString(x["type"]))), "file") { + if fileID := strings.TrimSpace(asString(x["id"])); fileID != "" { + addOpenAIRefFileID(out, seen, fileID) + } + } + if fileMap, ok := x["file"].(map[string]any); ok { + if fileID := strings.TrimSpace(asString(fileMap["file_id"])); fileID != "" { + addOpenAIRefFileID(out, seen, fileID) + } + if fileID := strings.TrimSpace(asString(fileMap["id"])); fileID != "" { + addOpenAIRefFileID(out, seen, fileID) + } + } + // Recurse into potential containers. Note: we do NOT recurse into 'content' or 'input' + // if they are plain strings (handled by the top-level switch), but they are usually + // nested inside the map branch anyway. + // To be safe, we only recurse into these known container keys. + for _, key := range []string{"ref_file_ids", "file_ids", "attachments", "messages", "input", "content", "files", "items", "data", "source"} { + if nested, ok := x[key]; ok { + // If it's a message content that is a string, we must NOT treat it as an ID. + if key == "content" || key == "input" { + if _, ok := nested.(string); ok { + continue + } + } + appendOpenAIRefFileIDs(out, seen, nested) + } + } + } +} + +func addOpenAIRefFileID(out *[]string, seen map[string]struct{}, fileID string) { + fileID = strings.TrimSpace(fileID) + if fileID == "" { + return + } + if _, ok := seen[fileID]; ok { + return + } + seen[fileID] = struct{}{} + *out = append(*out, fileID) +} diff --git a/internal/promptcompat/history_transcript.go b/internal/promptcompat/history_transcript.go new file mode 100644 index 0000000000000000000000000000000000000000..6ab66a943d360ee5843f62f050141d542e7e83c1 --- /dev/null +++ b/internal/promptcompat/history_transcript.go @@ -0,0 +1,121 @@ +package promptcompat + +import ( + "fmt" + "strings" +) + +const CurrentInputContextFilename = "context_context.txt" + +const historyTranscriptTitle = "# context_context.txt" + +func BuildOpenAIHistoryTranscript(messages []any) string { + return buildOpenAIHistoryTranscript(messages) +} + +func BuildOpenAICurrentUserInputTranscript(text string) string { + if strings.TrimSpace(text) == "" { + return "" + } + return buildOpenAIHistoryTranscript([]any{ + map[string]any{"role": "user", "content": text}, + }) +} + +func BuildOpenAICurrentInputContextTranscript(messages []any) string { + return buildOpenAIHistoryTranscript(messages) +} + +func buildOpenAIHistoryTranscript(messages []any) string { + if len(messages) == 0 { + return "" + } + var b strings.Builder + b.WriteString(historyTranscriptTitle) + b.WriteString("\n\n") + + entry := 0 + for _, raw := range messages { + msg, ok := raw.(map[string]any) + if !ok { + continue + } + role := normalizeOpenAIRoleForPrompt(strings.ToLower(strings.TrimSpace(asString(msg["role"])))) + content := strings.TrimSpace(buildOpenAIHistoryEntry(role, msg)) + if content == "" { + continue + } + entry++ + fmt.Fprintf(&b, "=== %d ===\n[r=%d]\n%s\n\n", entry, roleCodeForHistory(role), content) + } + + transcript := strings.TrimSpace(b.String()) + if transcript == "" { + return "" + } + return transcript + "\n" +} + +func buildOpenAIHistoryEntry(role string, msg map[string]any) string { + switch role { + case "assistant": + return strings.TrimSpace(buildAssistantContentForPrompt(msg)) + case "tool", "function": + return strings.TrimSpace(buildToolHistoryContent(msg)) + case "system", "user": + return strings.TrimSpace(NormalizeOpenAIContentForPrompt(msg["content"])) + default: + return strings.TrimSpace(NormalizeOpenAIContentForPrompt(msg["content"])) + } +} + +func buildToolHistoryContent(msg map[string]any) string { + content := strings.TrimSpace(NormalizeOpenAIContentForPrompt(msg["content"])) + parts := make([]string, 0, 2) + if name := strings.TrimSpace(asString(msg["name"])); name != "" { + parts = append(parts, "name="+name) + } + if callID := strings.TrimSpace(asString(msg["tool_call_id"])); callID != "" { + parts = append(parts, "tool_call_id="+callID) + } + header := "" + if len(parts) > 0 { + header = "[" + strings.Join(parts, " ") + "]" + } + switch { + case header != "" && content != "": + return header + "\n" + content + case header != "": + return header + default: + return content + } +} + +func roleLabelForHistory(role string) string { + role = strings.ToLower(strings.TrimSpace(role)) + switch role { + case "function": + return "tool" + case "": + return "unknown" + default: + return role + } +} + +func roleCodeForHistory(role string) int { + role = strings.ToLower(strings.TrimSpace(role)) + switch role { + case "system": + return 0 + case "user": + return 1 + case "assistant": + return 2 + case "tool", "function": + return 3 + default: + return 9 + } +} diff --git a/internal/promptcompat/message_normalize.go b/internal/promptcompat/message_normalize.go new file mode 100644 index 0000000000000000000000000000000000000000..7cfef490c6d39c4607288071bd614b110b2c44ed --- /dev/null +++ b/internal/promptcompat/message_normalize.go @@ -0,0 +1,214 @@ +package promptcompat + +import ( + "strings" + + "ds2api/internal/prompt" + "ds2api/internal/toolcall" +) + +const assistantReasoningLabel = "reasoning_content" + +func NormalizeOpenAIMessagesForPrompt(raw []any, traceID string) []map[string]any { + _ = traceID + out := make([]map[string]any, 0, len(raw)) + for _, item := range raw { + msg, ok := item.(map[string]any) + if !ok { + continue + } + role := strings.ToLower(strings.TrimSpace(asString(msg["role"]))) + switch role { + case "assistant": + content := buildAssistantContentForPrompt(msg) + if content == "" { + continue + } + out = append(out, map[string]any{ + "role": "assistant", + "content": content, + }) + case "tool", "function": + content := buildToolContentForPrompt(msg) + out = append(out, map[string]any{ + "role": "tool", + "content": content, + }) + case "user", "system", "developer": + out = append(out, map[string]any{ + "role": normalizeOpenAIRoleForPrompt(role), + "content": NormalizeOpenAIContentForPrompt(msg["content"]), + }) + default: + content := NormalizeOpenAIContentForPrompt(msg["content"]) + if content == "" { + continue + } + if role == "" { + role = "user" + } + out = append(out, map[string]any{ + "role": normalizeOpenAIRoleForPrompt(role), + "content": content, + }) + } + } + return out +} + +func buildAssistantContentForPrompt(msg map[string]any) string { + content := strings.TrimSpace(NormalizeOpenAIContentForPrompt(msg["content"])) + reasoning := strings.TrimSpace(normalizeOpenAIReasoningContentForPrompt(msg["reasoning_content"])) + if reasoning == "" { + reasoning = strings.TrimSpace(extractOpenAIReasoningContentFromMessage(msg["content"])) + } + toolHistory := prompt.FormatToolCallsForPrompt(msg["tool_calls"]) + if toolHistory == "" { + content = normalizeAssistantToolMarkupContentForPrompt(content) + } + parts := make([]string, 0, 3) + if reasoning != "" { + parts = append(parts, formatPromptLabeledBlock(assistantReasoningLabel, reasoning)) + } + if content != "" { + parts = append(parts, content) + } + if toolHistory != "" { + parts = append(parts, toolHistory) + } + switch len(parts) { + case 0: + return "" + case 1: + return parts[0] + default: + return strings.Join(parts, "\n\n") + } +} + +func normalizeAssistantToolMarkupContentForPrompt(content string) string { + trimmed := strings.TrimSpace(content) + if trimmed == "" || !isStandaloneAssistantToolMarkupBlock(trimmed) { + return content + } + parsed := toolcall.ParseStandaloneToolCallsDetailed(trimmed, nil) + if len(parsed.Calls) == 0 { + return content + } + raw := make([]any, 0, len(parsed.Calls)) + for _, call := range parsed.Calls { + raw = append(raw, map[string]any{ + "name": call.Name, + "input": call.Input, + }) + } + if formatted := prompt.FormatToolCallsForPrompt(raw); formatted != "" { + return formatted + } + return content +} + +func isStandaloneAssistantToolMarkupBlock(trimmed string) bool { + tag, ok := toolcall.FindToolMarkupTagOutsideIgnored(trimmed, 0) + if !ok || tag.Start != 0 || tag.Closing || tag.Name != "tool_calls" { + return false + } + closeTag, ok := toolcall.FindMatchingToolMarkupClose(trimmed, tag) + if !ok { + return false + } + return strings.TrimSpace(trimmed[closeTag.End+1:]) == "" +} + +func normalizeOpenAIReasoningContentForPrompt(v any) string { + switch x := v.(type) { + case string: + return x + case []any: + return strings.Join(extractOpenAIReasoningPartsFromItems(x), "\n") + case map[string]any: + return extractOpenAIReasoningTextFromItem(x) + default: + return "" + } +} + +func extractOpenAIReasoningContentFromMessage(v any) string { + switch x := v.(type) { + case []any: + return strings.Join(extractOpenAIReasoningPartsFromItems(x), "\n") + case map[string]any: + return extractOpenAIReasoningTextFromItem(x) + default: + return "" + } +} + +func extractOpenAIReasoningPartsFromItems(items []any) []string { + parts := make([]string, 0, len(items)) + for _, item := range items { + if text := extractOpenAIReasoningTextFromItemMap(item); text != "" { + parts = append(parts, text) + } + } + return parts +} + +func extractOpenAIReasoningTextFromItemMap(item any) string { + m, ok := item.(map[string]any) + if !ok { + return "" + } + return extractOpenAIReasoningTextFromItem(m) +} + +func extractOpenAIReasoningTextFromItem(m map[string]any) string { + if m == nil { + return "" + } + switch strings.ToLower(strings.TrimSpace(asString(m["type"]))) { + case "reasoning", "thinking": + for _, key := range []string{"text", "thinking", "content"} { + if text := strings.TrimSpace(asString(m[key])); text != "" { + return text + } + } + } + return "" +} + +func formatPromptLabeledBlock(label, text string) string { + label = strings.TrimSpace(label) + text = strings.TrimSpace(text) + if label == "" { + return text + } + return "[" + label + "]\n" + text + "\n[/" + label + "]" +} + +func buildToolContentForPrompt(msg map[string]any) string { + content := NormalizeOpenAIContentForPrompt(msg["content"]) + if strings.TrimSpace(content) == "" { + return "null" + } + return content +} + +func NormalizeOpenAIContentForPrompt(v any) string { + return prompt.NormalizeContent(v) +} + +func normalizeOpenAIRoleForPrompt(role string) string { + role = strings.ToLower(strings.TrimSpace(role)) + if role == "developer" { + return "system" + } + return role +} + +func asString(v any) string { + if s, ok := v.(string); ok { + return s + } + return "" +} diff --git a/internal/promptcompat/message_normalize_test.go b/internal/promptcompat/message_normalize_test.go new file mode 100644 index 0000000000000000000000000000000000000000..cd37f5526a78a28fcbcd8c333f6d2dc5bea37685 --- /dev/null +++ b/internal/promptcompat/message_normalize_test.go @@ -0,0 +1,362 @@ +package promptcompat + +import ( + "strings" + "testing" + + "ds2api/internal/util" +) + +func TestNormalizeOpenAIMessagesForPrompt_AssistantToolCallsAndToolResult(t *testing.T) { + raw := []any{ + map[string]any{"role": "system", "content": "You are helpful"}, + map[string]any{"role": "user", "content": "查北京天气"}, + map[string]any{ + "role": "assistant", + "content": nil, + "tool_calls": []any{ + map[string]any{ + "id": "call_1", + "type": "function", + "function": map[string]any{ + "name": "get_weather", + "arguments": "{\"city\":\"beijing\"}", + }, + }, + }, + }, + map[string]any{ + "role": "tool", + "tool_call_id": "call_1", + "name": "get_weather", + "content": "{\"temp\":18}", + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 4 { + t.Fatalf("expected 4 normalized messages with assistant tool history preserved, got %d", len(normalized)) + } + assistantContent, _ := normalized[2]["content"].(string) + if !strings.Contains(assistantContent, "<|DSML|tool_calls>") { + t.Fatalf("assistant tool history should be preserved in DSML form, got %q", assistantContent) + } + if !strings.Contains(assistantContent, `<|DSML|invoke name="get_weather">`) { + t.Fatalf("expected tool name in preserved history, got %q", assistantContent) + } + if !strings.Contains(normalized[3]["content"].(string), `"temp":18`) { + t.Fatalf("tool result should be transparently forwarded, got %#v", normalized[3]["content"]) + } + + prompt := util.MessagesPrepare(normalized) + if !strings.Contains(prompt, "<|DSML|tool_calls>") { + t.Fatalf("expected preserved assistant tool history in prompt: %q", prompt) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_ToolObjectContentPreserved(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "tool", + "tool_call_id": "call_2", + "name": "get_weather", + "content": map[string]any{ + "temp": 18, + "condition": "sunny", + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + got, _ := normalized[0]["content"].(string) + if !strings.Contains(got, `"temp":18`) || !strings.Contains(got, `"condition":"sunny"`) { + t.Fatalf("expected serialized object in tool content, got %q", got) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_ToolArrayBlocksJoined(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "tool", + "tool_call_id": "call_3", + "name": "read_file", + "content": []any{ + map[string]any{"type": "input_text", "text": "line-1"}, + map[string]any{"type": "output_text", "text": "line-2"}, + map[string]any{"type": "image_url", "image_url": "https://example.com/a.png"}, + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + got, _ := normalized[0]["content"].(string) + if !strings.Contains(got, `line-1`) || !strings.Contains(got, `line-2`) { + t.Fatalf("expected tool content blocks preserved, got %q", got) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_FunctionRoleCompatible(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "function", + "tool_call_id": "call_4", + "name": "legacy_tool", + "content": map[string]any{ + "ok": true, + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected one normalized message, got %d", len(normalized)) + } + if normalized[0]["role"] != "tool" { + t.Fatalf("expected function role normalized as tool, got %#v", normalized[0]["role"]) + } + got, _ := normalized[0]["content"].(string) + if !strings.Contains(got, `"ok":true`) || strings.Contains(got, `"name":"legacy_tool"`) { + t.Fatalf("unexpected normalized function-role content: %q", got) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_EmptyToolContentPreservedAsNull(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "tool", + "tool_call_id": "call_5", + "name": "noop_tool", + "content": "", + }, + map[string]any{ + "role": "assistant", + "content": "done", + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 2 { + t.Fatalf("expected tool completion turn to be preserved, got %#v", normalized) + } + if normalized[0]["role"] != "tool" { + t.Fatalf("expected tool role preserved, got %#v", normalized[0]["role"]) + } + got, _ := normalized[0]["content"].(string) + if got != "null" { + t.Fatalf("expected empty tool content normalized as null string, got %q", got) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_AssistantMultipleToolCallsRemainSeparated(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "tool_calls": []any{ + map[string]any{ + "id": "call_search", + "type": "function", + "function": map[string]any{ + "name": "search_web", + "arguments": `{"query":"latest ai news"}`, + }, + }, + map[string]any{ + "id": "call_eval", + "type": "function", + "function": map[string]any{ + "name": "eval_javascript", + "arguments": `{"code":"1+1"}`, + }, + }, + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected assistant tool_call-only message preserved, got %#v", normalized) + } + content, _ := normalized[0]["content"].(string) + if strings.Count(content, "<|DSML|invoke name=") != 2 { + t.Fatalf("expected two preserved tool call blocks, got %q", content) + } + if !strings.Contains(content, `<|DSML|invoke name="search_web">`) || !strings.Contains(content, `<|DSML|invoke name="eval_javascript">`) { + t.Fatalf("expected both tool names in preserved history, got %q", content) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_PreservesConcatenatedToolArguments(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "tool_calls": []any{ + map[string]any{ + "id": "call_1", + "function": map[string]any{ + "name": "search_web", + "arguments": `{}{"query":"测试工具调用"}`, + }, + }, + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected assistant tool_call-only content preserved, got %#v", normalized) + } + content, _ := normalized[0]["content"].(string) + if !strings.Contains(content, `{}{"query":"测试工具调用"}`) { + t.Fatalf("expected concatenated tool arguments preserved, got %q", content) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_AssistantToolCallsMissingNameAreDropped(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "tool_calls": []any{ + map[string]any{ + "id": "call_missing_name", + "type": "function", + "function": map[string]any{ + "arguments": `{"path":"README.MD"}`, + }, + }, + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 0 { + t.Fatalf("expected assistant tool_calls without text to be dropped when name is missing, got %#v", normalized) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_AssistantNilContentDoesNotInjectNullLiteral(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "content": nil, + "tool_calls": []any{ + map[string]any{ + "id": "call_screenshot", + "function": map[string]any{ + "name": "send_file_to_user", + "arguments": `{"file_path":"/tmp/a.png"}`, + }, + }, + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected nil-content assistant tool_call-only message preserved, got %#v", normalized) + } + content, _ := normalized[0]["content"].(string) + if strings.Contains(content, "null") { + t.Fatalf("expected no null literal injection, got %q", content) + } + if !strings.Contains(content, "<|DSML|tool_calls>") { + t.Fatalf("expected assistant tool history in normalized content, got %q", content) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_CanonicalizesStandaloneAssistantToolMarkupContent(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "content": `<!DSML!tool_calls> + <!DSML!invoke name=“Bash”> + <!DSML!parameter name=“command”><![CDATA[lsof -i :4321 -t]]><!/DSML!parameter> + <!DSML!parameter name=“description”><![CDATA[Verify port 4321 is free]]><!/DSML!parameter> + <!/DSML!invoke> + <!/DSML!tool_calls>`, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected one normalized assistant message, got %#v", normalized) + } + content, _ := normalized[0]["content"].(string) + for _, want := range []string{ + "<|DSML|tool_calls>", + `<|DSML|invoke name="Bash">`, + `<|DSML|parameter name="command">`, + `<|DSML|parameter name="description">`, + "", + } { + if !strings.Contains(content, want) { + t.Fatalf("expected canonicalized assistant tool markup to contain %q, got %q", want, content) + } + } + for _, bad := range []string{"<!DSML", "!tool_calls", "“", "”"} { + if strings.Contains(content, bad) { + t.Fatalf("expected malformed assistant tool markup to be removed from prompt history, found %q in %q", bad, content) + } + } +} + +func TestNormalizeOpenAIMessagesForPrompt_DeveloperRoleMapsToSystem(t *testing.T) { + raw := []any{ + map[string]any{"role": "developer", "content": "必须先走工具调用"}, + map[string]any{"role": "user", "content": "你好"}, + } + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 2 { + t.Fatalf("expected 2 normalized messages, got %d", len(normalized)) + } + if normalized[0]["role"] != "system" { + t.Fatalf("expected developer role converted to system, got %#v", normalized[0]["role"]) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_AssistantArrayContentFallbackWhenTextEmpty(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "content": []any{ + map[string]any{"type": "text", "text": "", "content": "工具说明文本"}, + }, + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected one normalized message, got %d", len(normalized)) + } + content, _ := normalized[0]["content"].(string) + if content != "工具说明文本" { + t.Fatalf("expected content fallback text preserved, got %q", content) + } +} + +func TestNormalizeOpenAIMessagesForPrompt_AssistantReasoningContentPreserved(t *testing.T) { + raw := []any{ + map[string]any{ + "role": "assistant", + "content": "visible answer", + "reasoning_content": "internal reasoning", + }, + } + + normalized := NormalizeOpenAIMessagesForPrompt(raw, "") + if len(normalized) != 1 { + t.Fatalf("expected one normalized assistant message, got %#v", normalized) + } + content, _ := normalized[0]["content"].(string) + if !strings.Contains(content, "[reasoning_content]") { + t.Fatalf("expected labeled reasoning block in assistant content, got %q", content) + } + if !strings.Contains(content, "internal reasoning") { + t.Fatalf("expected reasoning text in assistant content, got %q", content) + } + if !strings.Contains(content, "visible answer") { + t.Fatalf("expected visible answer in assistant content, got %q", content) + } + if reasoningIdx := strings.Index(content, "[reasoning_content]"); reasoningIdx < 0 || reasoningIdx > strings.Index(content, "visible answer") { + t.Fatalf("expected reasoning block before visible answer, got %q", content) + } +} diff --git a/internal/promptcompat/prompt_build.go b/internal/promptcompat/prompt_build.go new file mode 100644 index 0000000000000000000000000000000000000000..8cb056844e8827611fe70064f42944806b57bb32 --- /dev/null +++ b/internal/promptcompat/prompt_build.go @@ -0,0 +1,33 @@ +package promptcompat + +import ( + "ds2api/internal/prompt" +) + +func buildOpenAIFinalPrompt(messagesRaw []any, toolsRaw any, traceID string, thinkingEnabled bool) (string, []string) { + return BuildOpenAIPrompt(messagesRaw, toolsRaw, traceID, DefaultToolChoicePolicy(), thinkingEnabled) +} + +func BuildOpenAIPrompt(messagesRaw []any, toolsRaw any, traceID string, toolPolicy ToolChoicePolicy, thinkingEnabled bool) (string, []string) { + return buildOpenAIPrompt(messagesRaw, toolsRaw, traceID, toolPolicy, thinkingEnabled, true) +} + +func BuildOpenAIPromptWithToolInstructionsOnly(messagesRaw []any, toolsRaw any, traceID string, toolPolicy ToolChoicePolicy, thinkingEnabled bool) (string, []string) { + return buildOpenAIPrompt(messagesRaw, toolsRaw, traceID, toolPolicy, thinkingEnabled, false) +} + +func buildOpenAIPrompt(messagesRaw []any, toolsRaw any, traceID string, toolPolicy ToolChoicePolicy, thinkingEnabled bool, includeToolDescriptions bool) (string, []string) { + messages := NormalizeOpenAIMessagesForPrompt(messagesRaw, traceID) + toolNames := []string{} + if tools, ok := toolsRaw.([]any); ok && len(tools) > 0 { + toolNames = extractToolNames(tools, toolPolicy) + } + return prompt.MessagesPrepareWithThinking(messages, thinkingEnabled), toolNames +} + +// BuildOpenAIPromptForAdapter exposes the OpenAI-compatible prompt building flow so +// other protocol adapters (for example Gemini) can reuse the same tool/history +// normalization logic and remain behavior-compatible with chat/completions. +func BuildOpenAIPromptForAdapter(messagesRaw []any, toolsRaw any, traceID string, thinkingEnabled bool) (string, []string) { + return buildOpenAIFinalPrompt(messagesRaw, toolsRaw, traceID, thinkingEnabled) +} diff --git a/internal/promptcompat/prompt_build_test.go b/internal/promptcompat/prompt_build_test.go new file mode 100644 index 0000000000000000000000000000000000000000..07a437cbee5afc1d99767894256ed75a27361e78 --- /dev/null +++ b/internal/promptcompat/prompt_build_test.go @@ -0,0 +1,212 @@ +package promptcompat + +import ( + "strings" + "testing" +) + +func TestBuildOpenAIFinalPrompt_HandlerPathIncludesToolRoundtripSemantics(t *testing.T) { + messages := []any{ + map[string]any{"role": "user", "content": "查北京天气"}, + map[string]any{ + "role": "assistant", + "tool_calls": []any{ + map[string]any{ + "id": "call_1", + "function": map[string]any{ + "name": "get_weather", + "arguments": "{\"city\":\"beijing\"}", + }, + }, + }, + }, + map[string]any{ + "role": "tool", + "tool_call_id": "call_1", + "name": "get_weather", + "content": map[string]any{"temp": 18, "condition": "sunny"}, + }, + } + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "get_weather", + "description": "Get weather", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + } + + finalPrompt, toolNames := buildOpenAIFinalPrompt(messages, tools, "", false) + if len(toolNames) != 1 || toolNames[0] != "get_weather" { + t.Fatalf("unexpected tool names: %#v", toolNames) + } + if !strings.Contains(finalPrompt, `"condition":"sunny"`) { + t.Fatalf("handler finalPrompt should preserve tool output content: %q", finalPrompt) + } + if !strings.Contains(finalPrompt, "<|DSML|tool_calls>") { + t.Fatalf("handler finalPrompt should preserve assistant tool history: %q", finalPrompt) + } + if !strings.Contains(finalPrompt, `<|DSML|invoke name="get_weather">`) { + t.Fatalf("handler finalPrompt should include tool name history: %q", finalPrompt) + } +} + +func TestBuildOpenAIFinalPrompt_VercelPreparePathKeepsFinalAnswerInstruction(t *testing.T) { + messages := []any{ + map[string]any{"role": "system", "content": "You are helpful"}, + map[string]any{"role": "user", "content": "请调用工具"}, + } + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + } + + finalPrompt, _ := buildOpenAIFinalPrompt(messages, tools, "", false) + if strings.Contains(finalPrompt, "TOOL CALL SCHEME") || strings.Contains(finalPrompt, "<|DSML|tool_calls>") { + t.Fatalf("vercel prepare finalPrompt should not inject tool-call instructions: %q", finalPrompt) + } +} + +func TestBuildOpenAIPromptWithToolInstructionsOnlyOmitsSchemas(t *testing.T) { + messages := []any{ + map[string]any{"role": "system", "content": "You are helpful"}, + map[string]any{"role": "user", "content": "请调用工具"}, + } + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + } + + finalPrompt, toolNames := BuildOpenAIPromptWithToolInstructionsOnly(messages, tools, "", DefaultToolChoicePolicy(), false) + if len(toolNames) != 1 || toolNames[0] != "search" { + t.Fatalf("unexpected tool names: %#v", toolNames) + } + if strings.Contains(finalPrompt, "You have access to these tools") || strings.Contains(finalPrompt, "TOOL CALL SCHEME") { + t.Fatalf("expected tool prompts to be omitted entirely, got: %q", finalPrompt) + } +} + +func TestBuildOpenAIToolsContextTranscriptContainsOnlyDescriptions(t *testing.T) { + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + } + + transcript, toolNames := BuildOpenAIToolsContextTranscript(tools, DefaultToolChoicePolicy()) + if len(toolNames) != 1 || toolNames[0] != "search" { + t.Fatalf("unexpected tool names: %#v", toolNames) + } + if !strings.Contains(transcript, "# context_tools.txt") || !strings.Contains(transcript, "Tool: search") { + t.Fatalf("expected tools transcript to include tool schema, got: %q", transcript) + } +} + +func TestBuildOpenAIFinalPromptPrependsOutputIntegrityGuard(t *testing.T) { + messages := []any{ + map[string]any{"role": "system", "content": "You are helpful"}, + map[string]any{"role": "user", "content": "请调用工具"}, + } + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search docs", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + } + + finalPrompt, _ := buildOpenAIFinalPrompt(messages, tools, "", false) + if strings.Contains(finalPrompt, "TOOL CALL SCHEME") { + t.Fatalf("expected prompt guards to be omitted, got: %q", finalPrompt) + } +} + +func TestBuildOpenAIFinalPromptReadLikeToolIncludesCacheGuard(t *testing.T) { + messages := []any{ + map[string]any{"role": "user", "content": "请读取文件"}, + } + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "read_file", + "description": "Read a file", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + } + + finalPrompt, _ := buildOpenAIFinalPrompt(messages, tools, "", false) + if strings.Contains(finalPrompt, "Read-tool cache guard") { + t.Fatalf("expected read-tool cache guard to be omitted: %q", finalPrompt) + } +} + +func TestBuildOpenAIFinalPromptNonReadToolOmitsCacheGuard(t *testing.T) { + messages := []any{ + map[string]any{"role": "user", "content": "搜索一下"}, + } + tools := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "Search docs", + "parameters": map[string]any{ + "type": "object", + }, + }, + }, + } + + finalPrompt, _ := buildOpenAIFinalPrompt(messages, tools, "", false) + if strings.Contains(finalPrompt, "Read-tool cache guard") { + t.Fatalf("non-read tool prompt should not include read cache guard: %q", finalPrompt) + } +} + +func TestBuildOpenAIFinalPromptWithThinkingKeepsPromptUnchanged(t *testing.T) { + messages := []any{ + map[string]any{"role": "user", "content": "继续回答上一个问题"}, + } + + finalPromptThinking, _ := buildOpenAIFinalPrompt(messages, nil, "", true) + finalPromptPlain, _ := buildOpenAIFinalPrompt(messages, nil, "", false) + if finalPromptThinking != finalPromptPlain { + t.Fatalf("expected thinking flag not to prepend continuation contract, thinking=%q plain=%q", finalPromptThinking, finalPromptPlain) + } +} diff --git a/internal/promptcompat/request_normalize.go b/internal/promptcompat/request_normalize.go new file mode 100644 index 0000000000000000000000000000000000000000..ec7b2ffce8296ad7435153eac5ebb5d387ffcd54 --- /dev/null +++ b/internal/promptcompat/request_normalize.go @@ -0,0 +1,378 @@ +package promptcompat + +import ( + "fmt" + "strings" + + "ds2api/internal/config" + "ds2api/internal/util" +) + +type ConfigReader interface { + ModelAliases() map[string]string +} + +func NormalizeOpenAIChatRequest(store ConfigReader, req map[string]any, traceID string) (StandardRequest, error) { + model, _ := req["model"].(string) + messagesRaw, _ := req["messages"].([]any) + if strings.TrimSpace(model) == "" || len(messagesRaw) == 0 { + return StandardRequest{}, fmt.Errorf("request must include 'model' and 'messages'") + } + resolvedModel, ok := config.ResolveModel(store, model) + if !ok { + return StandardRequest{}, fmt.Errorf("model %q is not available", model) + } + defaultThinkingEnabled, searchEnabled, _ := config.GetModelConfig(resolvedModel) + thinkingEnabled := util.ResolveThinkingEnabled(req, defaultThinkingEnabled) + if config.IsNoThinkingModel(resolvedModel) { + thinkingEnabled = false + } + responseModel := strings.TrimSpace(model) + if responseModel == "" { + responseModel = resolvedModel + } + toolPolicy := DefaultToolChoicePolicy() + finalPrompt, toolNames := BuildOpenAIPrompt(messagesRaw, req["tools"], traceID, toolPolicy, thinkingEnabled) + toolNames = ensureToolDetectionEnabled(toolNames, req["tools"]) + passThrough := collectOpenAIChatPassThrough(req) + refFileIDs := CollectOpenAIRefFileIDs(req) + + return StandardRequest{ + Surface: "openai_chat", + RequestedModel: strings.TrimSpace(model), + ResolvedModel: resolvedModel, + ResponseModel: responseModel, + Messages: messagesRaw, + PromptTokenText: finalPrompt, + ToolsRaw: req["tools"], + FinalPrompt: finalPrompt, + ToolNames: toolNames, + ToolChoice: toolPolicy, + Stream: util.ToBool(req["stream"]), + Thinking: thinkingEnabled, + Search: searchEnabled, + RefFileIDs: refFileIDs, + RefFileTokens: estimateInlineFileTokens(req), + PassThrough: passThrough, + }, nil +} + +func NormalizeOpenAIResponsesRequest(store ConfigReader, req map[string]any, traceID string) (StandardRequest, error) { + model, _ := req["model"].(string) + model = strings.TrimSpace(model) + if model == "" { + return StandardRequest{}, fmt.Errorf("request must include 'model'") + } + resolvedModel, ok := config.ResolveModel(store, model) + if !ok { + return StandardRequest{}, fmt.Errorf("model %q is not available", model) + } + defaultThinkingEnabled, searchEnabled, _ := config.GetModelConfig(resolvedModel) + thinkingEnabled := util.ResolveThinkingEnabled(req, defaultThinkingEnabled) + if config.IsNoThinkingModel(resolvedModel) { + thinkingEnabled = false + } + + messagesRaw := ResponsesMessagesFromRequest(req) + if len(messagesRaw) == 0 { + return StandardRequest{}, fmt.Errorf("request must include 'input' or 'messages'") + } + toolPolicy, err := parseToolChoicePolicy(req["tool_choice"], req["tools"]) + if err != nil { + return StandardRequest{}, err + } + finalPrompt, toolNames := BuildOpenAIPrompt(messagesRaw, req["tools"], traceID, toolPolicy, thinkingEnabled) + toolNames = ensureToolDetectionEnabled(toolNames, req["tools"]) + if !toolPolicy.IsNone() { + toolPolicy.Allowed = namesToSet(toolNames) + } + passThrough := collectOpenAIChatPassThrough(req) + refFileIDs := CollectOpenAIRefFileIDs(req) + + return StandardRequest{ + Surface: "openai_responses", + RequestedModel: model, + ResolvedModel: resolvedModel, + ResponseModel: model, + Messages: messagesRaw, + PromptTokenText: finalPrompt, + ToolsRaw: req["tools"], + FinalPrompt: finalPrompt, + ToolNames: toolNames, + ToolChoice: toolPolicy, + Stream: util.ToBool(req["stream"]), + Thinking: thinkingEnabled, + Search: searchEnabled, + RefFileIDs: refFileIDs, + RefFileTokens: estimateInlineFileTokens(req), + PassThrough: passThrough, + }, nil +} + +func ensureToolDetectionEnabled(toolNames []string, toolsRaw any) []string { + if len(toolNames) > 0 { + return toolNames + } + tools, _ := toolsRaw.([]any) + if len(tools) == 0 { + return toolNames + } + // Keep stream sieve/tool buffering enabled even when client tool schemas + // are malformed or lack explicit names; parsed tool payload names are no + // longer filtered by this list. + return []string{"__any_tool__"} +} + +func collectOpenAIChatPassThrough(req map[string]any) map[string]any { + out := map[string]any{} + for _, k := range []string{ + "temperature", + "top_p", + "max_tokens", + "max_completion_tokens", + "presence_penalty", + "frequency_penalty", + "stop", + } { + if v, ok := req[k]; ok { + out[k] = v + } + } + return out +} + +func parseToolChoicePolicy(toolChoiceRaw any, toolsRaw any) (ToolChoicePolicy, error) { + policy := DefaultToolChoicePolicy() + declaredNames := extractDeclaredToolNames(toolsRaw) + declaredSet := namesToSet(declaredNames) + if len(declaredNames) > 0 { + policy.Allowed = declaredSet + } + + if toolChoiceRaw == nil { + return policy, nil + } + + switch v := toolChoiceRaw.(type) { + case string: + switch strings.ToLower(strings.TrimSpace(v)) { + case "", "auto": + policy.Mode = ToolChoiceAuto + case "none": + policy.Mode = ToolChoiceNone + policy.Allowed = nil + case "required": + policy.Mode = ToolChoiceRequired + default: + return ToolChoicePolicy{}, fmt.Errorf("unsupported tool_choice: %q", v) + } + case map[string]any: + allowedOverride, hasAllowedOverride, err := parseAllowedToolNames(v["allowed_tools"]) + if err != nil { + return ToolChoicePolicy{}, err + } + if hasAllowedOverride { + filtered := make([]string, 0, len(allowedOverride)) + for _, name := range allowedOverride { + if _, ok := declaredSet[name]; !ok { + return ToolChoicePolicy{}, fmt.Errorf("tool_choice.allowed_tools contains undeclared tool %q", name) + } + filtered = append(filtered, name) + } + policy.Allowed = namesToSet(filtered) + } + + typ := strings.ToLower(strings.TrimSpace(asString(v["type"]))) + switch typ { + case "", "auto": + if hasFunctionSelector(v) { + name, err := parseForcedToolName(v) + if err != nil { + return ToolChoicePolicy{}, err + } + policy.Mode = ToolChoiceForced + policy.ForcedName = name + policy.Allowed = namesToSet([]string{name}) + } else { + policy.Mode = ToolChoiceAuto + } + case "none": + policy.Mode = ToolChoiceNone + policy.Allowed = nil + case "required": + policy.Mode = ToolChoiceRequired + case "function": + name, err := parseForcedToolName(v) + if err != nil { + return ToolChoicePolicy{}, err + } + policy.Mode = ToolChoiceForced + policy.ForcedName = name + policy.Allowed = namesToSet([]string{name}) + default: + return ToolChoicePolicy{}, fmt.Errorf("unsupported tool_choice.type: %q", typ) + } + default: + return ToolChoicePolicy{}, fmt.Errorf("tool_choice must be a string or object") + } + + if policy.Mode == ToolChoiceRequired || policy.Mode == ToolChoiceForced { + if len(declaredNames) == 0 { + return ToolChoicePolicy{}, fmt.Errorf("tool_choice=%s requires non-empty tools", policy.Mode) + } + } + if policy.Mode == ToolChoiceForced { + if _, ok := declaredSet[policy.ForcedName]; !ok { + return ToolChoicePolicy{}, fmt.Errorf("tool_choice forced function %q is not declared in tools", policy.ForcedName) + } + } + if len(policy.Allowed) == 0 && (policy.Mode == ToolChoiceRequired || policy.Mode == ToolChoiceForced) { + return ToolChoicePolicy{}, fmt.Errorf("tool_choice policy resolved to empty allowed tool set") + } + return policy, nil +} + +func parseForcedToolName(v map[string]any) (string, error) { + if name := strings.TrimSpace(asString(v["name"])); name != "" { + return name, nil + } + if fn, ok := v["function"].(map[string]any); ok { + if name := strings.TrimSpace(asString(fn["name"])); name != "" { + return name, nil + } + } + return "", fmt.Errorf("tool_choice function requires name") +} + +func parseAllowedToolNames(raw any) ([]string, bool, error) { + if raw == nil { + return nil, false, nil + } + collectName := func(v any) string { + if name := strings.TrimSpace(asString(v)); name != "" { + return name + } + if m, ok := v.(map[string]any); ok { + if name := strings.TrimSpace(asString(m["name"])); name != "" { + return name + } + if fn, ok := m["function"].(map[string]any); ok { + if name := strings.TrimSpace(asString(fn["name"])); name != "" { + return name + } + } + } + return "" + } + + names := []string{} + switch x := raw.(type) { + case []any: + for _, item := range x { + name := collectName(item) + if name == "" { + return nil, true, fmt.Errorf("tool_choice.allowed_tools contains invalid item") + } + names = append(names, name) + } + case []string: + for _, item := range x { + name := strings.TrimSpace(item) + if name == "" { + return nil, true, fmt.Errorf("tool_choice.allowed_tools contains empty name") + } + names = append(names, name) + } + default: + return nil, true, fmt.Errorf("tool_choice.allowed_tools must be an array") + } + + if len(names) == 0 { + return nil, true, fmt.Errorf("tool_choice.allowed_tools must not be empty") + } + return names, true, nil +} + +func hasFunctionSelector(v map[string]any) bool { + if strings.TrimSpace(asString(v["name"])) != "" { + return true + } + if fn, ok := v["function"].(map[string]any); ok { + return strings.TrimSpace(asString(fn["name"])) != "" + } + return false +} + +func extractDeclaredToolNames(toolsRaw any) []string { + tools, ok := toolsRaw.([]any) + if !ok || len(tools) == 0 { + return nil + } + out := make([]string, 0, len(tools)) + seen := map[string]struct{}{} + for _, t := range tools { + tool, ok := t.(map[string]any) + if !ok { + continue + } + fn, _ := tool["function"].(map[string]any) + if len(fn) == 0 { + fn = tool + } + name := strings.TrimSpace(asString(fn["name"])) + if name == "" { + continue + } + if _, ok := seen[name]; ok { + continue + } + seen[name] = struct{}{} + out = append(out, name) + } + return out +} + +func namesToSet(names []string) map[string]struct{} { + if len(names) == 0 { + return nil + } + out := make(map[string]struct{}, len(names)) + for _, name := range names { + trimmed := strings.TrimSpace(name) + if trimmed == "" { + continue + } + out[trimmed] = struct{}{} + } + if len(out) == 0 { + return nil + } + return out +} + +// estimateInlineFileTokens extracts the byte count stashed by PreprocessInlineFileInputs +// and converts it to a conservative token estimate. Inline files are typically images or +// documents that the upstream model will process; we use bytes/3 (rather than bytes/4) +// as a slightly pessimistic approximation so the returned context token count stays +// safely above the real value. +func estimateInlineFileTokens(req map[string]any) int { + raw, ok := req["_inline_file_bytes"] + if !ok { + return 0 + } + var bytes int + switch v := raw.(type) { + case int: + bytes = v + case int64: + bytes = int(v) + case float64: + bytes = int(v) + default: + return 0 + } + if bytes <= 0 { + return 0 + } + return bytes / 3 +} diff --git a/internal/promptcompat/responses_input_items.go b/internal/promptcompat/responses_input_items.go new file mode 100644 index 0000000000000000000000000000000000000000..92139d377ec2bddbc83453e303e9f5c1603e7ca7 --- /dev/null +++ b/internal/promptcompat/responses_input_items.go @@ -0,0 +1,223 @@ +package promptcompat + +import ( + "fmt" + "strings" + + "ds2api/internal/config" + "ds2api/internal/prompt" +) + +func normalizeResponsesInputItem(m map[string]any) map[string]any { + return normalizeResponsesInputItemWithState(m, nil) +} + +func normalizeResponsesInputItemWithState(m map[string]any, callNameByID map[string]string) map[string]any { + if m == nil { + return nil + } + + role := strings.ToLower(strings.TrimSpace(asString(m["role"]))) + if role != "" { + if role == "assistant" { + return normalizeResponsesAssistantMessage(m) + } + content := m["content"] + if content == nil { + if txt, _ := m["text"].(string); strings.TrimSpace(txt) != "" { + content = txt + } + } + if content == nil { + return nil + } + out := map[string]any{ + "role": normalizeOpenAIRoleForPrompt(role), + "content": content, + } + if role == "tool" || role == "function" { + if callID := strings.TrimSpace(asString(m["tool_call_id"])); callID != "" { + out["tool_call_id"] = callID + } + if callID := strings.TrimSpace(asString(m["call_id"])); callID != "" { + out["tool_call_id"] = callID + } + if name := strings.TrimSpace(asString(m["name"])); name != "" { + out["name"] = name + } + } + return out + } + + itemType := strings.ToLower(strings.TrimSpace(asString(m["type"]))) + switch itemType { + case "message", "input_message": + role := strings.ToLower(strings.TrimSpace(asString(m["role"]))) + if role == "assistant" { + return normalizeResponsesAssistantMessage(m) + } + content := m["content"] + if content == nil { + if txt, _ := m["text"].(string); strings.TrimSpace(txt) != "" { + content = txt + } + } + if content == nil { + return nil + } + if role == "" { + role = "user" + } + return map[string]any{ + "role": normalizeOpenAIRoleForPrompt(role), + "content": content, + } + case "function_call_output", "tool_result": + content := m["output"] + if content == nil { + content = m["content"] + } + if content == nil { + content = "" + } + out := map[string]any{ + "role": "tool", + "content": content, + } + if callID := strings.TrimSpace(asString(m["call_id"])); callID != "" { + out["tool_call_id"] = callID + } else if callID = strings.TrimSpace(asString(m["tool_call_id"])); callID != "" { + out["tool_call_id"] = callID + } + if name := strings.TrimSpace(asString(m["name"])); name != "" { + out["name"] = name + } else if name = strings.TrimSpace(asString(m["tool_name"])); name != "" { + out["name"] = name + } else if callID := strings.TrimSpace(asString(out["tool_call_id"])); callID != "" { + if inferred := strings.TrimSpace(callNameByID[callID]); inferred != "" { + out["name"] = inferred + } else { + config.Logger.Warn( + "[responses] unable to backfill tool result name from call_id", + "call_id", callID, + ) + } + } + return out + case "function_call", "tool_call": + name := strings.TrimSpace(asString(m["name"])) + var fn map[string]any + if rawFn, ok := m["function"].(map[string]any); ok { + fn = rawFn + if name == "" { + name = strings.TrimSpace(asString(fn["name"])) + } + } + if name == "" { + return nil + } + + var argsRaw any + if v, ok := m["arguments"]; ok { + argsRaw = v + } else if v, ok := m["input"]; ok { + argsRaw = v + } + if argsRaw == nil && fn != nil { + if v, ok := fn["arguments"]; ok { + argsRaw = v + } else if v, ok := fn["input"]; ok { + argsRaw = v + } + } + + functionPayload := map[string]any{ + "name": name, + "arguments": prompt.StringifyToolCallArguments(argsRaw), + } + call := map[string]any{ + "type": "function", + "function": functionPayload, + } + if callID := strings.TrimSpace(asString(m["call_id"])); callID != "" { + call["id"] = callID + } else if callID = strings.TrimSpace(asString(m["id"])); callID != "" { + call["id"] = callID + } + if callID := strings.TrimSpace(asString(call["id"])); callID != "" && callNameByID != nil { + callNameByID[callID] = name + } + return map[string]any{ + "role": "assistant", + "tool_calls": []any{call}, + } + case "input_text": + if txt, _ := m["text"].(string); strings.TrimSpace(txt) != "" { + return map[string]any{ + "role": "user", + "content": txt, + } + } + } + + if txt, _ := m["text"].(string); strings.TrimSpace(txt) != "" { + return map[string]any{ + "role": "user", + "content": txt, + } + } + if content, ok := m["content"]; ok { + if strings.TrimSpace(NormalizeOpenAIContentForPrompt(content)) != "" { + return map[string]any{ + "role": "user", + "content": content, + } + } + } + return nil +} + +func normalizeResponsesAssistantMessage(m map[string]any) map[string]any { + out := map[string]any{ + "role": "assistant", + } + if toolCalls, ok := m["tool_calls"].([]any); ok && len(toolCalls) > 0 { + out["tool_calls"] = toolCalls + } + content := m["content"] + if content == nil { + if txt, _ := m["text"].(string); strings.TrimSpace(txt) != "" { + content = txt + } + } + if content != nil { + out["content"] = content + } + if reasoning := strings.TrimSpace(normalizeOpenAIReasoningContentForPrompt(m["reasoning_content"])); reasoning != "" { + out["reasoning_content"] = m["reasoning_content"] + } + if _, hasToolCalls := out["tool_calls"]; hasToolCalls || out["content"] != nil || out["reasoning_content"] != nil { + return out + } + return nil +} + +func normalizeResponsesFallbackPart(m map[string]any) string { + if m == nil { + return "" + } + if t, _ := m["type"].(string); strings.EqualFold(strings.TrimSpace(t), "input_text") { + if txt, _ := m["text"].(string); strings.TrimSpace(txt) != "" { + return txt + } + } + if txt, _ := m["text"].(string); strings.TrimSpace(txt) != "" { + return txt + } + if content, ok := m["content"]; ok { + if normalized := strings.TrimSpace(NormalizeOpenAIContentForPrompt(content)); normalized != "" { + return normalized + } + } + return strings.TrimSpace(fmt.Sprintf("%v", m)) +} diff --git a/internal/promptcompat/responses_input_items_test.go b/internal/promptcompat/responses_input_items_test.go new file mode 100644 index 0000000000000000000000000000000000000000..81c215748d85d9c0092d2d14efcf1db281949474 --- /dev/null +++ b/internal/promptcompat/responses_input_items_test.go @@ -0,0 +1,94 @@ +package promptcompat + +import ( + "strings" + "testing" +) + +func TestNormalizeResponsesInputItemPreservesAssistantReasoningContent(t *testing.T) { + item := map[string]any{ + "role": "assistant", + "reasoning_content": "hidden reasoning", + "tool_calls": []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "search", + "arguments": `{"q":"docs"}`, + }, + }, + }, + } + + got := normalizeResponsesInputItem(item) + if got == nil { + t.Fatal("expected assistant item to be preserved") + } + if got["role"] != "assistant" { + t.Fatalf("unexpected role: %#v", got["role"]) + } + if got["reasoning_content"] != "hidden reasoning" { + t.Fatalf("expected reasoning_content preserved, got %#v", got["reasoning_content"]) + } +} + +func TestNormalizeResponsesInputItemAssistantMessageWithReasoningBlocks(t *testing.T) { + item := map[string]any{ + "type": "message", + "role": "assistant", + "content": []any{ + map[string]any{"type": "reasoning", "text": "internal chain"}, + map[string]any{"type": "output_text", "text": "visible answer"}, + }, + } + + got := normalizeResponsesInputItem(item) + if got == nil { + t.Fatal("expected assistant message item to be preserved") + } + content, _ := got["content"].([]any) + if len(content) != 2 { + t.Fatalf("expected content blocks preserved, got %#v", got["content"]) + } +} + +func TestNormalizeResponsesInputArrayMergesReasoningMessageIntoFunctionCallHistory(t *testing.T) { + input := []any{ + map[string]any{ + "type": "message", + "role": "assistant", + "content": []any{ + map[string]any{"type": "reasoning", "text": "need fresh docs before answering"}, + }, + }, + map[string]any{ + "type": "function_call", + "call_id": "call_search", + "name": "search_web", + "arguments": `{"query":"docs"}`, + }, + } + + got := NormalizeResponsesInputAsMessages(input) + if len(got) != 1 { + t.Fatalf("expected reasoning and function_call merged into one assistant message, got %#v", got) + } + msg, _ := got[0].(map[string]any) + if msg["role"] != "assistant" { + t.Fatalf("expected assistant message, got %#v", msg) + } + if msg["reasoning_content"] != "need fresh docs before answering" { + t.Fatalf("expected reasoning_content on tool-call message, got %#v", msg) + } + toolCalls, _ := msg["tool_calls"].([]any) + if len(toolCalls) != 1 { + t.Fatalf("expected one tool call, got %#v", msg["tool_calls"]) + } + history := BuildOpenAIHistoryTranscript(got) + if !strings.Contains(history, "[reasoning_content]\nneed fresh docs before answering\n[/reasoning_content]") { + t.Fatalf("expected reasoning in history transcript, got %q", history) + } + if !strings.Contains(history, `<|DSML|invoke name="search_web">`) { + t.Fatalf("expected tool call in history transcript, got %q", history) + } +} diff --git a/internal/promptcompat/responses_input_normalize.go b/internal/promptcompat/responses_input_normalize.go new file mode 100644 index 0000000000000000000000000000000000000000..1e099e3e1778a07cc3d72dabe3090574d80effc7 --- /dev/null +++ b/internal/promptcompat/responses_input_normalize.go @@ -0,0 +1,173 @@ +package promptcompat + +import ( + "fmt" + "strings" +) + +func ResponsesMessagesFromRequest(req map[string]any) []any { + if msgs, ok := req["messages"].([]any); ok && len(msgs) > 0 { + return prependInstructionMessage(msgs, req["instructions"]) + } + if rawInput, ok := req["input"]; ok { + if msgs := NormalizeResponsesInputAsMessages(rawInput); len(msgs) > 0 { + return prependInstructionMessage(msgs, req["instructions"]) + } + } + return nil +} + +func prependInstructionMessage(messages []any, instructions any) []any { + sys, _ := instructions.(string) + sys = strings.TrimSpace(sys) + if sys == "" { + return messages + } + out := make([]any, 0, len(messages)+1) + out = append(out, map[string]any{"role": "system", "content": sys}) + out = append(out, messages...) + return out +} + +func NormalizeResponsesInputAsMessages(input any) []any { + switch v := input.(type) { + case string: + if strings.TrimSpace(v) == "" { + return nil + } + return []any{map[string]any{"role": "user", "content": v}} + case []any: + return normalizeResponsesInputArray(v) + case map[string]any: + if msg := normalizeResponsesInputItem(v); msg != nil { + return []any{msg} + } + if txt, _ := v["text"].(string); strings.TrimSpace(txt) != "" { + return []any{map[string]any{"role": "user", "content": txt}} + } + if content, ok := v["content"]; ok { + if strings.TrimSpace(NormalizeOpenAIContentForPrompt(content)) != "" { + return []any{map[string]any{"role": "user", "content": content}} + } + } + } + return nil +} + +func normalizeResponsesInputArray(items []any) []any { + if len(items) == 0 { + return nil + } + out := make([]any, 0, len(items)) + callNameByID := map[string]string{} + fallbackParts := make([]string, 0, len(items)) + pendingAssistantReasoning := "" + flushFallback := func() { + if len(fallbackParts) == 0 { + return + } + if pendingAssistantReasoning != "" { + out = append(out, map[string]any{"role": "assistant", "reasoning_content": pendingAssistantReasoning}) + pendingAssistantReasoning = "" + } + out = append(out, map[string]any{"role": "user", "content": strings.Join(fallbackParts, "\n")}) + fallbackParts = fallbackParts[:0] + } + flushPendingReasoning := func() { + if pendingAssistantReasoning == "" { + return + } + out = append(out, map[string]any{"role": "assistant", "reasoning_content": pendingAssistantReasoning}) + pendingAssistantReasoning = "" + } + + for _, item := range items { + switch x := item.(type) { + case map[string]any: + if msg := normalizeResponsesInputItemWithState(x, callNameByID); msg != nil { + if reasoning := assistantReasoningOnlyContent(msg); reasoning != "" { + if pendingAssistantReasoning == "" { + pendingAssistantReasoning = reasoning + } else { + pendingAssistantReasoning += "\n" + reasoning + } + continue + } + if isAssistantToolCallMessage(msg) && pendingAssistantReasoning != "" { + if strings.TrimSpace(normalizeOpenAIReasoningContentForPrompt(msg["reasoning_content"])) == "" { + msg["reasoning_content"] = pendingAssistantReasoning + } + pendingAssistantReasoning = "" + } else { + flushPendingReasoning() + } + flushFallback() + if isAssistantToolCallMessage(msg) && len(out) > 0 { + if merged := mergeResponsesAssistantToolCalls(out[len(out)-1], msg); merged { + continue + } + } + out = append(out, msg) + continue + } + if s := normalizeResponsesFallbackPart(x); s != "" { + fallbackParts = append(fallbackParts, s) + } + default: + if s := strings.TrimSpace(fmt.Sprintf("%v", item)); s != "" { + fallbackParts = append(fallbackParts, s) + } + } + } + flushPendingReasoning() + flushFallback() + if len(out) == 0 { + return nil + } + return out +} + +func assistantReasoningOnlyContent(msg map[string]any) string { + if !isAssistantMessage(msg) || isAssistantToolCallMessage(msg) { + return "" + } + if _, hasContent := msg["content"]; hasContent { + normalizedContent := strings.TrimSpace(NormalizeOpenAIContentForPrompt(msg["content"])) + reasoningFromContent := strings.TrimSpace(extractOpenAIReasoningContentFromMessage(msg["content"])) + if normalizedContent != "" && normalizedContent != reasoningFromContent { + return "" + } + if reasoningFromContent != "" { + return reasoningFromContent + } + } + return strings.TrimSpace(normalizeOpenAIReasoningContentForPrompt(msg["reasoning_content"])) +} + +func isAssistantMessage(msg map[string]any) bool { + return strings.EqualFold(strings.TrimSpace(asString(msg["role"])), "assistant") +} + +func isAssistantToolCallMessage(msg map[string]any) bool { + if !isAssistantMessage(msg) { + return false + } + toolCalls, ok := msg["tool_calls"].([]any) + return ok && len(toolCalls) > 0 +} + +func mergeResponsesAssistantToolCalls(prev any, next map[string]any) bool { + prevMsg, ok := prev.(map[string]any) + if !ok || !isAssistantToolCallMessage(prevMsg) || !isAssistantToolCallMessage(next) { + return false + } + prevCalls, _ := prevMsg["tool_calls"].([]any) + nextCalls, _ := next["tool_calls"].([]any) + prevMsg["tool_calls"] = append(prevCalls, nextCalls...) + if strings.TrimSpace(normalizeOpenAIReasoningContentForPrompt(prevMsg["reasoning_content"])) == "" { + if reasoning := strings.TrimSpace(normalizeOpenAIReasoningContentForPrompt(next["reasoning_content"])); reasoning != "" { + prevMsg["reasoning_content"] = reasoning + } + } + return true +} diff --git a/internal/promptcompat/standard_request.go b/internal/promptcompat/standard_request.go new file mode 100644 index 0000000000000000000000000000000000000000..f5910ed58608a6e1a1e3d6dd5327d17510585b0c --- /dev/null +++ b/internal/promptcompat/standard_request.go @@ -0,0 +1,96 @@ +package promptcompat + +import "ds2api/internal/config" + +type StandardRequest struct { + Surface string + RequestedModel string + ResolvedModel string + ResponseModel string + Messages []any + HistoryText string + PromptTokenText string + CurrentInputFileApplied bool + CurrentInputFileID string + CurrentInputFilename string + CurrentToolsFileID string + ToolsRaw any + FinalPrompt string + ToolNames []string + ToolChoice ToolChoicePolicy + Stream bool + Thinking bool + Search bool + RefFileIDs []string + RefFileTokens int + PassThrough map[string]any +} + +type ToolChoiceMode string + +const ( + ToolChoiceAuto ToolChoiceMode = "auto" + ToolChoiceNone ToolChoiceMode = "none" + ToolChoiceRequired ToolChoiceMode = "required" + ToolChoiceForced ToolChoiceMode = "forced" +) + +type ToolChoicePolicy struct { + Mode ToolChoiceMode + ForcedName string + Allowed map[string]struct{} +} + +func DefaultToolChoicePolicy() ToolChoicePolicy { + return ToolChoicePolicy{Mode: ToolChoiceAuto} +} + +func (p ToolChoicePolicy) IsNone() bool { + return p.Mode == ToolChoiceNone +} + +func (p ToolChoicePolicy) IsRequired() bool { + return p.Mode == ToolChoiceRequired || p.Mode == ToolChoiceForced +} + +func (p ToolChoicePolicy) Allows(name string) bool { + if len(p.Allowed) == 0 { + return true + } + _, ok := p.Allowed[name] + return ok +} + +func (r StandardRequest) CompletionPayload(sessionID string) map[string]any { + modelID := r.ResolvedModel + if modelID == "" { + modelID = r.RequestedModel + } + modelType := "default" + if resolvedType, ok := config.GetModelType(modelID); ok { + modelType = resolvedType + } + refFileIDs := make([]any, 0, len(r.RefFileIDs)) + for _, fileID := range r.RefFileIDs { + if fileID == "" { + continue + } + refFileIDs = append(refFileIDs, fileID) + } + payload := map[string]any{ + "chat_session_id": sessionID, + "model_type": modelType, + "parent_message_id": nil, + "prompt": r.FinalPrompt, + "audio_id": nil, + "preempt": false, + "action": nil, + "ref_file_ids": refFileIDs, + "thinking_enabled": r.Thinking, + "search_enabled": r.Search, + } + for k, v := range r.PassThrough { + payload[k] = v + } + return payload +} diff --git a/internal/promptcompat/standard_request_test.go b/internal/promptcompat/standard_request_test.go new file mode 100644 index 0000000000000000000000000000000000000000..fe5ea06252d02c40435d52f43606f20d805441e0 --- /dev/null +++ b/internal/promptcompat/standard_request_test.go @@ -0,0 +1,67 @@ +package promptcompat + +import "testing" + +func TestStandardRequestCompletionPayloadSetsModelTypeFromResolvedModel(t *testing.T) { + tests := []struct { + name string + model string + thinking bool + search bool + modelType string + }{ + {name: "default", model: "deepseek-v4-flash", thinking: false, search: false, modelType: "default"}, + {name: "default_nothinking", model: "deepseek-v4-flash-nothinking", thinking: false, search: false, modelType: "default"}, + {name: "expert", model: "deepseek-v4-pro", thinking: true, search: false, modelType: "expert"}, + {name: "vision", model: "deepseek-v4-vision", thinking: true, search: false, modelType: "vision"}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + req := StandardRequest{ + ResolvedModel: tc.model, + FinalPrompt: "hello", + Thinking: tc.thinking, + Search: tc.search, + RefFileIDs: []string{"file-a", "file-b"}, + PassThrough: map[string]any{ + "temperature": 0.3, + }, + } + + payload := req.CompletionPayload("session-123") + + if got := payload["model_type"]; got != tc.modelType { + t.Fatalf("expected model_type %s, got %#v", tc.modelType, got) + } + if got := payload["chat_session_id"]; got != "session-123" { + t.Fatalf("unexpected chat_session_id: %#v", got) + } + if got := payload["thinking_enabled"]; got != tc.thinking { + t.Fatalf("unexpected thinking_enabled: %#v", got) + } + if got := payload["search_enabled"]; got != tc.search { + t.Fatalf("unexpected search_enabled: %#v", got) + } + if _, ok := payload["audio_id"]; !ok { + t.Fatalf("expected audio_id to be present") + } + if got := payload["preempt"]; got != false { + t.Fatalf("expected preempt=false, got %#v", got) + } + if _, ok := payload["action"]; !ok { + t.Fatalf("expected action to be present") + } + if got := payload["temperature"]; got != 0.3 { + t.Fatalf("expected passthrough temperature, got %#v", got) + } + refFileIDs, ok := payload["ref_file_ids"].([]any) + if !ok { + t.Fatalf("expected ref_file_ids slice, got %#v", payload["ref_file_ids"]) + } + if len(refFileIDs) != 2 || refFileIDs[0] != "file-a" || refFileIDs[1] != "file-b" { + t.Fatalf("unexpected ref_file_ids: %#v", refFileIDs) + } + }) + } +} diff --git a/internal/promptcompat/thinking_injection.go b/internal/promptcompat/thinking_injection.go new file mode 100644 index 0000000000000000000000000000000000000000..a33fae614ac7202b95db5244e9ad5ad75fd0bda4 --- /dev/null +++ b/internal/promptcompat/thinking_injection.go @@ -0,0 +1,16 @@ +package promptcompat + +const ( + ThinkingInjectionMarker = "Reason carefully and provide a thorough response." + DefaultThinkingInjectionPrompt = ThinkingInjectionMarker + "\n" + + "Think step by step, cover edge cases, and verify your reasoning before concluding.\n" + + "Explain the key steps you took to reach the final answer." +) + +func AppendThinkingInjectionToLatestUser(messages []any) ([]any, bool) { + return messages, false +} + +func AppendThinkingInjectionPromptToLatestUser(messages []any, injectionPrompt string) ([]any, bool) { + return messages, false +} diff --git a/internal/promptcompat/thinking_injection_test.go b/internal/promptcompat/thinking_injection_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f72338bfbe8e5d6e877eecdfe71cbe3a33642dd0 --- /dev/null +++ b/internal/promptcompat/thinking_injection_test.go @@ -0,0 +1,66 @@ +package promptcompat + +import "testing" + +func TestAppendThinkingInjectionToLatestUserStringContent(t *testing.T) { + messages := []any{ + map[string]any{"role": "user", "content": "older"}, + map[string]any{"role": "assistant", "content": "ok"}, + map[string]any{"role": "user", "content": "latest"}, + } + + out, changed := AppendThinkingInjectionToLatestUser(messages) + if changed { + t.Fatal("expected thinking injection to be disabled") + } + if len(out) != len(messages) { + t.Fatalf("expected messages unchanged, got %#v", out) + } +} + +func TestAppendThinkingInjectionToLatestUserArrayContent(t *testing.T) { + messages := []any{ + map[string]any{ + "role": "user", + "content": []any{ + map[string]any{"type": "text", "text": "latest"}, + }, + }, + } + + out, changed := AppendThinkingInjectionToLatestUser(messages) + if changed { + t.Fatal("expected thinking injection to be disabled") + } + if len(out) != len(messages) { + t.Fatalf("expected messages unchanged, got %#v", out) + } +} + +func TestAppendThinkingInjectionToLatestUserCustomPrompt(t *testing.T) { + messages := []any{ + map[string]any{"role": "user", "content": "latest"}, + } + + out, changed := AppendThinkingInjectionPromptToLatestUser(messages, "custom thinking format") + if changed { + t.Fatal("expected custom thinking injection to be disabled") + } + if len(out) != len(messages) { + t.Fatalf("expected messages unchanged, got %#v", out) + } +} + +func TestAppendThinkingInjectionToLatestUserSkipsDuplicate(t *testing.T) { + messages := []any{ + map[string]any{"role": "user", "content": "latest\n\n" + DefaultThinkingInjectionPrompt}, + } + + out, changed := AppendThinkingInjectionToLatestUser(messages) + if changed { + t.Fatal("expected thinking injection to be disabled") + } + if len(out) != 1 { + t.Fatalf("unexpected messages: %#v", out) + } +} diff --git a/internal/promptcompat/tool_prompt.go b/internal/promptcompat/tool_prompt.go new file mode 100644 index 0000000000000000000000000000000000000000..1ead6e117d938ba80b956018287c9dbe13ae6e81 --- /dev/null +++ b/internal/promptcompat/tool_prompt.go @@ -0,0 +1,160 @@ +package promptcompat + +import ( + "encoding/json" + "fmt" + "strings" + "unicode" + + "ds2api/internal/toolcall" +) + +const CurrentToolsContextFilename = "context_tools.txt" + +const toolsTranscriptTitle = "# context_tools.txt" +const toolsTranscriptSummary = "Tool descriptions and parameter schemas for this request." + +type toolPromptParts struct { + Descriptions string + Instructions string + Names []string +} + +func injectToolPrompt(messages []map[string]any, tools []any, policy ToolChoicePolicy) ([]map[string]any, []string) { + return injectToolPromptWithDescriptions(messages, tools, policy, true) +} + +func injectToolPromptInstructionsOnly(messages []map[string]any, tools []any, policy ToolChoicePolicy) ([]map[string]any, []string) { + return injectToolPromptWithDescriptions(messages, tools, policy, false) +} + +func injectToolPromptWithDescriptions(messages []map[string]any, tools []any, policy ToolChoicePolicy, includeDescriptions bool) ([]map[string]any, []string) { + return messages, extractToolNames(tools, policy) +} + +func buildToolPromptParts(tools []any, policy ToolChoicePolicy) toolPromptParts { + toolSchemas := make([]string, 0, len(tools)) + names := make([]string, 0, len(tools)) + isAllowed := func(name string) bool { + if strings.TrimSpace(name) == "" { + return false + } + if len(policy.Allowed) == 0 { + return true + } + _, ok := policy.Allowed[name] + return ok + } + + for _, t := range tools { + tool, ok := t.(map[string]any) + if !ok { + continue + } + name, desc, schema := toolcall.ExtractToolMeta(tool) + name = strings.TrimSpace(name) + if !isAllowed(name) { + continue + } + names = append(names, name) + if desc == "" { + desc = "No description available" + } + b, _ := json.Marshal(schema) + toolSchemas = append(toolSchemas, fmt.Sprintf("Tool: %s\nDescription: %s\nParameters: %s", name, desc, string(b))) + } + if len(toolSchemas) == 0 { + return toolPromptParts{Names: names} + } + descriptions := "You have access to these tools:\n\n" + strings.Join(toolSchemas, "\n\n") + instructions := toolcall.BuildToolCallInstructions(names) + if hasReadLikeTool(names) { + instructions += "\n\nRead-tool cache guard: If a Read/read_file-style tool result says the file is unchanged, already available in history, should be referenced from previous context, or otherwise provides no file body, treat that result as missing content. Do not repeatedly call the same read request for that missing body. Request a full-content read if the tool supports it, or tell the user that the file contents need to be provided again." + } + if policy.Mode == ToolChoiceRequired { + instructions += "\n7) For this response, you MUST call at least one tool from the allowed list." + } + if policy.Mode == ToolChoiceForced && strings.TrimSpace(policy.ForcedName) != "" { + instructions += "\n7) For this response, you MUST call exactly this tool name: " + strings.TrimSpace(policy.ForcedName) + instructions += "\n8) Do not call any other tool." + } + return toolPromptParts{ + Descriptions: descriptions, + Instructions: instructions, + Names: names, + } +} + +func BuildOpenAIToolsContextTranscript(toolsRaw any, policy ToolChoicePolicy) (string, []string) { + tools, ok := toolsRaw.([]any) + if !ok || len(tools) == 0 || policy.IsNone() { + return "", nil + } + parts := buildToolPromptParts(tools, policy) + if strings.TrimSpace(parts.Descriptions) == "" { + return "", parts.Names + } + var b strings.Builder + b.WriteString(toolsTranscriptTitle) + b.WriteString("\n") + b.WriteString(toolsTranscriptSummary) + b.WriteString("\n\n") + b.WriteString(strings.TrimSpace(parts.Descriptions)) + b.WriteString("\n") + return b.String(), parts.Names +} + +func BuildOpenAIToolPromptInstructions(toolsRaw any, policy ToolChoicePolicy) (string, []string) { + tools, ok := toolsRaw.([]any) + if !ok || len(tools) == 0 || policy.IsNone() { + return "", nil + } + parts := buildToolPromptParts(tools, policy) + if strings.TrimSpace(parts.Instructions) == "" { + return "", parts.Names + } + return strings.TrimSpace(parts.Instructions), parts.Names +} + +func extractToolNames(tools []any, policy ToolChoicePolicy) []string { + if policy.IsNone() || len(tools) == 0 { + return nil + } + names := make([]string, 0, len(tools)) + for _, t := range tools { + tool, ok := t.(map[string]any) + if !ok { + continue + } + name, _, _ := toolcall.ExtractToolMeta(tool) + name = strings.TrimSpace(name) + if name == "" { + continue + } + if !policy.Allows(name) { + continue + } + names = append(names, name) + } + return names +} + +func hasReadLikeTool(names []string) bool { + for _, name := range names { + switch normalizeToolNameForGuard(name) { + case "read", "readfile": + return true + } + } + return false +} + +func normalizeToolNameForGuard(name string) string { + var b strings.Builder + for _, r := range strings.ToLower(strings.TrimSpace(name)) { + if unicode.IsLetter(r) || unicode.IsDigit(r) { + b.WriteRune(r) + } + } + return b.String() +} diff --git a/internal/proxyhealth/checker.go b/internal/proxyhealth/checker.go new file mode 100644 index 0000000000000000000000000000000000000000..4276c203e655be5a1dcdad47c8d54c373fb868de --- /dev/null +++ b/internal/proxyhealth/checker.go @@ -0,0 +1,370 @@ +package proxyhealth + +import ( + "context" + "fmt" + "strings" + "sync" + "time" + + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" +) + +const ( + defaultCheckInterval = 5 * time.Hour + bgRetryDelay = 1 * time.Minute + bgMaxRetries = 2 + connectivityTimeout = 15 * time.Second + onDemandRetryDelay = 3 * time.Second + onDemandMaxRetries = 1 + onDemandConnectTimeout = 10 * time.Second +) + +// Checker periodically tests proxy connectivity and auto-bans failing proxies. +type Checker struct { + store *config.Store + tester func(ctx context.Context, proxy config.Proxy) map[string]any + pool PoolResetter + + mu sync.RWMutex + results map[string]*CheckResult + stopCh chan struct{} + wg sync.WaitGroup +} + +// CheckResult holds the latest health check outcome for a single proxy. +type CheckResult struct { + ProxyID string `json:"proxy_id"` + Healthy bool `json:"healthy"` + Disabled bool `json:"disabled"` + Message string `json:"message,omitempty"` + ResponseTime int `json:"response_time_ms,omitempty"` + CheckedAt time.Time `json:"checked_at"` + RetriesUsed int `json:"retries_used,omitempty"` +} + +// PoolResetter resets the account pool after proxy reassignment. +type PoolResetter interface { + Reset() +} + +// NewChecker creates a new proxy health checker. +func NewChecker(store *config.Store, pool PoolResetter) *Checker { + return &Checker{ + store: store, + tester: dsclient.TestProxyConnectivity, + pool: pool, + results: make(map[string]*CheckResult), + stopCh: make(chan struct{}), + } +} + +// Start begins the periodic health check loop. +// On Vercel (serverless) the background loop is skipped because the +// process will not survive between requests; use CheckAll instead. +func (c *Checker) Start() { + if config.IsVercel() { + config.Logger.Info("[proxy_health] Vercel detected, skipping background loop; use on-demand CheckAll") + return + } + c.wg.Add(1) + go c.loop() +} + +// Stop gracefully shuts down the health checker. +func (c *Checker) Stop() { + close(c.stopCh) + c.wg.Wait() +} + +// Results returns a snapshot of all proxy health check results. +func (c *Checker) Results() map[string]*CheckResult { + c.mu.RLock() + defer c.mu.RUnlock() + out := make(map[string]*CheckResult, len(c.results)) + for k, v := range c.results { + cp := *v + out[k] = &cp + } + return out +} + +// CheckAll synchronously tests all proxies and returns the results. +// Uses short timeouts and minimal retries to stay within serverless +// function execution limits (e.g. Vercel 10s free / 60s Pro). +func (c *Checker) CheckAll(ctx context.Context) []CheckResult { + proxies := c.store.Snapshot().Proxies + if len(proxies) == 0 { + return nil + } + + var wg sync.WaitGroup + results := make([]CheckResult, len(proxies)) + + for i, p := range proxies { + p = config.NormalizeProxy(p) + if p.Disabled { + r := CheckResult{ + ProxyID: p.ID, + Healthy: false, + Disabled: true, + Message: "代理已被禁用(自动封禁)", + CheckedAt: time.Now(), + } + c.setResult(p.ID, &r) + results[i] = r + continue + } + + wg.Add(1) + go func(idx int, proxy config.Proxy) { + defer wg.Done() + r := c.checkProxyOnDemand(ctx, proxy) + c.setResult(proxy.ID, &r) + results[idx] = r + + // Auto-ban if unhealthy after retries. + if !r.Healthy { + c.banProxy(proxy.ID) + } + }(i, p) + } + + wg.Wait() + return results +} + +// checkProxyOnDemand tests a single proxy with short timeouts and 1 retry, +// suitable for on-demand / serverless checks that must complete quickly. +func (c *Checker) checkProxyOnDemand(ctx context.Context, p config.Proxy) CheckResult { + var lastResult CheckResult + + for attempt := 0; attempt <= onDemandMaxRetries; attempt++ { + if attempt > 0 { + select { + case <-time.After(onDemandRetryDelay): + case <-ctx.Done(): + return CheckResult{ + ProxyID: p.ID, + Healthy: false, + Message: "上下文已取消", + CheckedAt: time.Now(), + RetriesUsed: attempt, + } + } + } + + checkCtx, cancel := context.WithTimeout(ctx, onDemandConnectTimeout) + result := c.tester(checkCtx, p) + cancel() + + success, _ := result["success"].(bool) + message, _ := result["message"].(string) + responseTime, _ := result["response_time"].(int) + + lastResult = CheckResult{ + ProxyID: p.ID, + Healthy: success, + Disabled: false, + Message: message, + ResponseTime: responseTime, + CheckedAt: time.Now(), + RetriesUsed: attempt, + } + + if success { + return lastResult + } + } + + lastResult.Disabled = true + lastResult.Message = fmt.Sprintf("自动封禁: %s", lastResult.Message) + return lastResult +} + +func (c *Checker) loop() { + defer c.wg.Done() + + select { + case <-time.After(30 * time.Second): + case <-c.stopCh: + return + } + c.runCheckCycle() + + ticker := time.NewTicker(defaultCheckInterval) + defer ticker.Stop() + + for { + select { + case <-ticker.C: + c.runCheckCycle() + case <-c.stopCh: + return + } + } +} + +func (c *Checker) runCheckCycle() { + proxies := c.store.Snapshot().Proxies + if len(proxies) == 0 { + return + } + + for _, p := range proxies { + p = config.NormalizeProxy(p) + if p.Disabled { + c.setResult(p.ID, &CheckResult{ + ProxyID: p.ID, + Healthy: false, + Disabled: true, + Message: "代理已被禁用(自动封禁)", + CheckedAt: time.Now(), + }) + continue + } + c.checkProxyBackground(p) + } +} + +// checkProxyBackground tests a proxy with the full retry policy (1min delay, 2 retries). +// Only used by the background loop on non-serverless deployments. +func (c *Checker) checkProxyBackground(p config.Proxy) { + var lastResult *CheckResult + + for attempt := 0; attempt <= bgMaxRetries; attempt++ { + if attempt > 0 { + select { + case <-time.After(bgRetryDelay): + case <-c.stopCh: + return + } + } + + ctx, cancel := context.WithTimeout(context.Background(), connectivityTimeout) + result := c.tester(ctx, p) + cancel() + + success, _ := result["success"].(bool) + message, _ := result["message"].(string) + responseTime, _ := result["response_time"].(int) + + lastResult = &CheckResult{ + ProxyID: p.ID, + Healthy: success, + Disabled: false, + Message: message, + ResponseTime: responseTime, + CheckedAt: time.Now(), + RetriesUsed: attempt, + } + + if success { + c.setResult(p.ID, lastResult) + return + } + } + + lastResult.Disabled = true + lastResult.Message = fmt.Sprintf("自动封禁: %s", lastResult.Message) + c.setResult(p.ID, lastResult) + c.banProxy(p.ID) +} + +func (c *Checker) setResult(proxyID string, r *CheckResult) { + c.mu.Lock() + defer c.mu.Unlock() + c.results[proxyID] = r +} + +func (c *Checker) banProxy(proxyID string) { + config.Logger.Warn("[proxy_health] auto-banning proxy", "proxy_id", proxyID) + + err := c.store.Update(func(cfg *config.Config) error { + banned := false + for i := range cfg.Proxies { + p := config.NormalizeProxy(cfg.Proxies[i]) + if p.ID == proxyID { + cfg.Proxies[i].Disabled = true + banned = true + break + } + } + if !banned { + return nil + } + + var availableID string + for _, p := range cfg.Proxies { + p = config.NormalizeProxy(p) + if !p.Disabled && p.ID != proxyID { + availableID = p.ID + break + } + } + + for i := range cfg.Accounts { + if strings.TrimSpace(cfg.Accounts[i].ProxyID) == proxyID { + cfg.Accounts[i].ProxyID = availableID + if availableID != "" { + config.Logger.Info("[proxy_health] migrated account to new proxy", + "account", cfg.Accounts[i].Identifier(), + "old_proxy", proxyID, + "new_proxy", availableID, + ) + } else { + config.Logger.Warn("[proxy_health] no available proxy, account now has no proxy", + "account", cfg.Accounts[i].Identifier(), + ) + } + } + } + + return nil + }) + if err != nil { + config.Logger.Error("[proxy_health] failed to ban proxy", "proxy_id", proxyID, "error", err) + return + } + + if c.pool != nil { + c.pool.Reset() + } +} + +// UnbanProxy re-enables a previously auto-banned proxy and runs a health check. +func (c *Checker) UnbanProxy(proxyID string) error { + err := c.store.Update(func(cfg *config.Config) error { + for i := range cfg.Proxies { + p := config.NormalizeProxy(cfg.Proxies[i]) + if p.ID == proxyID { + cfg.Proxies[i].Disabled = false + return nil + } + } + return fmt.Errorf("proxy not found: %s", proxyID) + }) + if err != nil { + return err + } + + if p, ok := c.findProxy(proxyID); ok { + if config.IsVercel() { + c.checkProxyOnDemand(context.Background(), p) + } else { + c.checkProxyBackground(p) + } + } + return nil +} + +func (c *Checker) findProxy(proxyID string) (config.Proxy, bool) { + for _, p := range c.store.Snapshot().Proxies { + p = config.NormalizeProxy(p) + if p.ID == proxyID { + return p, true + } + } + return config.Proxy{}, false +} diff --git a/internal/rawsample/rawsample.go b/internal/rawsample/rawsample.go new file mode 100644 index 0000000000000000000000000000000000000000..28b13355f63ee646467e6f92127d5f708fede068 --- /dev/null +++ b/internal/rawsample/rawsample.go @@ -0,0 +1,199 @@ +package rawsample + +import ( + "encoding/json" + "errors" + "fmt" + "os" + "path/filepath" + "regexp" + "strings" + "time" + + "github.com/google/uuid" +) + +var referenceMarkerRe = regexp.MustCompile(`(?i)\[reference:\s*\d+\]`) + +type CaptureRound struct { + Label string `json:"label,omitempty"` + URL string `json:"url,omitempty"` + StatusCode int `json:"status_code"` + ResponseBytes int `json:"response_bytes"` +} + +type CaptureSummary struct { + Label string `json:"label,omitempty"` + URL string `json:"url,omitempty"` + StatusCode int `json:"status_code"` + ResponseBytes int `json:"response_bytes"` + Rounds []CaptureRound `json:"rounds,omitempty"` + ContainsReferenceMarkers bool `json:"contains_reference_markers,omitempty"` + ReferenceMarkerCount int `json:"reference_marker_count,omitempty"` + ContainsFinishedToken bool `json:"contains_finished_token,omitempty"` + FinishedTokenCount int `json:"finished_token_count,omitempty"` +} + +type Meta struct { + SampleID string `json:"sample_id"` + CapturedAtUTC string `json:"captured_at_utc"` + Source string `json:"source,omitempty"` + Request any `json:"request"` + Capture CaptureSummary `json:"capture"` +} + +type PersistOptions struct { + RootDir string + SampleID string + Source string + Request any + Capture CaptureSummary + UpstreamBody []byte +} + +type SavedSample struct { + SampleID string + Dir string + MetaPath string + UpstreamPath string + Meta Meta +} + +func Persist(opts PersistOptions) (SavedSample, error) { + root := strings.TrimSpace(opts.RootDir) + if root == "" { + return SavedSample{}, errors.New("root dir is required") + } + if len(opts.UpstreamBody) == 0 { + return SavedSample{}, errors.New("upstream body is required") + } + + if err := os.MkdirAll(root, 0o755); err != nil { + return SavedSample{}, fmt.Errorf("create root dir: %w", err) + } + + baseID := NormalizeSampleID(opts.SampleID) + if baseID == "" { + baseID = DefaultSampleID("capture") + } + sampleID, err := uniqueSampleID(root, baseID) + if err != nil { + return SavedSample{}, err + } + + tempID := ".tmp-" + sampleID + "-" + strings.ToLower(strings.ReplaceAll(uuid.NewString(), "-", "")) + tempDir := filepath.Join(root, tempID) + finalDir := filepath.Join(root, sampleID) + if err := os.MkdirAll(tempDir, 0o755); err != nil { + return SavedSample{}, fmt.Errorf("create temp dir: %w", err) + } + cleanup := func() { + _ = os.RemoveAll(tempDir) + } + + upstreamPath := filepath.Join(tempDir, "upstream.stream.sse") + if err := os.WriteFile(upstreamPath, opts.UpstreamBody, 0o644); err != nil { + cleanup() + return SavedSample{}, fmt.Errorf("write upstream stream: %w", err) + } + + now := time.Now().UTC() + capture := opts.Capture + capture.ResponseBytes = len(opts.UpstreamBody) + capture.ContainsReferenceMarkers, capture.ReferenceMarkerCount, capture.ContainsFinishedToken, capture.FinishedTokenCount = analyzeBytes(opts.UpstreamBody) + + meta := Meta{ + SampleID: sampleID, + CapturedAtUTC: now.Format(time.RFC3339), + Source: strings.TrimSpace(opts.Source), + Request: opts.Request, + Capture: capture, + } + metaBytes, err := json.MarshalIndent(meta, "", " ") + if err != nil { + cleanup() + return SavedSample{}, fmt.Errorf("marshal meta: %w", err) + } + metaPath := filepath.Join(tempDir, "meta.json") + if err := os.WriteFile(metaPath, append(metaBytes, '\n'), 0o644); err != nil { + cleanup() + return SavedSample{}, fmt.Errorf("write meta: %w", err) + } + + if err := os.Rename(tempDir, finalDir); err != nil { + cleanup() + return SavedSample{}, fmt.Errorf("promote sample dir: %w", err) + } + + return SavedSample{ + SampleID: sampleID, + Dir: finalDir, + MetaPath: filepath.Join(finalDir, "meta.json"), + UpstreamPath: filepath.Join(finalDir, "upstream.stream.sse"), + Meta: meta, + }, nil +} + +func NormalizeSampleID(raw string) string { + raw = strings.TrimSpace(strings.ToLower(raw)) + if raw == "" { + return "" + } + var b strings.Builder + prevDash := false + for _, r := range raw { + switch { + case r >= 'a' && r <= 'z', r >= '0' && r <= '9', r == '-', r == '_', r == '.': + b.WriteRune(r) + prevDash = false + default: + if !prevDash { + b.WriteRune('-') + prevDash = true + } + } + } + out := strings.Trim(b.String(), "-_.") + if out == "" { + return "" + } + return out +} + +func DefaultSampleID(prefix string) string { + prefix = NormalizeSampleID(prefix) + if prefix == "" { + prefix = "capture" + } + return fmt.Sprintf("%s-%s", prefix, time.Now().UTC().Format("20060102T150405Z")) +} + +func uniqueSampleID(root, base string) (string, error) { + if base == "" { + base = DefaultSampleID("capture") + } + candidate := base + for i := 2; ; i++ { + finalDir := filepath.Join(root, candidate) + if _, err := os.Stat(finalDir); err != nil { + if os.IsNotExist(err) { + return candidate, nil + } + return "", fmt.Errorf("stat sample dir: %w", err) + } + candidate = fmt.Sprintf("%s-%d", base, i) + } +} + +func analyzeBytes(raw []byte) (containsReferenceMarkers bool, referenceMarkerCount int, containsFinishedToken bool, finishedTokenCount int) { + if len(raw) == 0 { + return false, 0, false, 0 + } + text := string(raw) + referenceMarkerCount = len(referenceMarkerRe.FindAllStringIndex(text, -1)) + containsReferenceMarkers = referenceMarkerCount > 0 + upper := strings.ToUpper(text) + finishedTokenCount = strings.Count(upper, "FINISHED") + containsFinishedToken = finishedTokenCount > 0 + return +} diff --git a/internal/rawsample/rawsample_test.go b/internal/rawsample/rawsample_test.go new file mode 100644 index 0000000000000000000000000000000000000000..e22c2cc919c13278d09ec922bb87c5372de1fa33 --- /dev/null +++ b/internal/rawsample/rawsample_test.go @@ -0,0 +1,79 @@ +package rawsample + +import ( + "encoding/json" + "os" + "path/filepath" + "strings" + "testing" +) + +func TestNormalizeSampleID(t *testing.T) { + got := NormalizeSampleID(" Hello, World! ") + if got != "hello-world" { + t.Fatalf("expected hello-world, got %q", got) + } +} + +func TestPersistWritesSampleFilesAndMeta(t *testing.T) { + root := t.TempDir() + saved, err := Persist(PersistOptions{ + RootDir: root, + SampleID: "My Sample! 01", + Source: "unit-test", + Request: map[string]any{ + "model": "deepseek-v4-flash", + "stream": true, + "messages": []any{ + map[string]any{"role": "user", "content": "广州天气"}, + }, + }, + Capture: CaptureSummary{ + Label: "deepseek_completion", + URL: "https://chat.deepseek.com/api/v0/chat/completion", + StatusCode: 200, + }, + UpstreamBody: []byte("data: {\"v\":\"hello [reference:1]\"}\n\n" + + "data: {\"v\":\"FINISHED\",\"p\":\"response/status\"}\n\n"), + }) + if err != nil { + t.Fatalf("Persist failed: %v", err) + } + + if saved.SampleID != "my-sample-01" { + t.Fatalf("expected normalized sample id, got %q", saved.SampleID) + } + if _, err := os.Stat(saved.Dir); err != nil { + t.Fatalf("sample dir missing: %v", err) + } + if _, err := os.Stat(saved.UpstreamPath); err != nil { + t.Fatalf("upstream file missing: %v", err) + } + if _, err := os.Stat(filepath.Join(saved.Dir, "openai.stream.sse")); !os.IsNotExist(err) { + t.Fatalf("unexpected processed stream file: %v", err) + } + if _, err := os.Stat(filepath.Join(saved.Dir, "openai.output.txt")); !os.IsNotExist(err) { + t.Fatalf("unexpected processed text file: %v", err) + } + + metaBytes, err := os.ReadFile(saved.MetaPath) + if err != nil { + t.Fatalf("read meta: %v", err) + } + var meta Meta + if err := json.Unmarshal(metaBytes, &meta); err != nil { + t.Fatalf("decode meta: %v", err) + } + if meta.SampleID != saved.SampleID { + t.Fatalf("expected meta sample id %q, got %q", saved.SampleID, meta.SampleID) + } + if meta.Capture.ReferenceMarkerCount != 1 { + t.Fatalf("expected one reference marker, got %+v", meta.Capture) + } + if meta.Capture.FinishedTokenCount != 1 { + t.Fatalf("expected one finished token, got %+v", meta.Capture) + } + if strings.Contains(string(metaBytes), "\"processed\"") { + t.Fatalf("meta should not include processed payload: %s", string(metaBytes)) + } +} diff --git a/internal/rawsample/visible_text.go b/internal/rawsample/visible_text.go new file mode 100644 index 0000000000000000000000000000000000000000..1896dd6c57d2b5cfbaf5ad9eefc3a92313f5f672 --- /dev/null +++ b/internal/rawsample/visible_text.go @@ -0,0 +1,118 @@ +package rawsample + +import ( + "encoding/json" + "strings" +) + +//nolint:unused // retained for raw-sample processing entrypoints. +func extractProcessedVisibleText(raw []byte, kind, contentType string) string { + if len(raw) == 0 { + return "" + } + switch strings.ToLower(strings.TrimSpace(kind)) { + case "json": + return parseOpenAIJSONText(string(raw)) + case "stream": + return parseOpenAIStreamText(string(raw)) + } + ct := strings.ToLower(strings.TrimSpace(contentType)) + if strings.Contains(ct, "application/json") { + return parseOpenAIJSONText(string(raw)) + } + return parseOpenAIStreamText(string(raw)) +} + +//nolint:unused // retained for raw-sample processing entrypoints. +func parseOpenAIStreamText(raw string) string { + if strings.TrimSpace(raw) == "" { + return "" + } + var out strings.Builder + for _, block := range strings.Split(raw, "\n\n") { + if strings.TrimSpace(block) == "" { + continue + } + dataLines := make([]string, 0, 2) + for _, line := range strings.Split(block, "\n") { + if !strings.HasPrefix(line, "data:") { + continue + } + dataLines = append(dataLines, strings.TrimSpace(strings.TrimPrefix(line, "data:"))) + } + if len(dataLines) == 0 { + continue + } + payload := strings.TrimSpace(strings.Join(dataLines, "\n")) + if payload == "" || payload == "[DONE]" || !strings.HasPrefix(payload, "{") { + continue + } + var decoded any + if err := json.Unmarshal([]byte(payload), &decoded); err != nil { + continue + } + out.WriteString(extractOpenAIVisibleTextValue(decoded)) + } + return out.String() +} + +//nolint:unused // retained for raw-sample processing entrypoints. +func parseOpenAIJSONText(raw string) string { + if strings.TrimSpace(raw) == "" { + return "" + } + var decoded any + if err := json.Unmarshal([]byte(raw), &decoded); err != nil { + return "" + } + return extractOpenAIVisibleTextValue(decoded) +} + +//nolint:unused // retained for raw-sample processing entrypoints. +func extractOpenAIVisibleTextValue(v any) string { + switch x := v.(type) { + case nil: + return "" + case string: + return x + case []any: + var out strings.Builder + for _, item := range x { + out.WriteString(extractOpenAIVisibleTextValue(item)) + } + return out.String() + case map[string]any: + var out strings.Builder + if s, ok := x["output_text"].(string); ok { + out.WriteString(s) + } + if arr, ok := x["output"].([]any); ok { + for _, item := range arr { + out.WriteString(extractOpenAIVisibleTextValue(item)) + } + } + if arr, ok := x["choices"].([]any); ok { + for _, item := range arr { + out.WriteString(extractOpenAIVisibleTextValue(item)) + } + } + if msg, ok := x["message"]; ok { + out.WriteString(extractOpenAIVisibleTextValue(msg)) + } + if delta, ok := x["delta"]; ok { + out.WriteString(extractOpenAIVisibleTextValue(delta)) + } + if content, ok := x["content"]; ok { + out.WriteString(extractOpenAIVisibleTextValue(content)) + } + if reasoning, ok := x["reasoning_content"]; ok { + out.WriteString(extractOpenAIVisibleTextValue(reasoning)) + } + if text, ok := x["text"]; ok { + out.WriteString(extractOpenAIVisibleTextValue(text)) + } + return out.String() + default: + return "" + } +} diff --git a/internal/responsehistory/session.go b/internal/responsehistory/session.go new file mode 100644 index 0000000000000000000000000000000000000000..dd10f16bf3fd8653a8c16ff349e9e262933d5aaf --- /dev/null +++ b/internal/responsehistory/session.go @@ -0,0 +1,289 @@ +package responsehistory + +import ( + "errors" + "net/http" + "strings" + "time" + + "ds2api/internal/assistantturn" + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/config" + "ds2api/internal/prompt" + "ds2api/internal/promptcompat" +) + +type Session struct { + store *chathistory.Store + entryID string + startedAt time.Time + lastPersist time.Time + startParams chathistory.StartParams + disabled bool +} + +type StartParams struct { + Store *chathistory.Store + Request *http.Request + Auth *auth.RequestAuth + Surface string + Standard promptcompat.StandardRequest +} + +func Start(params StartParams) *Session { + if params.Store == nil || params.Request == nil || params.Auth == nil { + return nil + } + if !params.Store.Enabled() || !shouldCapture(params.Request) { + return nil + } + startParams := chathistory.StartParams{ + CallerID: strings.TrimSpace(params.Auth.CallerID), + AccountID: strings.TrimSpace(params.Auth.AccountID), + Surface: strings.TrimSpace(params.Surface), + Model: strings.TrimSpace(params.Standard.ResponseModel), + Stream: params.Standard.Stream, + UserInput: ExtractSingleUserInput(params.Standard.Messages), + Messages: ExtractAllMessages(params.Standard.Messages), + HistoryText: params.Standard.HistoryText, + FinalPrompt: params.Standard.FinalPrompt, + } + entry, err := params.Store.Start(startParams) + session := &Session{ + store: params.Store, + entryID: entry.ID, + startedAt: time.Now(), + lastPersist: time.Now(), + startParams: startParams, + } + if err != nil { + if entry.ID == "" { + config.Logger.Warn("[response_history] start failed", "surface", startParams.Surface, "error", err) + return nil + } + config.Logger.Warn("[response_history] start persisted in memory after write failure", "surface", startParams.Surface, "error", err) + } + return session +} + +func shouldCapture(r *http.Request) bool { + if r == nil || r.URL == nil { + return false + } + if strings.TrimSpace(r.URL.Query().Get("__stream_prepare")) == "1" { + return false + } + if strings.TrimSpace(r.URL.Query().Get("__stream_release")) == "1" { + return false + } + return true +} + +func ExtractSingleUserInput(messages []any) string { + for i := len(messages) - 1; i >= 0; i-- { + msg, ok := messages[i].(map[string]any) + if !ok { + continue + } + role := strings.ToLower(strings.TrimSpace(asString(msg["role"]))) + if role != "user" { + continue + } + if normalized := strings.TrimSpace(prompt.NormalizeContent(msg["content"])); normalized != "" { + return normalized + } + } + return "" +} + +func ExtractAllMessages(messages []any) []chathistory.Message { + out := make([]chathistory.Message, 0, len(messages)) + for _, raw := range messages { + msg, ok := raw.(map[string]any) + if !ok { + continue + } + role := strings.ToLower(strings.TrimSpace(asString(msg["role"]))) + content := strings.TrimSpace(prompt.NormalizeContent(msg["content"])) + if role == "" || content == "" { + continue + } + out = append(out, chathistory.Message{ + Role: role, + Content: content, + }) + } + return out +} + +func (s *Session) Progress(thinking, content string) { + if s == nil || s.store == nil || s.disabled { + return + } + now := time.Now() + if now.Sub(s.lastPersist) < 250*time.Millisecond { + return + } + s.lastPersist = now + s.persistUpdate(chathistory.UpdateParams{ + Status: "streaming", + ReasoningContent: thinking, + Content: content, + StatusCode: http.StatusOK, + ElapsedMs: time.Since(s.startedAt).Milliseconds(), + }) +} + +func (s *Session) Success(statusCode int, thinking, content, finishReason string, usage map[string]any) { + if s == nil || s.store == nil || s.disabled { + return + } + s.persistUpdate(chathistory.UpdateParams{ + Status: "success", + ReasoningContent: thinking, + Content: content, + StatusCode: statusCode, + ElapsedMs: time.Since(s.startedAt).Milliseconds(), + FinishReason: finishReason, + Usage: usage, + Completed: true, + }) +} + +func (s *Session) Error(statusCode int, message, finishReason, thinking, content string) { + if s == nil || s.store == nil || s.disabled { + return + } + s.persistUpdate(chathistory.UpdateParams{ + Status: "error", + ReasoningContent: thinking, + Content: content, + Error: message, + StatusCode: statusCode, + ElapsedMs: time.Since(s.startedAt).Milliseconds(), + FinishReason: finishReason, + Completed: true, + }) +} + +func (s *Session) SuccessTurn(statusCode int, turn assistantturn.Turn, usage map[string]any) { + outcome := assistantturn.FinalizeTurn(turn, assistantturn.FinalizeOptions{}) + s.Success( + statusCode, + ThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), + TextForArchive(turn.RawText, turn.Text), + outcome.FinishReason, + usage, + ) +} + +func (s *Session) ErrorTurn(statusCode int, message, finishReason string, turn assistantturn.Turn) { + s.Error( + statusCode, + message, + finishReason, + ThinkingForArchive(turn.RawThinking, turn.DetectionThinking, turn.Thinking), + TextForArchive(turn.RawText, turn.Text), + ) +} + +func TextForArchive(raw, visible string) string { + if strings.TrimSpace(raw) != "" { + return raw + } + return visible +} + +func ThinkingForArchive(raw, detection, visible string) string { + if strings.TrimSpace(raw) != "" { + return raw + } + if strings.TrimSpace(detection) != "" { + return detection + } + return visible +} + +func GenericUsage(turn assistantturn.Turn) map[string]any { + return map[string]any{ + "input_tokens": turn.Usage.InputTokens, + "output_tokens": turn.Usage.OutputTokens, + "reasoning_tokens": turn.Usage.ReasoningTokens, + "total_tokens": turn.Usage.TotalTokens, + } +} + +func (s *Session) retryMissingEntry() bool { + if s == nil || s.store == nil || s.disabled { + return false + } + entry, err := s.store.Start(s.startParams) + if errors.Is(err, chathistory.ErrDisabled) { + s.disabled = true + return false + } + if entry.ID == "" { + if err != nil { + config.Logger.Warn("[response_history] recreate missing entry failed", "surface", s.startParams.Surface, "error", err) + } + return false + } + s.entryID = entry.ID + if err != nil { + config.Logger.Warn("[response_history] recreate missing entry persisted in memory after write failure", "surface", s.startParams.Surface, "error", err) + } + return true +} + +func (s *Session) persistUpdate(params chathistory.UpdateParams) { + if s == nil || s.store == nil || s.disabled { + return + } + if _, err := s.store.Update(s.entryID, params); err != nil { + s.handlePersistError(params, err) + } +} + +func (s *Session) handlePersistError(params chathistory.UpdateParams, err error) { + if err == nil || s == nil { + return + } + if errors.Is(err, chathistory.ErrDisabled) { + s.disabled = true + return + } + if isMissingError(err) { + if s.retryMissingEntry() { + if _, retryErr := s.store.Update(s.entryID, params); retryErr != nil { + if errors.Is(retryErr, chathistory.ErrDisabled) || isMissingError(retryErr) { + s.disabled = true + return + } + config.Logger.Warn("[response_history] retry after missing entry failed", "surface", s.startParams.Surface, "error", retryErr) + } + return + } + s.disabled = true + return + } + config.Logger.Warn("[response_history] update failed", "surface", s.startParams.Surface, "error", err) +} + +func isMissingError(err error) bool { + if err == nil { + return false + } + return strings.Contains(strings.ToLower(err.Error()), "not found") +} + +func asString(v any) string { + switch x := v.(type) { + case string: + return x + case nil: + return "" + default: + return strings.TrimSpace(prompt.NormalizeContent(x)) + } +} diff --git a/internal/server/router.go b/internal/server/router.go new file mode 100644 index 0000000000000000000000000000000000000000..8bf7ef9dafd51d2d99cc9dc193cc2128c2adec5f --- /dev/null +++ b/internal/server/router.go @@ -0,0 +1,418 @@ +package server + +import ( + "context" + "encoding/json" + "fmt" + "log" + "net/http" + "net/url" + "os" + "runtime" + "strings" + "time" + + "github.com/go-chi/chi/v5" + "github.com/go-chi/chi/v5/middleware" + + "ds2api/internal/account" + "ds2api/internal/auth" + "ds2api/internal/chathistory" + "ds2api/internal/config" + dsclient "ds2api/internal/deepseek/client" + "ds2api/internal/httpapi/admin" + "ds2api/internal/httpapi/claude" + "ds2api/internal/httpapi/gemini" + "ds2api/internal/httpapi/ollama" + "ds2api/internal/httpapi/openai/chat" + "ds2api/internal/httpapi/openai/embeddings" + "ds2api/internal/httpapi/openai/files" + "ds2api/internal/httpapi/openai/responses" + "ds2api/internal/httpapi/openai/shared" + "ds2api/internal/httpapi/requestbody" + "ds2api/internal/proxyhealth" + "ds2api/internal/webui" +) + +type App struct { + Store *config.Store + Pool *account.Pool + Resolver *auth.Resolver + DS *dsclient.Client + ProxyChecker *proxyhealth.Checker + Router http.Handler +} + +func NewApp() (*App, error) { + store, err := config.LoadStoreWithError() + if err != nil { + return nil, fmt.Errorf("load config: %w", err) + } + pool := account.NewPool(store) + var dsClient *dsclient.Client + resolver := auth.NewResolver(store, pool, func(ctx context.Context, acc config.Account) (string, error) { + return dsClient.Login(ctx, acc) + }) + dsClient = dsclient.NewClient(store, resolver) + if err := dsClient.PreloadPow(context.Background()); err != nil { + config.Logger.Warn("[PoW] init failed", "error", err) + } else { + config.Logger.Info("[PoW] pure Go solver ready") + } + chatHistoryStore := chathistory.New(config.ChatHistoryPath()) + if err := chatHistoryStore.Err(); err != nil { + config.Logger.Warn("[chat_history] unavailable", "path", chatHistoryStore.Path(), "error", err) + } + + modelsHandler := &shared.ModelsHandler{Store: store} + chatHandler := &chat.Handler{Store: store, Auth: resolver, DS: dsClient, ChatHistory: chatHistoryStore} + responsesHandler := &responses.Handler{Store: store, Auth: resolver, DS: dsClient, ChatHistory: chatHistoryStore} + filesHandler := &files.Handler{Store: store, Auth: resolver, DS: dsClient, ChatHistory: chatHistoryStore} + embeddingsHandler := &embeddings.Handler{Store: store, Auth: resolver, DS: dsClient, ChatHistory: chatHistoryStore} + claudeHandler := &claude.Handler{Store: store, Auth: resolver, DS: dsClient, OpenAI: chatHandler, ChatHistory: chatHistoryStore} + geminiHandler := &gemini.Handler{Store: store, Auth: resolver, DS: dsClient, OpenAI: chatHandler, ChatHistory: chatHistoryStore} + proxyChecker := proxyhealth.NewChecker(store, pool) + adminHandler := &admin.Handler{Store: store, Pool: pool, DS: dsClient, OpenAI: chatHandler, ChatHistory: chatHistoryStore, Checker: proxyChecker} + ollamaHandler := &ollama.Handler{Store: store} + webuiHandler := webui.NewHandler() + + r := chi.NewRouter() + r.Use(middleware.RequestID) + r.Use(middleware.RealIP) + r.Use(filteredLogger()) + r.Use(middleware.Recoverer) + r.Use(cors) + r.Use(requestbody.ValidateJSONUTF8) + r.Use(timeout(0)) + + healthzHandler := func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"status":"ok"}`)) + } + readyzHandler := func(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(`{"status":"ready"}`)) + } + r.Get("/healthz", healthzHandler) + r.Head("/healthz", healthzHandler) + r.Get("/readyz", readyzHandler) + r.Head("/readyz", readyzHandler) + r.Get("/v1/models", modelsHandler.ListModels) + r.Get("/v1/models/{model_id}", modelsHandler.GetModel) + r.Post("/v1/chat/completions", chatHandler.ChatCompletions) + r.Post("/v1/responses", responsesHandler.Responses) + r.Get("/v1/responses/{response_id}", responsesHandler.GetResponseByID) + r.Post("/v1/files", filesHandler.UploadFile) + r.Get("/v1/files/{file_id}", filesHandler.RetrieveFile) + r.Post("/v1/embeddings", embeddingsHandler.Embeddings) + // Root OpenAI aliases support clients configured with the bare DS2API service URL. + r.Get("/models", modelsHandler.ListModels) + r.Get("/models/{model_id}", modelsHandler.GetModel) + r.Post("/chat/completions", chatHandler.ChatCompletions) + r.Post("/responses", responsesHandler.Responses) + r.Get("/responses/{response_id}", responsesHandler.GetResponseByID) + r.Post("/files", filesHandler.UploadFile) + r.Get("/files/{file_id}", filesHandler.RetrieveFile) + r.Post("/embeddings", embeddingsHandler.Embeddings) + claude.RegisterRoutes(r, claudeHandler) + gemini.RegisterRoutes(r, geminiHandler) + ollama.RegisterRoutes(r, ollamaHandler) + r.Route("/admin", func(ar chi.Router) { + admin.RegisterRoutes(ar, adminHandler) + }) + webui.RegisterRoutes(r, webuiHandler) + r.NotFound(func(w http.ResponseWriter, req *http.Request) { + if strings.HasPrefix(req.URL.Path, "/admin/") && webuiHandler.HandleAdminFallback(w, req) { + return + } + http.NotFound(w, req) + }) + + proxyChecker.Start() + + return &App{Store: store, Pool: pool, Resolver: resolver, DS: dsClient, ProxyChecker: proxyChecker, Router: r}, nil +} + +func timeout(d time.Duration) func(http.Handler) http.Handler { + if d <= 0 { + return func(next http.Handler) http.Handler { return next } + } + return middleware.Timeout(d) +} + +func filteredLogger() func(http.Handler) http.Handler { + color := !isWindowsRuntime() + base := &middleware.DefaultLogFormatter{ + Logger: log.New(os.Stdout, "", log.LstdFlags), + NoColor: !color, + } + return middleware.RequestLogger(&filteredLogFormatter{base: base}) +} + +func isWindowsRuntime() bool { + return runtime.GOOS == "windows" +} + +type filteredLogFormatter struct { + base *middleware.DefaultLogFormatter +} + +func (f *filteredLogFormatter) NewLogEntry(r *http.Request) middleware.LogEntry { + if r != nil && r.Method == http.MethodGet { + path := strings.TrimSpace(r.URL.Path) + if path == "/admin/chat-history" || strings.HasPrefix(path, "/admin/chat-history/") { + return noopLogEntry{} + } + } + if r != nil && r.URL != nil { + if redacted, changed := redactSensitiveQueryParams(r.URL); changed { + cloned := *r + clonedURL := *r.URL + clonedURL.RawQuery = redacted + cloned.URL = &clonedURL + cloned.RequestURI = clonedURL.RequestURI() + return f.base.NewLogEntry(&cloned) + } + } + return f.base.NewLogEntry(r) +} + +type noopLogEntry struct{} + +func (noopLogEntry) Write(_ int, _ int, _ http.Header, _ time.Duration, _ interface{}) {} + +func (noopLogEntry) Panic(_ interface{}, _ []byte) {} + +func redactSensitiveQueryParams(u *url.URL) (string, bool) { + if u == nil || u.RawQuery == "" { + return "", false + } + values, err := url.ParseQuery(u.RawQuery) + if err != nil { + return redactSensitiveRawQueryParams(u.RawQuery) + } + changed := false + for name, vals := range values { + if !isSensitiveQueryParam(name) { + continue + } + for i := range vals { + vals[i] = "REDACTED" + } + values[name] = vals + changed = true + } + if !changed { + return "", false + } + return values.Encode(), true +} + +func redactSensitiveRawQueryParams(rawQuery string) (string, bool) { + if rawQuery == "" { + return "", false + } + var b strings.Builder + b.Grow(len(rawQuery)) + changed := false + start := 0 + for i := 0; i <= len(rawQuery); i++ { + if i < len(rawQuery) && rawQuery[i] != '&' && rawQuery[i] != ';' { + continue + } + segment := rawQuery[start:i] + b.WriteString(redactSensitiveRawQuerySegment(segment, &changed)) + if i < len(rawQuery) { + b.WriteByte(rawQuery[i]) + } + start = i + 1 + } + if !changed { + return "", false + } + return b.String(), true +} + +func redactSensitiveRawQuerySegment(segment string, changed *bool) string { + if segment == "" { + return segment + } + name := segment + valueStart := -1 + if eq := strings.IndexByte(segment, '='); eq >= 0 { + name = segment[:eq] + valueStart = eq + 1 + } + decodedName, err := url.QueryUnescape(name) + if err != nil { + decodedName = name + } + if !isSensitiveQueryParam(decodedName) { + return segment + } + if changed != nil { + *changed = true + } + if valueStart < 0 { + return name + "=REDACTED" + } + return segment[:valueStart] + "REDACTED" +} + +func isSensitiveQueryParam(name string) bool { + return strings.EqualFold(name, "key") || strings.EqualFold(name, "api_key") +} + +var defaultCORSAllowHeaders = []string{ + "Content-Type", + "Authorization", + "X-API-Key", + "X-Ds2-Target-Account", + "X-Ds2-Source", + "X-Vercel-Protection-Bypass", + "X-Goog-Api-Key", + "Anthropic-Version", + "Anthropic-Beta", +} + +var blockedCORSRequestHeaders = map[string]struct{}{ + "x-ds2-internal-token": {}, +} + +func cors(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + setCORSHeaders(w, r) + if r.Method == http.MethodOptions { + w.WriteHeader(http.StatusNoContent) + return + } + next.ServeHTTP(w, r) + }) +} + +func setCORSHeaders(w http.ResponseWriter, r *http.Request) { + origin := strings.TrimSpace(r.Header.Get("Origin")) + if origin == "" { + w.Header().Set("Access-Control-Allow-Origin", "*") + } else { + w.Header().Set("Access-Control-Allow-Origin", origin) + addVaryHeaderToken(w.Header(), "Origin") + } + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS, PUT, DELETE") + w.Header().Set("Access-Control-Allow-Headers", buildCORSAllowHeaders(r)) + w.Header().Set("Access-Control-Max-Age", "600") + addVaryHeaderToken(w.Header(), "Access-Control-Request-Headers") + if strings.EqualFold(strings.TrimSpace(r.Header.Get("Access-Control-Request-Private-Network")), "true") { + w.Header().Set("Access-Control-Allow-Private-Network", "true") + addVaryHeaderToken(w.Header(), "Access-Control-Request-Private-Network") + } +} + +func buildCORSAllowHeaders(r *http.Request) string { + names := make([]string, 0, len(defaultCORSAllowHeaders)+4) + seen := make(map[string]struct{}, len(defaultCORSAllowHeaders)+4) + for _, name := range defaultCORSAllowHeaders { + appendCORSHeaderName(&names, seen, name) + } + if r == nil { + return strings.Join(names, ", ") + } + for _, name := range splitCORSRequestHeaders(r.Header.Get("Access-Control-Request-Headers")) { + appendCORSHeaderName(&names, seen, name) + } + return strings.Join(names, ", ") +} + +func splitCORSRequestHeaders(raw string) []string { + if strings.TrimSpace(raw) == "" { + return nil + } + parts := strings.Split(raw, ",") + out := make([]string, 0, len(parts)) + for _, part := range parts { + name := strings.TrimSpace(part) + if !isValidCORSHeaderToken(name) { + continue + } + if _, blocked := blockedCORSRequestHeaders[strings.ToLower(name)]; blocked { + continue + } + out = append(out, name) + } + return out +} + +func appendCORSHeaderName(dst *[]string, seen map[string]struct{}, name string) { + name = strings.TrimSpace(name) + if !isValidCORSHeaderToken(name) { + return + } + key := strings.ToLower(name) + if _, blocked := blockedCORSRequestHeaders[key]; blocked { + return + } + if _, ok := seen[key]; ok { + return + } + seen[key] = struct{}{} + *dst = append(*dst, name) +} + +func isValidCORSHeaderToken(v string) bool { + if v == "" { + return false + } + for i := 0; i < len(v); i++ { + c := v[i] + if (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') { + continue + } + switch c { + case '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.', '^', '_', '`', '|', '~': + continue + default: + return false + } + } + return true +} + +func addVaryHeaderToken(h http.Header, token string) { + if h == nil { + return + } + token = strings.TrimSpace(token) + if token == "" { + return + } + current := h.Values("Vary") + seen := map[string]struct{}{} + merged := make([]string, 0, len(current)+1) + for _, value := range current { + for _, part := range strings.Split(value, ",") { + name := strings.TrimSpace(part) + if name == "" { + continue + } + key := strings.ToLower(name) + if _, ok := seen[key]; ok { + continue + } + seen[key] = struct{}{} + merged = append(merged, name) + } + } + key := strings.ToLower(token) + if _, ok := seen[key]; !ok { + merged = append(merged, token) + } + h.Set("Vary", strings.Join(merged, ", ")) +} + +func WriteUnhandledError(w http.ResponseWriter, err error) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + _ = json.NewEncoder(w).Encode(map[string]any{"error": map[string]any{"type": "api_error", "message": "Internal Server Error", "detail": err.Error()}}) +} diff --git a/internal/server/router_cors_test.go b/internal/server/router_cors_test.go new file mode 100644 index 0000000000000000000000000000000000000000..448b1f1b6d4e88fc6b6df6f4f69631e282f7584d --- /dev/null +++ b/internal/server/router_cors_test.go @@ -0,0 +1,119 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestCORSPreflightAllowsThirdPartyRequestedHeaders(t *testing.T) { + handler := cors(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { + w.WriteHeader(http.StatusTeapot) + })) + + req := httptest.NewRequest(http.MethodOptions, "/v1/chat/completions", nil) + req.Header.Set("Origin", "app://obsidian.md") + req.Header.Set("Access-Control-Request-Headers", "authorization, x-stainless-os, x-stainless-runtime, x-ds2-internal-token") + req.Header.Set("Access-Control-Request-Private-Network", "true") + + rec := httptest.NewRecorder() + handler.ServeHTTP(rec, req) + + if rec.Code != http.StatusNoContent { + t.Fatalf("expected 204 for preflight, got %d", rec.Code) + } + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "app://obsidian.md" { + t.Fatalf("expected origin echo, got %q", got) + } + if got := rec.Header().Get("Access-Control-Allow-Private-Network"); got != "true" { + t.Fatalf("expected private network allow header, got %q", got) + } + + allowHeaders := strings.ToLower(rec.Header().Get("Access-Control-Allow-Headers")) + for _, want := range []string{"authorization", "x-stainless-os", "x-stainless-runtime"} { + if !strings.Contains(allowHeaders, want) { + t.Fatalf("expected allow headers to include %q, got %q", want, rec.Header().Get("Access-Control-Allow-Headers")) + } + } + if strings.Contains(allowHeaders, "x-ds2-internal-token") { + t.Fatalf("expected internal-only header to stay blocked, got %q", rec.Header().Get("Access-Control-Allow-Headers")) + } + + vary := strings.ToLower(rec.Header().Get("Vary")) + for _, want := range []string{"origin", "access-control-request-headers", "access-control-request-private-network"} { + if !strings.Contains(vary, want) { + t.Fatalf("expected vary to include %q, got %q", want, rec.Header().Get("Vary")) + } + } +} + +func TestBuildCORSAllowHeadersKeepsDefaultsWithoutRequest(t *testing.T) { + got := strings.ToLower(buildCORSAllowHeaders(nil)) + for _, want := range []string{"content-type", "x-goog-api-key", "anthropic-version", "x-ds2-source"} { + if !strings.Contains(got, want) { + t.Fatalf("expected default allow headers to include %q, got %q", want, got) + } + } +} + +func TestAppCORSPreflightIsUnifiedAcrossInterfaces(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[{"email":"u@example.com","password":"p"}]}`) + t.Setenv("DS2API_ENV_WRITEBACK", "0") + + app, err := NewApp() + if err != nil { + t.Fatalf("NewApp() error: %v", err) + } + + cases := []struct { + name string + path string + headers string + }{ + { + name: "openai", + path: "/v1/chat/completions", + headers: "authorization, x-stainless-os", + }, + { + name: "claude", + path: "/anthropic/v1/messages", + headers: "x-api-key, anthropic-version, x-stainless-os", + }, + { + name: "gemini", + path: "/v1beta/models/gemini-2.5-pro:generateContent", + headers: "x-goog-api-key, x-client-version", + }, + { + name: "admin", + path: "/admin/login", + headers: "content-type, x-requested-with", + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + req := httptest.NewRequest(http.MethodOptions, tc.path, nil) + req.Header.Set("Origin", "app://obsidian.md") + req.Header.Set("Access-Control-Request-Headers", tc.headers) + + rec := httptest.NewRecorder() + app.Router.ServeHTTP(rec, req) + + if rec.Code != http.StatusNoContent { + t.Fatalf("expected %s preflight status 204, got %d", tc.path, rec.Code) + } + if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "app://obsidian.md" { + t.Fatalf("expected origin echo for %s, got %q", tc.path, got) + } + allowHeaders := strings.ToLower(rec.Header().Get("Access-Control-Allow-Headers")) + for _, want := range splitCORSRequestHeaders(tc.headers) { + if !strings.Contains(allowHeaders, strings.ToLower(want)) { + t.Fatalf("expected allow headers for %s to include %q, got %q", tc.path, want, rec.Header().Get("Access-Control-Allow-Headers")) + } + } + }) + } +} diff --git a/internal/server/router_health_test.go b/internal/server/router_health_test.go new file mode 100644 index 0000000000000000000000000000000000000000..7c79d319f86ec5e426a8a1144a07c4c67bb60cf0 --- /dev/null +++ b/internal/server/router_health_test.go @@ -0,0 +1,26 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestHealthEndpointsSupportHEAD(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[{"email":"u@example.com","password":"p"}]}`) + t.Setenv("DS2API_ENV_WRITEBACK", "0") + + app, err := NewApp() + if err != nil { + t.Fatalf("NewApp() error: %v", err) + } + + for _, path := range []string{"/healthz", "/readyz"} { + req := httptest.NewRequest(http.MethodHead, path, nil) + rec := httptest.NewRecorder() + app.Router.ServeHTTP(rec, req) + if rec.Code != http.StatusOK { + t.Fatalf("expected %s HEAD status 200, got %d", path, rec.Code) + } + } +} diff --git a/internal/server/router_log_test.go b/internal/server/router_log_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5ecde480933bf0b78e9579789056b7ebd4d19fbe --- /dev/null +++ b/internal/server/router_log_test.go @@ -0,0 +1,104 @@ +package server + +import ( + "bytes" + "log" + "net/http" + "net/http/httptest" + "strings" + "testing" + "time" + + "github.com/go-chi/chi/v5/middleware" +) + +func TestFilteredLogFormatterRedactsSensitiveQueryParams(t *testing.T) { + var buf bytes.Buffer + formatter := &filteredLogFormatter{ + base: &middleware.DefaultLogFormatter{ + Logger: log.New(&buf, "", 0), + NoColor: true, + }, + } + req := httptest.NewRequest( + http.MethodPost, + "/v1beta/models/gemini-2.5-pro:generateContent?key=caller-secret&api_key=second-secret&alt=sse", + nil, + ) + + entry := formatter.NewLogEntry(req) + entry.Write(http.StatusOK, 0, http.Header{}, time.Millisecond, nil) + + got := buf.String() + for _, secret := range []string{"caller-secret", "second-secret"} { + if strings.Contains(got, secret) { + t.Fatalf("log line contains sensitive query value %q: %s", secret, got) + } + } + if !strings.Contains(got, "key=REDACTED") || !strings.Contains(got, "api_key=REDACTED") { + t.Fatalf("log line did not include redacted sensitive params: %s", got) + } + if !strings.Contains(got, "alt=sse") { + t.Fatalf("log line did not preserve non-sensitive query param: %s", got) + } + if req.URL.RawQuery != "key=caller-secret&api_key=second-secret&alt=sse" { + t.Fatalf("request was mutated, RawQuery = %q", req.URL.RawQuery) + } +} + +func TestFilteredLogFormatterRedactsSensitiveQueryParamsWhenMalformed(t *testing.T) { + tests := []struct { + name string + target string + secrets []string + redacted []string + preserved []string + }{ + { + name: "semicolon separator", + target: "/v1beta/models/gemini-2.5-pro:generateContent?key=caller-secret;alt=sse", + secrets: []string{"caller-secret"}, + redacted: []string{"key=REDACTED"}, + preserved: []string{"alt=sse"}, + }, + { + name: "bad escape in sensitive value", + target: "/v1beta/models/gemini-2.5-pro:generateContent?api_key=second-secret%ZZ", + secrets: []string{"second-secret"}, + redacted: []string{"api_key=REDACTED"}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + var buf bytes.Buffer + formatter := &filteredLogFormatter{ + base: &middleware.DefaultLogFormatter{ + Logger: log.New(&buf, "", 0), + NoColor: true, + }, + } + req := httptest.NewRequest(http.MethodPost, tt.target, nil) + + entry := formatter.NewLogEntry(req) + entry.Write(http.StatusOK, 0, http.Header{}, time.Millisecond, nil) + + got := buf.String() + for _, secret := range tt.secrets { + if strings.Contains(got, secret) { + t.Fatalf("log line contains sensitive query value %q: %s", secret, got) + } + } + for _, want := range tt.redacted { + if !strings.Contains(got, want) { + t.Fatalf("log line missing redacted query %q: %s", want, got) + } + } + for _, want := range tt.preserved { + if !strings.Contains(got, want) { + t.Fatalf("log line missing preserved query %q: %s", want, got) + } + } + }) + } +} diff --git a/internal/server/router_routes_test.go b/internal/server/router_routes_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f6e0a0a17ee91fdcca23d0ff7fdc6d5d803cfc6c --- /dev/null +++ b/internal/server/router_routes_test.go @@ -0,0 +1,108 @@ +package server + +import ( + "fmt" + "net/http" + "testing" + + "github.com/go-chi/chi/v5" +) + +func TestAPIRoutesRemainRegistered(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["k1"],"accounts":[{"email":"u@example.com","password":"p"}]}`) + t.Setenv("DS2API_ENV_WRITEBACK", "0") + + app, err := NewApp() + if err != nil { + t.Fatalf("NewApp() error: %v", err) + } + routes, ok := app.Router.(chi.Routes) + if !ok { + t.Fatalf("app router does not expose chi routes: %T", app.Router) + } + + got := map[string]bool{} + if err := chi.Walk(routes, func(method string, route string, _ http.Handler, _ ...func(http.Handler) http.Handler) error { + got[fmt.Sprintf("%s %s", method, route)] = true + return nil + }); err != nil { + t.Fatalf("walk routes: %v", err) + } + + for _, want := range []string{ + "GET /v1/models", + "GET /v1/models/{model_id}", + "POST /v1/chat/completions", + "POST /v1/responses", + "GET /v1/responses/{response_id}", + "POST /v1/files", + "GET /v1/files/{file_id}", + "POST /v1/embeddings", + "GET /models", + "GET /models/{model_id}", + "POST /chat/completions", + "POST /responses", + "GET /responses/{response_id}", + "POST /files", + "GET /files/{file_id}", + "POST /embeddings", + "GET /anthropic/v1/models", + "POST /anthropic/v1/messages", + "POST /anthropic/v1/messages/count_tokens", + "POST /v1/messages", + "POST /messages", + "POST /v1/messages/count_tokens", + "POST /messages/count_tokens", + "POST /v1beta/models/{model}:generateContent", + "POST /v1beta/models/{model}:streamGenerateContent", + "POST /v1/models/{model}:generateContent", + "POST /v1/models/{model}:streamGenerateContent", + "POST /admin/login", + "GET /admin/verify", + "GET /admin/config", + "POST /admin/config", + "GET /admin/settings", + "PUT /admin/settings", + "POST /admin/settings/password", + "POST /admin/config/import", + "GET /admin/config/export", + "POST /admin/keys", + "PUT /admin/keys/{key}", + "DELETE /admin/keys/{key}", + "GET /admin/proxies", + "POST /admin/proxies", + "PUT /admin/proxies/{proxyID}", + "DELETE /admin/proxies/{proxyID}", + "POST /admin/proxies/test", + "GET /admin/accounts", + "POST /admin/accounts", + "PUT /admin/accounts/{identifier}", + "DELETE /admin/accounts/{identifier}", + "PUT /admin/accounts/{identifier}/proxy", + "GET /admin/queue/status", + "POST /admin/accounts/test", + "POST /admin/accounts/test-all", + "POST /admin/accounts/sessions/delete-all", + "POST /admin/import", + "POST /admin/test", + "POST /admin/dev/raw-samples/capture", + "GET /admin/dev/raw-samples/query", + "POST /admin/dev/raw-samples/save", + "POST /admin/vercel/sync", + "GET /admin/vercel/status", + "POST /admin/vercel/status", + "GET /admin/export", + "GET /admin/dev/captures", + "DELETE /admin/dev/captures", + "GET /admin/chat-history", + "GET /admin/chat-history/{id}", + "DELETE /admin/chat-history", + "DELETE /admin/chat-history/{id}", + "PUT /admin/chat-history/settings", + "GET /admin/version", + } { + if !got[want] { + t.Fatalf("expected route %s to be registered", want) + } + } +} diff --git a/internal/server/router_utf8_test.go b/internal/server/router_utf8_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f06d6bb8f2f353bd3fbf1a071c6281ffd4b8a937 --- /dev/null +++ b/internal/server/router_utf8_test.go @@ -0,0 +1,89 @@ +package server + +import ( + "bytes" + "net/http" + "net/http/httptest" + "strings" + "testing" +) + +func TestJSONRequestsRejectInvalidUTF8BeforeDecode(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["managed-key"],"accounts":[{"email":"u@example.com","password":"p"}]}`) + t.Setenv("DS2API_ENV_WRITEBACK", "0") + + app, err := NewApp() + if err != nil { + t.Fatalf("NewApp() error: %v", err) + } + + body := append([]byte(`{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"`), 0xff) + body = append(body, []byte(`"}]}`)...) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json; charset=utf-8") + req.Header.Set("x-api-key", "direct-token") + + rec := httptest.NewRecorder() + app.Router.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400 for invalid utf-8 request body, got %d body=%q", rec.Code, rec.Body.String()) + } + if !strings.Contains(strings.ToLower(rec.Body.String()), "invalid json") { + t.Fatalf("expected invalid json error, got %q", rec.Body.String()) + } +} + +func TestKnownJSONRequestsRejectInvalidUTF8WithoutJSONContentType(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["managed-key"],"accounts":[{"email":"u@example.com","password":"p"}]}`) + t.Setenv("DS2API_ENV_WRITEBACK", "0") + + app, err := NewApp() + if err != nil { + t.Fatalf("NewApp() error: %v", err) + } + + body := append([]byte(`{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"`), 0xff) + body = append(body, []byte(`"}]}`)...) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", bytes.NewReader(body)) + req.Header.Set("Content-Type", "text/plain") + req.Header.Set("x-api-key", "direct-token") + + rec := httptest.NewRecorder() + app.Router.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400 for invalid utf-8 request body, got %d body=%q", rec.Code, rec.Body.String()) + } + if !strings.Contains(strings.ToLower(rec.Body.String()), "invalid json") { + t.Fatalf("expected invalid json error, got %q", rec.Body.String()) + } +} + +func TestJSONRequestsRejectTrailingInvalidUTF8AfterCompleteJSON(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":["managed-key"],"accounts":[{"email":"u@example.com","password":"p"}]}`) + t.Setenv("DS2API_ENV_WRITEBACK", "0") + + app, err := NewApp() + if err != nil { + t.Fatalf("NewApp() error: %v", err) + } + + body := append([]byte(`{"model":"deepseek-v4-flash","messages":[{"role":"user","content":"ok"}]}`), 0xff) + + req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", bytes.NewReader(body)) + req.Header.Set("Content-Type", "application/json") + req.Header.Set("x-api-key", "direct-token") + + rec := httptest.NewRecorder() + app.Router.ServeHTTP(rec, req) + + if rec.Code != http.StatusBadRequest { + t.Fatalf("expected 400 for trailing invalid utf-8, got %d body=%q", rec.Code, rec.Body.String()) + } + if !strings.Contains(strings.ToLower(rec.Body.String()), "invalid json") { + t.Fatalf("expected invalid json error, got %q", rec.Body.String()) + } +} diff --git a/internal/sse/citation_links.go b/internal/sse/citation_links.go new file mode 100644 index 0000000000000000000000000000000000000000..ef744b4c42b1b60cb7ef03efb628906578af33e3 --- /dev/null +++ b/internal/sse/citation_links.go @@ -0,0 +1,168 @@ +package sse + +import ( + "strconv" + "strings" +) + +type citationLinkCollector struct { + ordered []string + explicitRaw map[int]string + hasZeroIdx bool +} + +func newCitationLinkCollector() *citationLinkCollector { + return &citationLinkCollector{ + explicitRaw: map[int]string{}, + } +} + +func (c *citationLinkCollector) ingestChunk(chunk map[string]any) { + if c == nil || len(chunk) == 0 { + return + } + c.walkValue(chunk) +} + +func (c *citationLinkCollector) build() map[int]string { + out := make(map[int]string, len(c.explicitRaw)+len(c.ordered)) + for idx, u := range c.buildNormalizedExplicit() { + out[idx] = u + } + for i, u := range c.ordered { + idx := i + 1 + if _, exists := out[idx]; !exists { + out[idx] = u + } + } + return out +} + +func (c *citationLinkCollector) buildNormalizedExplicit() map[int]string { + out := make(map[int]string, len(c.explicitRaw)) + + // Default behavior keeps positive indices as-is (one-based payloads). + for idx, u := range c.explicitRaw { + if idx <= 0 || strings.TrimSpace(u) == "" { + continue + } + out[idx] = u + } + + if !c.hasZeroIdx { + return out + } + + // If zero index appears, upstream may be using zero-based indices. + // Add shifted candidates and resolve conflicts using ordered appearance, + // which matches visible citation marker order in response text. + for rawIdx, u := range c.explicitRaw { + if rawIdx < 0 || strings.TrimSpace(u) == "" { + continue + } + normalized := rawIdx + 1 + existing, exists := out[normalized] + if !exists { + out[normalized] = u + continue + } + if c.preferURLForIndex(normalized, existing, u) == u { + out[normalized] = u + } + } + + return out +} + +func (c *citationLinkCollector) preferURLForIndex(idx int, current, candidate string) string { + if idx <= 0 || idx > len(c.ordered) { + return current + } + expected := c.ordered[idx-1] + switch { + case strings.TrimSpace(expected) == "": + return current + case candidate == expected && current != expected: + return candidate + default: + return current + } +} + +func (c *citationLinkCollector) walkValue(v any) { + switch x := v.(type) { + case []any: + for _, item := range x { + c.walkValue(item) + } + case map[string]any: + c.captureURLAndIndex(x) + for _, vv := range x { + c.walkValue(vv) + } + } +} + +func (c *citationLinkCollector) captureURLAndIndex(m map[string]any) { + url := strings.TrimSpace(asString(m["url"])) + if !isWebURL(url) { + return + } + c.addOrdered(url) + + idx, hasIdx := citationIndexFromAny(m["cite_index"]) + if !hasIdx { + return + } + if idx < 0 { + return + } + if idx == 0 { + c.hasZeroIdx = true + } + if existing, ok := c.explicitRaw[idx]; ok && strings.TrimSpace(existing) != "" { + return + } + c.explicitRaw[idx] = url +} + +func (c *citationLinkCollector) addOrdered(url string) { + c.ordered = append(c.ordered, url) +} + +func citationIndexFromAny(v any) (int, bool) { + switch x := v.(type) { + case int: + return x, true + case int32: + return int(x), true + case int64: + return int(x), true + case float32: + return int(x), true + case float64: + return int(x), true + case string: + s := strings.TrimSpace(x) + if s == "" { + return 0, false + } + n, err := strconv.Atoi(s) + if err != nil { + return 0, false + } + return n, true + default: + return 0, false + } +} + +func isWebURL(v string) bool { + v = strings.ToLower(strings.TrimSpace(v)) + return strings.HasPrefix(v, "http://") || strings.HasPrefix(v, "https://") +} + +func asString(v any) string { + s, _ := v.(string) + return s +} diff --git a/internal/sse/consumer.go b/internal/sse/consumer.go new file mode 100644 index 0000000000000000000000000000000000000000..db42bf563b3e904bd4328a082bc0c8dd30c8590a --- /dev/null +++ b/internal/sse/consumer.go @@ -0,0 +1,119 @@ +package sse + +import ( + "net/http" + "strings" + + dsprotocol "ds2api/internal/deepseek/protocol" + "ds2api/internal/util" +) + +// CollectResult holds the aggregated text and thinking content from a +// DeepSeek SSE stream, consumed to completion (non-streaming use case). +type CollectResult struct { + Text string + Thinking string + ToolDetectionThinking string + ContentFilter bool + CitationLinks map[int]string + ResponseMessageID int +} + +// CollectStream fully consumes a DeepSeek SSE response and separates +// thinking content from text content. This replaces the duplicated +// stream-collection logic in openai.handleNonStream, claude.collectDeepSeek, +// and admin.testAccount. +// +// The caller is responsible for closing resp.Body unless closeBody is true. +func CollectStream(resp *http.Response, thinkingEnabled bool, closeBody bool) CollectResult { + if closeBody { + defer func() { _ = resp.Body.Close() }() + } + text := strings.Builder{} + thinking := strings.Builder{} + toolDetectionThinking := strings.Builder{} + contentFilter := false + stopped := false + collector := newCitationLinkCollector() + responseMessageID := 0 + currentType := "text" + if thinkingEnabled { + currentType = "thinking" + } + _ = dsprotocol.ScanSSELines(resp, func(line []byte) bool { + chunk, done, parsed := ParseDeepSeekSSELine(line) + if parsed && !done { + collector.ingestChunk(chunk) + observeResponseMessageID(chunk, &responseMessageID) + } + if done { + return false + } + if stopped { + return true + } + result := ParseDeepSeekContentLine(line, thinkingEnabled, currentType) + currentType = result.NextType + if !result.Parsed { + return true + } + if result.Stop { + if result.ContentFilter { + contentFilter = true + } + // Keep scanning to collect late-arriving citation metadata lines + // that can appear after response/status=FINISHED, but stop as soon + // as [DONE] arrives. + stopped = true + return true + } + for _, p := range result.Parts { + if p.Type == "thinking" { + trimmed := TrimContinuationOverlap(thinking.String(), p.Text) + thinking.WriteString(trimmed) + } else { + trimmed := TrimContinuationOverlap(text.String(), p.Text) + text.WriteString(trimmed) + } + } + for _, p := range result.ToolDetectionThinkingParts { + trimmed := TrimContinuationOverlap(toolDetectionThinking.String(), p.Text) + toolDetectionThinking.WriteString(trimmed) + } + return true + }) + return CollectResult{ + Text: text.String(), + Thinking: thinking.String(), + ToolDetectionThinking: toolDetectionThinking.String(), + ContentFilter: contentFilter, + CitationLinks: collector.build(), + ResponseMessageID: responseMessageID, + } +} + +// observeResponseMessageID extracts the response_message_id from a parsed SSE +// chunk. It mirrors the extraction logic in client_continue.go's observe +// method, checking top-level response_message_id, v.response.message_id, and +// message.response.message_id. +func observeResponseMessageID(chunk map[string]any, out *int) { + if chunk == nil || out == nil { + return + } + if id := util.IntFrom(chunk["response_message_id"]); id > 0 { + *out = id + } + v, _ := chunk["v"].(map[string]any) + if response, _ := v["response"].(map[string]any); response != nil { + if id := util.IntFrom(response["message_id"]); id > 0 { + *out = id + } + } + if message, _ := chunk["message"].(map[string]any); message != nil { + if response, _ := message["response"].(map[string]any); response != nil { + if id := util.IntFrom(response["message_id"]); id > 0 { + *out = id + } + } + } +} diff --git a/internal/sse/consumer_edge_test.go b/internal/sse/consumer_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..8d39a3f19a7d3e9179f6dda7beff8b94bea2eda8 --- /dev/null +++ b/internal/sse/consumer_edge_test.go @@ -0,0 +1,298 @@ +package sse + +import ( + "io" + "net/http" + "strings" + "testing" + "time" +) + +// ─── CollectStream edge cases ──────────────────────────────────────── + +func makeHTTPResponse(body string) *http.Response { + return &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(body)), + } +} + +func TestCollectStreamEmpty(t *testing.T) { + resp := makeHTTPResponse("") + result := CollectStream(resp, false, false) + if result.Text != "" || result.Thinking != "" { + t.Fatalf("expected empty result, got text=%q think=%q", result.Text, result.Thinking) + } +} + +func TestCollectStreamTextOnly(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/content\",\"v\":\"Hello\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\" World\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, false) + if result.Text != "Hello World" { + t.Fatalf("expected 'Hello World', got %q", result.Text) + } + if result.Thinking != "" { + t.Fatalf("expected no thinking, got %q", result.Thinking) + } +} + +func TestCollectStreamHandlesLongSingleSSELine(t *testing.T) { + payload := strings.Repeat("x", 2*1024*1024+4096) + resp := makeHTTPResponse(makeLargeContentSSEBody(t, payload)) + result := CollectStream(resp, false, true) + if result.Text != payload { + t.Fatalf("long SSE line payload mismatch: got len=%d want len=%d", len(result.Text), len(payload)) + } +} + +func TestCollectStreamThinkingAndText(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/thinking_content\",\"v\":\"Thinking...\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\"Answer\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, true, true) + if result.Thinking != "Thinking..." { + t.Fatalf("expected 'Thinking...', got %q", result.Thinking) + } + if result.Text != "Answer" { + t.Fatalf("expected 'Answer', got %q", result.Text) + } +} + +func TestCollectStreamDropsThinkingWhenDisabled(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/thinking_content\",\"v\":\"Thinking...\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\"Answer\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, true) + if result.Thinking != "" { + t.Fatalf("expected disabled thinking to be dropped, got %q", result.Thinking) + } + if result.Text != "Answer" { + t.Fatalf("expected only visible answer, got %q", result.Text) + } +} + +func TestCollectStreamOnlyThinking(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/thinking_content\",\"v\":\"Only thinking\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, true, true) + if result.Thinking != "Only thinking" { + t.Fatalf("expected 'Only thinking', got %q", result.Thinking) + } + if result.Text != "" { + t.Fatalf("expected empty text, got %q", result.Text) + } +} + +func TestCollectStreamSkipsInvalidLines(t *testing.T) { + resp := makeHTTPResponse( + "event: comment\n" + + "data: invalid_json\n" + + "data: {\"p\":\"response/content\",\"v\":\"valid\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, false) + if result.Text != "valid" { + t.Fatalf("expected 'valid', got %q", result.Text) + } +} + +func TestCollectStreamWithFragments(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/fragments\",\"o\":\"APPEND\",\"v\":[{\"type\":\"THINK\",\"content\":\"Think\"}]}\n" + + "data: {\"p\":\"response/fragments\",\"o\":\"APPEND\",\"v\":[{\"type\":\"RESPONSE\",\"content\":\"Done\"}]}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, true, true) + if result.Thinking != "Think" { + t.Fatalf("expected 'Think' thinking, got %q", result.Thinking) + } + if result.Text != "Done" { + t.Fatalf("expected 'Done' text, got %q", result.Text) + } +} + +func TestCollectStreamWithCitation(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/content\",\"v\":\"Hello\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\"[citation:1] cited text\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\" more\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, false) + // CollectStream does NOT filter citations (that's done by the adapters) + // So citations are passed through as-is + if !strings.Contains(result.Text, "[citation:1]") { + t.Fatalf("expected citations to be passed through, got %q", result.Text) + } + if result.Text != "Hello[citation:1] cited text more" { + t.Fatalf("expected full text with citation, got %q", result.Text) + } +} + +func TestCollectStreamExtractsCitationLinks(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/fragments/-1/results\",\"v\":[{\"url\":\"https://example.com/a\",\"cite_index\":0},{\"url\":\"https://example.com/b\",\"cite_index\":1}]}\n" + + "data: {\"p\":\"response/content\",\"v\":\"结论[citation:1][citation:2]\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, false) + + if got := result.CitationLinks[1]; got != "https://example.com/a" { + t.Fatalf("expected citation 1 link, got %q", got) + } + if got := result.CitationLinks[2]; got != "https://example.com/b" { + t.Fatalf("expected citation 2 link, got %q", got) + } +} + +func TestCollectStreamExtractsCitationLinksForSequentialZeroBasedIndices(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/fragments/-1/results\",\"v\":[{\"url\":\"https://example.com/a\",\"cite_index\":0},{\"url\":\"https://example.com/b\",\"cite_index\":1},{\"url\":\"https://example.com/c\",\"cite_index\":2}]}\n" + + "data: {\"p\":\"response/content\",\"v\":\"结论[citation:1][citation:2][citation:3]\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, false) + + if got := result.CitationLinks[1]; got != "https://example.com/a" { + t.Fatalf("expected citation 1 link, got %q", got) + } + if got := result.CitationLinks[2]; got != "https://example.com/b" { + t.Fatalf("expected citation 2 link, got %q", got) + } + if got := result.CitationLinks[3]; got != "https://example.com/c" { + t.Fatalf("expected citation 3 link, got %q", got) + } +} + +func TestCollectStreamExtractsCitationLinksForOneBasedIndices(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/fragments/-1/results\",\"v\":[{\"url\":\"https://example.com/a\",\"cite_index\":1},{\"url\":\"https://example.com/b\",\"cite_index\":2}]}\n" + + "data: {\"p\":\"response/content\",\"v\":\"结论[citation:1][citation:2]\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, false) + + if got := result.CitationLinks[1]; got != "https://example.com/a" { + t.Fatalf("expected citation 1 link, got %q", got) + } + if got := result.CitationLinks[2]; got != "https://example.com/b" { + t.Fatalf("expected citation 2 link, got %q", got) + } +} + +func TestCollectStreamExtractsCitationLinksWithRepeatedURLsAndNilIndices(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/fragments/-1/results\",\"v\":[{\"url\":\"https://example.com/a\",\"cite_index\":null},{\"url\":\"https://example.com/a\",\"cite_index\":null},{\"url\":\"https://example.com/b\",\"cite_index\":null}]}\n" + + "data: {\"p\":\"response/content\",\"v\":\"结论[citation:1][citation:2][citation:3]\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, false, false) + + if got := result.CitationLinks[1]; got != "https://example.com/a" { + t.Fatalf("expected citation 1 link, got %q", got) + } + if got := result.CitationLinks[2]; got != "https://example.com/a" { + t.Fatalf("expected citation 2 link, got %q", got) + } + if got := result.CitationLinks[3]; got != "https://example.com/b" { + t.Fatalf("expected citation 3 link, got %q", got) + } +} + +func TestCollectStreamCollectsCitationLinksAfterFinished(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/content\",\"v\":\"结论[citation:1]\"}\n" + + "data: {\"p\":\"response/status\",\"v\":\"FINISHED\"}\n" + + "data: {\"p\":\"response/fragments/-1/results\",\"v\":[{\"url\":\"https://example.com/a\",\"cite_index\":1}]}\n" + + "data: {\"p\":\"response/content\",\"v\":\"should-not-append\"}\n" + + "data: [DONE]\n", + ) + + result := CollectStream(resp, false, false) + if result.Text != "结论[citation:1]" { + t.Fatalf("expected text to freeze after finished, got %q", result.Text) + } + if got := result.CitationLinks[1]; got != "https://example.com/a" { + t.Fatalf("expected citation 1 link, got %q", got) + } +} + +func TestCollectStreamMultipleThinkingChunks(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/thinking_content\",\"v\":\"part1\"}\n" + + "data: {\"p\":\"response/thinking_content\",\"v\":\" part2\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\"answer\"}\n" + + "data: [DONE]\n", + ) + result := CollectStream(resp, true, true) + if result.Thinking != "part1 part2" { + t.Fatalf("expected 'part1 part2', got %q", result.Thinking) + } +} + +func TestCollectStreamStatusFinished(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/content\",\"v\":\"Hello\"}\n" + + "data: {\"p\":\"response/status\",\"v\":\"FINISHED\"}\n", + ) + result := CollectStream(resp, false, false) + if result.Text != "Hello" { + t.Fatalf("expected 'Hello', got %q", result.Text) + } +} + +func TestCollectStreamStopsOnDoneAfterFinished(t *testing.T) { + pr, pw := io.Pipe() + defer func() { _ = pw.Close() }() + + resp := &http.Response{ + StatusCode: http.StatusOK, + Header: make(http.Header), + Body: pr, + } + + resultCh := make(chan CollectResult, 1) + go func() { + resultCh <- CollectStream(resp, false, false) + }() + + _, _ = io.WriteString(pw, "data: {\"p\":\"response/content\",\"v\":\"Hello\"}\n") + _, _ = io.WriteString(pw, "data: {\"p\":\"response/status\",\"v\":\"FINISHED\"}\n") + _, _ = io.WriteString(pw, "data: {\"p\":\"response/fragments/-1/results\",\"v\":[{\"url\":\"https://example.com/a\",\"cite_index\":1}]}\n") + _, _ = io.WriteString(pw, "data: [DONE]\n") + + select { + case result := <-resultCh: + if result.Text != "Hello" { + t.Fatalf("expected text to freeze at FINISHED, got %q", result.Text) + } + if got := result.CitationLinks[1]; got != "https://example.com/a" { + t.Fatalf("expected citation metadata after FINISHED, got %q", got) + } + case <-time.After(500 * time.Millisecond): + t.Fatal("CollectStream did not stop on [DONE] after FINISHED") + } +} + +func TestCollectStreamStopsOnContentFilterStatus(t *testing.T) { + resp := makeHTTPResponse( + "data: {\"p\":\"response/content\",\"v\":\"safe\"}\n" + + "data: {\"p\":\"response/status\",\"v\":\"CONTENT_FILTER\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\"blocked\"}\n", + ) + result := CollectStream(resp, false, false) + if result.Text != "safe" { + t.Fatalf("expected stream to stop before blocked tail, got %q", result.Text) + } +} diff --git a/internal/sse/consumer_test.go b/internal/sse/consumer_test.go new file mode 100644 index 0000000000000000000000000000000000000000..c9ed048fe211e6ed2d36cae290bfe473249e2a68 --- /dev/null +++ b/internal/sse/consumer_test.go @@ -0,0 +1,30 @@ +package sse + +import ( + "io" + "net/http" + "strings" + "testing" +) + +func TestCollectStreamDedupesContinueSnapshotReplay(t *testing.T) { + prefix := "我们被问到:这是一个很长的续答快照前缀,用来验证去重逻辑不会误伤正常 token。" + body := strings.Join([]string{ + `data: {"v":{"response":{"fragments":[{"id":2,"type":"THINK","content":"` + prefix + `","references":[],"stage_id":1}]}}}`, + ``, + `data: {"p":"response/status","v":"INCOMPLETE"}`, + ``, + `data: {"v":{"response":{"fragments":[{"id":2,"type":"THINK","content":"` + prefix + `继续","references":[],"stage_id":1}]}}}`, + ``, + `data: {"v":"分析"}`, + ``, + `data: {"p":"response/status","v":"FINISHED"}`, + ``, + }, "\n") + + resp := &http.Response{Body: io.NopCloser(strings.NewReader(body))} + got := CollectStream(resp, true, true) + if got.Thinking != prefix+"继续分析" { + t.Fatalf("unexpected thinking after dedupe: %q", got.Thinking) + } +} diff --git a/internal/sse/content_filter_leak.go b/internal/sse/content_filter_leak.go new file mode 100644 index 0000000000000000000000000000000000000000..874b46a26eb7d0527f26972a6be1944a26c70612 --- /dev/null +++ b/internal/sse/content_filter_leak.go @@ -0,0 +1,49 @@ +package sse + +import "strings" + +func filterLeakedContentFilterParts(parts []ContentPart) []ContentPart { + if len(parts) == 0 { + return parts + } + out := make([]ContentPart, 0, len(parts)) + for _, p := range parts { + cleaned, stripped := stripLeakedContentFilterSuffix(p.Text) + // Only drop the chunk when we actually stripped a leaked CONTENT_FILTER + // suffix. Plain whitespace chunks are valid SSE content and must stay. + if stripped && shouldDropCleanedLeakedChunk(cleaned) { + continue + } + if stripped { + p.Text = cleaned + } + out = append(out, p) + } + return out +} + +func stripLeakedContentFilterSuffix(text string) (string, bool) { + if text == "" { + return text, false + } + upperText := strings.ToUpper(text) + idx := strings.Index(upperText, "CONTENT_FILTER") + if idx < 0 { + return text, false + } + // Keep "\n" so we don't collapse line structure when the upstream model + // appends leaked CONTENT_FILTER markers after a line break. + return strings.TrimRight(text[:idx], " \t\r"), true +} + +func shouldDropCleanedLeakedChunk(cleaned string) bool { + if cleaned == "" { + return true + } + // Preserve newline-only chunks to avoid dropping legitimate line breaks + // before a leaked CONTENT_FILTER suffix. + if strings.Contains(cleaned, "\n") { + return false + } + return strings.TrimSpace(cleaned) == "" +} diff --git a/internal/sse/dedupe.go b/internal/sse/dedupe.go new file mode 100644 index 0000000000000000000000000000000000000000..259c89ff9c9adea487abc0760a5f7d332df6109f --- /dev/null +++ b/internal/sse/dedupe.go @@ -0,0 +1,57 @@ +package sse + +import ( + "strings" + "unicode/utf8" +) + +const minContinuationSnapshotLen = 32 + +func TrimContinuationOverlap(existing, incoming string) string { + if incoming == "" { + return "" + } + if existing == "" { + return incoming + } + if utf8.RuneCountInString(incoming) < minContinuationSnapshotLen { + return incoming + } + if len(incoming) > len(existing) { + if strings.HasPrefix(incoming, existing) { + return incoming[len(existing):] + } + return incoming + } + if len(incoming) < len(existing) && strings.HasPrefix(existing, incoming) { + return "" + } + return incoming +} + +func TrimContinuationOverlapFromBuilder(existing *strings.Builder, incoming string) string { + if incoming == "" { + return "" + } + if existing == nil || existing.Len() == 0 { + return incoming + } + if utf8.RuneCountInString(incoming) < minContinuationSnapshotLen { + return incoming + } + existingLen := existing.Len() + if len(incoming) > existingLen { + existingStr := existing.String() + if strings.HasPrefix(incoming, existingStr) { + return incoming[existingLen:] + } + return incoming + } + if len(incoming) < existingLen { + existingStr := existing.String() + if strings.HasPrefix(existingStr, incoming) { + return "" + } + } + return incoming +} diff --git a/internal/sse/dedupe_test.go b/internal/sse/dedupe_test.go new file mode 100644 index 0000000000000000000000000000000000000000..71692c4e2907193048a6feb4d7dc9497bcb40183 --- /dev/null +++ b/internal/sse/dedupe_test.go @@ -0,0 +1,51 @@ +package sse + +import ( + "strings" + "testing" +) + +func TestTrimContinuationOverlapReturnsSuffixForSnapshotReplay(t *testing.T) { + existing := "我们被问到:这是一个很长的续答快照前缀,用来验证去重逻辑不会误伤正常 token。" + incoming := existing + "继续分析" + got := TrimContinuationOverlap(existing, incoming) + if got != "继续分析" { + t.Fatalf("expected suffix only, got %q", got) + } +} + +func TestTrimContinuationOverlapDropsStaleShorterSnapshot(t *testing.T) { + incoming := "我们被问到:这是一个很长的续答快照前缀,用来验证去重逻辑不会误伤正常 token。" + existing := incoming + "继续分析" + got := TrimContinuationOverlap(existing, incoming) + if got != "" { + t.Fatalf("expected stale snapshot to be dropped, got %q", got) + } +} + +func TestTrimContinuationOverlapPreservesNormalIncrement(t *testing.T) { + existing := "我们" + incoming := "被" + got := TrimContinuationOverlap(existing, incoming) + if got != "被" { + t.Fatalf("expected normal increment unchanged, got %q", got) + } +} + +func TestTrimContinuationOverlapKeepsShortPrefixLikeNormalToken(t *testing.T) { + existing := "我们被问到" + incoming := "我们" + got := TrimContinuationOverlap(existing, incoming) + if got != "我们" { + t.Fatalf("expected short token preserved, got %q", got) + } +} + +func TestTrimContinuationOverlapKeepsShortMultibyteChunk(t *testing.T) { + existing := strings.Repeat("字", 36) + incoming := strings.Repeat("字", 16) + got := TrimContinuationOverlap(existing, incoming) + if got != incoming { + t.Fatalf("expected short multibyte chunk preserved, got %q", got) + } +} diff --git a/internal/sse/line.go b/internal/sse/line.go new file mode 100644 index 0000000000000000000000000000000000000000..a52a9ab19f41360f7dd27a12d9a3bbf417969f00 --- /dev/null +++ b/internal/sse/line.go @@ -0,0 +1,66 @@ +package sse + +import ( + "fmt" +) + +// LineResult is the normalized parse result for one DeepSeek SSE line. +type LineResult struct { + Parsed bool + Stop bool + ContentFilter bool + ErrorMessage string + Parts []ContentPart + ToolDetectionThinkingParts []ContentPart + NextType string + ResponseMessageID int +} + +// ParseDeepSeekContentLine centralizes one-line DeepSeek SSE parsing for both +// streaming and non-streaming handlers. +func ParseDeepSeekContentLine(raw []byte, thinkingEnabled bool, currentType string) LineResult { + chunk, done, parsed := ParseDeepSeekSSELine(raw) + if !parsed { + return LineResult{NextType: currentType} + } + if done { + return LineResult{Parsed: true, Stop: true, NextType: currentType} + } + if errObj, hasErr := chunk["error"]; hasErr { + return LineResult{ + Parsed: true, + Stop: true, + ErrorMessage: fmt.Sprintf("%v", errObj), + NextType: currentType, + } + } + if code, _ := chunk["code"].(string); code == "content_filter" { + return LineResult{ + Parsed: true, + Stop: true, + ContentFilter: true, + NextType: currentType, + } + } + if hasContentFilterStatus(chunk) { + return LineResult{ + Parsed: true, + Stop: true, + ContentFilter: true, + NextType: currentType, + } + } + parts, detectionThinkingParts, finished, nextType := ParseSSEChunkForContentDetailed(chunk, thinkingEnabled, currentType) + parts = filterLeakedContentFilterParts(parts) + detectionThinkingParts = filterLeakedContentFilterParts(detectionThinkingParts) + var respMsgID int + observeResponseMessageID(chunk, &respMsgID) + return LineResult{ + Parsed: true, + Stop: finished, + Parts: parts, + ToolDetectionThinkingParts: detectionThinkingParts, + NextType: nextType, + ResponseMessageID: respMsgID, + } +} diff --git a/internal/sse/line_edge_test.go b/internal/sse/line_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..4d507fc023960a843f7d4e1aa05f53ec0b838351 --- /dev/null +++ b/internal/sse/line_edge_test.go @@ -0,0 +1,70 @@ +package sse + +import "testing" + +func TestParseDeepSeekContentLineNotParsed(t *testing.T) { + res := ParseDeepSeekContentLine([]byte("not a data line"), false, "text") + if res.Parsed { + t.Fatal("expected not parsed") + } + if res.NextType != "text" { + t.Fatalf("expected nextType preserved, got %q", res.NextType) + } +} + +func TestParseDeepSeekContentLinePreservesNextType(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/thinking_content","v":"think"}`), true, "thinking") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop: %#v", res) + } + if len(res.Parts) != 1 || res.Parts[0].Type != "thinking" { + t.Fatalf("unexpected parts: %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLineFragmentSwitchType(t *testing.T) { + res := ParseDeepSeekContentLine( + []byte(`data: {"p":"response/fragments","o":"APPEND","v":[{"type":"RESPONSE","content":"hi"}]}`), + true, "thinking", + ) + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop: %#v", res) + } + if res.NextType != "text" { + t.Fatalf("expected nextType text after RESPONSE fragment, got %q", res.NextType) + } +} + +func TestParseDeepSeekContentLineContentFilterMessage(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"code":"content_filter"}`), false, "text") + if !res.ContentFilter { + t.Fatal("expected content filter flag") + } + if res.ErrorMessage != "" { + t.Fatalf("expected empty error message on content filter, got %q", res.ErrorMessage) + } +} + +func TestParseDeepSeekContentLineErrorObjectFormat(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"error":{"message":"rate limit","code":429}}`), false, "text") + if !res.Parsed || !res.Stop { + t.Fatalf("expected parsed stop: %#v", res) + } + if res.ErrorMessage == "" { + t.Fatal("expected non-empty error message") + } +} + +func TestParseDeepSeekContentLineInvalidJSON(t *testing.T) { + res := ParseDeepSeekContentLine([]byte("data: {broken"), false, "text") + if res.Parsed { + t.Fatal("expected not parsed for broken JSON") + } +} + +func TestParseDeepSeekContentLineEmptyBytes(t *testing.T) { + res := ParseDeepSeekContentLine([]byte{}, false, "text") + if res.Parsed { + t.Fatal("expected not parsed for empty bytes") + } +} diff --git a/internal/sse/line_test.go b/internal/sse/line_test.go new file mode 100644 index 0000000000000000000000000000000000000000..26c6e95fb5333b72952cd9adf039ead37b27e624 --- /dev/null +++ b/internal/sse/line_test.go @@ -0,0 +1,155 @@ +package sse + +import "testing" + +func TestParseDeepSeekContentLineDone(t *testing.T) { + res := ParseDeepSeekContentLine([]byte("data: [DONE]"), false, "text") + if !res.Parsed || !res.Stop { + t.Fatalf("expected parsed stop result: %#v", res) + } +} + +func TestParseDeepSeekContentLineError(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"error":"boom"}`), false, "text") + if !res.Parsed || !res.Stop { + t.Fatalf("expected stop on error: %#v", res) + } + if res.ErrorMessage == "" { + t.Fatalf("expected non-empty error message") + } +} + +func TestParseDeepSeekContentLineContentFilter(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"code":"content_filter"}`), false, "text") + if !res.Parsed || !res.Stop || !res.ContentFilter { + t.Fatalf("expected content-filter stop result: %#v", res) + } +} + +func TestParseDeepSeekContentLineContentFilterCodeStops(t *testing.T) { + res := ParseDeepSeekContentLine( + []byte(`data: {"code":"content_filter","accumulated_token_usage":99}`), + false, "text", + ) + if !res.Parsed || !res.Stop || !res.ContentFilter { + t.Fatalf("expected content-filter stop result: %#v", res) + } +} + +func TestParseDeepSeekContentLineContentFilterStatus(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/status","v":"CONTENT_FILTER"}`), false, "text") + if !res.Parsed || !res.Stop || !res.ContentFilter { + t.Fatalf("expected status-based content-filter stop result: %#v", res) + } +} + +func TestParseDeepSeekContentLineIgnoresAccumulatedTokenUsage(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response","o":"BATCH","v":[{"p":"accumulated_token_usage","v":1383},{"p":"quasi_status","v":"FINISHED"}]}`), false, "text") + if !res.Parsed { + t.Fatalf("expected parsed result") + } +} + +func TestParseDeepSeekContentLineIgnoresAccumulatedTokenUsageString(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response","o":"BATCH","v":[{"p":"accumulated_token_usage","v":"190"},{"p":"quasi_status","v":"FINISHED"}]}`), false, "text") + if !res.Parsed { + t.Fatalf("expected parsed result") + } +} + +func TestParseDeepSeekContentLineErrorStops(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"error":"boom","accumulated_token_usage":123}`), false, "text") + if !res.Parsed || !res.Stop { + t.Fatalf("expected stop on error: %#v", res) + } +} + +func TestParseDeepSeekContentLineContent(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/content","v":"hi"}`), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 1 || res.Parts[0].Text != "hi" || res.Parts[0].Type != "text" { + t.Fatalf("unexpected parts: %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLineFiltersIncompleteStatusText(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/status","v":"INCOMPLETE"}`), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 0 { + t.Fatalf("expected INCOMPLETE status to be filtered, got %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLinePreservesSpaceOnlyChunk(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"v":" "}`), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 1 || res.Parts[0].Text != " " || res.Parts[0].Type != "text" { + t.Fatalf("unexpected parts for space-only chunk: %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLineStripsLeakedContentFilterSuffix(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/content","v":"正常输出CONTENT_FILTER你好,这个问题我暂时无法回答"}`), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 1 || res.Parts[0].Text != "正常输出" { + t.Fatalf("unexpected parts after filter: %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLineDropsPureLeakedContentFilterChunk(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/content","v":"CONTENT_FILTER你好,这个问题我暂时无法回答"}`), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 0 { + t.Fatalf("expected empty parts, got %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLineTrimsFromContentFilterKeyword(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/content","v":"模型会在命中 CONTENT_FILTER 时返回拒绝原因。"}`), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 1 || res.Parts[0].Text != "模型会在命中" { + t.Fatalf("unexpected parts after filter: %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLineContentTextEqualContentFilterDoesNotStop(t *testing.T) { + res := ParseDeepSeekContentLine([]byte(`data: {"p":"response/content","v":"content_filter"}`), false, "text") + if !res.Parsed { + t.Fatalf("expected parsed result: %#v", res) + } + if res.Stop || res.ContentFilter { + t.Fatalf("did not expect content-filter stop for content text: %#v", res) + } +} + +func TestParseDeepSeekContentLinePreservesTrailingNewlineBeforeLeakedContentFilter(t *testing.T) { + res := ParseDeepSeekContentLine([]byte("data: {\"p\":\"response/content\",\"v\":\"line1\\nCONTENT_FILTERblocked\"}"), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 1 || res.Parts[0].Text != "line1\n" { + t.Fatalf("expected trailing newline preserved, got %#v", res.Parts) + } +} + +func TestParseDeepSeekContentLineKeepsNewlineOnlyChunkBeforeLeakedContentFilter(t *testing.T) { + res := ParseDeepSeekContentLine([]byte("data: {\"p\":\"response/content\",\"v\":\"\\nCONTENT_FILTERblocked\"}"), false, "text") + if !res.Parsed || res.Stop { + t.Fatalf("expected parsed non-stop result: %#v", res) + } + if len(res.Parts) != 1 || res.Parts[0].Text != "\n" { + t.Fatalf("expected newline-only chunk preserved, got %#v", res.Parts) + } +} diff --git a/internal/sse/parser.go b/internal/sse/parser.go new file mode 100644 index 0000000000000000000000000000000000000000..528bd2fee902ae31f9de57f150401d3c8f385849 --- /dev/null +++ b/internal/sse/parser.go @@ -0,0 +1,504 @@ +package sse + +import ( + "bytes" + "encoding/json" + "regexp" + "strings" + + dsprotocol "ds2api/internal/deepseek/protocol" +) + +type ContentPart struct { + Text string + Type string +} + +func ParseDeepSeekSSELine(raw []byte) (map[string]any, bool, bool) { + line := strings.TrimSpace(string(raw)) + if line == "" || !strings.HasPrefix(line, "data:") { + return nil, false, false + } + dataStr := strings.TrimSpace(strings.TrimPrefix(line, "data:")) + if dataStr == "[DONE]" { + return nil, true, true + } + chunk := map[string]any{} + if err := json.Unmarshal([]byte(dataStr), &chunk); err != nil { + return nil, false, false + } + return chunk, false, true +} + +func shouldSkipPath(path string) bool { + if isFragmentStatusPath(path) { + return true + } + if _, ok := dsprotocol.SkipExactPathSet[path]; ok { + return true + } + for _, p := range dsprotocol.SkipContainsPatterns { + if strings.Contains(path, p) { + return true + } + } + return false +} + +func isFragmentStatusPath(path string) bool { + if path == "" || path == "response/status" { + return false + } + if !strings.HasPrefix(path, "response/fragments/") || !strings.HasSuffix(path, "/status") { + return false + } + mid := strings.TrimSuffix(strings.TrimPrefix(path, "response/fragments/"), "/status") + if mid == "" { + return false + } + mid = strings.TrimPrefix(mid, "-") + if mid == "" { + return false + } + for _, r := range mid { + if r < '0' || r > '9' { + return false + } + } + return true +} + +func ParseSSEChunkForContent(chunk map[string]any, thinkingEnabled bool, currentFragmentType string) ([]ContentPart, bool, string) { + parts, _, finished, nextType := ParseSSEChunkForContentDetailed(chunk, thinkingEnabled, currentFragmentType) + return parts, finished, nextType +} + +func ParseSSEChunkForContentDetailed(chunk map[string]any, thinkingEnabled bool, currentFragmentType string) ([]ContentPart, []ContentPart, bool, string) { + v, ok := chunk["v"] + if !ok { + return nil, nil, false, currentFragmentType + } + path, _ := chunk["p"].(string) + if shouldSkipPath(path) { + return nil, nil, false, currentFragmentType + } + if isStatusPath(path) { + if s, ok := v.(string); ok { + if strings.EqualFold(strings.TrimSpace(s), "FINISHED") { + return nil, nil, true, currentFragmentType + } + return nil, nil, false, currentFragmentType + } + } + newType := currentFragmentType + parts := make([]ContentPart, 0, 8) + updateTypeFromExplicitPath(path, thinkingEnabled, &newType) + collectDirectFragments(path, chunk, v, &newType, &parts) + updateTypeFromNestedResponse(path, v, &newType) + partType := resolvePartType(path, thinkingEnabled, newType) + finished := appendChunkValueContent(v, partType, &newType, &parts, path) + if finished { + return nil, nil, true, newType + } + var transitioned bool + parts, transitioned = splitThinkingParts(parts) + if transitioned { + newType = "text" + } + detectionThinkingParts := selectThinkingParts(parts) + if !thinkingEnabled { + parts = dropThinkingParts(parts) + } + return parts, detectionThinkingParts, false, newType +} + +func updateTypeFromExplicitPath(path string, thinkingEnabled bool, newType *string) { + if newType == nil { + return + } + switch path { + case "response/content": + *newType = "text" + case "response/thinking_content": + if !thinkingEnabled || *newType != "text" { + *newType = "thinking" + } + } +} + +func selectThinkingParts(parts []ContentPart) []ContentPart { + if len(parts) == 0 { + return nil + } + out := make([]ContentPart, 0, len(parts)) + for _, p := range parts { + if p.Type == "thinking" { + out = append(out, p) + } + } + return out +} + +func collectDirectFragments(path string, chunk map[string]any, v any, newType *string, parts *[]ContentPart) { + if path != "response/fragments" { + return + } + op, _ := chunk["o"].(string) + if !strings.EqualFold(op, "APPEND") { + return + } + frags, ok := v.([]any) + if !ok { + return + } + for _, frag := range frags { + m, ok := frag.(map[string]any) + if !ok { + continue + } + typeName, content, fragType := parseFragmentTypeContent(m) + if typeName == "" { + typeName = fragType + } + switch typeName { + case "THINK", "THINKING": + *newType = "thinking" + appendContentPart(parts, content, "thinking") + case "RESPONSE": + *newType = "text" + appendContentPart(parts, content, "text") + default: + appendContentPart(parts, content, "text") + } + } +} + +func updateTypeFromNestedResponse(path string, v any, newType *string) { + if path != "response" { + return + } + arr, ok := v.([]any) + if !ok { + return + } + for _, it := range arr { + m, ok := it.(map[string]any) + if !ok || m["p"] != "fragments" || m["o"] != "APPEND" { + continue + } + frags, ok := m["v"].([]any) + if !ok { + continue + } + for _, frag := range frags { + fm, ok := frag.(map[string]any) + if !ok { + continue + } + typeName, _, _ := parseFragmentTypeContent(fm) + switch typeName { + case "THINK", "THINKING": + *newType = "thinking" + case "RESPONSE": + *newType = "text" + } + } + } +} + +func resolvePartType(path string, thinkingEnabled bool, newType string) string { + switch { + case path == "response/thinking_content": + if !thinkingEnabled { + return "thinking" + } + if newType == "text" { + return "text" + } + return "thinking" + case path == "response/content": + return "text" + case strings.Contains(path, "response/fragments") && strings.Contains(path, "/content"): + return newType + case path == "": + if newType != "" { + return newType + } + return "text" + default: + return "text" + } +} + +func dropThinkingParts(parts []ContentPart) []ContentPart { + if len(parts) == 0 { + return parts + } + out := parts[:0] + for _, p := range parts { + if p.Type == "thinking" { + continue + } + out = append(out, p) + } + return out +} + +func appendChunkValueContent(v any, partType string, newType *string, parts *[]ContentPart, path string) bool { + switch val := v.(type) { + case string: + if val == "FINISHED" && (path == "" || path == "status") { + return true + } + if isStatusPath(path) { + return false + } + appendContentPart(parts, val, partType) + case []any: + pp, finished := extractContentRecursive(val, partType) + if finished { + return true + } + *parts = append(*parts, pp...) + case map[string]any: + if appendObjectContentByPath(path, val, partType, parts) { + return false + } + appendWrappedFragments(val, partType, newType, parts) + } + return false +} + +func appendObjectContentByPath(path string, val map[string]any, partType string, parts *[]ContentPart) bool { + if path != "response/content" && path != "response/thinking_content" && path != "" { + return false + } + text, _ := val["text"].(string) + if text == "" { + text, _ = val["content"].(string) + } + if text == "" { + return false + } + appendContentPart(parts, text, partType) + return true +} + +func appendWrappedFragments(val map[string]any, partType string, newType *string, parts *[]ContentPart) { + resp := val + if wrapped, ok := val["response"].(map[string]any); ok { + resp = wrapped + } + frags, ok := resp["fragments"].([]any) + if !ok { + return + } + for _, item := range frags { + m, ok := item.(map[string]any) + if !ok { + continue + } + typeName, content, fragType := parseFragmentTypeContent(m) + if typeName == "" { + typeName = fragType + } + switch typeName { + case "THINK", "THINKING": + *newType = "thinking" + appendContentPart(parts, content, "thinking") + case "RESPONSE": + *newType = "text" + appendContentPart(parts, content, "text") + default: + appendContentPart(parts, content, partType) + } + } +} + +func parseFragmentTypeContent(m map[string]any) (string, string, string) { + typeName, _ := m["type"].(string) + content, _ := m["content"].(string) + return strings.ToUpper(typeName), content, strings.ToUpper(typeName) +} + +func appendContentPart(parts *[]ContentPart, content, kind string) { + if content == "" { + return + } + *parts = append(*parts, ContentPart{Text: content, Type: kind}) +} + +var thinkClosePattern = regexp.MustCompile(`(?i)`) +var thinkOpenPattern = regexp.MustCompile(`(?i)<\s*think\s*>`) + +// splitThinkingParts detects inside thinking content and +// auto-transitions everything after it to text. This handles the +// DeepSeek API bug where the upstream SSE keeps sending +// reasoning_content even though the model has finished thinking. +func splitThinkingParts(parts []ContentPart) ([]ContentPart, bool) { + var out []ContentPart + thinkingDone := false + for _, p := range parts { + if thinkingDone && p.Type == "thinking" { + // Already transitioned — treat remaining thinking as text. + cleaned := stripThinkTags(p.Text) + if cleaned != "" { + out = append(out, ContentPart{Text: cleaned, Type: "text"}) + } + continue + } + if p.Type != "thinking" { + cleaned := stripThinkTags(p.Text) + if cleaned != "" { + out = append(out, ContentPart{Text: cleaned, Type: p.Type}) + } + continue + } + loc := thinkClosePattern.FindStringIndex(p.Text) + if loc == nil { + out = append(out, p) + continue + } + // Split at : before is still thinking, after becomes text. + thinkingDone = true + before := p.Text[:loc[0]] + after := p.Text[loc[1]:] + if before != "" { + out = append(out, ContentPart{Text: before, Type: "thinking"}) + } + after = stripThinkTags(after) + if after != "" { + out = append(out, ContentPart{Text: after, Type: "text"}) + } + } + if !thinkingDone { + // Return 'out' instead of 'parts' because text parts might have been cleaned via stripThinkTags + return out, false + } + return out, true +} + +// stripThinkTags removes any remaining or tags from text. +func stripThinkTags(s string) string { + s = thinkClosePattern.ReplaceAllString(s, "") + s = thinkOpenPattern.ReplaceAllString(s, "") + return s +} + +func isStatusPath(path string) bool { + return path == "response/status" || path == "status" +} + +func extractContentRecursive(items []any, defaultType string) ([]ContentPart, bool) { + parts := make([]ContentPart, 0, len(items)) + for _, it := range items { + m, ok := it.(map[string]any) + if !ok { + continue + } + itemPath, _ := m["p"].(string) + itemV, hasV := m["v"] + if !hasV { + continue + } + if isStatusPath(itemPath) { + if s, ok := itemV.(string); ok && strings.EqualFold(strings.TrimSpace(s), "FINISHED") { + return nil, true + } + continue + } + if shouldSkipPath(itemPath) { + continue + } + if content, ok := m["content"].(string); ok && content != "" { + typeName, _ := m["type"].(string) + typeName = strings.ToUpper(typeName) + switch typeName { + case "THINK", "THINKING": + parts = append(parts, ContentPart{Text: content, Type: "thinking"}) + case "RESPONSE": + parts = append(parts, ContentPart{Text: content, Type: "text"}) + default: + parts = append(parts, ContentPart{Text: content, Type: defaultType}) + } + continue + } + partType := defaultType + if strings.Contains(itemPath, "thinking") { + partType = "thinking" + } else if strings.Contains(itemPath, "content") || itemPath == "response" || itemPath == "fragments" { + partType = "text" + } + switch v := itemV.(type) { + case string: + if isStatusPath(itemPath) { + continue + } + if v != "" && v != "FINISHED" { + parts = append(parts, ContentPart{Text: v, Type: partType}) + } + case []any: + for _, inner := range v { + switch x := inner.(type) { + case map[string]any: + ct, _ := x["content"].(string) + if ct == "" { + continue + } + typeName, _ := x["type"].(string) + typeName = strings.ToUpper(typeName) + switch typeName { + case "THINK", "THINKING": + parts = append(parts, ContentPart{Text: ct, Type: "thinking"}) + case "RESPONSE": + parts = append(parts, ContentPart{Text: ct, Type: "text"}) + default: + parts = append(parts, ContentPart{Text: ct, Type: partType}) + } + case string: + if x != "" { + parts = append(parts, ContentPart{Text: x, Type: partType}) + } + } + } + } + } + return parts, false +} + +func IsCitation(text string) bool { + return bytes.HasPrefix([]byte(strings.TrimSpace(text)), []byte("[citation:")) +} + +func hasContentFilterStatus(chunk map[string]any) bool { + if code, _ := chunk["code"].(string); strings.EqualFold(strings.TrimSpace(code), "content_filter") { + return true + } + return hasContentFilterStatusValue(chunk) +} + +func hasContentFilterStatusValue(v any) bool { + switch x := v.(type) { + case []any: + for _, item := range x { + if hasContentFilterStatusValue(item) { + return true + } + } + case map[string]any: + if p, _ := x["p"].(string); strings.Contains(strings.ToLower(p), "status") { + if s, _ := x["v"].(string); strings.EqualFold(strings.TrimSpace(s), "content_filter") { + return true + } + } + if code, _ := x["code"].(string); strings.EqualFold(strings.TrimSpace(code), "content_filter") { + return true + } + for _, vv := range x { + if hasContentFilterStatusValue(vv) { + return true + } + } + } + return false +} diff --git a/internal/sse/parser_edge_test.go b/internal/sse/parser_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f0e7f9ad4e0ffe00bd77fa0e41cae2ed147d8c35 --- /dev/null +++ b/internal/sse/parser_edge_test.go @@ -0,0 +1,628 @@ +package sse + +import "testing" + +// ─── ParseDeepSeekSSELine edge cases ───────────────────────────────── + +func TestParseDeepSeekSSELineEmptyLine(t *testing.T) { + _, _, ok := ParseDeepSeekSSELine([]byte("")) + if ok { + t.Fatal("expected not parsed for empty line") + } +} + +func TestParseDeepSeekSSELineNoDataPrefix(t *testing.T) { + _, _, ok := ParseDeepSeekSSELine([]byte("event: message")) + if ok { + t.Fatal("expected not parsed for non-data line") + } +} + +func TestParseDeepSeekSSELineInvalidJSON(t *testing.T) { + _, _, ok := ParseDeepSeekSSELine([]byte("data: {invalid json")) + if ok { + t.Fatal("expected not parsed for invalid JSON") + } +} + +func TestParseDeepSeekSSELineWhitespaceOnly(t *testing.T) { + _, _, ok := ParseDeepSeekSSELine([]byte(" ")) + if ok { + t.Fatal("expected not parsed for whitespace-only line") + } +} + +func TestParseDeepSeekSSELineDataWithExtraSpaces(t *testing.T) { + chunk, done, ok := ParseDeepSeekSSELine([]byte(`data: {"v":"hello"} `)) + if !ok || done { + t.Fatalf("expected parsed chunk for spaced data line") + } + if chunk["v"] != "hello" { + t.Fatalf("unexpected chunk: %#v", chunk) + } +} + +// ─── shouldSkipPath edge cases ─────────────────────────────────────── + +func TestShouldSkipPathQuasiStatus(t *testing.T) { + if !shouldSkipPath("response/quasi_status") { + t.Fatal("expected skip for quasi_status path") + } +} + +func TestShouldSkipPathPendingFragment(t *testing.T) { + if !shouldSkipPath("response/pending_fragment") { + t.Fatal("expected skip for pending_fragment path") + } +} + +func TestShouldSkipPathConversationMode(t *testing.T) { + if !shouldSkipPath("response/conversation_mode") { + t.Fatal("expected skip for conversation_mode path") + } +} + +func TestShouldSkipPathSearchStatus(t *testing.T) { + if !shouldSkipPath("response/search_status") { + t.Fatal("expected skip for search_status path") + } +} + +func TestShouldSkipPathFragmentStatus(t *testing.T) { + if !shouldSkipPath("response/fragments/-1/status") { + t.Fatal("expected skip for fragment -1 status") + } + if !shouldSkipPath("response/fragments/-2/status") { + t.Fatal("expected skip for fragment -2 status") + } + if !shouldSkipPath("response/fragments/-3/status") { + t.Fatal("expected skip for fragment -3 status") + } + if !shouldSkipPath("response/fragments/-16/status") { + t.Fatal("expected skip for fragment -16 status") + } + if !shouldSkipPath("response/fragments/7/status") { + t.Fatal("expected skip for fragment 7 status") + } + if shouldSkipPath("response/status") { + t.Fatal("expected response/status to be handled by finish logic, not skipped") + } +} + +func TestShouldSkipPathRegularContent(t *testing.T) { + if shouldSkipPath("response/content") { + t.Fatal("expected not skip for content path") + } + if shouldSkipPath("response/thinking_content") { + t.Fatal("expected not skip for thinking_content path") + } +} + +// ─── ParseSSEChunkForContent edge cases ────────────────────────────── + +func TestParseSSEChunkForContentNoVField(t *testing.T) { + parts, finished, nextType := ParseSSEChunkForContent(map[string]any{"p": "response/content"}, false, "text") + if finished { + t.Fatal("expected not finished") + } + if len(parts) != 0 { + t.Fatalf("expected no parts when v is missing, got %#v", parts) + } + if nextType != "text" { + t.Fatalf("expected type preserved, got %q", nextType) + } +} + +func TestParseSSEChunkForContentSkippedPath(t *testing.T) { + parts, finished, nextType := ParseSSEChunkForContent(map[string]any{ + "p": "response/quasi_status", + "v": "some data", + }, false, "text") + if finished || len(parts) > 0 { + t.Fatalf("expected skipped path to produce no output") + } + if nextType != "text" { + t.Fatalf("expected type preserved for skipped path") + } +} + +func TestParseSSEChunkForContentFinishedStatus(t *testing.T) { + parts, finished, _ := ParseSSEChunkForContent(map[string]any{ + "p": "response/status", + "v": "FINISHED", + }, false, "text") + if !finished { + t.Fatal("expected finished on status FINISHED") + } + if len(parts) != 0 { + t.Fatalf("expected no parts on finished, got %d", len(parts)) + } +} + +func TestParseSSEChunkForContentStatusNotFinished(t *testing.T) { + parts, finished, _ := ParseSSEChunkForContent(map[string]any{ + "p": "response/status", + "v": "IN_PROGRESS", + }, false, "text") + if finished { + t.Fatal("expected not finished for non-FINISHED status") + } + if len(parts) != 0 { + t.Fatalf("expected non-finished status to be filtered, got %#v", parts) + } +} + +func TestParseSSEChunkForContentEmptyStringV(t *testing.T) { + parts, finished, _ := ParseSSEChunkForContent(map[string]any{ + "p": "response/content", + "v": "", + }, false, "text") + if finished { + t.Fatal("expected not finished") + } + if len(parts) != 0 { + t.Fatalf("expected no parts for empty string v, got %#v", parts) + } +} + +func TestParseSSEChunkForContentFinishedOnEmptyPath(t *testing.T) { + parts, finished, _ := ParseSSEChunkForContent(map[string]any{ + "p": "", + "v": "FINISHED", + }, false, "text") + if !finished { + t.Fatal("expected finished on empty path with FINISHED value") + } + if len(parts) != 0 { + t.Fatalf("expected no parts on finished") + } +} + +func TestParseSSEChunkForContentFinishedOnStatusPath(t *testing.T) { + _, finished, _ := ParseSSEChunkForContent(map[string]any{ + "p": "status", + "v": "FINISHED", + }, false, "text") + if !finished { + t.Fatal("expected finished on status path with FINISHED value") + } +} + +func TestParseSSEChunkForContentThinkingPathEmptyPath(t *testing.T) { + parts, _, nextType := ParseSSEChunkForContent(map[string]any{ + "v": "some thought", + }, true, "thinking") + if len(parts) != 1 || parts[0].Type != "thinking" { + t.Fatalf("expected thinking part on empty path, got %#v", parts) + } + if nextType != "thinking" { + t.Fatalf("expected nextType thinking, got %q", nextType) + } +} + +func TestParseSSEChunkForContentThinkingEnabledTextType(t *testing.T) { + parts, _, nextType := ParseSSEChunkForContent(map[string]any{ + "v": "text content", + }, true, "text") + if len(parts) != 1 || parts[0].Type != "text" { + t.Fatalf("expected text part when currentType=text, got %#v", parts) + } + if nextType != "text" { + t.Fatalf("expected nextType text, got %q", nextType) + } +} + +// ─── ParseSSEChunkForContent: fragments path with THINK type ───────── + +func TestParseSSEChunkForContentFragmentsAppendThink(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": []any{ + map[string]any{ + "type": "THINK", + "content": "深入思考...", + }, + }, + } + parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "text") + if finished { + t.Fatal("expected not finished") + } + if nextType != "thinking" { + t.Fatalf("expected nextType thinking, got %q", nextType) + } + if len(parts) != 1 || parts[0].Type != "thinking" || parts[0].Text != "深入思考..." { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestParseSSEChunkForContentFragmentsAppendEmptyContent(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": []any{ + map[string]any{ + "type": "RESPONSE", + "content": "", + }, + }, + } + parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "thinking") + if finished { + t.Fatal("expected not finished") + } + if nextType != "text" { + t.Fatalf("expected nextType text, got %q", nextType) + } + if len(parts) != 0 { + t.Fatalf("expected no parts for empty content, got %#v", parts) + } +} + +func TestParseSSEChunkForContentFragmentsAppendDefaultType(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": []any{ + map[string]any{ + "type": "UNKNOWN", + "content": "some text", + }, + }, + } + parts, _, _ := ParseSSEChunkForContent(chunk, true, "text") + if len(parts) != 1 || parts[0].Type != "text" { + t.Fatalf("expected text type for unknown fragment type, got %#v", parts) + } +} + +func TestParseSSEChunkForContentFragmentsAppendNonArray(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": "not an array", + } + parts, finished, _ := ParseSSEChunkForContent(chunk, true, "text") + if finished { + t.Fatal("expected not finished") + } + // "not an array" should be treated as string value at the end + if len(parts) != 1 || parts[0].Text != "not an array" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestParseSSEChunkForContentFragmentsAppendNonMap(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": []any{"string item"}, + } + parts, _, _ := ParseSSEChunkForContent(chunk, false, "text") + // Non-map items in fragment array are skipped; the []any itself is handled later + _ = parts // just checking it doesn't panic +} + +// ─── ParseSSEChunkForContent: response path with nested fragment ───── + +func TestParseSSEChunkForContentResponsePathFragmentsAppend(t *testing.T) { + chunk := map[string]any{ + "p": "response", + "v": []any{ + map[string]any{ + "p": "fragments", + "o": "APPEND", + "v": []any{ + map[string]any{ + "type": "THINKING", + }, + }, + }, + }, + } + _, _, nextType := ParseSSEChunkForContent(chunk, true, "text") + if nextType != "thinking" { + t.Fatalf("expected nextType thinking from response path fragments, got %q", nextType) + } +} + +func TestParseSSEChunkForContentResponsePathResponseFragment(t *testing.T) { + chunk := map[string]any{ + "p": "response", + "v": []any{ + map[string]any{ + "p": "fragments", + "o": "APPEND", + "v": []any{ + map[string]any{ + "type": "RESPONSE", + }, + }, + }, + }, + } + _, _, nextType := ParseSSEChunkForContent(chunk, true, "thinking") + if nextType != "text" { + t.Fatalf("expected nextType text for RESPONSE fragment, got %q", nextType) + } +} + +// ─── ParseSSEChunkForContent: map value with wrapped response ──────── + +func TestParseSSEChunkForContentMapValueWithFragments(t *testing.T) { + chunk := map[string]any{ + "v": map[string]any{ + "response": map[string]any{ + "fragments": []any{ + map[string]any{ + "type": "THINK", + "content": "思考...", + }, + map[string]any{ + "type": "RESPONSE", + "content": "回答...", + }, + }, + }, + }, + } + parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "text") + if finished { + t.Fatal("expected not finished") + } + if nextType != "text" { + t.Fatalf("expected nextType text after RESPONSE, got %q", nextType) + } + if len(parts) != 2 { + t.Fatalf("expected 2 parts, got %d: %#v", len(parts), parts) + } + if parts[0].Type != "thinking" || parts[0].Text != "思考..." { + t.Fatalf("first part mismatch: %#v", parts[0]) + } + if parts[1].Type != "text" || parts[1].Text != "回答..." { + t.Fatalf("second part mismatch: %#v", parts[1]) + } +} + +func TestParseSSEChunkForContentMapValueDirectFragments(t *testing.T) { + chunk := map[string]any{ + "v": map[string]any{ + "fragments": []any{ + map[string]any{ + "type": "RESPONSE", + "content": "直接回答", + }, + }, + }, + } + parts, _, _ := ParseSSEChunkForContent(chunk, false, "text") + if len(parts) != 1 || parts[0].Text != "直接回答" || parts[0].Type != "text" { + t.Fatalf("unexpected parts for direct fragments: %#v", parts) + } +} + +func TestParseSSEChunkForContentMapValueUnknownType(t *testing.T) { + chunk := map[string]any{ + "v": map[string]any{ + "fragments": []any{ + map[string]any{ + "type": "CUSTOM", + "content": "custom content", + }, + }, + }, + } + parts, _, _ := ParseSSEChunkForContent(chunk, false, "text") + if len(parts) != 1 || parts[0].Type != "text" { + t.Fatalf("expected partType fallback for unknown type, got %#v", parts) + } +} + +func TestParseSSEChunkForContentMapValueEmptyFragmentContent(t *testing.T) { + chunk := map[string]any{ + "v": map[string]any{ + "fragments": []any{ + map[string]any{ + "type": "RESPONSE", + "content": "", + }, + }, + }, + } + parts, _, _ := ParseSSEChunkForContent(chunk, false, "text") + if len(parts) != 0 { + t.Fatalf("expected no parts for empty fragment content, got %#v", parts) + } +} + +// ─── ParseSSEChunkForContent: fragments/-1/content path ────────────── + +func TestParseSSEChunkForContentFragmentContentPathInheritsType(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments/-1/content", + "v": "继续思考", + } + parts, _, _ := ParseSSEChunkForContent(chunk, true, "thinking") + if len(parts) != 1 || parts[0].Type != "thinking" { + t.Fatalf("expected inherited thinking type, got %#v", parts) + } +} + +// ─── IsCitation edge cases ─────────────────────────────────────────── + +func TestIsCitationWithLeadingWhitespace(t *testing.T) { + if !IsCitation(" [citation:2] text") { + t.Fatal("expected citation true with leading whitespace") + } +} + +func TestIsCitationEmpty(t *testing.T) { + if IsCitation("") { + t.Fatal("expected citation false for empty string") + } +} + +func TestIsCitationSimilarPrefix(t *testing.T) { + if IsCitation("[cite:1] text") { + t.Fatal("expected citation false for [cite: prefix") + } +} + +// ─── extractContentRecursive edge cases ────────────────────────────── + +func TestExtractContentRecursiveFinishedStatus(t *testing.T) { + items := []any{ + map[string]any{"p": "status", "v": "FINISHED"}, + } + parts, finished := extractContentRecursive(items, "text") + if !finished { + t.Fatal("expected finished on status FINISHED") + } + if len(parts) != 0 { + t.Fatalf("expected no parts, got %#v", parts) + } +} + +func TestExtractContentRecursiveSkipsPath(t *testing.T) { + items := []any{ + map[string]any{"p": "quasi_status", "v": "data"}, + } + parts, finished := extractContentRecursive(items, "text") + if finished { + t.Fatal("expected not finished") + } + if len(parts) != 0 { + t.Fatalf("expected no parts for skipped path, got %#v", parts) + } +} + +func TestExtractContentRecursiveContentField(t *testing.T) { + items := []any{ + map[string]any{"p": "x", "v": "val", "content": "actual content", "type": "RESPONSE"}, + } + parts, _ := extractContentRecursive(items, "text") + if len(parts) != 1 || parts[0].Text != "actual content" || parts[0].Type != "text" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestExtractContentRecursiveContentFieldThinkType(t *testing.T) { + items := []any{ + map[string]any{"p": "x", "v": "val", "content": "think text", "type": "THINK"}, + } + parts, _ := extractContentRecursive(items, "text") + if len(parts) != 1 || parts[0].Type != "thinking" { + t.Fatalf("expected thinking type for THINK content, got %#v", parts) + } +} + +func TestExtractContentRecursiveThinkingPath(t *testing.T) { + items := []any{ + map[string]any{"p": "thinking_content", "v": "deep thought"}, + } + parts, _ := extractContentRecursive(items, "text") + if len(parts) != 1 || parts[0].Type != "thinking" || parts[0].Text != "deep thought" { + t.Fatalf("unexpected parts for thinking path: %#v", parts) + } +} + +func TestExtractContentRecursiveContentPath(t *testing.T) { + items := []any{ + map[string]any{"p": "content", "v": "text content"}, + } + parts, _ := extractContentRecursive(items, "thinking") + if len(parts) != 1 || parts[0].Type != "text" { + t.Fatalf("expected text type for content path, got %#v", parts) + } +} + +func TestExtractContentRecursiveResponsePath(t *testing.T) { + items := []any{ + map[string]any{"p": "response", "v": "text content"}, + } + parts, _ := extractContentRecursive(items, "thinking") + if len(parts) != 1 || parts[0].Type != "text" { + t.Fatalf("expected text type for response path, got %#v", parts) + } +} + +func TestExtractContentRecursiveFragmentsPath(t *testing.T) { + items := []any{ + map[string]any{"p": "fragments", "v": "fragment text"}, + } + parts, _ := extractContentRecursive(items, "thinking") + if len(parts) != 1 || parts[0].Type != "text" { + t.Fatalf("expected text type for fragments path, got %#v", parts) + } +} + +func TestExtractContentRecursiveNestedArrayWithTypes(t *testing.T) { + items := []any{ + map[string]any{ + "p": "fragments", + "v": []any{ + map[string]any{"content": "thought", "type": "THINKING"}, + map[string]any{"content": "answer", "type": "RESPONSE"}, + "raw string", + }, + }, + } + parts, _ := extractContentRecursive(items, "text") + if len(parts) != 3 { + t.Fatalf("expected 3 parts, got %d: %#v", len(parts), parts) + } + if parts[0].Type != "thinking" || parts[0].Text != "thought" { + t.Fatalf("first part mismatch: %#v", parts[0]) + } + if parts[1].Type != "text" || parts[1].Text != "answer" { + t.Fatalf("second part mismatch: %#v", parts[1]) + } + if parts[2].Type != "text" || parts[2].Text != "raw string" { + t.Fatalf("third part mismatch: %#v", parts[2]) + } +} + +func TestExtractContentRecursiveEmptyContentSkipped(t *testing.T) { + items := []any{ + map[string]any{ + "p": "fragments", + "v": []any{ + map[string]any{"content": "", "type": "RESPONSE"}, + }, + }, + } + parts, _ := extractContentRecursive(items, "text") + if len(parts) != 0 { + t.Fatalf("expected no parts for empty nested content, got %#v", parts) + } +} + +func TestExtractContentRecursiveFinishedString(t *testing.T) { + items := []any{ + map[string]any{"p": "content", "v": "FINISHED"}, + } + parts, _ := extractContentRecursive(items, "text") + // "FINISHED" string value on non-status path should be skipped + if len(parts) != 0 { + t.Fatalf("expected FINISHED string to be skipped, got %#v", parts) + } +} + +func TestExtractContentRecursiveNoVField(t *testing.T) { + items := []any{ + map[string]any{"p": "content"}, + } + parts, _ := extractContentRecursive(items, "text") + if len(parts) != 0 { + t.Fatalf("expected no parts for missing v field, got %#v", parts) + } +} + +func TestExtractContentRecursiveNonMapItem(t *testing.T) { + items := []any{"just a string", 42} + parts, _ := extractContentRecursive(items, "text") + if len(parts) != 0 { + t.Fatalf("expected no parts for non-map items, got %#v", parts) + } +} diff --git a/internal/sse/parser_test.go b/internal/sse/parser_test.go new file mode 100644 index 0000000000000000000000000000000000000000..16c02b957ef370d2beacf36059515ccb584a2662 --- /dev/null +++ b/internal/sse/parser_test.go @@ -0,0 +1,271 @@ +package sse + +import "testing" + +func TestParseDeepSeekSSELine(t *testing.T) { + chunk, done, ok := ParseDeepSeekSSELine([]byte(`data: {"v":"你好"}`)) + if !ok || done { + t.Fatalf("expected parsed chunk") + } + if chunk["v"] != "你好" { + t.Fatalf("unexpected chunk: %#v", chunk) + } +} + +func TestParseDeepSeekSSELineDone(t *testing.T) { + _, done, ok := ParseDeepSeekSSELine([]byte(`data: [DONE]`)) + if !ok || !done { + t.Fatalf("expected done signal") + } +} + +func TestParseSSEChunkForContentSimple(t *testing.T) { + parts, finished, _ := ParseSSEChunkForContent(map[string]any{"v": "hello"}, false, "text") + if finished { + t.Fatal("expected unfinished") + } + if len(parts) != 1 || parts[0].Text != "hello" || parts[0].Type != "text" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestParseSSEChunkForContentThinking(t *testing.T) { + parts, finished, _ := ParseSSEChunkForContent(map[string]any{"p": "response/thinking_content", "v": "think"}, true, "thinking") + if finished { + t.Fatal("expected unfinished") + } + if len(parts) != 1 || parts[0].Type != "thinking" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestIsCitation(t *testing.T) { + if !IsCitation("[citation:1] abc") { + t.Fatal("expected citation true") + } + if IsCitation("normal text") { + t.Fatal("expected citation false") + } +} + +func TestParseSSEChunkForContentFragmentsAppendSwitchToResponse(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": []any{ + map[string]any{ + "type": "RESPONSE", + "content": "你好", + }, + }, + } + parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "thinking") + if finished { + t.Fatal("expected unfinished") + } + if nextType != "text" { + t.Fatalf("expected next type text, got %q", nextType) + } + if len(parts) != 1 || parts[0].Type != "text" || parts[0].Text != "你好" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestParseSSEChunkForContentAfterAppendUsesUpdatedType(t *testing.T) { + chunk := map[string]any{ + "p": "response/fragments/-1/content", + "v": "!", + } + parts, finished, nextType := ParseSSEChunkForContent(chunk, true, "text") + if finished { + t.Fatal("expected unfinished") + } + if nextType != "text" { + t.Fatalf("expected next type text, got %q", nextType) + } + if len(parts) != 1 || parts[0].Type != "text" || parts[0].Text != "!" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestParseSSEChunkForContentThinkingDisabledKeepsHiddenFragmentState(t *testing.T) { + chunk1 := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": []any{ + map[string]any{"type": "THINK", "content": "我们"}, + }, + } + parts1, finished1, nextType1 := ParseSSEChunkForContent(chunk1, false, "text") + if finished1 { + t.Fatal("expected first chunk unfinished") + } + if nextType1 != "thinking" { + t.Fatalf("expected hidden THINK fragment to keep next type thinking, got %q", nextType1) + } + if len(parts1) != 0 { + t.Fatalf("expected hidden thinking to be dropped, got %#v", parts1) + } + + chunk2 := map[string]any{ + "p": "response/fragments/-1/content", + "v": "被", + } + parts2, finished2, nextType2 := ParseSSEChunkForContent(chunk2, false, nextType1) + if finished2 { + t.Fatal("expected second chunk unfinished") + } + if nextType2 != "thinking" { + t.Fatalf("expected hidden continuation to keep next type thinking, got %q", nextType2) + } + if len(parts2) != 0 { + t.Fatalf("expected hidden continuation to be dropped, got %#v", parts2) + } + + chunk3 := map[string]any{"v": "要求"} + parts3, finished3, nextType3 := ParseSSEChunkForContent(chunk3, false, nextType2) + if finished3 { + t.Fatal("expected third chunk unfinished") + } + if nextType3 != "thinking" { + t.Fatalf("expected pathless hidden continuation to keep next type thinking, got %q", nextType3) + } + if len(parts3) != 0 { + t.Fatalf("expected pathless hidden continuation to be dropped, got %#v", parts3) + } + + chunk4 := map[string]any{ + "p": "response/fragments", + "o": "APPEND", + "v": []any{ + map[string]any{"type": "RESPONSE", "content": "答"}, + }, + } + parts4, finished4, nextType4 := ParseSSEChunkForContent(chunk4, false, nextType3) + if finished4 { + t.Fatal("expected fourth chunk unfinished") + } + if nextType4 != "text" { + t.Fatalf("expected RESPONSE fragment to switch next type text, got %q", nextType4) + } + if len(parts4) != 1 || parts4[0].Type != "text" || parts4[0].Text != "答" { + t.Fatalf("expected visible response text, got %#v", parts4) + } +} + +func TestParseSSEChunkForContentAutoTransitionsThinkClose(t *testing.T) { + chunk := map[string]any{ + "p": "response/thinking_content", + "v": "deep thoughtsactual answer", + } + parts, _, _ := ParseSSEChunkForContent(chunk, true, "thinking") + if len(parts) != 2 { + t.Fatalf("expected 2 parts from split, got %d: %#v", len(parts), parts) + } + if parts[0].Type != "thinking" || parts[0].Text != "deep thoughts" { + t.Fatalf("first part should be thinking: %#v", parts[0]) + } + if parts[1].Type != "text" || parts[1].Text != "actual answer" { + t.Fatalf("second part should be text: %#v", parts[1]) + } +} + +func TestParseSSEChunkForContentStripsLeakedThinkTags(t *testing.T) { + chunk := map[string]any{ + "p": "response/thinking_content", + "v": "more thoughts answer", + } + parts, _, _ := ParseSSEChunkForContent(chunk, true, "thinking") + if len(parts) != 2 { + t.Fatalf("expected 2 parts, got %d: %#v", len(parts), parts) + } + if parts[0].Type != "thinking" || parts[0].Text != "more thoughts" { + // note: the open tag is before the split, so it remains in the thinking part. + // that's fine, the output sanitization handles the final string. + t.Fatalf("first part mismatch: %#v", parts[0]) + } + if parts[1].Type != "text" || parts[1].Text != " answer" { + t.Fatalf("second part mismatch: %#v", parts[1]) + } +} + +func TestParseSSEChunkForContentAutoTransitionsState(t *testing.T) { + chunk1 := map[string]any{ + "p": "response/thinking_content", + "v": "end of thoughtstart of text", + } + parts1, _, nextType1 := ParseSSEChunkForContent(chunk1, true, "thinking") + if len(parts1) != 2 || parts1[1].Type != "text" { + t.Fatalf("expected split parts, got %#v", parts1) + } + if nextType1 != "text" { + t.Fatalf("expected nextType to transition to text, got %q", nextType1) + } + + chunk2 := map[string]any{ + "p": "response/thinking_content", + "v": "more actual text sent to thinking path", + } + parts2, _, nextType2 := ParseSSEChunkForContent(chunk2, true, nextType1) + if len(parts2) != 1 || parts2[0].Type != "text" { + t.Fatalf("expected subsequent parts to be text, got %#v", parts2) + } + if nextType2 != "text" { + t.Fatalf("expected nextType2 to remain text, got %q", nextType2) + } +} + +func TestParseSSEChunkForContentStripsLeakedThinkTagsFromText(t *testing.T) { + chunk := map[string]any{ + "p": "response/content", // This makes the part type "text" + "v": "normal text leaked end", + } + parts, _, _ := ParseSSEChunkForContent(chunk, true, "text") + if len(parts) != 1 { + t.Fatalf("expected 1 part, got %d: %#v", len(parts), parts) + } + if parts[0].Type != "text" || parts[0].Text != "normal text leaked end" { + t.Fatalf("expected leaked think tag to be stripped, got %#v", parts[0]) + } +} + +func TestParseSSEChunkForContentResponseContentObjectShape(t *testing.T) { + chunk := map[string]any{ + "p": "response/content", + "v": map[string]any{"text": "对象内容"}, + } + parts, finished, _ := ParseSSEChunkForContent(chunk, false, "text") + if finished { + t.Fatal("expected unfinished") + } + if len(parts) != 1 || parts[0].Text != "对象内容" || parts[0].Type != "text" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestParseSSEChunkForThinkingContentObjectShape(t *testing.T) { + chunk := map[string]any{ + "p": "response/thinking_content", + "v": map[string]any{"content": "对象思考"}, + } + parts, finished, _ := ParseSSEChunkForContent(chunk, true, "thinking") + if finished { + t.Fatal("expected unfinished") + } + if len(parts) != 1 || parts[0].Text != "对象思考" || parts[0].Type != "thinking" { + t.Fatalf("unexpected parts: %#v", parts) + } +} + +func TestParseSSEChunkForContentObjectShapeWithoutPath(t *testing.T) { + chunk := map[string]any{ + "v": map[string]any{"text": "无路径对象内容"}, + } + parts, finished, _ := ParseSSEChunkForContent(chunk, false, "text") + if finished { + t.Fatal("expected unfinished") + } + if len(parts) != 1 || parts[0].Text != "无路径对象内容" || parts[0].Type != "text" { + t.Fatalf("unexpected parts: %#v", parts) + } +} diff --git a/internal/sse/stream.go b/internal/sse/stream.go new file mode 100644 index 0000000000000000000000000000000000000000..977ed5a5d55740b3a0523e498ecfc43e31bc318c --- /dev/null +++ b/internal/sse/stream.go @@ -0,0 +1,362 @@ +package sse + +import ( + "bufio" + "context" + "io" + "strings" + "time" + "unicode/utf8" +) + +const ( + parsedLineBufferSize = 128 + lineReaderBufferSize = 64 * 1024 +) + +type AccumulateConfig struct { + Enabled bool + MinChars int + MaxWait time.Duration + FlushOnFinish bool + WordBoundary bool + FlushOnNewline bool +} + +var productionAccumulate = AccumulateConfig{ + Enabled: true, + MinChars: 16, + MaxWait: 10 * time.Millisecond, + FlushOnFinish: true, + WordBoundary: false, + FlushOnNewline: true, +} + +func StartParsedLinePump(ctx context.Context, body io.Reader, thinkingEnabled bool, initialType string) (<-chan LineResult, <-chan error) { + return startParsedLinePumpWithConfig(ctx, body, thinkingEnabled, initialType, productionAccumulate) +} + +func startParsedLinePumpWithConfig(ctx context.Context, body io.Reader, thinkingEnabled bool, initialType string, cfg AccumulateConfig) (<-chan LineResult, <-chan error) { + out := make(chan LineResult, parsedLineBufferSize) + done := make(chan error, 1) + + go func() { + defer close(out) + + reader := bufio.NewReaderSize(body, lineReaderBufferSize) + currentType := initialType + + var pumpErr error + + var textBuffer strings.Builder + var thinkingBuffer strings.Builder + var toolDetectionThinkingBuffer strings.Builder + var textPendingType string + var thinkingPendingType string + var anyFlushed bool + var pendingResponseMessageID int + + scanCh := make(chan []byte, parsedLineBufferSize) + scanDone := make(chan error, 1) + + go func() { + for { + line, err := reader.ReadBytes('\n') + if len(line) > 0 { + copied := append([]byte(nil), line...) + select { + case scanCh <- copied: + case <-ctx.Done(): + close(scanCh) + scanDone <- ctx.Err() + return + } + } + if err != nil { + close(scanCh) + if err == io.EOF { + err = nil + } + scanDone <- err + return + } + } + }() + + maxWaitTimer := time.NewTimer(0) + if !maxWaitTimer.Stop() { + <-maxWaitTimer.C + } + maxWaitActive := false + + resetMaxWait := func() { + if maxWaitActive { + if !maxWaitTimer.Stop() { + select { + case <-maxWaitTimer.C: + default: + } + } + } + maxWaitTimer.Reset(cfg.MaxWait) + maxWaitActive = true + } + + stopMaxWait := func() { + if maxWaitActive { + if !maxWaitTimer.Stop() { + select { + case <-maxWaitTimer.C: + default: + } + } + maxWaitActive = false + } + } + + defer stopMaxWait() + + shouldFlushImmediate := func(text string) bool { + if cfg.FlushOnNewline && strings.ContainsAny(text, "\n\r") { + return true + } + return false + } + + hasBufferedData := func() bool { + return textBuffer.Len() > 0 || thinkingBuffer.Len() > 0 || toolDetectionThinkingBuffer.Len() > 0 + } + + flushBuffer := func(force bool) { + if !cfg.Enabled { + return + } + + textChars := utf8.RuneCountInString(textBuffer.String()) + thinkingChars := utf8.RuneCountInString(thinkingBuffer.String()) + + shouldFlush := force || + !anyFlushed || + textChars >= cfg.MinChars || + (thinkingChars > 0 && textChars >= 50) + + if !shouldFlush { + return + } + + anyFlushed = true + + var parts []ContentPart + + if thinkingChars > 0 { + parts = append(parts, ContentPart{Text: thinkingBuffer.String(), Type: thinkingPendingType}) + thinkingBuffer.Reset() + } + + if textChars > 0 { + parts = append(parts, ContentPart{Text: textBuffer.String(), Type: textPendingType}) + textBuffer.Reset() + } + + if len(parts) > 0 || toolDetectionThinkingBuffer.Len() > 0 { + var detectionParts []ContentPart + if toolDetectionThinkingBuffer.Len() > 0 { + detectionParts = append(detectionParts, ContentPart{Text: toolDetectionThinkingBuffer.String(), Type: "thinking"}) + toolDetectionThinkingBuffer.Reset() + } + + result := LineResult{ + Parsed: true, + Stop: false, + Parts: parts, + ToolDetectionThinkingParts: detectionParts, + NextType: currentType, + ResponseMessageID: pendingResponseMessageID, + } + pendingResponseMessageID = 0 + select { + case out <- result: + case <-ctx.Done(): + pumpErr = ctx.Err() + return + } + } + + if hasBufferedData() { + resetMaxWait() + } else { + stopMaxWait() + } + } + + processLine := func(result LineResult) bool { + currentType = result.NextType + if result.ResponseMessageID > 0 { + pendingResponseMessageID = result.ResponseMessageID + } + + if result.Stop { + if cfg.Enabled && cfg.FlushOnFinish { + for _, p := range result.ToolDetectionThinkingParts { + toolDetectionThinkingBuffer.WriteString(p.Text) + } + if textBuffer.Len() > 0 || len(result.Parts) > 0 || toolDetectionThinkingBuffer.Len() > 0 { + for _, p := range result.Parts { + if p.Type == "thinking" { + thinkingBuffer.WriteString(p.Text) + thinkingPendingType = "thinking" + } else { + textBuffer.WriteString(p.Text) + textPendingType = p.Type + } + } + flushBuffer(true) + } + } else if !cfg.Enabled { + var filteredParts []ContentPart + for _, p := range result.Parts { + if p.Type == "thinking" && !thinkingEnabled { + continue + } + filteredParts = append(filteredParts, p) + } + result.Parts = filteredParts + } + if result.ErrorMessage != "" || result.ContentFilter { + select { + case out <- result: + case <-ctx.Done(): + pumpErr = ctx.Err() + return false + } + } else { + stopResult := LineResult{ + Parsed: true, + Stop: true, + NextType: currentType, + ResponseMessageID: pendingResponseMessageID, + } + pendingResponseMessageID = 0 + select { + case out <- stopResult: + case <-ctx.Done(): + pumpErr = ctx.Err() + return false + } + } + return true + } + + if !result.Parsed { + return true + } + + if cfg.Enabled { + for _, p := range result.ToolDetectionThinkingParts { + toolDetectionThinkingBuffer.WriteString(p.Text) + } + for _, p := range result.Parts { + if p.Type == "thinking" { + if textBuffer.Len() > 0 { + flushBuffer(true) + } + thinkingBuffer.WriteString(p.Text) + thinkingPendingType = "thinking" + } else { + textBuffer.WriteString(p.Text) + textPendingType = p.Type + if shouldFlushImmediate(p.Text) { + flushBuffer(true) + } + } + } + if utf8.RuneCountInString(textBuffer.String()) >= cfg.MinChars { + flushBuffer(false) + } + if hasBufferedData() && !maxWaitActive { + resetMaxWait() + } + } else { + var parts []ContentPart + for _, p := range result.Parts { + if p.Type == "thinking" && !thinkingEnabled { + continue + } + parts = append(parts, p) + } + if len(parts) > 0 || len(result.ToolDetectionThinkingParts) > 0 { + filteredResult := LineResult{ + Parsed: true, + Stop: false, + Parts: parts, + ToolDetectionThinkingParts: result.ToolDetectionThinkingParts, + NextType: currentType, + } + select { + case out <- filteredResult: + case <-ctx.Done(): + pumpErr = ctx.Err() + return false + } + } + } + return true + } + + for { + select { + case <-ctx.Done(): + pumpErr = ctx.Err() + goto done + + case line, ok := <-scanCh: + if !ok { + scanCh = nil + err := <-scanDone + if err != nil { + pumpErr = err + } + goto done + } + result := ParseDeepSeekContentLine(line, thinkingEnabled, currentType) + if !processLine(result) { + goto done + } + + case err, ok := <-scanDone: + if !ok || scanCh == nil { + goto done + } + if err != nil { + pumpErr = err + } + for line := range scanCh { + result := ParseDeepSeekContentLine(line, thinkingEnabled, currentType) + if !processLine(result) { + goto done + } + } + goto done + + case <-maxWaitTimer.C: + maxWaitActive = false + if hasBufferedData() { + flushBuffer(true) + } + } + } + + done: + stopMaxWait() + if cfg.Enabled { + flushBuffer(true) + } + + if pumpErr != nil { + done <- pumpErr + } else { + done <- nil + } + }() + return out, done +} diff --git a/internal/sse/stream_edge_test.go b/internal/sse/stream_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..785a59ab7bb8f8180a8252b0153ea7d0a733ba8b --- /dev/null +++ b/internal/sse/stream_edge_test.go @@ -0,0 +1,258 @@ +package sse + +import ( + "context" + "io" + "strings" + "testing" + "time" +) + +func TestStartParsedLinePumpEmptyBody(t *testing.T) { + body := strings.NewReader("") + results, done := StartParsedLinePump(context.Background(), body, false, "text") + + collected := make([]LineResult, 0) + for r := range results { + collected = append(collected, r) + } + if err := <-done; err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(collected) != 0 { + t.Fatalf("expected no results for empty body, got %d", len(collected)) + } +} + +func TestStartParsedLinePumpMultipleLines(t *testing.T) { + body := strings.NewReader( + "data: {\"p\":\"response/thinking_content\",\"v\":\"think\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\"text\"}\n" + + "data: [DONE]\n", + ) + results, done := StartParsedLinePump(context.Background(), body, true, "thinking") + + collected := make([]LineResult, 0) + for r := range results { + collected = append(collected, r) + } + if err := <-done; err != nil { + t.Fatalf("unexpected error: %v", err) + } + if len(collected) < 2 { + t.Fatalf("expected at least 2 results, got %d", len(collected)) + } + hasThinking := false + for _, r := range collected { + for _, p := range r.Parts { + if p.Type == "thinking" { + hasThinking = true + } + } + } + if !hasThinking { + t.Fatal("expected thinking part in results") + } + last := collected[len(collected)-1] + if !last.Stop { + t.Fatal("expected last result to be stop") + } +} + +func TestStartParsedLinePumpTypeTracking(t *testing.T) { + body := strings.NewReader( + "data: {\"p\":\"response/fragments\",\"o\":\"APPEND\",\"v\":[{\"type\":\"THINK\",\"content\":\"思\"}]}\n" + + "data: {\"p\":\"response/fragments/-1/content\",\"v\":\"考\"}\n" + + "data: {\"p\":\"response/fragments\",\"o\":\"APPEND\",\"v\":[{\"type\":\"RESPONSE\",\"content\":\"答\"}]}\n" + + "data: {\"p\":\"response/fragments/-1/content\",\"v\":\"案\"}\n" + + "data: [DONE]\n", + ) + results, done := StartParsedLinePump(context.Background(), body, true, "text") + + types := make([]string, 0) + for r := range results { + for _, p := range r.Parts { + types = append(types, p.Type) + } + } + <-done + + if len(types) == 0 { + t.Fatal("expected some parts, got none") + } + hasThinking := false + hasText := false + for _, tp := range types { + if tp == "thinking" { + hasThinking = true + } + if tp == "text" { + hasText = true + } + } + if !hasThinking { + t.Fatalf("expected thinking type in results, got %v", types) + } + if !hasText { + t.Fatalf("expected text type in results, got %v", types) + } +} + +func TestStartParsedLinePumpContextCancellation(t *testing.T) { + pr, pw := io.Pipe() + + ctx, cancel := context.WithCancel(context.Background()) + results, done := StartParsedLinePump(ctx, pr, false, "text") + + go func() { + _, _ = io.WriteString(pw, "data: {\"p\":\"response/content\",\"v\":\"hello\"}\n") + time.Sleep(50 * time.Millisecond) + _ = pw.Close() + }() + + r := <-results + if !r.Parsed || len(r.Parts) == 0 { + t.Fatalf("expected first parsed result, got %#v", r) + } + + cancel() + + for range results { + } + + err := <-done + if err != nil && err != context.Canceled { + t.Fatalf("expected context.Canceled or nil error, got %v", err) + } +} + +func TestStartParsedLinePumpOnlyDONE(t *testing.T) { + body := strings.NewReader("data: [DONE]\n") + results, done := StartParsedLinePump(context.Background(), body, false, "text") + + collected := make([]LineResult, 0) + for r := range results { + collected = append(collected, r) + } + <-done + + if len(collected) != 1 { + t.Fatalf("expected 1 result, got %d", len(collected)) + } + if !collected[0].Stop { + t.Fatal("expected stop on [DONE]") + } +} + +func TestStartParsedLinePumpNonSSELines(t *testing.T) { + body := strings.NewReader( + "event: update\n" + + ": comment line\n" + + "data: {\"p\":\"response/content\",\"v\":\"valid\"}\n" + + "data: [DONE]\n", + ) + results, done := StartParsedLinePump(context.Background(), body, false, "text") + + var validCount int + for r := range results { + if r.Parsed && len(r.Parts) > 0 { + validCount++ + } + } + <-done + + if validCount != 1 { + t.Fatalf("expected 1 valid result, got %d", validCount) + } +} + +func TestStartParsedLinePumpThinkingDisabled(t *testing.T) { + body := strings.NewReader( + "data: {\"p\":\"response/fragments\",\"o\":\"APPEND\",\"v\":[{\"type\":\"THINK\",\"content\":\"思\"}]}\n" + + "data: {\"p\":\"response/fragments/-1/content\",\"v\":\"考\"}\n" + + "data: {\"v\":\"隐藏\"}\n" + + "data: {\"p\":\"response/fragments\",\"o\":\"APPEND\",\"v\":[{\"type\":\"RESPONSE\",\"content\":\"答\"}]}\n" + + "data: {\"p\":\"response/content\",\"v\":\"response\"}\n" + + "data: [DONE]\n", + ) + results, done := StartParsedLinePump(context.Background(), body, false, "text") + + var parts []ContentPart + for r := range results { + parts = append(parts, r.Parts...) + } + <-done + + got := strings.Builder{} + for _, p := range parts { + if p.Type != "text" { + t.Fatalf("expected only text parts with thinking disabled, got %#v", parts) + } + got.WriteString(p.Text) + } + if got.String() != "答response" { + t.Fatalf("expected hidden thinking to be dropped, got %q from %#v", got.String(), parts) + } +} + +func TestStartParsedLinePumpAccumulatesSmallChunks(t *testing.T) { + body := strings.NewReader( + "data: {\"p\":\"response/content\",\"v\":\"h\"}\n" + + "data: {\"p\":\"response/content\",\"v\":\"i\"}\n" + + "data: [DONE]\n", + ) + + results, done := StartParsedLinePump(context.Background(), body, false, "text") + + collected := make([]LineResult, 0) + for r := range results { + collected = append(collected, r) + } + if err := <-done; err != nil { + t.Fatalf("unexpected error: %v", err) + } + + last := collected[len(collected)-1] + if !last.Stop { + t.Fatal("expected last result to stop") + } + + allText := strings.Builder{} + for _, r := range collected { + for _, p := range r.Parts { + allText.WriteString(p.Text) + } + } + if allText.String() != "hi" { + t.Fatalf("expected accumulated text 'hi', got %q", allText.String()) + } +} + +func TestStartParsedLinePumpFirstFlushImmediate(t *testing.T) { + body := strings.NewReader( + "data: {\"p\":\"response/content\",\"v\":\"Hi\"}\n" + + "data: [DONE]\n", + ) + + results, done := StartParsedLinePump(context.Background(), body, false, "text") + + collected := make([]LineResult, 0) + for r := range results { + collected = append(collected, r) + } + if err := <-done; err != nil { + t.Fatalf("unexpected error: %v", err) + } + + hasContent := false + for _, r := range collected { + for _, p := range r.Parts { + if p.Text == "Hi" { + hasContent = true + } + } + } + if !hasContent { + t.Fatal("expected 'Hi' content in results") + } +} diff --git a/internal/sse/stream_test.go b/internal/sse/stream_test.go new file mode 100644 index 0000000000000000000000000000000000000000..ccad248f4d5789110c177f7135493d29946f6e15 --- /dev/null +++ b/internal/sse/stream_test.go @@ -0,0 +1,68 @@ +package sse + +import ( + "context" + "encoding/json" + "strings" + "testing" +) + +func makeLargeContentSSEBody(t *testing.T, payload string) string { + t.Helper() + line, err := json.Marshal(map[string]any{ + "p": "response/content", + "v": payload, + }) + if err != nil { + t.Fatalf("marshal SSE line failed: %v", err) + } + return "data: " + string(line) + "\n" + "data: [DONE]\n" +} + +func TestStartParsedLinePumpParsesAndStops(t *testing.T) { + body := strings.NewReader("data: {\"p\":\"response/content\",\"v\":\"hi\"}\n\ndata: [DONE]\n") + results, done := StartParsedLinePump(context.Background(), body, false, "text") + + collected := make([]LineResult, 0, 2) + for r := range results { + collected = append(collected, r) + } + if err := <-done; err != nil { + t.Fatalf("unexpected scanner error: %v", err) + } + if len(collected) < 2 { + t.Fatalf("expected at least 2 parsed results, got %d", len(collected)) + } + if !collected[0].Parsed || len(collected[0].Parts) == 0 { + t.Fatalf("expected first line to contain parsed content") + } + last := collected[len(collected)-1] + if !last.Parsed || !last.Stop { + t.Fatalf("expected last line to stop stream, got parsed=%v stop=%v", last.Parsed, last.Stop) + } +} + +func TestStartParsedLinePumpHandlesLongSingleSSELine(t *testing.T) { + payload := strings.Repeat("x", 5*1024*1024+4096) + results, done := StartParsedLinePump(context.Background(), strings.NewReader(makeLargeContentSSEBody(t, payload)), false, "text") + + var got strings.Builder + var sawDone bool + for r := range results { + for _, p := range r.Parts { + got.WriteString(p.Text) + } + if r.Stop { + sawDone = true + } + } + if err := <-done; err != nil { + t.Fatalf("unexpected long-line read error: %v", err) + } + if got.String() != payload { + t.Fatalf("long SSE line payload mismatch: got len=%d want len=%d", got.Len(), len(payload)) + } + if !sawDone { + t.Fatal("expected DONE after long SSE line") + } +} diff --git a/internal/stream/engine.go b/internal/stream/engine.go new file mode 100644 index 0000000000000000000000000000000000000000..162394604433faf5b1dee01818e78ad4e519571b --- /dev/null +++ b/internal/stream/engine.go @@ -0,0 +1,146 @@ +package stream + +import ( + "context" + "io" + "time" + + "ds2api/internal/sse" +) + +type StopReason string + +const ( + StopReasonNone StopReason = "" + StopReasonContextCancelled StopReason = "context_cancelled" + StopReasonNoContentTimeout StopReason = "no_content_timeout" + StopReasonIdleTimeout StopReason = "idle_timeout" + StopReasonUpstreamCompleted StopReason = "upstream_completed" + StopReasonHandlerRequested StopReason = "handler_requested" +) + +type ConsumeConfig struct { + Context context.Context + Body io.Reader + ThinkingEnabled bool + InitialType string + KeepAliveInterval time.Duration + IdleTimeout time.Duration + MaxKeepAliveNoInput int +} + +type ParsedDecision struct { + Stop bool + StopReason StopReason + ContentSeen bool +} + +type ConsumeHooks struct { + OnParsed func(parsed sse.LineResult) ParsedDecision + OnKeepAlive func() + OnFinalize func(reason StopReason, scannerErr error) + OnContextDone func() +} + +func ConsumeSSE(cfg ConsumeConfig, hooks ConsumeHooks) { + if cfg.Context == nil { + cfg.Context = context.Background() + } + initialType := cfg.InitialType + if initialType == "" { + if cfg.ThinkingEnabled { + initialType = "thinking" + } else { + initialType = "text" + } + } + parsedLines, done := sse.StartParsedLinePump(cfg.Context, cfg.Body, cfg.ThinkingEnabled, initialType) + + var ticker *time.Ticker + if cfg.KeepAliveInterval > 0 { + ticker = time.NewTicker(cfg.KeepAliveInterval) + defer ticker.Stop() + } + + hasContent := false + lastContent := time.Now() + keepaliveCount := 0 + + finalize := func(reason StopReason, scannerErr error) { + if hooks.OnFinalize != nil { + hooks.OnFinalize(reason, scannerErr) + } + } + contextDone := func() bool { + if cfg.Context.Err() == nil { + return false + } + if hooks.OnContextDone != nil { + hooks.OnContextDone() + } + return true + } + + for { + if contextDone() { + return + } + select { + case <-cfg.Context.Done(): + if contextDone() { + return + } + return + case <-tickCh(ticker): + if contextDone() { + return + } + if !hasContent { + keepaliveCount++ + if cfg.MaxKeepAliveNoInput > 0 && keepaliveCount >= cfg.MaxKeepAliveNoInput { + finalize(StopReasonNoContentTimeout, nil) + return + } + } + if hasContent && cfg.IdleTimeout > 0 && time.Since(lastContent) > cfg.IdleTimeout { + finalize(StopReasonIdleTimeout, nil) + return + } + if hooks.OnKeepAlive != nil { + hooks.OnKeepAlive() + } + case parsed, ok := <-parsedLines: + if contextDone() { + return + } + if !ok { + finalize(StopReasonUpstreamCompleted, <-done) + return + } + if hooks.OnParsed == nil { + continue + } + decision := hooks.OnParsed(parsed) + if decision.ContentSeen { + hasContent = true + lastContent = time.Now() + keepaliveCount = 0 + } + if decision.Stop { + reason := decision.StopReason + if reason == StopReasonNone { + reason = StopReasonHandlerRequested + } + finalize(reason, nil) + return + } + } + } +} + +func tickCh(ticker *time.Ticker) <-chan time.Time { + if ticker == nil { + return nil + } + return ticker.C +} diff --git a/internal/stream/engine_test.go b/internal/stream/engine_test.go new file mode 100644 index 0000000000000000000000000000000000000000..b23474bbca487fb9d3868e6b735108119936119a --- /dev/null +++ b/internal/stream/engine_test.go @@ -0,0 +1,47 @@ +package stream + +import ( + "context" + "strings" + "testing" + + "ds2api/internal/sse" +) + +func TestConsumeSSEPrefersContextCancellationOverReadyParsedLines(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + var finalized bool + var contextDone bool + var parsedCalled bool + + ConsumeSSE(ConsumeConfig{ + Context: ctx, + Body: strings.NewReader("data: {\"p\":\"response/content\",\"v\":\"hello\"}\n\ndata: [DONE]\n"), + ThinkingEnabled: false, + InitialType: "text", + KeepAliveInterval: 0, + }, ConsumeHooks{ + OnParsed: func(_ sse.LineResult) ParsedDecision { + parsedCalled = true + return ParsedDecision{} + }, + OnFinalize: func(_ StopReason, _ error) { + finalized = true + }, + OnContextDone: func() { + contextDone = true + }, + }) + + if !contextDone { + t.Fatal("expected OnContextDone to run for an already-cancelled context") + } + if finalized { + t.Fatal("expected OnFinalize not to run after context cancellation wins") + } + if parsedCalled { + t.Fatal("expected parsed lines not to be processed after context cancellation wins") + } +} diff --git a/internal/testsuite/edge_cases.go b/internal/testsuite/edge_cases.go new file mode 100644 index 0000000000000000000000000000000000000000..a2d5d1969623a2a57a60628c0de295b2c70a2fcf --- /dev/null +++ b/internal/testsuite/edge_cases.go @@ -0,0 +1,266 @@ +package testsuite + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" + "sync" + "time" +) + +func (r *Runner) caseConcurrencyThresholdLimit(ctx context.Context, cc *caseContext) error { + status, err := r.fetchQueueStatus(ctx, cc) + if err != nil { + return err + } + total := toInt(status["total"]) + maxInflight := toInt(status["max_inflight_per_account"]) + maxQueue := toInt(status["max_queue_size"]) + if total <= 0 || maxInflight <= 0 { + cc.assert("queue_capacity_known", false, fmt.Sprintf("queue_status=%v", status)) + return nil + } + capacity := total*maxInflight + maxQueue + if capacity <= 0 { + capacity = total * maxInflight + } + n := capacity + 8 + if n < 8 { + n = 8 + } + type one struct { + Status int + Err string + } + res := make([]one, n) + var wg sync.WaitGroup + for i := 0; i < n; i++ { + wg.Add(1) + go func(idx int) { + defer wg.Done() + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": fmt.Sprintf("并发边界测试 #%d,请输出不少于300字。", idx)}, + }, + "stream": true, + }, + Stream: true, + Retryable: true, + }) + if err != nil { + res[idx] = one{Err: err.Error()} + return + } + res[idx] = one{Status: resp.StatusCode} + }(i) + } + wg.Wait() + + dist := map[int]int{} + for _, it := range res { + if it.Status > 0 { + dist[it.Status]++ + } + } + cc.assert("has_200", dist[http.StatusOK] > 0, fmt.Sprintf("distribution=%v", dist)) + cc.assert("has_429_when_over_capacity", dist[http.StatusTooManyRequests] > 0, fmt.Sprintf("distribution=%v capacity=%d n=%d", dist, capacity, n)) + _, has5xx := has5xx(dist) + cc.assert("no_5xx", !has5xx, fmt.Sprintf("distribution=%v", dist)) + return nil +} + +func (r *Runner) caseStreamAbortRelease(ctx context.Context, cc *caseContext) error { + before, err := r.fetchQueueStatus(ctx, cc) + if err != nil { + return err + } + baseInUse := toInt(before["in_use"]) + for i := 0; i < 3; i++ { + if err := cc.abortStreamRequest(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": fmt.Sprintf("中断释放测试 #%d,请流式回复", i)}, + }, + "stream": true, + }, + Stream: true, + }); err != nil { + cc.assert("abort_request_no_error", false, err.Error()) + } + } + + deadline := time.Now().Add(25 * time.Second) + recovered := false + lastInUse := -1 + for time.Now().Before(deadline) { + st, err := r.fetchQueueStatus(ctx, cc) + if err != nil { + time.Sleep(500 * time.Millisecond) + continue + } + lastInUse = toInt(st["in_use"]) + if lastInUse <= baseInUse { + recovered = true + break + } + time.Sleep(time.Second) + } + cc.assert("in_use_recovered_after_abort", recovered, fmt.Sprintf("base=%d last=%d", baseInUse, lastInUse)) + return nil +} + +func (r *Runner) caseToolcallStreamMixed(ctx context.Context, cc *caseContext) error { + payload := toolcallPayload(true) + payload["messages"] = []map[string]any{ + { + "role": "user", + "content": "请先输出一句普通文本,再调用工具 search 查询 golang,最后再输出一句普通文本。", + }, + } + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: payload, + Stream: true, + Retryable: false, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + frames, done := parseSSEFrames(resp.Body) + hasTool := false + hasText := false + rawLeak := false + for _, f := range frames { + choices, _ := f["choices"].([]any) + for _, c := range choices { + ch, _ := c.(map[string]any) + delta, _ := ch["delta"].(map[string]any) + if _, ok := delta["tool_calls"]; ok { + hasTool = true + } + content := asString(delta["content"]) + if content != "" { + hasText = true + } + if strings.Contains(strings.ToLower(content), `"tool_calls"`) { + rawLeak = true + } + } + } + cc.assert("tool_calls_delta_present", hasTool, "tool_calls delta missing") + cc.assert("no_raw_tool_json_leak", !rawLeak, "raw tool_calls leaked") + cc.assert("done_terminated", done, "expected [DONE]") + if !hasTool || !hasText { + r.warnings = append(r.warnings, "toolcall mixed stream did not produce both text and tool_calls in this run (model-side behavior dependent)") + } + return nil +} + +func (r *Runner) caseSSEJSONIntegrity(ctx context.Context, cc *caseContext) error { + openaiResp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": "输出一句话"}, + }, + "stream": true, + }, + Stream: true, + Retryable: false, + }) + if err != nil { + return err + } + cc.assert("openai_status_200", openaiResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", openaiResp.StatusCode)) + badOpenAI := countMalformedSSEJSONLines(openaiResp.Body) + cc.assert("openai_sse_json_valid", badOpenAI == 0, fmt.Sprintf("malformed=%d", badOpenAI)) + + anthropicResp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/anthropic/v1/messages", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + "anthropic-version": "2023-06-01", + }, + Body: map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []map[string]any{ + {"role": "user", "content": "stream json integrity"}, + }, + "stream": true, + }, + Stream: true, + Retryable: false, + }) + if err != nil { + return err + } + cc.assert("anthropic_status_200", anthropicResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", anthropicResp.StatusCode)) + badAnthropic := countMalformedSSEJSONLines(anthropicResp.Body) + cc.assert("anthropic_sse_json_valid", badAnthropic == 0, fmt.Sprintf("malformed=%d", badAnthropic)) + return nil +} + +func (r *Runner) fetchQueueStatus(ctx context.Context, cc *caseContext) (map[string]any, error) { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/admin/queue/status", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Retryable: true, + }) + if err != nil { + return nil, err + } + var m map[string]any + if err := json.Unmarshal(resp.Body, &m); err != nil { + return nil, err + } + return m, nil +} + +func countMalformedSSEJSONLines(body []byte) int { + lines := strings.Split(string(body), "\n") + bad := 0 + for _, line := range lines { + line = strings.TrimSpace(line) + if !strings.HasPrefix(line, "data:") { + continue + } + payload := strings.TrimSpace(strings.TrimPrefix(line, "data:")) + if payload == "" || payload == "[DONE]" { + continue + } + var v any + if err := json.Unmarshal([]byte(payload), &v); err != nil { + bad++ + } + } + return bad +} diff --git a/internal/testsuite/edge_cases_abort.go b/internal/testsuite/edge_cases_abort.go new file mode 100644 index 0000000000000000000000000000000000000000..a6895addaf0023c8f10c0e6fd20ccf52b27f55cb --- /dev/null +++ b/internal/testsuite/edge_cases_abort.go @@ -0,0 +1,76 @@ +package testsuite + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "net/http" + "time" +) + +func (cc *caseContext) abortStreamRequest(ctx context.Context, spec requestSpec) error { + cc.seq++ + traceID := fmt.Sprintf("ts_%s_%s_%03d", cc.runner.runID, sanitizeID(cc.id), cc.seq) + cc.traceIDsSet[traceID] = struct{}{} + fullURL, err := withTraceQuery(cc.runner.baseURL+spec.Path, traceID) + if err != nil { + return err + } + headers := map[string]string{} + for k, v := range spec.Headers { + headers[k] = v + } + headers["X-Ds2-Test-Trace"] = traceID + bodyBytes, _ := json.Marshal(spec.Body) + headers["Content-Type"] = "application/json" + cc.requests = append(cc.requests, requestLog{ + Seq: cc.seq, + Attempt: 1, + TraceID: traceID, + Method: spec.Method, + URL: fullURL, + Headers: headers, + Body: spec.Body, + Timestamp: time.Now().Format(time.RFC3339Nano), + }) + + reqCtx, cancel := context.WithTimeout(ctx, cc.runner.opts.Timeout) + defer cancel() + req, err := http.NewRequestWithContext(reqCtx, spec.Method, fullURL, bytes.NewReader(bodyBytes)) + if err != nil { + return err + } + for k, v := range headers { + req.Header.Set(k, v) + } + start := time.Now() + resp, err := cc.runner.httpClient.Do(req) + if err != nil { + cc.responses = append(cc.responses, responseLog{ + Seq: cc.seq, + Attempt: 1, + TraceID: traceID, + StatusCode: 0, + DurationMS: time.Since(start).Milliseconds(), + NetworkErr: err.Error(), + ReceivedAt: time.Now().Format(time.RFC3339Nano), + }) + return err + } + defer func() { _ = resp.Body.Close() }() + buf := make([]byte, 512) + _, _ = resp.Body.Read(buf) + _ = resp.Body.Close() + cc.responses = append(cc.responses, responseLog{ + Seq: cc.seq, + Attempt: 1, + TraceID: traceID, + StatusCode: resp.StatusCode, + Headers: resp.Header, + BodyText: "aborted_after_first_chunk", + DurationMS: time.Since(start).Milliseconds(), + ReceivedAt: time.Now().Format(time.RFC3339Nano), + }) + return nil +} diff --git a/internal/testsuite/edge_cases_error_contract.go b/internal/testsuite/edge_cases_error_contract.go new file mode 100644 index 0000000000000000000000000000000000000000..f177155a24f34ac099d24c5629bf4735a45a9aa0 --- /dev/null +++ b/internal/testsuite/edge_cases_error_contract.go @@ -0,0 +1,176 @@ +package testsuite + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" +) + +func (r *Runner) caseInvalidModel(ctx context.Context, cc *caseContext) error { + resp, err := cc.requestOnce(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-not-exists", + "messages": []map[string]any{ + {"role": "user", "content": "hi"}, + }, + "stream": false, + }, + Retryable: false, + }, 1) + if err != nil { + return err + } + cc.assert("status_503", resp.StatusCode == http.StatusServiceUnavailable, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + e, _ := m["error"].(map[string]any) + cc.assert("error_type_service_unavailable", asString(e["type"]) == "service_unavailable_error", fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} + +func (r *Runner) caseMissingMessages(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "stream": false, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_400", resp.StatusCode == http.StatusBadRequest, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + e, _ := m["error"].(map[string]any) + cc.assert("error_type_invalid_request", asString(e["type"]) == "invalid_request_error", fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} + +func (r *Runner) caseAdminUnauthorized(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/admin/config", + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_401", resp.StatusCode == http.StatusUnauthorized, fmt.Sprintf("status=%d", resp.StatusCode)) + return nil +} + +func (r *Runner) caseTokenRefreshManagedAccount(ctx context.Context, cc *caseContext) error { + if len(r.configRaw.Accounts) == 0 { + cc.assert("account_present", false, "no account in config") + return nil + } + acc := r.configRaw.Accounts[0] + id := strings.TrimSpace(acc.Email) + if id == "" { + id = strings.TrimSpace(acc.Mobile) + } + if id == "" { + cc.assert("account_identifier", false, "first account has no identifier") + return nil + } + if strings.TrimSpace(acc.Password) == "" { + r.warnings = append(r.warnings, "token refresh edge case skipped strict check: first account password empty") + cc.assert("account_password_present", true, "skipped strict refresh check due empty password") + return nil + } + invalidToken := "invalid-testsuite-refresh-token-" + sanitizeID(r.runID) + update := map[string]any{ + "keys": r.configRaw.Keys, + "accounts": []map[string]any{ + { + "email": acc.Email, + "mobile": acc.Mobile, + "password": acc.Password, + "token": invalidToken, + }, + }, + } + updResp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/admin/config", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Body: update, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("update_config_status_200", updResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", updResp.StatusCode)) + + chatResp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + "X-Ds2-Target-Account": id, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": "token refresh test"}, + }, + "stream": false, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("chat_status_200", chatResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d body=%s", chatResp.StatusCode, string(chatResp.Body))) + + cfgResp, err := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/admin/config", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Retryable: true, + }) + if err != nil { + return err + } + var cfg map[string]any + _ = json.Unmarshal(cfgResp.Body, &cfg) + accounts, _ := cfg["accounts"].([]any) + preview := "" + hasToken := false + for _, item := range accounts { + m, _ := item.(map[string]any) + e := asString(m["email"]) + mo := asString(m["mobile"]) + if e == acc.Email && mo == acc.Mobile { + preview = asString(m["token_preview"]) + hasToken, _ = m["has_token"].(bool) + break + } + } + cc.assert("has_token_after_refresh", hasToken, fmt.Sprintf("config=%s", string(cfgResp.Body))) + maskedInvalid := invalidToken + if len(maskedInvalid) <= 4 { + maskedInvalid = strings.Repeat("*", len(maskedInvalid)) + } else { + maskedInvalid = maskedInvalid[:2] + "****" + maskedInvalid[len(maskedInvalid)-2:] + } + cc.assert("token_preview_changed_from_invalid", preview != maskedInvalid, fmt.Sprintf("preview=%s invalid_mask=%s", preview, maskedInvalid)) + return nil +} diff --git a/internal/testsuite/runner_cases_admin.go b/internal/testsuite/runner_cases_admin.go new file mode 100644 index 0000000000000000000000000000000000000000..a908575b934e26961dd1308bdfff18d033aa66d6 --- /dev/null +++ b/internal/testsuite/runner_cases_admin.go @@ -0,0 +1,161 @@ +package testsuite + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "net/url" + "strings" +) + +func (r *Runner) caseAdminLoginVerify(ctx context.Context, cc *caseContext) error { + loginResp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/admin/login", + Body: map[string]any{"admin_key": r.adminKey, "expire_hours": 24}, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("login_status_200", loginResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", loginResp.StatusCode)) + var payload map[string]any + _ = json.Unmarshal(loginResp.Body, &payload) + token := asString(payload["token"]) + cc.assert("token_exists", token != "", fmt.Sprintf("body=%s", string(loginResp.Body))) + if token == "" { + return nil + } + verifyResp, err := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/admin/verify", + Headers: map[string]string{ + "Authorization": "Bearer " + token, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("verify_status_200", verifyResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", verifyResp.StatusCode)) + var v map[string]any + _ = json.Unmarshal(verifyResp.Body, &v) + valid, _ := v["valid"].(bool) + cc.assert("verify_valid_true", valid, fmt.Sprintf("body=%s", string(verifyResp.Body))) + return nil +} + +func (r *Runner) caseAdminQueueStatus(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/admin/queue/status", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + _, hasRec := m["recommended_concurrency"] + _, hasQueue := m["max_queue_size"] + cc.assert("has_recommended_concurrency", hasRec, fmt.Sprintf("body=%s", string(resp.Body))) + cc.assert("has_max_queue_size", hasQueue, fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} +func (r *Runner) caseAdminAccountTest(ctx context.Context, cc *caseContext) error { + if strings.TrimSpace(r.accountID) == "" { + cc.assert("account_present", false, "no account in config") + return nil + } + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/admin/accounts/test", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Body: map[string]any{ + "identifier": r.accountID, + "model": "deepseek-v4-flash", + "message": "ping", + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + ok, _ := m["success"].(bool) + cc.assert("success_true", ok, fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} +func (r *Runner) caseConfigWriteIsolated(ctx context.Context, cc *caseContext) error { + k := "testsuite-temp-" + sanitizeID(r.runID) + add, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/admin/keys", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Body: map[string]any{"key": k}, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("add_key_status_200", add.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", add.StatusCode)) + + cfg1, err := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/admin/config", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Retryable: true, + }) + if err != nil { + return err + } + containsAdded := strings.Contains(string(cfg1.Body), k) + cc.assert("key_present_in_isolated_config", containsAdded, "added key not found in isolated config") + + delPath := "/admin/keys/" + url.PathEscape(k) + del, err := cc.request(ctx, requestSpec{ + Method: http.MethodDelete, + Path: delPath, + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("delete_key_status_200", del.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", del.StatusCode)) + + cfg2, err := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/admin/config", + Headers: map[string]string{ + "Authorization": "Bearer " + r.adminJWT, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("key_removed_in_isolated_config", !strings.Contains(string(cfg2.Body), k), "temporary key still present") + + if err := r.ensureOriginalConfigUntouched(); err != nil { + cc.assert("original_config_unchanged", false, err.Error()) + } else { + cc.assert("original_config_unchanged", true, "") + } + return nil +} diff --git a/internal/testsuite/runner_cases_claude.go b/internal/testsuite/runner_cases_claude.go new file mode 100644 index 0000000000000000000000000000000000000000..590e524ba1b930945fb454b57d8469467d8ccdd0 --- /dev/null +++ b/internal/testsuite/runner_cases_claude.go @@ -0,0 +1,103 @@ +package testsuite + +import ( + "context" + "encoding/json" + "fmt" + "net/http" +) + +func (r *Runner) caseModelsClaude(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{Method: http.MethodGet, Path: "/anthropic/v1/models", Retryable: true}) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + ids := extractModelIDs(resp.Body) + cc.assert("non_empty", len(ids) > 0, fmt.Sprintf("models=%v", ids)) + return nil +} +func (r *Runner) caseAnthropicNonstream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/anthropic/v1/messages", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + "anthropic-version": "2023-06-01", + "content-type": "application/json", + }, + Body: map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []map[string]any{ + {"role": "user", "content": "hello"}, + }, + "stream": false, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + cc.assert("type_message", asString(m["type"]) == "message", fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} + +func (r *Runner) caseAnthropicStream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/anthropic/v1/messages", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + "anthropic-version": "2023-06-01", + "content-type": "application/json", + }, + Body: map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []map[string]any{ + {"role": "user", "content": "stream hello"}, + }, + "stream": true, + }, + Stream: true, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + events := parseClaudeStreamEvents(resp.Body) + cc.assert("has_message_start", contains(events, "message_start"), fmt.Sprintf("events=%v", events)) + cc.assert("has_message_stop", contains(events, "message_stop"), fmt.Sprintf("events=%v", events)) + return nil +} + +func (r *Runner) caseAnthropicCountTokens(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/anthropic/v1/messages/count_tokens", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + "anthropic-version": "2023-06-01", + "content-type": "application/json", + }, + Body: map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []map[string]any{ + {"role": "user", "content": "count me"}, + }, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + v := toInt(m["input_tokens"]) + cc.assert("input_tokens_gt_zero", v > 0, fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} diff --git a/internal/testsuite/runner_cases_openai.go b/internal/testsuite/runner_cases_openai.go new file mode 100644 index 0000000000000000000000000000000000000000..bd22971a6d2980bc1ab3ccb9bb2026b9a95e9f9f --- /dev/null +++ b/internal/testsuite/runner_cases_openai.go @@ -0,0 +1,237 @@ +package testsuite + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" +) + +func (r *Runner) caseHealthz(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{Method: http.MethodGet, Path: "/healthz", Retryable: true}) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + cc.assert("status_ok", asString(m["status"]) == "ok", fmt.Sprintf("body=%s", string(resp.Body))) + + headResp, headErr := cc.request(ctx, requestSpec{Method: http.MethodHead, Path: "/healthz", Retryable: true}) + if headErr != nil { + return headErr + } + cc.assert("head_status_200", headResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", headResp.StatusCode)) + return nil +} + +func (r *Runner) caseReadyz(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{Method: http.MethodGet, Path: "/readyz", Retryable: true}) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + cc.assert("status_ready", asString(m["status"]) == "ready", fmt.Sprintf("body=%s", string(resp.Body))) + + headResp, headErr := cc.request(ctx, requestSpec{Method: http.MethodHead, Path: "/readyz", Retryable: true}) + if headErr != nil { + return headErr + } + cc.assert("head_status_200", headResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", headResp.StatusCode)) + return nil +} + +func (r *Runner) caseModelsOpenAI(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{Method: http.MethodGet, Path: "/v1/models", Retryable: true}) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + ids := extractModelIDs(resp.Body) + cc.assert("has_deepseek_chat", contains(ids, "deepseek-v4-flash"), strings.Join(ids, ",")) + cc.assert("has_deepseek_reasoner", contains(ids, "deepseek-v4-pro"), strings.Join(ids, ",")) + cc.assert("has_deepseek_expert_chat", contains(ids, "deepseek-v4-pro"), strings.Join(ids, ",")) + cc.assert("has_deepseek_expert_reasoner", contains(ids, "deepseek-v4-pro"), strings.Join(ids, ",")) + cc.assert("has_deepseek_vision_chat", contains(ids, "deepseek-v4-vision"), strings.Join(ids, ",")) + cc.assert("has_deepseek_vision_reasoner", contains(ids, "deepseek-v4-vision"), strings.Join(ids, ",")) + return nil +} + +func (r *Runner) caseModelOpenAIByID(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{Method: http.MethodGet, Path: "/v1/models/gpt-4o", Retryable: true}) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + cc.assert("object_model", asString(m["object"]) == "model", fmt.Sprintf("body=%s", string(resp.Body))) + cc.assert("id_deepseek_chat", asString(m["id"]) == "deepseek-v4-flash", fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} +func (r *Runner) caseChatNonstream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": "请简单回复一句话"}, + }, + "stream": false, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + cc.assert("object_chat_completion", asString(m["object"]) == "chat.completion", fmt.Sprintf("body=%s", string(resp.Body))) + choices, _ := m["choices"].([]any) + cc.assert("choices_non_empty", len(choices) > 0, fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} + +func (r *Runner) caseChatStream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": "请流式回复一句话"}, + }, + "stream": true, + }, + Stream: true, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + frames, done := parseSSEFrames(resp.Body) + cc.assert("frames_non_empty", len(frames) > 0, fmt.Sprintf("len=%d", len(frames))) + cc.assert("done_terminated", done, "expected [DONE]") + return nil +} + +func (r *Runner) caseResponsesNonstream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/responses", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "gpt-4o", + "input": "请简要回答 hello", + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + cc.assert("object_response", asString(m["object"]) == "response", fmt.Sprintf("body=%s", string(resp.Body))) + responseID := asString(m["id"]) + cc.assert("response_id_present", responseID != "", fmt.Sprintf("body=%s", string(resp.Body))) + if responseID != "" { + getResp, getErr := cc.request(ctx, requestSpec{ + Method: http.MethodGet, + Path: "/v1/responses/" + responseID, + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Retryable: true, + }) + if getErr != nil { + return getErr + } + cc.assert("get_status_200", getResp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", getResp.StatusCode)) + } + return nil +} + +func (r *Runner) caseResponsesStream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/responses", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "gpt-4o", + "input": "请流式回答 hello", + "stream": true, + }, + Stream: true, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + frames, done := parseSSEFrames(resp.Body) + cc.assert("frames_non_empty", len(frames) > 0, fmt.Sprintf("len=%d", len(frames))) + hasCreated := false + hasCompleted := false + for _, f := range frames { + switch asString(f["type"]) { + case "response.created": + hasCreated = true + case "response.completed": + hasCompleted = true + } + } + cc.assert("has_response_created", hasCreated, fmt.Sprintf("body=%s", string(resp.Body))) + cc.assert("has_response_completed", hasCompleted, fmt.Sprintf("body=%s", string(resp.Body))) + cc.assert("done_terminated", done, "expected [DONE]") + return nil +} + +func (r *Runner) caseEmbeddings(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/embeddings", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "gpt-4o", + "input": []string{"hello", "world"}, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200_or_501", resp.StatusCode == http.StatusOK || resp.StatusCode == http.StatusNotImplemented, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + if resp.StatusCode == http.StatusOK { + cc.assert("object_list", asString(m["object"]) == "list", fmt.Sprintf("body=%s", string(resp.Body))) + data, _ := m["data"].([]any) + cc.assert("data_non_empty", len(data) > 0, fmt.Sprintf("body=%s", string(resp.Body))) + return nil + } + errObj, _ := m["error"].(map[string]any) + _, hasCode := errObj["code"] + _, hasParam := errObj["param"] + cc.assert("error_has_code", hasCode, fmt.Sprintf("body=%s", string(resp.Body))) + cc.assert("error_has_param", hasParam, fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} diff --git a/internal/testsuite/runner_cases_openai_advanced.go b/internal/testsuite/runner_cases_openai_advanced.go new file mode 100644 index 0000000000000000000000000000000000000000..f0ec3cff5bdde74e68be4f3adf6b902edf508437 --- /dev/null +++ b/internal/testsuite/runner_cases_openai_advanced.go @@ -0,0 +1,236 @@ +package testsuite + +import ( + "context" + "encoding/json" + "fmt" + "net/http" + "strings" + "sync" +) + +func (r *Runner) caseReasonerStream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-pro", + "messages": []map[string]any{ + {"role": "user", "content": "先思考后回答:1+1"}, + }, + "stream": true, + }, + Stream: true, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + frames, done := parseSSEFrames(resp.Body) + hasReasoning := false + for _, f := range frames { + choices, _ := f["choices"].([]any) + for _, c := range choices { + ch, _ := c.(map[string]any) + delta, _ := ch["delta"].(map[string]any) + if asString(delta["reasoning_content"]) != "" { + hasReasoning = true + } + } + } + cc.assert("has_reasoning_content", hasReasoning, "reasoning_content not found") + cc.assert("done_terminated", done, "expected [DONE]") + return nil +} + +func (r *Runner) caseToolcallNonstream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: toolcallPayload(false), + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + choices, _ := m["choices"].([]any) + if len(choices) == 0 { + cc.assert("choices_non_empty", false, fmt.Sprintf("body=%s", string(resp.Body))) + return nil + } + c0, _ := choices[0].(map[string]any) + cc.assert("finish_reason_tool_calls", asString(c0["finish_reason"]) == "tool_calls", fmt.Sprintf("body=%s", string(resp.Body))) + msg, _ := c0["message"].(map[string]any) + tc, _ := msg["tool_calls"].([]any) + cc.assert("tool_calls_present", len(tc) > 0, fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} + +func (r *Runner) caseToolcallStream(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: toolcallPayload(true), + Stream: true, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_200", resp.StatusCode == http.StatusOK, fmt.Sprintf("status=%d", resp.StatusCode)) + frames, done := parseSSEFrames(resp.Body) + hasTool := false + rawLeak := false + for _, f := range frames { + choices, _ := f["choices"].([]any) + for _, c := range choices { + ch, _ := c.(map[string]any) + delta, _ := ch["delta"].(map[string]any) + if _, ok := delta["tool_calls"]; ok { + hasTool = true + } + content := asString(delta["content"]) + if strings.Contains(strings.ToLower(content), `"tool_calls"`) { + rawLeak = true + } + } + } + cc.assert("tool_calls_delta_present", hasTool, "tool_calls delta missing") + cc.assert("no_raw_tool_json_leak", !rawLeak, "raw tool_calls JSON leaked in content") + cc.assert("done_terminated", done, "expected [DONE]") + return nil +} + +func (r *Runner) caseConcurrencyBurst(ctx context.Context, cc *caseContext) error { + accountCount := len(r.configRaw.Accounts) + n := accountCount*2 + 2 + if n < 2 { + n = 2 + } + type one struct { + Status int + Err string + } + results := make([]one, n) + var wg sync.WaitGroup + for i := 0; i < n; i++ { + wg.Add(1) + go func(idx int) { + defer wg.Done() + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer " + r.apiKey, + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": fmt.Sprintf("并发请求 #%d,请回复ok", idx)}, + }, + "stream": true, + }, + Stream: true, + Retryable: true, + }) + if err != nil { + results[idx] = one{Err: err.Error()} + return + } + results[idx] = one{Status: resp.StatusCode} + }(i) + } + wg.Wait() + + dist := map[int]int{} + success := 0 + for _, it := range results { + if it.Status > 0 { + dist[it.Status]++ + if it.Status == http.StatusOK { + success++ + } + } + } + cc.assert("success_gt_zero", success > 0, fmt.Sprintf("distribution=%v", dist)) + _, has5xx := has5xx(dist) + cc.assert("no_5xx", !has5xx, fmt.Sprintf("distribution=%v", dist)) + if err := r.ping("/healthz"); err != nil { + cc.assert("server_alive", false, err.Error()) + } else { + cc.assert("server_alive", true, "") + } + return nil +} + +func (r *Runner) caseInvalidKey(ctx context.Context, cc *caseContext) error { + resp, err := cc.request(ctx, requestSpec{ + Method: http.MethodPost, + Path: "/v1/chat/completions", + Headers: map[string]string{ + "Authorization": "Bearer invalid-testsuite-key-" + sanitizeID(r.runID), + }, + Body: map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + {"role": "user", "content": "hi"}, + }, + "stream": false, + }, + Retryable: true, + }) + if err != nil { + return err + } + cc.assert("status_401", resp.StatusCode == http.StatusUnauthorized, fmt.Sprintf("status=%d", resp.StatusCode)) + var m map[string]any + _ = json.Unmarshal(resp.Body, &m) + e, _ := m["error"].(map[string]any) + cc.assert("error_object_present", len(e) > 0, fmt.Sprintf("body=%s", string(resp.Body))) + cc.assert("error_message_present", asString(e["message"]) != "", fmt.Sprintf("body=%s", string(resp.Body))) + return nil +} + +func toolcallPayload(stream bool) map[string]any { + return map[string]any{ + "model": "deepseek-v4-flash", + "messages": []map[string]any{ + { + "role": "user", + "content": "你必须调用工具 search 查询 golang,并仅返回工具调用。", + }, + }, + "tools": []map[string]any{ + { + "type": "function", + "function": map[string]any{ + "name": "search", + "description": "search documents", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "q": map[string]any{ + "type": "string", + }, + }, + "required": []string{"q"}, + }, + }, + }, + }, + "stream": stream, + } +} diff --git a/internal/testsuite/runner_core.go b/internal/testsuite/runner_core.go new file mode 100644 index 0000000000000000000000000000000000000000..06eafa5aa573fa5135e65158a6383922ad311193 --- /dev/null +++ b/internal/testsuite/runner_core.go @@ -0,0 +1,290 @@ +package testsuite + +import ( + "context" + "fmt" + "net/http" + "os" + "os/exec" + "path/filepath" + "sort" + "strings" + "sync" + "time" +) + +type Options struct { + ConfigPath string + AdminKey string + OutputDir string + Port int + Timeout time.Duration + Retries int + NoPreflight bool + MaxKeepRuns int +} + +type runSummary struct { + RunID string `json:"run_id"` + StartedAt string `json:"started_at"` + EndedAt string `json:"ended_at"` + DurationMS int64 `json:"duration_ms"` + Stats map[string]any `json:"stats"` + Environment map[string]any `json:"environment"` + Cases []caseResult `json:"cases"` + Warnings []string `json:"warnings,omitempty"` +} + +type caseResult struct { + CaseID string `json:"case_id"` + Passed bool `json:"passed"` + DurationMS int64 `json:"duration_ms"` + TraceIDs []string `json:"trace_ids"` + StatusCodes []int `json:"status_codes"` + Error string `json:"error,omitempty"` + ArtifactPath string `json:"artifact_path"` + Assertions []assertionResult `json:"assertions"` +} + +type assertionResult struct { + Name string `json:"name"` + Passed bool `json:"passed"` + Detail string `json:"detail,omitempty"` +} + +type requestLog struct { + Seq int `json:"seq"` + Attempt int `json:"attempt"` + TraceID string `json:"trace_id"` + Method string `json:"method"` + URL string `json:"url"` + Headers map[string]string `json:"headers"` + Body any `json:"body,omitempty"` + Timestamp string `json:"timestamp"` +} + +type responseLog struct { + Seq int `json:"seq"` + Attempt int `json:"attempt"` + TraceID string `json:"trace_id"` + StatusCode int `json:"status_code"` + Headers map[string][]string `json:"headers"` + BodyText string `json:"body_text"` + DurationMS int64 `json:"duration_ms"` + NetworkErr string `json:"network_error,omitempty"` + ReceivedAt string `json:"received_at"` +} + +type caseContext struct { + runner *Runner + id string + dir string + startedAt time.Time + mu sync.Mutex + seq int + assertions []assertionResult + requests []requestLog + responses []responseLog + streamRaw strings.Builder + traceIDsSet map[string]struct{} +} + +type requestSpec struct { + Method string + Path string + Headers map[string]string + Body any + Stream bool + Retryable bool +} + +type responseResult struct { + StatusCode int + Headers http.Header + Body []byte + TraceID string + URL string +} + +type Runner struct { + opts Options + + runID string + runDir string + serverLog string + preflightLog string + + baseURL string + httpClient *http.Client + serverCmd *exec.Cmd + serverLogFd *os.File + + configCopyPath string + originalConfigPath string + originalConfigHash string + + configRaw runConfig + apiKey string + adminKey string + adminJWT string + accountID string + + warnings []string + results []caseResult +} + +type runConfig struct { + Keys []string `json:"keys"` + Accounts []struct { + Email string `json:"email,omitempty"` + Mobile string `json:"mobile,omitempty"` + Password string `json:"password,omitempty"` + Token string `json:"token,omitempty"` + } `json:"accounts"` +} + +func Run(ctx context.Context, opts Options) error { + r, err := newRunner(opts) + if err != nil { + return err + } + start := time.Now() + defer func() { + _ = r.stopServer() + }() + + if err := r.prepareRunDir(); err != nil { + return err + } + + if !r.opts.NoPreflight { + if err := r.runPreflight(ctx); err != nil { + _ = r.writeSummary(start, time.Now()) + return err + } + } + + if err := r.prepareConfigIsolation(); err != nil { + _ = r.writeSummary(start, time.Now()) + return err + } + + if err := r.startServer(ctx); err != nil { + _ = r.writeSummary(start, time.Now()) + return err + } + + if err := r.prepareAuth(ctx); err != nil { + r.warnings = append(r.warnings, "auth prepare failed: "+err.Error()) + } + + for _, c := range r.cases() { + r.runCase(ctx, c) + } + + if err := r.ensureOriginalConfigUntouched(); err != nil { + r.warnings = append(r.warnings, err.Error()) + } + + end := time.Now() + if err := r.writeSummary(start, end); err != nil { + return err + } + + // Prune old test runs, keeping only the most recent N. + if err := r.pruneOldRuns(); err != nil { + r.warnings = append(r.warnings, "prune old runs: "+err.Error()) + } + + failed := 0 + for _, cs := range r.results { + if !cs.Passed { + failed++ + } + } + if failed > 0 { + return fmt.Errorf("testsuite failed: %d case(s) failed, see %s", failed, filepath.Join(r.runDir, "summary.md")) + } + return nil +} + +func newRunner(opts Options) (*Runner, error) { + if strings.TrimSpace(opts.ConfigPath) == "" { + opts.ConfigPath = "config.json" + } + if strings.TrimSpace(opts.OutputDir) == "" { + opts.OutputDir = "artifacts/testsuite" + } + if opts.Timeout <= 0 { + opts.Timeout = 120 * time.Second + } + if opts.Retries < 0 { + opts.Retries = 0 + } + adminKey := strings.TrimSpace(opts.AdminKey) + if adminKey == "" { + adminKey = strings.TrimSpace(os.Getenv("DS2API_ADMIN_KEY")) + } + if adminKey == "" { + adminKey = "admin" + } + opts.AdminKey = adminKey + + return &Runner{ + opts: opts, + httpClient: &http.Client{ + Timeout: 0, + }, + runID: time.Now().UTC().Format("20060102T150405Z"), + adminKey: adminKey, + }, nil +} +func (r *Runner) runCase(ctx context.Context, c caseDef) { + caseDir := filepath.Join(r.runDir, "cases", c.ID) + _ = os.MkdirAll(caseDir, 0o755) + cc := &caseContext{ + runner: r, + id: c.ID, + dir: caseDir, + startedAt: time.Now(), + traceIDsSet: map[string]struct{}{}, + } + err := c.Run(ctx, cc) + duration := time.Since(cc.startedAt).Milliseconds() + + if err != nil { + cc.assertions = append(cc.assertions, assertionResult{ + Name: "case_error", + Passed: false, + Detail: err.Error(), + }) + } + passed := err == nil + for _, a := range cc.assertions { + if !a.Passed { + passed = false + break + } + } + + traceIDs := make([]string, 0, len(cc.traceIDsSet)) + for t := range cc.traceIDsSet { + traceIDs = append(traceIDs, t) + } + sort.Strings(traceIDs) + statuses := uniqueStatusCodes(cc.responses) + cs := caseResult{ + CaseID: c.ID, + Passed: passed, + DurationMS: duration, + TraceIDs: traceIDs, + StatusCodes: statuses, + ArtifactPath: caseDir, + Assertions: cc.assertions, + } + if err != nil { + cs.Error = err.Error() + } + _ = cc.flushArtifacts(cs) + r.results = append(r.results, cs) +} diff --git a/internal/testsuite/runner_defaults.go b/internal/testsuite/runner_defaults.go new file mode 100644 index 0000000000000000000000000000000000000000..ab30bf1fa5f8db20649ab1a3ff6a763c14b797ff --- /dev/null +++ b/internal/testsuite/runner_defaults.go @@ -0,0 +1,20 @@ +package testsuite + +import ( + "os" + "strings" + "time" +) + +func DefaultOptions() Options { + return Options{ + ConfigPath: "config.json", + AdminKey: strings.TrimSpace(os.Getenv("DS2API_ADMIN_KEY")), + OutputDir: "artifacts/testsuite", + Port: 0, + Timeout: 120 * time.Second, + Retries: 2, + NoPreflight: false, + MaxKeepRuns: 5, + } +} diff --git a/internal/testsuite/runner_env.go b/internal/testsuite/runner_env.go new file mode 100644 index 0000000000000000000000000000000000000000..7d4fa1127a25041d850fbc7ad4658de0d1ae0376 --- /dev/null +++ b/internal/testsuite/runner_env.go @@ -0,0 +1,263 @@ +package testsuite + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "net/http" + "os" + "os/exec" + "path/filepath" + "sort" + "strconv" + "strings" + "time" +) + +func (r *Runner) prepareRunDir() error { + r.runDir = filepath.Join(r.opts.OutputDir, r.runID) + if err := os.MkdirAll(r.runDir, 0o755); err != nil { + return err + } + if err := os.MkdirAll(filepath.Join(r.runDir, "cases"), 0o755); err != nil { + return err + } + r.serverLog = filepath.Join(r.runDir, "server.log") + r.preflightLog = filepath.Join(r.runDir, "preflight.log") + return nil +} + +// pruneOldRuns removes old test run directories, keeping the most recent MaxKeepRuns. +// Run IDs use the format "20060102T150405Z", so alphabetical order == chronological order. +func (r *Runner) pruneOldRuns() error { + keep := r.opts.MaxKeepRuns + if keep <= 0 { + return nil // 0 or negative means no pruning + } + + entries, err := os.ReadDir(r.opts.OutputDir) + if err != nil { + return err + } + + // Collect only directories (each run is a directory). + var runDirs []string + for _, e := range entries { + if !e.IsDir() { + continue + } + runDirs = append(runDirs, e.Name()) + } + + sort.Strings(runDirs) + + if len(runDirs) <= keep { + return nil + } + + // Remove oldest runs (those at the beginning of the sorted list). + toRemove := runDirs[:len(runDirs)-keep] + var errs []string + for _, name := range toRemove { + dirPath := filepath.Join(r.opts.OutputDir, name) + if err := os.RemoveAll(dirPath); err != nil { + errs = append(errs, fmt.Sprintf("remove %s: %v", name, err)) + } else { + _, _ = fmt.Fprintf(os.Stdout, "pruned old test run: %s\n", name) + } + } + + if len(errs) > 0 { + return errors.New(strings.Join(errs, "; ")) + } + return nil +} + +func (r *Runner) runPreflight(ctx context.Context) error { + steps := preflightSteps() + f, err := os.OpenFile(r.preflightLog, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) + if err != nil { + return err + } + defer func() { _ = f.Close() }() + for _, step := range steps { + if _, err := fmt.Fprintf(f, "\n$ %s\n", strings.Join(step, " ")); err != nil { + return err + } + cmd := exec.CommandContext(ctx, step[0], step[1:]...) + cmd.Stdout = f + cmd.Stderr = f + if err := cmd.Run(); err != nil { + return fmt.Errorf("preflight failed at `%s`: %w", strings.Join(step, " "), err) + } + } + return nil +} + +func preflightSteps() [][]string { + return [][]string{ + {"go", "test", "./...", "-count=1"}, + {"./tests/scripts/check-node-split-syntax.sh"}, + {"node", "--test", "tests/node/stream-tool-sieve.test.js", "tests/node/chat-stream.test.js", "tests/node/js_compat_test.js"}, + {"npm", "run", "build", "--prefix", "webui"}, + } +} + +func (r *Runner) prepareConfigIsolation() error { + abs, err := filepath.Abs(r.opts.ConfigPath) + if err != nil { + return err + } + r.originalConfigPath = abs + raw, err := os.ReadFile(abs) + if err != nil { + return err + } + sum := sha256.Sum256(raw) + r.originalConfigHash = hex.EncodeToString(sum[:]) + + tmpDir := filepath.Join(r.runDir, "tmp") + if err := os.MkdirAll(tmpDir, 0o755); err != nil { + return err + } + r.configCopyPath = filepath.Join(tmpDir, "config.json") + if err := os.WriteFile(r.configCopyPath, raw, 0o644); err != nil { + return err + } + var cfg runConfig + if err := json.Unmarshal(raw, &cfg); err != nil { + return fmt.Errorf("parse config failed: %w", err) + } + r.configRaw = cfg + if len(cfg.Keys) > 0 { + r.apiKey = strings.TrimSpace(cfg.Keys[0]) + } + for _, acc := range cfg.Accounts { + id := strings.TrimSpace(acc.Email) + if id == "" { + id = strings.TrimSpace(acc.Mobile) + } + if id != "" { + r.accountID = id + break + } + } + return nil +} + +func (r *Runner) startServer(ctx context.Context) error { + port := r.opts.Port + if port <= 0 { + p, err := findFreePort() + if err != nil { + return err + } + port = p + } + r.baseURL = "http://127.0.0.1:" + strconv.Itoa(port) + + logFd, err := os.OpenFile(r.serverLog, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644) + if err != nil { + return err + } + r.serverLogFd = logFd + cmd := exec.CommandContext(ctx, "go", "run", "./cmd/ds2api") + cmd.Stdout = logFd + cmd.Stderr = logFd + cmd.Env = prepareServerEnv(os.Environ(), map[string]string{ + "PORT": strconv.Itoa(port), + "DS2API_CONFIG_PATH": r.configCopyPath, + "DS2API_AUTO_BUILD_WEBUI": "false", + "DS2API_CONFIG_JSON": "", + }) + if err := cmd.Start(); err != nil { + _ = logFd.Close() + return err + } + r.serverCmd = cmd + + deadline := time.Now().Add(90 * time.Second) + for time.Now().Before(deadline) { + if r.ping("/healthz") == nil && r.ping("/readyz") == nil { + return nil + } + time.Sleep(500 * time.Millisecond) + } + return errors.New("server readiness timeout") +} + +func (r *Runner) stopServer() error { + var errs []string + if r.serverCmd != nil && r.serverCmd.Process != nil { + _ = r.serverCmd.Process.Signal(os.Interrupt) + done := make(chan error, 1) + go func() { done <- r.serverCmd.Wait() }() + select { + case <-time.After(5 * time.Second): + _ = r.serverCmd.Process.Kill() + <-done + case <-done: + } + } + if r.serverLogFd != nil { + if err := r.serverLogFd.Close(); err != nil { + errs = append(errs, err.Error()) + } + } + if len(errs) > 0 { + return errors.New(strings.Join(errs, "; ")) + } + return nil +} + +func (r *Runner) ping(path string) error { + resp, err := r.httpClient.Get(r.baseURL + path) + if err != nil { + return err + } + defer func() { _ = resp.Body.Close() }() + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("status=%d", resp.StatusCode) + } + return nil +} + +func (r *Runner) prepareAuth(ctx context.Context) error { + reqBody := map[string]any{ + "admin_key": r.adminKey, + "expire_hours": 24, + } + resp, err := r.doSimpleJSON(ctx, http.MethodPost, "/admin/login", nil, reqBody) + if err != nil { + return err + } + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("admin login status=%d body=%s", resp.StatusCode, string(resp.Body)) + } + var m map[string]any + if err := json.Unmarshal(resp.Body, &m); err != nil { + return err + } + token, _ := m["token"].(string) + if strings.TrimSpace(token) == "" { + return errors.New("empty admin jwt token") + } + r.adminJWT = token + return nil +} + +func (r *Runner) ensureOriginalConfigUntouched() error { + raw, err := os.ReadFile(r.originalConfigPath) + if err != nil { + return err + } + sum := sha256.Sum256(raw) + current := hex.EncodeToString(sum[:]) + if current != r.originalConfigHash { + return fmt.Errorf("original config changed unexpectedly: %s", r.originalConfigPath) + } + return nil +} diff --git a/internal/testsuite/runner_env_test.go b/internal/testsuite/runner_env_test.go new file mode 100644 index 0000000000000000000000000000000000000000..98df72cc4a237af322d7fdbbaacf9ca70f9d04f8 --- /dev/null +++ b/internal/testsuite/runner_env_test.go @@ -0,0 +1,20 @@ +package testsuite + +import ( + "reflect" + "testing" +) + +func TestPreflightStepsExactSequence(t *testing.T) { + want := [][]string{ + {"go", "test", "./...", "-count=1"}, + {"./tests/scripts/check-node-split-syntax.sh"}, + {"node", "--test", "tests/node/stream-tool-sieve.test.js", "tests/node/chat-stream.test.js", "tests/node/js_compat_test.js"}, + {"npm", "run", "build", "--prefix", "webui"}, + } + + got := preflightSteps() + if !reflect.DeepEqual(got, want) { + t.Fatalf("preflight steps mismatch\nwant=%v\ngot=%v", want, got) + } +} diff --git a/internal/testsuite/runner_http.go b/internal/testsuite/runner_http.go new file mode 100644 index 0000000000000000000000000000000000000000..1942bc0e316a7ba387166fd9ab14fb51c21a82a2 --- /dev/null +++ b/internal/testsuite/runner_http.go @@ -0,0 +1,217 @@ +package testsuite + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "io" + "net/http" + "os" + "path/filepath" + "strings" + "time" +) + +func (cc *caseContext) assert(name string, ok bool, detail string) { + cc.mu.Lock() + defer cc.mu.Unlock() + cc.assertions = append(cc.assertions, assertionResult{ + Name: name, + Passed: ok, + Detail: detail, + }) +} + +func (cc *caseContext) request(ctx context.Context, spec requestSpec) (*responseResult, error) { + retries := cc.runner.opts.Retries + if !spec.Retryable { + retries = 0 + } + var lastErr error + for attempt := 1; attempt <= retries+1; attempt++ { + resp, err := cc.requestOnce(ctx, spec, attempt) + if err == nil && resp.StatusCode < 500 { + return resp, nil + } + if err != nil { + lastErr = err + } else if resp.StatusCode >= 500 { + lastErr = fmt.Errorf("status=%d", resp.StatusCode) + } + if attempt <= retries { + sleep := time.Duration(300*(1<<(attempt-1))) * time.Millisecond + time.Sleep(sleep) + } + } + return nil, lastErr +} + +func (cc *caseContext) requestOnce(ctx context.Context, spec requestSpec, attempt int) (*responseResult, error) { + cc.mu.Lock() + cc.seq++ + seq := cc.seq + traceID := fmt.Sprintf("ts_%s_%s_%03d", cc.runner.runID, sanitizeID(cc.id), seq) + cc.traceIDsSet[traceID] = struct{}{} + cc.mu.Unlock() + + fullURL, err := withTraceQuery(cc.runner.baseURL+spec.Path, traceID) + if err != nil { + return nil, err + } + + headers := map[string]string{} + for k, v := range spec.Headers { + headers[k] = v + } + headers["X-Ds2-Test-Trace"] = traceID + + var bodyBytes []byte + var bodyAny any + if spec.Body != nil { + b, err := json.Marshal(spec.Body) + if err != nil { + return nil, err + } + bodyBytes = b + bodyAny = spec.Body + headers["Content-Type"] = "application/json" + } + cc.mu.Lock() + cc.requests = append(cc.requests, requestLog{ + Seq: seq, + Attempt: attempt, + TraceID: traceID, + Method: spec.Method, + URL: fullURL, + Headers: headers, + Body: bodyAny, + Timestamp: time.Now().Format(time.RFC3339Nano), + }) + cc.mu.Unlock() + + reqCtx, cancel := context.WithTimeout(ctx, cc.runner.opts.Timeout) + defer cancel() + req, err := http.NewRequestWithContext(reqCtx, spec.Method, fullURL, bytes.NewReader(bodyBytes)) + if err != nil { + return nil, err + } + for k, v := range headers { + req.Header.Set(k, v) + } + start := time.Now() + resp, err := cc.runner.httpClient.Do(req) + if err != nil { + cc.mu.Lock() + cc.responses = append(cc.responses, responseLog{ + Seq: seq, + Attempt: attempt, + TraceID: traceID, + StatusCode: 0, + DurationMS: time.Since(start).Milliseconds(), + NetworkErr: err.Error(), + ReceivedAt: time.Now().Format(time.RFC3339Nano), + }) + cc.mu.Unlock() + return nil, err + } + defer func() { _ = resp.Body.Close() }() + body, _ := io.ReadAll(resp.Body) + + cc.mu.Lock() + cc.responses = append(cc.responses, responseLog{ + Seq: seq, + Attempt: attempt, + TraceID: traceID, + StatusCode: resp.StatusCode, + Headers: resp.Header, + BodyText: string(body), + DurationMS: time.Since(start).Milliseconds(), + ReceivedAt: time.Now().Format(time.RFC3339Nano), + }) + + if spec.Stream { + _, _ = fmt.Fprintf(&cc.streamRaw, "### trace=%s url=%s\n", traceID, fullURL) + cc.streamRaw.Write(body) + cc.streamRaw.WriteString("\n\n") + } + cc.mu.Unlock() + + return &responseResult{ + StatusCode: resp.StatusCode, + Headers: resp.Header, + Body: body, + TraceID: traceID, + URL: fullURL, + }, nil +} + +func (cc *caseContext) flushArtifacts(cs caseResult) error { + requestPath := filepath.Join(cc.dir, "request.json") + headersPath := filepath.Join(cc.dir, "response.headers") + bodyPath := filepath.Join(cc.dir, "response.body") + streamPath := filepath.Join(cc.dir, "stream.raw") + assertPath := filepath.Join(cc.dir, "assertions.json") + metaPath := filepath.Join(cc.dir, "meta.json") + + if err := writeJSONFile(requestPath, cc.requests); err != nil { + return err + } + respHeaders := make([]map[string]any, 0, len(cc.responses)) + respBodies := make([]map[string]any, 0, len(cc.responses)) + for _, r := range cc.responses { + respHeaders = append(respHeaders, map[string]any{ + "seq": r.Seq, + "attempt": r.Attempt, + "trace_id": r.TraceID, + "status_code": r.StatusCode, + "headers": r.Headers, + }) + respBodies = append(respBodies, map[string]any{ + "seq": r.Seq, + "attempt": r.Attempt, + "trace_id": r.TraceID, + "status_code": r.StatusCode, + "body_text": r.BodyText, + "network_error": r.NetworkErr, + "duration_ms": r.DurationMS, + }) + } + if err := writeJSONFile(headersPath, respHeaders); err != nil { + return err + } + if err := writeJSONFile(bodyPath, respBodies); err != nil { + return err + } + if err := os.WriteFile(streamPath, []byte(cc.streamRaw.String()), 0o644); err != nil { + return err + } + if err := writeJSONFile(assertPath, cc.assertions); err != nil { + return err + } + meta := map[string]any{ + "case_id": cs.CaseID, + "trace_id": strings.Join(cs.TraceIDs, ","), + "attempt": len(cc.responses), + "duration_ms": cs.DurationMS, + "status": map[bool]string{true: "passed", false: "failed"}[cs.Passed], + "status_codes": cs.StatusCodes, + "assertions": cs.Assertions, + "artifact_path": cs.ArtifactPath, + } + return writeJSONFile(metaPath, meta) +} +func (r *Runner) doSimpleJSON(ctx context.Context, method, path string, headers map[string]string, body any) (*responseResult, error) { + cc := &caseContext{ + runner: r, + id: "auth_prepare", + traceIDsSet: map[string]struct{}{}, + } + return cc.request(ctx, requestSpec{ + Method: method, + Path: path, + Headers: headers, + Body: body, + Retryable: true, + }) +} diff --git a/internal/testsuite/runner_registry.go b/internal/testsuite/runner_registry.go new file mode 100644 index 0000000000000000000000000000000000000000..08b602a37e10f34147062cbfb75d1ffc901290e2 --- /dev/null +++ b/internal/testsuite/runner_registry.go @@ -0,0 +1,43 @@ +package testsuite + +import "context" + +type caseDef struct { + ID string + Run func(context.Context, *caseContext) error +} + +func (r *Runner) cases() []caseDef { + return []caseDef{ + {ID: "healthz_ok", Run: r.caseHealthz}, + {ID: "readyz_ok", Run: r.caseReadyz}, + {ID: "models_openai", Run: r.caseModelsOpenAI}, + {ID: "model_openai_by_id", Run: r.caseModelOpenAIByID}, + {ID: "models_claude", Run: r.caseModelsClaude}, + {ID: "admin_login_verify", Run: r.caseAdminLoginVerify}, + {ID: "admin_queue_status", Run: r.caseAdminQueueStatus}, + {ID: "chat_nonstream_basic", Run: r.caseChatNonstream}, + {ID: "chat_stream_basic", Run: r.caseChatStream}, + {ID: "responses_nonstream_basic", Run: r.caseResponsesNonstream}, + {ID: "responses_stream_basic", Run: r.caseResponsesStream}, + {ID: "embeddings_contract", Run: r.caseEmbeddings}, + {ID: "reasoner_stream", Run: r.caseReasonerStream}, + {ID: "toolcall_nonstream", Run: r.caseToolcallNonstream}, + {ID: "toolcall_stream", Run: r.caseToolcallStream}, + {ID: "anthropic_messages_nonstream", Run: r.caseAnthropicNonstream}, + {ID: "anthropic_messages_stream", Run: r.caseAnthropicStream}, + {ID: "anthropic_count_tokens", Run: r.caseAnthropicCountTokens}, + {ID: "admin_account_test_single", Run: r.caseAdminAccountTest}, + {ID: "concurrency_burst", Run: r.caseConcurrencyBurst}, + {ID: "concurrency_threshold_limit", Run: r.caseConcurrencyThresholdLimit}, + {ID: "stream_abort_release", Run: r.caseStreamAbortRelease}, + {ID: "toolcall_stream_mixed", Run: r.caseToolcallStreamMixed}, + {ID: "sse_json_integrity", Run: r.caseSSEJSONIntegrity}, + {ID: "error_contract_invalid_model", Run: r.caseInvalidModel}, + {ID: "error_contract_missing_messages", Run: r.caseMissingMessages}, + {ID: "admin_unauthorized_contract", Run: r.caseAdminUnauthorized}, + {ID: "config_write_isolated", Run: r.caseConfigWriteIsolated}, + {ID: "token_refresh_managed_account", Run: r.caseTokenRefreshManagedAccount}, + {ID: "error_contract_invalid_key", Run: r.caseInvalidKey}, + } +} diff --git a/internal/testsuite/runner_registry_test.go b/internal/testsuite/runner_registry_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5e5cd7e6c6732f7effebc5eed119465c149ebe90 --- /dev/null +++ b/internal/testsuite/runner_registry_test.go @@ -0,0 +1,85 @@ +package testsuite + +import ( + "sort" + "testing" +) + +func TestRunnerCasesRegistryExactSet(t *testing.T) { + r := &Runner{} + got := r.cases() + wantIDs := []string{ + "healthz_ok", + "readyz_ok", + "models_openai", + "model_openai_by_id", + "models_claude", + "admin_login_verify", + "admin_queue_status", + "chat_nonstream_basic", + "chat_stream_basic", + "responses_nonstream_basic", + "responses_stream_basic", + "embeddings_contract", + "reasoner_stream", + "toolcall_nonstream", + "toolcall_stream", + "anthropic_messages_nonstream", + "anthropic_messages_stream", + "anthropic_count_tokens", + "admin_account_test_single", + "concurrency_burst", + "concurrency_threshold_limit", + "stream_abort_release", + "toolcall_stream_mixed", + "sse_json_integrity", + "error_contract_invalid_model", + "error_contract_missing_messages", + "admin_unauthorized_contract", + "config_write_isolated", + "token_refresh_managed_account", + "error_contract_invalid_key", + } + + if len(got) != len(wantIDs) { + t.Fatalf("unexpected case count: got=%d want=%d", len(got), len(wantIDs)) + } + + wantSet := map[string]struct{}{} + for _, id := range wantIDs { + wantSet[id] = struct{}{} + } + + gotSet := map[string]struct{}{} + for i, cs := range got { + if cs.ID == "" { + t.Fatalf("case[%d] has empty ID", i) + } + if cs.Run == nil { + t.Fatalf("case[%d] (%s) has nil Run", i, cs.ID) + } + if _, exists := gotSet[cs.ID]; exists { + t.Fatalf("duplicate case ID: %s", cs.ID) + } + gotSet[cs.ID] = struct{}{} + } + + var missing []string + for id := range wantSet { + if _, ok := gotSet[id]; !ok { + missing = append(missing, id) + } + } + var extra []string + for id := range gotSet { + if _, ok := wantSet[id]; !ok { + extra = append(extra, id) + } + } + sort.Strings(missing) + sort.Strings(extra) + + if len(missing) > 0 || len(extra) > 0 { + t.Fatalf("registry mismatch: missing=%v extra=%v", missing, extra) + } +} diff --git a/internal/testsuite/runner_summary.go b/internal/testsuite/runner_summary.go new file mode 100644 index 0000000000000000000000000000000000000000..25b44a447bbd321b6c1e3aed7a33cf734e057e3c --- /dev/null +++ b/internal/testsuite/runner_summary.go @@ -0,0 +1,97 @@ +package testsuite + +import ( + "fmt" + "os" + "path/filepath" + "runtime" + "strings" + "time" +) + +func (r *Runner) writeSummary(start, end time.Time) error { + passed := 0 + failed := 0 + for _, cs := range r.results { + if cs.Passed { + passed++ + } else { + failed++ + } + } + summary := runSummary{ + RunID: r.runID, + StartedAt: start.Format(time.RFC3339Nano), + EndedAt: end.Format(time.RFC3339Nano), + DurationMS: end.Sub(start).Milliseconds(), + Stats: map[string]any{ + "total": len(r.results), + "passed": passed, + "failed": failed, + }, + Environment: map[string]any{ + "go_version": runtime.Version(), + "os": runtime.GOOS, + "arch": runtime.GOARCH, + "base_url": r.baseURL, + "config_source": r.originalConfigPath, + "config_isolated": r.configCopyPath, + "server_log": r.serverLog, + "preflight_log": r.preflightLog, + "retries": r.opts.Retries, + "timeout_seconds": int(r.opts.Timeout.Seconds()), + }, + Cases: r.results, + Warnings: r.warnings, + } + if err := writeJSONFile(filepath.Join(r.runDir, "summary.json"), summary); err != nil { + return err + } + return os.WriteFile(filepath.Join(r.runDir, "summary.md"), []byte(r.summaryMarkdown(summary)), 0o644) +} + +func (r *Runner) summaryMarkdown(s runSummary) string { + var b strings.Builder + b.WriteString("# DS2API Live Testsuite Summary\n\n") + b.WriteString("**Sensitive Notice:** this run stores full raw request/response logs. Do not share artifacts publicly.\n\n") + fmt.Fprintf(&b, "- Run ID: `%s`\n", s.RunID) + fmt.Fprintf(&b, "- Started: `%s`\n", s.StartedAt) + fmt.Fprintf(&b, "- Ended: `%s`\n", s.EndedAt) + fmt.Fprintf(&b, "- Duration: `%d ms`\n", s.DurationMS) + fmt.Fprintf(&b, "- Passed/Failed: `%d/%d`\n\n", s.Stats["passed"], s.Stats["failed"]) + if len(s.Warnings) > 0 { + b.WriteString("## Warnings\n\n") + for _, w := range s.Warnings { + fmt.Fprintf(&b, "- %s\n", w) + } + b.WriteString("\n") + } + b.WriteString("## Failed Cases\n\n") + hasFailed := false + for _, c := range s.Cases { + if c.Passed { + continue + } + hasFailed = true + fmt.Fprintf(&b, "- `%s`: %s\n", c.CaseID, c.Error) + if len(c.TraceIDs) > 0 { + fmt.Fprintf(&b, " - trace_ids: `%s`\n", strings.Join(c.TraceIDs, ", ")) + fmt.Fprintf(&b, " - grep: `rg \"%s\" %s`\n", c.TraceIDs[0], filepath.Join(r.runDir, "server.log")) + } + fmt.Fprintf(&b, " - artifact: `%s`\n", c.ArtifactPath) + } + if !hasFailed { + b.WriteString("- none\n") + } + b.WriteString("\n## Case Table\n\n") + b.WriteString("| case_id | status | duration_ms | statuses | artifact |\n") + b.WriteString("|---|---:|---:|---|---|\n") + for _, c := range s.Cases { + status := "PASS" + if !c.Passed { + status = "FAIL" + } + fmt.Fprintf(&b, "| %s | %s | %d | %v | `%s` |\n", c.CaseID, status, c.DurationMS, c.StatusCodes, c.ArtifactPath) + } + return b.String() +} diff --git a/internal/testsuite/runner_utils.go b/internal/testsuite/runner_utils.go new file mode 100644 index 0000000000000000000000000000000000000000..d86710a687ccb735ec3a29a377ff1739e5ae8a76 --- /dev/null +++ b/internal/testsuite/runner_utils.go @@ -0,0 +1,202 @@ +package testsuite + +import ( + "encoding/json" + "errors" + "fmt" + "net" + "net/url" + "os" + "sort" + "strings" +) + +func parseSSEFrames(body []byte) ([]map[string]any, bool) { + lines := strings.Split(string(body), "\n") + frames := make([]map[string]any, 0, len(lines)) + done := false + for _, line := range lines { + line = strings.TrimSpace(line) + if !strings.HasPrefix(line, "data:") { + continue + } + payload := strings.TrimSpace(strings.TrimPrefix(line, "data:")) + if payload == "" { + continue + } + if payload == "[DONE]" { + done = true + continue + } + var m map[string]any + if err := json.Unmarshal([]byte(payload), &m); err == nil { + frames = append(frames, m) + } + } + return frames, done +} + +func parseClaudeStreamEvents(body []byte) []string { + events := []string{} + seen := map[string]bool{} + lines := strings.Split(string(body), "\n") + for _, line := range lines { + line = strings.TrimSpace(line) + if !strings.HasPrefix(line, "data:") { + continue + } + payload := strings.TrimSpace(strings.TrimPrefix(line, "data:")) + if payload == "" { + continue + } + var m map[string]any + if err := json.Unmarshal([]byte(payload), &m); err != nil { + continue + } + t := asString(m["type"]) + if t == "" || seen[t] { + continue + } + seen[t] = true + events = append(events, t) + } + return events +} + +func extractModelIDs(body []byte) []string { + var m map[string]any + if err := json.Unmarshal(body, &m); err != nil { + return nil + } + out := []string{} + data, _ := m["data"].([]any) + for _, it := range data { + item, _ := it.(map[string]any) + id := asString(item["id"]) + if id != "" { + out = append(out, id) + } + } + return out +} + +func withTraceQuery(rawURL, traceID string) (string, error) { + u, err := url.Parse(rawURL) + if err != nil { + return "", err + } + q := u.Query() + q.Set("__trace_id", traceID) + u.RawQuery = q.Encode() + return u.String(), nil +} + +func writeJSONFile(path string, v any) error { + b, err := json.MarshalIndent(v, "", " ") + if err != nil { + return err + } + return os.WriteFile(path, b, 0o644) +} + +func prepareServerEnv(base []string, overrides map[string]string) []string { + out := make([]string, 0, len(base)+len(overrides)) + skip := map[string]struct{}{} + for k := range overrides { + skip[k] = struct{}{} + } + for _, e := range base { + parts := strings.SplitN(e, "=", 2) + if len(parts) != 2 { + continue + } + if _, ok := skip[parts[0]]; ok { + continue + } + out = append(out, e) + } + for k, v := range overrides { + out = append(out, k+"="+v) + } + return out +} + +func findFreePort() (int, error) { + ln, err := net.Listen("tcp", "127.0.0.1:0") + if err != nil { + return 0, err + } + defer func() { _ = ln.Close() }() + addr, ok := ln.Addr().(*net.TCPAddr) + if !ok { + return 0, errors.New("failed to detect tcp port") + } + return addr.Port, nil +} + +func uniqueStatusCodes(in []responseLog) []int { + set := map[int]struct{}{} + for _, it := range in { + if it.StatusCode > 0 { + set[it.StatusCode] = struct{}{} + } + } + out := make([]int, 0, len(set)) + for k := range set { + out = append(out, k) + } + sort.Ints(out) + return out +} + +func has5xx(dist map[int]int) (int, bool) { + for k := range dist { + if k >= 500 { + return k, true + } + } + return 0, false +} + +func sanitizeID(s string) string { + s = strings.ReplaceAll(s, ":", "_") + s = strings.ReplaceAll(s, "/", "_") + s = strings.ReplaceAll(s, " ", "_") + return s +} + +func asString(v any) string { + if v == nil { + return "" + } + switch x := v.(type) { + case string: + return strings.TrimSpace(x) + default: + return strings.TrimSpace(fmt.Sprintf("%v", v)) + } +} + +func toInt(v any) int { + switch x := v.(type) { + case float64: + return int(x) + case float32: + return int(x) + case int: + return x + case int64: + return int(x) + default: + return 0 + } +} + +func contains(xs []string, target string) bool { + for _, x := range xs { + if x == target { + return true + } + } + return false +} diff --git a/internal/textclean/reference_markers.go b/internal/textclean/reference_markers.go new file mode 100644 index 0000000000000000000000000000000000000000..267f0fb2b9e12404ebafefb6630c64983f7bc979 --- /dev/null +++ b/internal/textclean/reference_markers.go @@ -0,0 +1,19 @@ +package textclean + +import "regexp" + +var citationReferenceMarkerPattern = regexp.MustCompile(`(?i)\[(citation|reference):\s*\d+\]`) + +func StripReferenceMarkers(text string) string { + if text == "" { + return text + } + return citationReferenceMarkerPattern.ReplaceAllString(text, "") +} + +// StripReferenceMarkersEnabled returns the default for streaming surfaces, +// where partial citation/reference markers are hidden before the final +// link metadata is available. +func StripReferenceMarkersEnabled() bool { + return true +} diff --git a/internal/toolcall/fence_edge_test.go b/internal/toolcall/fence_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f09455a34772af84d98f2897a3e7a2e4a28b635f --- /dev/null +++ b/internal/toolcall/fence_edge_test.go @@ -0,0 +1,107 @@ +package toolcall + +import ( + "strings" + "testing" +) + +// 4 反引号嵌套 3 反引号 +func TestStripFencedCodeBlocks_NestedFourBackticks(t *testing.T) { + text := "Before\n\x60\x60\x60\x60markdown\nHere is \x60\x60\x60 nested \x60\x60\x60 example\n\x60\x60\x60\x60\nAfter" + got := stripFencedCodeBlocks(text) + if !strings.Contains(got, "Before") || !strings.Contains(got, "After") { + t.Fatalf("expected Before and After preserved, got %q", got) + } + if strings.Contains(got, "nested") { + t.Fatalf("expected nested content stripped, got %q", got) + } +} + +// 波浪线围栏 +func TestStripFencedCodeBlocks_TildeFence(t *testing.T) { + text := "Before\n~~~python\ncode here\n~~~\nAfter" + got := stripFencedCodeBlocks(text) + if !strings.Contains(got, "Before") || !strings.Contains(got, "After") { + t.Fatalf("expected Before/After, got %q", got) + } + if strings.Contains(got, "code here") { + t.Fatalf("expected code stripped, got %q", got) + } +} + +// 未闭合围栏 + 后面跟真正的工具调用:不应返回空字符串 +func TestStripFencedCodeBlocks_UnclosedFencePreservesToolCall(t *testing.T) { + text := "Example:\n\x60\x60\x60xml\nREADME.md\n\ngo" + got := stripFencedCodeBlocks(text) + if got == "" { + t.Fatalf("unclosed fence should not truncate everything — real tool call after the fence is lost") + } +} + +// CDATA 内的围栏不应被剥离 +func TestStripFencedCodeBlocks_FenceInsideCDATA(t *testing.T) { + text := "\n\n" + got := stripFencedCodeBlocks(text) + if !strings.Contains(got, "\x60\x60\x60python") { + t.Fatalf("fenced code inside CDATA should be preserved, got %q", got) + } +} + +// 连续多个围栏 +func TestStripFencedCodeBlocks_MultipleFences(t *testing.T) { + text := "Before\n\x60\x60\x60\nfence1\n\x60\x60\x60\nMiddle\n\x60\x60\x60\nfence2\n\x60\x60\x60\nAfter" + got := stripFencedCodeBlocks(text) + if !strings.Contains(got, "Before") || !strings.Contains(got, "Middle") || !strings.Contains(got, "After") { + t.Fatalf("expected non-fenced content preserved, got %q", got) + } +} + +// 围栏包含内嵌 ``` 行但没有独立成行 +func TestStripFencedCodeBlocks_InlineBackticksNotFence(t *testing.T) { + text := "Before\n\x60\x60\x60go\nfmt.Println(\x60\x60\x60hello\x60\x60\x60)\n\x60\x60\x60\nAfter" + got := stripFencedCodeBlocks(text) + if !strings.Contains(got, "Before") || !strings.Contains(got, "After") { + t.Fatalf("expected Before/After, got %q", got) + } +} + +func TestParseToolCalls_IgnoresMarkdownDocumentationExamples(t *testing.T) { + text := "解析器支持多种工具调用格式。\n\n" + + "入口函数 `ParseToolCalls(text, availableToolNames)` 会返回调用列表。\n\n" + + "核心流程会解析 XML 格式的 `` / `` 标记。\n\n" + + "### 标准 XML 结构\n" + + "```xml\n" + + "\n" + + " \n" + + " config.json\n" + + " \n" + + "\n" + + "```\n\n" + + "DSML 风格形如 `...`,也可能提到 `` 包裹。\n" + + got := ParseToolCallsDetailed(text, []string{"read_file"}) + if len(got.Calls) != 0 { + t.Fatalf("markdown documentation examples should not parse as tool calls, got %#v", got.Calls) + } +} + +func TestParseToolCalls_IgnoresInlineMarkdownToolCallExample(t *testing.T) { + text := "示例:`README.md`" + + got := ParseToolCallsDetailed(text, []string{"read_file"}) + if len(got.Calls) != 0 { + t.Fatalf("inline markdown tool example should not parse as tool calls, got %#v", got.Calls) + } +} + +func TestParseToolCalls_PreservesBackticksInsideToolParameters(t *testing.T) { + text := "echo `date`" + + got := ParseToolCallsDetailed(text, []string{"Bash"}) + if len(got.Calls) != 1 { + t.Fatalf("expected one tool call, got %#v", got.Calls) + } + if got.Calls[0].Input["command"] != "echo `date`" { + t.Fatalf("expected command backticks preserved, got %#v", got.Calls[0].Input["command"]) + } +} diff --git a/internal/toolcall/regression_test.go b/internal/toolcall/regression_test.go new file mode 100644 index 0000000000000000000000000000000000000000..fc88db0a1ccdaa7077e902a0b16011d7d2f899a5 --- /dev/null +++ b/internal/toolcall/regression_test.go @@ -0,0 +1,81 @@ +package toolcall + +import ( + "reflect" + "testing" +) + +func TestRegression_RobustXMLAndCDATA(t *testing.T) { + tests := []struct { + name string + text string + expected []ParsedToolCall + }{ + { + name: "Standard JSON scalar parameters (Regression)", + text: `1`, + expected: []ParsedToolCall{{Name: "foo", Input: map[string]any{"a": float64(1)}}}, + }, + { + name: "XML tags parameters (Regression)", + text: `hello`, + expected: []ParsedToolCall{{Name: "foo", Input: map[string]any{"arg1": "hello"}}}, + }, + { + name: "CDATA parameters (New Feature)", + text: ` and & symbols]]>`, + expected: []ParsedToolCall{{ + Name: "write_file", + Input: map[string]any{"content": "line 1\nline 2 with and & symbols"}, + }}, + }, + { + name: "Nested XML with repeated parameters (New Feature)", + text: `script.shfirstsecond`, + expected: []ParsedToolCall{{ + Name: "write_file", + Input: map[string]any{ + "path": "script.sh", + "content": "#!/bin/bash\necho \"hello\"\n", + "item": []any{"first", "second"}, + }, + }}, + }, + { + name: "Dirty XML with unescaped symbols (Robustness Improvement)", + text: `echo "hello" > out.txt && cat out.txt`, + expected: []ParsedToolCall{{ + Name: "bash", + Input: map[string]any{"command": "echo \"hello\" > out.txt && cat out.txt"}, + }}, + }, + { + name: "Mixed JSON inside CDATA (New Hybrid Case)", + text: ``, + expected: []ParsedToolCall{{ + Name: "foo", + Input: map[string]any{"json_param": "works"}, + }}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := ParseToolCalls(tt.text, []string{"foo", "write_file", "bash"}) + if len(got) != len(tt.expected) { + t.Fatalf("expected %d calls, got %d", len(tt.expected), len(got)) + } + for i := range got { + if got[i].Name != tt.expected[i].Name { + t.Errorf("expected name %q, got %q", tt.expected[i].Name, got[i].Name) + } + if !reflect.DeepEqual(got[i].Input, tt.expected[i].Input) { + t.Errorf("expected input %#v, got %#v", tt.expected[i].Input, got[i].Input) + } + } + }) + } +} diff --git a/internal/toolcall/tool_prompt.go b/internal/toolcall/tool_prompt.go new file mode 100644 index 0000000000000000000000000000000000000000..c56fe9952ead5f62b75ba48928bcbf22e19dbcf9 --- /dev/null +++ b/internal/toolcall/tool_prompt.go @@ -0,0 +1,258 @@ +package toolcall + +import "strings" + +// BuildToolCallInstructions generates the unified tool-calling instruction block +// used by all adapters (OpenAI, Claude, Gemini). It uses attention-optimized +// structure: rules → negative examples → positive examples → anchor. +// +// The toolNames slice should contain the actual tool names available in the +// current request; the function picks real names for examples. +func BuildToolCallInstructions(toolNames []string) string { + return `TOOL CALL SCHEME — MANDATORY: + +<|DSML|tool_calls> + <|DSML|invoke name="TOOL_NAME"> + <|DSML|parameter name="ARG_NAME"> + + + +GUIDELINES: +1) Use the <|DSML|tool_calls> structure. +2) One or more <|DSML|invoke> entries per call. +3) Tool name goes in the invoke attribute. +3a) Tag punctuation alphabet: ASCII < > / = " plus the halfwidth pipe |. +4) All string values must use , even short ones. This includes code, scripts, file contents, prompts, paths, names, and queries. +5) Every top-level argument must be a <|DSML|parameter name="ARG_NAME">... node. +6) Objects use nested XML elements inside the parameter body. Arrays may repeat children. +7) Numbers, booleans, and null stay plain text. +8) Use only the parameter names in the tool schema. Do not invent fields. +9) Fill parameters with the actual values required for this call. Do not emit placeholder, blank, or whitespace-only parameters. +10) If a required parameter value is unknown, ask the user or answer normally instead of outputting an empty tool call. +11) For shell tools such as Bash / execute_command, the command/script must be inside the command parameter. Never call them with an empty command. +12) Do NOT wrap XML in markdown fences. Do NOT output explanations, role markers, or internal monologue. +13) If you call a tool, the first non-whitespace characters of that tool block must be exactly <|DSML|tool_calls>. +14) Never omit the opening <|DSML|tool_calls> tag, even if you already plan to close with . +15) Compatibility note: the runtime also accepts the legacy XML tags / / , but prefer the DSML-prefixed form above. + +PARAMETER SHAPES: +- string => <|DSML|parameter name="x"> +- object => <|DSML|parameter name="x">... +- array => <|DSML|parameter name="x">...... +- number/bool/null => <|DSML|parameter name="x">plain_text + +【WRONG — Do NOT do these】: + +Wrong 1 — mixed text after XML: + <|DSML|tool_calls>... I hope this helps. +Wrong 2 — Markdown code fences: + ` + "```xml" + ` + <|DSML|tool_calls>... + ` + "```" + ` +Wrong 3 — missing opening wrapper: + <|DSML|invoke name="TOOL_NAME">... + +Wrong 4 — empty parameters: + <|DSML|tool_calls> + <|DSML|invoke name="Bash"> + <|DSML|parameter name="command"> + + + +Remember: The ONLY valid way to use tools is the <|DSML|tool_calls>... block at the end of your response. +` + buildCorrectToolExamples(toolNames) +} + +type promptToolExample struct { + name string + params string +} + +func buildCorrectToolExamples(toolNames []string) string { + names := uniqueToolNames(toolNames) + examples := make([]string, 0, 4) + + if single, ok := firstBasicExample(names); ok { + examples = append(examples, "Example A — Single tool:\n"+renderToolExampleBlock([]promptToolExample{single})) + } + + if parallel := firstNBasicExamples(names, 2); len(parallel) >= 2 { + examples = append(examples, "Example B — Two tools in parallel:\n"+renderToolExampleBlock(parallel)) + } + + if nested, ok := firstNestedExample(names); ok { + examples = append(examples, "Example C — Tool with nested XML parameters:\n"+renderToolExampleBlock([]promptToolExample{nested})) + } + + if script, ok := firstScriptExample(names); ok { + examples = append(examples, "Example D — Tool with long script using CDATA (RELIABLE FOR CODE/SCRIPTS):\n"+renderToolExampleBlock([]promptToolExample{script})) + } + + if len(examples) == 0 { + return "" + } + return "【CORRECT EXAMPLES】:\n\n" + strings.Join(examples, "\n\n") + "\n\n" +} + +func uniqueToolNames(toolNames []string) []string { + names := make([]string, 0, len(toolNames)) + seen := map[string]bool{} + for _, name := range toolNames { + name = strings.TrimSpace(name) + if name == "" || seen[name] { + continue + } + seen[name] = true + names = append(names, name) + } + return names +} + +func firstBasicExample(names []string) (promptToolExample, bool) { + for _, name := range names { + if params, ok := exampleBasicParams(name); ok { + return promptToolExample{name: name, params: params}, true + } + } + return promptToolExample{}, false +} + +func firstNBasicExamples(names []string, count int) []promptToolExample { + out := make([]promptToolExample, 0, count) + for _, name := range names { + if params, ok := exampleBasicParams(name); ok { + out = append(out, promptToolExample{name: name, params: params}) + if len(out) == count { + return out + } + } + } + return out +} + +func firstNestedExample(names []string) (promptToolExample, bool) { + for _, name := range names { + if params, ok := exampleNestedParams(name); ok { + return promptToolExample{name: name, params: params}, true + } + } + return promptToolExample{}, false +} + +func firstScriptExample(names []string) (promptToolExample, bool) { + for _, name := range names { + if params, ok := exampleScriptParams(name); ok { + return promptToolExample{name: name, params: params}, true + } + } + return promptToolExample{}, false +} + +func renderToolExampleBlock(calls []promptToolExample) string { + var b strings.Builder + b.WriteString("<|DSML|tool_calls>\n") + for _, call := range calls { + b.WriteString(` <|DSML|invoke name="`) + b.WriteString(call.name) + b.WriteString(`">` + "\n") + b.WriteString(indentPromptParameters(call.params, " ")) + b.WriteString("\n \n") + } + b.WriteString("") + return b.String() +} + +func indentPromptParameters(body, indent string) string { + if strings.TrimSpace(body) == "" { + return indent + `<|DSML|parameter name="content">` + } + lines := strings.Split(body, "\n") + for i, line := range lines { + if strings.TrimSpace(line) == "" { + lines[i] = line + continue + } + lines[i] = indent + line + } + return strings.Join(lines, "\n") +} + +func wrapParameter(name, inner string) string { + return `<|DSML|parameter name="` + name + `">` + inner + `` +} + +func exampleBasicParams(name string) (string, bool) { + switch strings.TrimSpace(name) { + case "Read": + return wrapParameter("file_path", promptCDATA("README.md")), true + case "Glob": + return wrapParameter("pattern", promptCDATA("**/*.go")) + "\n" + wrapParameter("path", promptCDATA(".")), true + case "read_file": + return wrapParameter("path", promptCDATA("src/main.go")), true + case "list_files": + return wrapParameter("path", promptCDATA(".")), true + case "search_files": + return wrapParameter("query", promptCDATA("tool call parser")), true + case "Bash", "execute_command": + return wrapParameter("command", promptCDATA("pwd")), true + case "exec_command": + return wrapParameter("cmd", promptCDATA("pwd")), true + case "Write": + return wrapParameter("file_path", promptCDATA("notes.txt")) + "\n" + wrapParameter("content", promptCDATA("Hello world")), true + case "write_to_file": + return wrapParameter("path", promptCDATA("notes.txt")) + "\n" + wrapParameter("content", promptCDATA("Hello world")), true + case "Edit": + return wrapParameter("file_path", promptCDATA("README.md")) + "\n" + wrapParameter("old_string", promptCDATA("foo")) + "\n" + wrapParameter("new_string", promptCDATA("bar")), true + case "MultiEdit": + return wrapParameter("file_path", promptCDATA("README.md")) + "\n" + `<|DSML|parameter name="edits">` + promptCDATA("foo") + `` + promptCDATA("bar") + ``, true + } + return "", false +} + +func exampleNestedParams(name string) (string, bool) { + switch strings.TrimSpace(name) { + case "MultiEdit": + return wrapParameter("file_path", promptCDATA("README.md")) + "\n" + `<|DSML|parameter name="edits">` + promptCDATA("foo") + `` + promptCDATA("bar") + ``, true + case "Task": + return wrapParameter("description", promptCDATA("Investigate flaky tests")) + "\n" + wrapParameter("prompt", promptCDATA("Run targeted tests and summarize failures")), true + case "ask_followup_question": + return wrapParameter("question", promptCDATA("Which approach do you prefer?")) + "\n" + `<|DSML|parameter name="follow_up">` + promptCDATA("Option A") + `` + promptCDATA("Option B") + ``, true + } + return "", false +} + +func exampleScriptParams(name string) (string, bool) { + scriptCommand := `cat > /tmp/test_escape.sh <<'EOF' +#!/bin/bash +echo 'single "double"' +echo "literal dollar: \$HOME" +EOF +bash /tmp/test_escape.sh` + scriptContent := `#!/bin/bash +echo 'single "double"' +echo "literal dollar: $HOME"` + + switch strings.TrimSpace(name) { + case "Bash": + return wrapParameter("command", promptCDATA(scriptCommand)) + "\n" + wrapParameter("description", promptCDATA("Test shell escaping")), true + case "execute_command": + return wrapParameter("command", promptCDATA(scriptCommand)), true + case "exec_command": + return wrapParameter("cmd", promptCDATA(scriptCommand)), true + case "Write": + return wrapParameter("file_path", promptCDATA("test_escape.sh")) + "\n" + wrapParameter("content", promptCDATA(scriptContent)), true + case "write_to_file": + return wrapParameter("path", promptCDATA("test_escape.sh")) + "\n" + wrapParameter("content", promptCDATA(scriptContent)), true + } + return "", false +} + +func promptCDATA(text string) string { + if text == "" { + return "" + } + if strings.Contains(text, "]]>") { + return "", "]]]]>") + "]]>" + } + return "" +} diff --git a/internal/toolcall/tool_prompt_test.go b/internal/toolcall/tool_prompt_test.go new file mode 100644 index 0000000000000000000000000000000000000000..66bbe7a239ba2b33def7b785716a6dfd04d5d143 --- /dev/null +++ b/internal/toolcall/tool_prompt_test.go @@ -0,0 +1,167 @@ +package toolcall + +import ( + "strings" + "testing" +) + +func TestBuildToolCallInstructions_ExecCommandUsesCmdExample(t *testing.T) { + out := BuildToolCallInstructions([]string{"exec_command"}) + if !strings.Contains(out, `<|DSML|invoke name="exec_command">`) { + t.Fatalf("expected exec_command in examples, got: %s", out) + } + if !strings.Contains(out, `<|DSML|parameter name="cmd">`) { + t.Fatalf("expected cmd parameter example for exec_command, got: %s", out) + } +} + +func TestBuildToolCallInstructions_ExecuteCommandUsesCommandExample(t *testing.T) { + out := BuildToolCallInstructions([]string{"execute_command"}) + if !strings.Contains(out, `<|DSML|invoke name="execute_command">`) { + t.Fatalf("expected execute_command in examples, got: %s", out) + } + if !strings.Contains(out, `<|DSML|parameter name="command">`) { + t.Fatalf("expected command parameter example for execute_command, got: %s", out) + } +} + +func TestBuildToolCallInstructions_BashUsesCommandAndDescriptionExamples(t *testing.T) { + out := BuildToolCallInstructions([]string{"Bash"}) + blocks := findInvokeBlocks(out, "Bash") + if len(blocks) == 0 { + t.Fatalf("expected Bash examples, got: %s", out) + } + + sawDescription := false + for _, block := range blocks { + if !strings.Contains(block, `<|DSML|parameter name="command">`) { + t.Fatalf("expected every Bash example to use command parameter, got: %s", block) + } + if strings.Contains(block, `<|DSML|parameter name="path">`) || strings.Contains(block, `<|DSML|parameter name="content">`) { + t.Fatalf("expected Bash examples not to use file write parameters, got: %s", block) + } + if strings.Contains(block, `<|DSML|parameter name="description">`) { + sawDescription = true + } + } + if !sawDescription { + t.Fatalf("expected Bash long-script example to include description, got: %s", out) + } + if strings.Contains(out, `<|DSML|invoke name="Read">`) { + t.Fatalf("expected examples to avoid unavailable hard-coded Read tool, got: %s", out) + } +} + +func TestBuildToolCallInstructions_ExecuteCommandLongScriptUsesCommand(t *testing.T) { + out := BuildToolCallInstructions([]string{"execute_command"}) + blocks := findInvokeBlocks(out, "execute_command") + if len(blocks) == 0 { + t.Fatalf("expected execute_command examples, got: %s", out) + } + + for _, block := range blocks { + if !strings.Contains(block, `<|DSML|parameter name="command">`) { + t.Fatalf("expected execute_command examples to use command parameter, got: %s", block) + } + if strings.Contains(block, `<|DSML|parameter name="path">`) || strings.Contains(block, `<|DSML|parameter name="content">`) { + t.Fatalf("expected execute_command examples not to use file write parameters, got: %s", block) + } + } + if !strings.Contains(out, `test_escape.sh`) { + t.Fatalf("expected execute_command long-script example, got: %s", out) + } +} + +func TestBuildToolCallInstructions_ExecCommandLongScriptUsesCmd(t *testing.T) { + out := BuildToolCallInstructions([]string{"exec_command"}) + blocks := findInvokeBlocks(out, "exec_command") + if len(blocks) == 0 { + t.Fatalf("expected exec_command examples, got: %s", out) + } + + for _, block := range blocks { + if !strings.Contains(block, `<|DSML|parameter name="cmd">`) { + t.Fatalf("expected exec_command examples to use cmd parameter, got: %s", block) + } + if strings.Contains(block, `<|DSML|parameter name="command">`) || strings.Contains(block, `<|DSML|parameter name="path">`) || strings.Contains(block, `<|DSML|parameter name="content">`) { + t.Fatalf("expected exec_command examples not to use command or file write parameters, got: %s", block) + } + } + if !strings.Contains(out, `test_escape.sh`) { + t.Fatalf("expected exec_command long-script example, got: %s", out) + } +} + +func TestBuildToolCallInstructions_WriteUsesFilePathAndContent(t *testing.T) { + out := BuildToolCallInstructions([]string{"Write"}) + blocks := findInvokeBlocks(out, "Write") + if len(blocks) == 0 { + t.Fatalf("expected Write examples, got: %s", out) + } + + for _, block := range blocks { + if !strings.Contains(block, `<|DSML|parameter name="file_path">`) || !strings.Contains(block, `<|DSML|parameter name="content">`) { + t.Fatalf("expected Write examples to use file_path and content, got: %s", block) + } + if strings.Contains(block, `<|DSML|parameter name="path">`) { + t.Fatalf("expected Write examples not to use path, got: %s", block) + } + } +} + +func TestBuildToolCallInstructions_AnchorsMissingOpeningWrapperFailureMode(t *testing.T) { + out := BuildToolCallInstructions([]string{"read_file"}) + if !strings.Contains(out, "Never omit the opening <|DSML|tool_calls> tag") { + t.Fatalf("expected explicit missing-opening-tag warning, got: %s", out) + } + if !strings.Contains(out, "Wrong 3 — missing opening wrapper") { + t.Fatalf("expected missing-opening-wrapper negative example, got: %s", out) + } +} + +func TestBuildToolCallInstructions_RejectsEmptyParametersInPrompt(t *testing.T) { + out := BuildToolCallInstructions([]string{"Bash"}) + for _, want := range []string{ + "Do not emit placeholder, blank, or whitespace-only parameters.", + "If a required parameter value is unknown, ask the user or answer normally instead of outputting an empty tool call.", + "Never call them with an empty command.", + "Wrong 4 — empty parameters", + } { + if !strings.Contains(out, want) { + t.Fatalf("expected empty-parameter instruction %q, got: %s", want, out) + } + } +} + +func TestBuildToolCallInstructions_UsesPositiveTagPunctuationAlphabet(t *testing.T) { + out := BuildToolCallInstructions([]string{"Bash"}) + want := `Tag punctuation alphabet: ASCII < > / = " plus the halfwidth pipe |.` + if !strings.Contains(out, want) { + t.Fatalf("expected positive tag punctuation alphabet %q, got: %s", want, out) + } + for _, bad := range []string{"lookalike", "substitute", "!", "〈", "〉", "“", "”", "、"} { + if strings.Contains(out, bad) { + t.Fatalf("tool prompt should not include negative punctuation examples %q, got: %s", bad, out) + } + } +} + +func findInvokeBlocks(text, name string) []string { + open := `<|DSML|invoke name="` + name + `">` + remaining := text + blocks := []string{} + for { + start := strings.Index(remaining, open) + if start < 0 { + return blocks + } + remaining = remaining[start:] + end := strings.Index(remaining, ``) + if end < 0 { + return blocks + } + end += len(``) + blocks = append(blocks, remaining[:end]) + remaining = remaining[end:] + } +} diff --git a/internal/toolcall/toolcall_edge_test.go b/internal/toolcall/toolcall_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..690b7560b53b9e8e95c5e90fad17c0379f116d9b --- /dev/null +++ b/internal/toolcall/toolcall_edge_test.go @@ -0,0 +1,32 @@ +package toolcall + +import ( + "testing" +) + +// --- FormatOpenAIStreamToolCalls --- + +func TestFormatOpenAIStreamToolCalls(t *testing.T) { + formatted := FormatOpenAIStreamToolCalls([]ParsedToolCall{ + {Name: "search", Input: map[string]any{"q": "test"}}, + }, nil) + if len(formatted) != 1 { + t.Fatalf("expected 1, got %d", len(formatted)) + } + fn, _ := formatted[0]["function"].(map[string]any) + if fn["name"] != "search" { + t.Fatalf("unexpected function name: %#v", fn) + } + if formatted[0]["index"] != 0 { + t.Fatalf("expected index 0, got %v", formatted[0]["index"]) + } +} + +// --- ParseToolCalls edge cases --- + +func TestParseToolCallsEmptyText(t *testing.T) { + calls := ParseToolCalls("", []string{"search"}) + if len(calls) != 0 { + t.Fatalf("expected 0 calls for empty text, got %d", len(calls)) + } +} diff --git a/internal/toolcall/toolcalls_array_parse.go b/internal/toolcall/toolcalls_array_parse.go new file mode 100644 index 0000000000000000000000000000000000000000..8f712ec9c3c7e19581a60afbcde2a4b71e260184 --- /dev/null +++ b/internal/toolcall/toolcalls_array_parse.go @@ -0,0 +1,164 @@ +package toolcall + +import ( + "encoding/json" + "html" + "strings" +) + +func parseLooseJSONArrayValue(raw, paramName string) ([]any, bool) { + if preservesCDATAStringParameter(paramName) { + return nil, false + } + trimmed := strings.TrimSpace(html.UnescapeString(raw)) + if trimmed == "" { + return nil, false + } + + if parsed, ok := parseLooseJSONArrayCandidate(trimmed, paramName); ok { + return parsed, true + } + + segments, ok := splitTopLevelJSONValues(trimmed) + if !ok { + return nil, false + } + + out := make([]any, 0, len(segments)) + for _, segment := range segments { + parsed, ok := parseLooseArrayElementValue(segment) + if !ok { + return nil, false + } + out = append(out, parsed) + } + return out, true +} + +func parseLooseJSONArrayCandidate(raw, paramName string) ([]any, bool) { + parsed, ok := parseLooseArrayElementValue(raw) + if !ok { + return nil, false + } + return coerceArrayValue(parsed, paramName) +} + +func parseLooseArrayElementValue(raw string) (any, bool) { + trimmed := strings.TrimSpace(html.UnescapeString(raw)) + if trimmed == "" { + return nil, false + } + + var parsed any + if err := json.Unmarshal([]byte(trimmed), &parsed); err == nil { + return parsed, true + } + + repairedBackslashes := repairInvalidJSONBackslashes(trimmed) + if repairedBackslashes != trimmed { + if err := json.Unmarshal([]byte(repairedBackslashes), &parsed); err == nil { + return parsed, true + } + } + + repairedLoose := RepairLooseJSON(trimmed) + if repairedLoose != trimmed { + if err := json.Unmarshal([]byte(repairedLoose), &parsed); err == nil { + return parsed, true + } + } + + if strings.Contains(trimmed, "<") && strings.Contains(trimmed, ">") { + if parsedXML, ok := parseXMLFragmentValue(trimmed); ok { + return parsedXML, true + } + } + + return nil, false +} + +func coerceArrayValue(value any, paramName string) ([]any, bool) { + switch x := value.(type) { + case []any: + return x, true + case map[string]any: + if len(x) != 1 { + return nil, false + } + + if items, ok := x["item"]; ok { + if arr, ok := coerceArrayValue(items, ""); ok { + return arr, true + } + return []any{items}, true + } + + if paramName != "" { + if wrapped, ok := x[paramName]; ok { + if arr, ok := coerceArrayValue(wrapped, ""); ok { + return arr, true + } + } + } + } + return nil, false +} + +func splitTopLevelJSONValues(raw string) ([]string, bool) { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return nil, false + } + + values := make([]string, 0, 2) + start := 0 + depth := 0 + inString := false + escaped := false + + for i, r := range trimmed { + if inString { + if escaped { + escaped = false + continue + } + switch r { + case '\\': + escaped = true + case '"': + inString = false + } + continue + } + + switch r { + case '"': + inString = true + case '{', '[': + depth++ + case '}', ']': + if depth > 0 { + depth-- + } + case ',': + if depth == 0 { + segment := strings.TrimSpace(trimmed[start:i]) + if segment == "" { + return nil, false + } + values = append(values, segment) + start = i + 1 + } + } + } + + last := strings.TrimSpace(trimmed[start:]) + if last == "" { + return nil, false + } + values = append(values, last) + if len(values) < 2 { + return nil, false + } + return values, true +} diff --git a/internal/toolcall/toolcalls_candidates.go b/internal/toolcall/toolcalls_candidates.go new file mode 100644 index 0000000000000000000000000000000000000000..f9b3cbbc778f205ccd075648a9b302f644f4df9c --- /dev/null +++ b/internal/toolcall/toolcalls_candidates.go @@ -0,0 +1,691 @@ +package toolcall + +import ( + "strings" + "unicode" + "unicode/utf8" +) + +type canonicalToolMarkupAttr struct { + Key string + Value string +} + +func canonicalizeToolCallCandidateSpans(text string) string { + if text == "" { + return "" + } + var b strings.Builder + b.Grow(len(text)) + for i := 0; i < len(text); { + next, advanced, blocked := skipXMLIgnoredSection(text, i) + if blocked { + b.WriteString(text[i:]) + break + } + if advanced { + b.WriteString(text[i:next]) + i = next + continue + } + if end, ok := markdownCodeSpanEnd(text, i); ok { + b.WriteString(text[i:end]) + i = end + continue + } + tag, ok := scanToolMarkupTagAt(text, i) + if !ok { + b.WriteByte(text[i]) + i++ + continue + } + b.WriteString(canonicalizeRecognizedToolMarkupTag(text[tag.Start:tag.End+1], tag)) + i = tag.End + 1 + } + return b.String() +} + +func canonicalizeRecognizedToolMarkupTag(raw string, tag ToolMarkupTag) string { + if raw == "" { + return raw + } + idx := 0 + if delimLen := xmlTagStartDelimiterLenAt(raw, idx); delimLen > 0 { + idx += delimLen + } + for { + idx = skipToolMarkupIgnorables(raw, idx) + if delimLen := xmlTagStartDelimiterLenAt(raw, idx); delimLen > 0 { + idx += delimLen + continue + } + break + } + idx = skipToolMarkupIgnorables(raw, idx) + if tag.Closing { + if next, ok := consumeToolMarkupClosingSlash(raw, idx); ok { + idx = next + } + } + idx, _ = consumeToolMarkupNamePrefix(raw, idx) + afterName, ok := consumeToolKeyword(raw, idx, rawNameForTag(tag)) + if !ok { + afterName = idx + } + + attrs := parseCanonicalToolMarkupAttrs(raw, afterName) + + var b strings.Builder + b.Grow(len(raw) + 8) + b.WriteByte('<') + if tag.Closing { + b.WriteByte('/') + } + if tag.DSMLLike { + b.WriteString("|DSML|") + } + b.WriteString(tag.Name) + for _, attr := range attrs { + if attr.Key == "" { + continue + } + b.WriteByte(' ') + b.WriteString(attr.Key) + b.WriteString(`="`) + b.WriteString(quoteCanonicalXMLAttrValue(attr.Value)) + b.WriteByte('"') + } + if tag.SelfClosing { + b.WriteByte('/') + } + b.WriteByte('>') + return b.String() +} + +func rawNameForTag(tag ToolMarkupTag) string { + for _, name := range toolMarkupNames { + if name.canonical == tag.Name { + return name.raw + } + } + return tag.Name +} + +func parseCanonicalToolMarkupAttrs(raw string, idx int) []canonicalToolMarkupAttr { + if raw == "" || idx >= len(raw) { + return nil + } + var out []canonicalToolMarkupAttr + for idx < len(raw) { + idx = skipToolMarkupIgnorables(raw, idx) + if idx >= len(raw) { + break + } + if spacingLen := toolMarkupWhitespaceLikeLenAt(raw, idx); spacingLen > 0 { + idx += spacingLen + continue + } + if xmlTagEndDelimiterLenAt(raw, idx) > 0 { + break + } + if next, ok := consumeToolMarkupPipe(raw, idx); ok { + idx = next + continue + } + if next, ok := consumeToolMarkupClosingSlash(raw, idx); ok { + idx = next + continue + } + + keyStart := idx + for idx < len(raw) { + idx = skipToolMarkupIgnorables(raw, idx) + if idx >= len(raw) { + break + } + if spacingLen := toolMarkupWhitespaceLikeLenAt(raw, idx); spacingLen > 0 { + break + } + if toolMarkupEqualsLenAt(raw, idx) > 0 || xmlTagEndDelimiterLenAt(raw, idx) > 0 { + break + } + if _, ok := consumeToolMarkupPipe(raw, idx); ok { + break + } + if _, ok := consumeToolMarkupClosingSlash(raw, idx); ok { + break + } + _, size := utf8.DecodeRuneInString(raw[idx:]) + if size <= 0 { + idx++ + } else { + idx += size + } + } + keyEnd := idx + key := normalizeCanonicalToolAttrKey(raw[keyStart:keyEnd]) + idx = skipToolMarkupIgnorables(raw, idx) + for { + spacingLen := toolMarkupWhitespaceLikeLenAt(raw, idx) + if spacingLen == 0 { + break + } + idx += spacingLen + idx = skipToolMarkupIgnorables(raw, idx) + } + if eqLen := toolMarkupEqualsLenAt(raw, idx); eqLen > 0 { + idx += eqLen + } else { + continue + } + idx = skipToolMarkupIgnorables(raw, idx) + for { + spacingLen := toolMarkupWhitespaceLikeLenAt(raw, idx) + if spacingLen == 0 { + break + } + idx += spacingLen + idx = skipToolMarkupIgnorables(raw, idx) + } + if key == "" { + _, size := utf8.DecodeRuneInString(raw[idx:]) + if size <= 0 { + idx++ + } else { + idx += size + } + continue + } + + value := "" + if quote, quoteLen := xmlQuotePairAt(raw, idx); quoteLen > 0 { + valueStart := idx + quoteLen + idx = valueStart + for idx < len(raw) { + if closeLen := xmlQuoteCloseDelimiterLenAt(raw, idx, quote); closeLen > 0 { + value = raw[valueStart:idx] + idx += closeLen + break + } + _, size := utf8.DecodeRuneInString(raw[idx:]) + if size <= 0 { + idx++ + } else { + idx += size + } + } + } else { + valueStart := idx + for idx < len(raw) { + if spacingLen := toolMarkupWhitespaceLikeLenAt(raw, idx); spacingLen > 0 { + break + } + if xmlTagEndDelimiterLenAt(raw, idx) > 0 || toolMarkupEqualsLenAt(raw, idx) > 0 { + break + } + if _, ok := consumeToolMarkupPipe(raw, idx); ok { + break + } + if _, ok := consumeToolMarkupClosingSlash(raw, idx); ok { + break + } + _, size := utf8.DecodeRuneInString(raw[idx:]) + if size <= 0 { + idx++ + } else { + idx += size + } + } + value = raw[valueStart:idx] + } + + out = append(out, canonicalToolMarkupAttr{ + Key: key, + Value: value, + }) + } + return out +} + +func normalizeCanonicalToolAttrKey(raw string) string { + trimmed := strings.TrimSpace(removeToolMarkupIgnorables(raw)) + if trimmed == "" { + return "" + } + if next, ok := consumeToolKeyword(trimmed, 0, "name"); ok { + if skipToolMarkupIgnorables(trimmed, next) == len(trimmed) { + return "name" + } + } + return "" +} + +func quoteCanonicalXMLAttrValue(raw string) string { + if raw == "" { + return "" + } + return strings.ReplaceAll(raw, `"`, """) +} + +func removeToolMarkupIgnorables(raw string) string { + if raw == "" { + return "" + } + var b strings.Builder + b.Grow(len(raw)) + for i := 0; i < len(raw); { + if ignorableLen := toolMarkupIgnorableLenAt(raw, i); ignorableLen > 0 { + i += ignorableLen + continue + } + r, size := utf8.DecodeRuneInString(raw[i:]) + if size <= 0 { + b.WriteByte(raw[i]) + i++ + continue + } + b.WriteRune(r) + i += size + } + return b.String() +} + +func skipToolMarkupIgnorables(text string, idx int) int { + for idx < len(text) { + if ignorableLen := toolMarkupIgnorableLenAt(text, idx); ignorableLen > 0 { + idx += ignorableLen + continue + } + break + } + return idx +} + +func toolMarkupIgnorableLenAt(text string, idx int) int { + if idx < 0 || idx >= len(text) { + return 0 + } + r, size := utf8.DecodeRuneInString(text[idx:]) + if size <= 0 { + return 0 + } + if unicode.Is(unicode.Cf, r) { + return size + } + if unicode.IsControl(r) && !unicode.IsSpace(r) { + return size + } + return 0 +} + +func toolMarkupEqualsLenAt(text string, idx int) int { + idx = skipToolMarkupIgnorables(text, idx) + if idx < 0 || idx >= len(text) { + return 0 + } + switch { + case text[idx] == '=': + return 1 + case strings.HasPrefix(text[idx:], "="): + return len("=") + case strings.HasPrefix(text[idx:], "﹦"): + return len("﹦") + case strings.HasPrefix(text[idx:], "꞊"): + return len("꞊") + default: + return 0 + } +} + +func toolMarkupDashLenAt(text string, idx int) int { + idx = skipToolMarkupIgnorables(text, idx) + if idx < 0 || idx >= len(text) { + return 0 + } + switch { + case text[idx] == '-': + return 1 + case strings.HasPrefix(text[idx:], "‐"): + return len("‐") + case strings.HasPrefix(text[idx:], "‑"): + return len("‑") + case strings.HasPrefix(text[idx:], "‒"): + return len("‒") + case strings.HasPrefix(text[idx:], "–"): + return len("–") + case strings.HasPrefix(text[idx:], "—"): + return len("—") + case strings.HasPrefix(text[idx:], "―"): + return len("―") + case strings.HasPrefix(text[idx:], "−"): + return len("−") + case strings.HasPrefix(text[idx:], "﹣"): + return len("﹣") + case strings.HasPrefix(text[idx:], "-"): + return len("-") + default: + return 0 + } +} + +func toolMarkupUnderscoreLenAt(text string, idx int) int { + idx = skipToolMarkupIgnorables(text, idx) + if idx < 0 || idx >= len(text) { + return 0 + } + switch { + case text[idx] == '_': + return 1 + case strings.HasPrefix(text[idx:], "_"): + return len("_") + case strings.HasPrefix(text[idx:], "﹍"): + return len("﹍") + case strings.HasPrefix(text[idx:], "﹎"): + return len("﹎") + case strings.HasPrefix(text[idx:], "﹏"): + return len("﹏") + default: + return 0 + } +} + +func consumeToolKeyword(text string, idx int, keyword string) (int, bool) { + next := idx + for i := 0; i < len(keyword); i++ { + next = skipToolMarkupIgnorables(text, next) + if next >= len(text) { + return idx, false + } + target := asciiLower(keyword[i]) + switch target { + case '_': + if underscoreLen := toolMarkupUnderscoreLenAt(text, next); underscoreLen > 0 { + next += underscoreLen + continue + } + return idx, false + case '-': + if dashLen := toolMarkupDashLenAt(text, next); dashLen > 0 { + next += dashLen + continue + } + return idx, false + default: + r, size := utf8.DecodeRuneInString(text[next:]) + if size <= 0 { + return idx, false + } + folded, ok := foldToolKeywordRune(r) + if !ok || folded != target { + return idx, false + } + next += size + } + } + return next, true +} + +func foldToolKeywordRune(r rune) (byte, bool) { + if r >= 'A' && r <= 'Z' { + r = r - 'A' + 'A' + } + if r >= 'a' && r <= 'z' { + r = r - 'a' + 'a' + } + r = unicode.ToLower(r) + switch r { + case 'a', 'c', 'd', 'e', 'i', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'v': + return byte(r), true + case 'а', 'Α', 'α': + return 'a', true + case 'с', 'С', 'ϲ', 'Ϲ': + return 'c', true + case 'ԁ', 'ⅾ': + return 'd', true + case 'е', 'Е', 'Ε', 'ε': + return 'e', true + case 'і', 'І', 'Ι', 'ι', 'ı': + return 'i', true + case 'к', 'К', 'Κ', 'κ': + return 'k', true + case 'ⅼ': + return 'l', true + case 'м', 'М', 'Μ', 'μ': + return 'm', true + case 'ո': + return 'n', true + case 'о', 'О', 'Ο', 'ο': + return 'o', true + case 'р', 'Р', 'Ρ', 'ρ': + return 'p', true + case 'ѕ', 'Ѕ': + return 's', true + case 'т', 'Т', 'Τ', 'τ': + return 't', true + case 'ν', 'Ν', 'ѵ', 'ⅴ': + return 'v', true + default: + return 0, false + } +} + +func toolMarkupWhitespaceLikeLenAt(text string, idx int) int { + idx = skipToolMarkupIgnorables(text, idx) + if idx < 0 || idx >= len(text) { + return 0 + } + switch text[idx] { + case ' ', '\t', '\n', '\r': + return 1 + } + if strings.HasPrefix(text[idx:], "▁") { + return len("▁") + } + r, size := utf8.DecodeRuneInString(text[idx:]) + if size > 0 && unicode.IsSpace(r) { + return size + } + return 0 +} + +func consumeToolMarkupPipe(text string, idx int) (int, bool) { + idx = skipToolMarkupIgnorables(text, idx) + if idx >= len(text) { + return idx, false + } + switch { + case text[idx] == '|': + return idx + 1, true + case strings.HasPrefix(text[idx:], "│"): + return idx + len("│"), true + case strings.HasPrefix(text[idx:], "∣"): + return idx + len("∣"), true + case strings.HasPrefix(text[idx:], "❘"): + return idx + len("❘"), true + case strings.HasPrefix(text[idx:], "ǀ"): + return idx + len("ǀ"), true + case strings.HasPrefix(text[idx:], "│"): + return idx + len("│"), true + default: + return idx, false + } +} + +func consumeToolMarkupClosingSlash(text string, idx int) (int, bool) { + idx = skipToolMarkupIgnorables(text, idx) + if idx >= len(text) { + return idx, false + } + switch { + case text[idx] == '/': + return idx + 1, true + case strings.HasPrefix(text[idx:], "/"): + return idx + len("/"), true + case strings.HasPrefix(text[idx:], "∕"): + return idx + len("∕"), true + case strings.HasPrefix(text[idx:], "⁄"): + return idx + len("⁄"), true + case strings.HasPrefix(text[idx:], "⧸"): + return idx + len("⧸"), true + default: + return idx, false + } +} + +func xmlTagStartDelimiterLenAt(text string, idx int) int { + idx = skipToolMarkupIgnorables(text, idx) + if idx < 0 || idx >= len(text) { + return 0 + } + switch { + case text[idx] == '<': + return 1 + case strings.HasPrefix(text[idx:], "<"): + return len("<") + case strings.HasPrefix(text[idx:], "﹤"): + return len("﹤") + case strings.HasPrefix(text[idx:], "〈"): + return len("〈") + default: + return 0 + } +} + +func xmlTagEndDelimiterLenAt(text string, idx int) int { + idx = skipToolMarkupIgnorables(text, idx) + if idx < 0 || idx >= len(text) { + return 0 + } + switch { + case text[idx] == '>': + return 1 + case strings.HasPrefix(text[idx:], ">"): + return len(">") + case strings.HasPrefix(text[idx:], "﹥"): + return len("﹥") + case strings.HasPrefix(text[idx:], "〉"): + return len("〉") + default: + return 0 + } +} + +func xmlTagEndDelimiterLenEndingAt(text string, end int) int { + if end < 0 || end >= len(text) { + return 0 + } + if text[end] == '>' { + return 1 + } + if end+1 >= len(">") && text[end+1-len(">"):end+1] == ">" { + return len(">") + } + return 0 +} + +func xmlQuotePairAt(text string, idx int) (string, int) { + idx = skipToolMarkupIgnorables(text, idx) + if idx < 0 || idx >= len(text) { + return "", 0 + } + switch { + case text[idx] == '"': + return `"`, 1 + case text[idx] == '\'': + return `'`, 1 + case strings.HasPrefix(text[idx:], "“"): + return "”", len("“") + case strings.HasPrefix(text[idx:], "‘"): + return "’", len("‘") + case strings.HasPrefix(text[idx:], """): + return """, len(""") + case strings.HasPrefix(text[idx:], "'"): + return "'", len("'") + case strings.HasPrefix(text[idx:], "„"): + return "”", len("„") + case strings.HasPrefix(text[idx:], "‟"): + return "”", len("‟") + default: + return "", 0 + } +} + +func xmlQuoteCloseDelimiterLenAt(text string, idx int, quote string) int { + if quote == "" || idx < 0 || idx >= len(text) { + return 0 + } + if strings.HasPrefix(text[idx:], quote) { + return len(quote) + } + return 0 +} + +func hasRepairableXMLToolCallsWrapper(text string) bool { + if strings.TrimSpace(text) == "" { + return false + } + if _, ok := firstToolMarkupTagByName(text, "tool_calls", false); ok { + return false + } + invokeTag, ok := firstToolMarkupTagByName(text, "invoke", false) + if !ok { + return false + } + closeTag, ok := lastToolMarkupTagByName(text, "tool_calls", true) + if !ok { + return false + } + return invokeTag.Start < closeTag.Start +} + +func toolCDATAOpenLenAt(text string, idx int) int { + start := skipToolMarkupIgnorables(text, idx) + ltLen := xmlTagStartDelimiterLenAt(text, start) + if ltLen == 0 { + return 0 + } + pos := start + ltLen + for skipped := 0; skipped <= 4 && pos < len(text); skipped++ { + pos = skipToolMarkupIgnorables(text, pos) + if pos >= len(text) { + return 0 + } + if text[pos] == '[' { + pos++ + next, ok := consumeToolKeyword(text, pos, "cdata") + if !ok { + return 0 + } + pos = skipToolMarkupIgnorables(text, next) + if pos >= len(text) || text[pos] != '[' { + return 0 + } + pos++ + return pos - idx + } + r, size := utf8.DecodeRuneInString(text[pos:]) + if size <= 0 || !isToolMarkupSeparator(r) { + return 0 + } + pos += size + } + return 0 +} + +func indexToolCDATAOpen(text string, start int) int { + for i := maxInt(start, 0); i < len(text); i++ { + if toolCDATAOpenLenAt(text, i) > 0 { + return i + } + } + return -1 +} + +func findTrailingToolCDATACloseStart(text string) int { + for i := len(text) - 1; i >= 0; i-- { + if closeLen := toolCDATACloseLenAt(text, i); closeLen > 0 && i+closeLen == len(text) { + return i + } + } + return -1 +} diff --git a/internal/toolcall/toolcalls_dsml.go b/internal/toolcall/toolcalls_dsml.go new file mode 100644 index 0000000000000000000000000000000000000000..5217c5dc0ec09a4337ef94726b161f15d8d62c77 --- /dev/null +++ b/internal/toolcall/toolcalls_dsml.go @@ -0,0 +1,62 @@ +package toolcall + +import ( + "strings" +) + +func normalizeDSMLToolCallMarkup(text string) (string, bool) { + if text == "" { + return "", true + } + canonicalized := canonicalizeToolCallCandidateSpans(text) + hasDSMLLikeMarkup, hasCanonicalMarkup := ContainsToolMarkupSyntaxOutsideIgnored(canonicalized) + if !hasDSMLLikeMarkup && !hasCanonicalMarkup { + return canonicalized, true + } + return rewriteDSMLToolMarkupOutsideIgnored(canonicalized), true +} + +func rewriteDSMLToolMarkupOutsideIgnored(text string) string { + if text == "" { + return "" + } + var b strings.Builder + b.Grow(len(text)) + for i := 0; i < len(text); { + next, advanced, blocked := skipXMLIgnoredSection(text, i) + if blocked { + b.WriteString(text[i:]) + break + } + if advanced { + b.WriteString(text[i:next]) + i = next + continue + } + if end, ok := markdownCodeSpanEnd(text, i); ok { + b.WriteString(text[i:end]) + i = end + continue + } + tag, ok := scanToolMarkupTagAt(text, i) + if !ok { + b.WriteByte(text[i]) + i++ + continue + } + b.WriteByte('<') + if tag.Closing { + b.WriteByte('/') + } + b.WriteString(tag.Name) + if delimLen := xmlTagEndDelimiterLenEndingAt(text, tag.End); delimLen > 0 { + b.WriteString(text[tag.NameEnd : tag.End+1-delimLen]) + b.WriteByte('>') + } else { + b.WriteString(text[tag.NameEnd : tag.End+1]) + b.WriteByte('>') + } + i = tag.End + 1 + } + return b.String() +} diff --git a/internal/toolcall/toolcalls_format.go b/internal/toolcall/toolcalls_format.go new file mode 100644 index 0000000000000000000000000000000000000000..9f7d001f672e91c835acffa959b38dd56374ebab --- /dev/null +++ b/internal/toolcall/toolcalls_format.go @@ -0,0 +1,43 @@ +package toolcall + +import ( + "encoding/json" + "strings" + + "github.com/google/uuid" +) + +func FormatOpenAIToolCalls(calls []ParsedToolCall, toolsRaw any) []map[string]any { + normalized := NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + out := make([]map[string]any, 0, len(calls)) + for _, c := range normalized { + args, _ := json.Marshal(c.Input) + out = append(out, map[string]any{ + "id": "call_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + "type": "function", + "function": map[string]any{ + "name": c.Name, + "arguments": string(args), + }, + }) + } + return out +} + +func FormatOpenAIStreamToolCalls(calls []ParsedToolCall, toolsRaw any) []map[string]any { + normalized := NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + out := make([]map[string]any, 0, len(calls)) + for i, c := range normalized { + args, _ := json.Marshal(c.Input) + out = append(out, map[string]any{ + "index": i, + "id": "call_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + "type": "function", + "function": map[string]any{ + "name": c.Name, + "arguments": string(args), + }, + }) + } + return out +} diff --git a/internal/toolcall/toolcalls_input_parse.go b/internal/toolcall/toolcalls_input_parse.go new file mode 100644 index 0000000000000000000000000000000000000000..4b7ef7e5530bf0cdc8b12e32b7542e8865848b36 --- /dev/null +++ b/internal/toolcall/toolcalls_input_parse.go @@ -0,0 +1,109 @@ +package toolcall + +import ( + "encoding/json" + "html" + "strings" + "unicode" +) + +func parseToolCallInput(v any) map[string]any { + switch x := v.(type) { + case nil: + return map[string]any{} + case map[string]any: + return x + case string: + raw := strings.TrimSpace(html.UnescapeString(x)) + if raw == "" { + return map[string]any{} + } + var parsed map[string]any + if err := json.Unmarshal([]byte(raw), &parsed); err == nil && parsed != nil { + repairPathLikeControlChars(parsed) + return parsed + } + // Try to repair invalid backslashes (common in Windows paths output by models) + repaired := repairInvalidJSONBackslashes(raw) + if repaired != raw { + if err := json.Unmarshal([]byte(repaired), &parsed); err == nil && parsed != nil { + repairPathLikeControlChars(parsed) + return parsed + } + } + // Try to repair loose JSON in string argument as well + repairedLoose := RepairLooseJSON(raw) + if repairedLoose != raw { + if err := json.Unmarshal([]byte(repairedLoose), &parsed); err == nil && parsed != nil { + repairPathLikeControlChars(parsed) + return parsed + } + } + return map[string]any{"_raw": raw} + default: + b, err := json.Marshal(x) + if err != nil { + return map[string]any{} + } + var parsed map[string]any + if err := json.Unmarshal(b, &parsed); err == nil && parsed != nil { + return parsed + } + return map[string]any{} + } +} + +func repairPathLikeControlChars(m map[string]any) { + for k, v := range m { + switch vv := v.(type) { + case map[string]any: + repairPathLikeControlChars(vv) + case []any: + for _, item := range vv { + if child, ok := item.(map[string]any); ok { + repairPathLikeControlChars(child) + } + } + case string: + if isPathLikeKey(k) && containsControlRune(vv) { + m[k] = escapeControlRunes(vv) + } + } + } +} + +func isPathLikeKey(key string) bool { + k := strings.ToLower(strings.TrimSpace(key)) + return strings.Contains(k, "path") || strings.Contains(k, "file") +} + +func containsControlRune(s string) bool { + for _, r := range s { + if unicode.IsControl(r) { + return true + } + } + return false +} + +func escapeControlRunes(s string) string { + var b strings.Builder + b.Grow(len(s) + 8) + for _, r := range s { + switch r { + case '\b': + b.WriteString(`\b`) + case '\f': + b.WriteString(`\f`) + case '\n': + b.WriteString(`\n`) + case '\r': + b.WriteString(`\r`) + case '\t': + b.WriteString(`\t`) + default: + b.WriteRune(r) + } + } + return b.String() +} diff --git a/internal/toolcall/toolcalls_json_repair.go b/internal/toolcall/toolcalls_json_repair.go new file mode 100644 index 0000000000000000000000000000000000000000..884d26ffac15e4f10cf8c45182eb06aed1774fab --- /dev/null +++ b/internal/toolcall/toolcalls_json_repair.go @@ -0,0 +1,79 @@ +package toolcall + +import ( + "regexp" + "strings" +) + +func repairInvalidJSONBackslashes(s string) string { + if !strings.Contains(s, "\\") { + return s + } + var out strings.Builder + out.Grow(len(s) + 10) + runes := []rune(s) + for i := 0; i < len(runes); i++ { + if runes[i] == '\\' { + if i+1 < len(runes) { + next := runes[i+1] + switch next { + case '"', '\\', '/', 'b', 'f', 'n', 'r', 't': + out.WriteRune('\\') + out.WriteRune(next) + i++ + continue + case 'u': + if i+5 < len(runes) { + isHex := true + for j := 1; j <= 4; j++ { + r := runes[i+1+j] + if (r < '0' || r > '9') && (r < 'a' || r > 'f') && (r < 'A' || r > 'F') { + isHex = false + break + } + } + if isHex { + out.WriteRune('\\') + out.WriteRune('u') + for j := 1; j <= 4; j++ { + out.WriteRune(runes[i+1+j]) + } + i += 5 + continue + } + } + } + } + // Not a valid escape sequence, double it + out.WriteString("\\\\") + } else { + out.WriteRune(runes[i]) + } + } + return out.String() +} + +var unquotedKeyPattern = regexp.MustCompile(`([{,]\s*)([a-zA-Z_][a-zA-Z0-9_]*)\s*:`) + +// missingArrayBracketsPattern identifies a sequence of two or more JSON objects separated by commas +// that immediately follow a colon, which indicates a missing array bracket `[` `]`. +// E.g., "key": {"a": 1}, {"b": 2} -> "key": [{"a": 1}, {"b": 2}] +// NOTE: The pattern uses (?:[^{}]|\{[^{}]*\})* to support single-level nested {} objects, +// which handles cases like {"content": "x", "input": {"q": "y"}} +var missingArrayBracketsPattern = regexp.MustCompile(`(:\s*)(\{(?:[^{}]|\{[^{}]*\})*\}(?:\s*,\s*\{(?:[^{}]|\{[^{}]*\})*\})+)`) + +func RepairLooseJSON(s string) string { + s = strings.TrimSpace(s) + if s == "" { + return s + } + // 1. Replace unquoted keys: {key: -> {"key": + s = unquotedKeyPattern.ReplaceAllString(s, `$1"$2":`) + + // 2. Heuristic: Fix missing array brackets for list of objects + // e.g., : {obj1}, {obj2} -> : [{obj1}, {obj2}] + // This specifically addresses DeepSeek's "list hallucination" + s = missingArrayBracketsPattern.ReplaceAllString(s, `$1[$2]`) + + return s +} diff --git a/internal/toolcall/toolcalls_markup.go b/internal/toolcall/toolcalls_markup.go new file mode 100644 index 0000000000000000000000000000000000000000..fc457317a2cee39607a7422f7465dace4794143c --- /dev/null +++ b/internal/toolcall/toolcalls_markup.go @@ -0,0 +1,178 @@ +package toolcall + +import ( + "encoding/json" + "html" + "regexp" + "strings" +) + +var toolCallMarkupKVPattern = regexp.MustCompile(`(?is)<(?:[a-z0-9_:-]+:)?([a-z0-9_\-.]+)\b[^>]*>(.*?)`) + +func parseMarkupKVObject(text string) map[string]any { + matches := toolCallMarkupKVPattern.FindAllStringSubmatch(strings.TrimSpace(text), -1) + if len(matches) == 0 { + return nil + } + out := map[string]any{} + for _, m := range matches { + if len(m) < 4 { + continue + } + key := strings.TrimSpace(m[1]) + endKey := strings.TrimSpace(m[3]) + if key == "" { + continue + } + if !strings.EqualFold(key, endKey) { + continue + } + value := parseMarkupValue(m[2]) + if value == nil { + continue + } + appendMarkupValue(out, key, value) + } + if len(out) == 0 { + return nil + } + return out +} + +func parseMarkupValue(inner string) any { + if value, ok := extractStandaloneCDATA(inner); ok { + return value + } + value := strings.TrimSpace(extractRawTagValue(inner)) + if value == "" { + return "" + } + + if strings.Contains(value, "<") && strings.Contains(value, ">") { + if parsed := parseStructuredToolCallInput(value); len(parsed) > 0 { + if len(parsed) == 1 { + if raw, ok := parsed["_raw"].(string); ok { + return raw + } + } + return parsed + } + } + + var jsonValue any + if json.Unmarshal([]byte(value), &jsonValue) == nil { + return jsonValue + } + return value +} + +func appendMarkupValue(out map[string]any, key string, value any) { + if existing, ok := out[key]; ok { + switch current := existing.(type) { + case []any: + out[key] = append(current, value) + default: + out[key] = []any{current, value} + } + return + } + out[key] = value +} + +// extractRawTagValue treats the inner content of a tag robustly. +// It detects CDATA and strips it, otherwise it unescapes standard HTML entities. +// It avoids over-aggressive tag stripping that might break user content. +func extractRawTagValue(inner string) string { + trimmed := strings.TrimSpace(inner) + if trimmed == "" { + return "" + } + + // 1. Check for CDATA - if present, it's the ultimate "safe" container. + if value, ok := extractStandaloneCDATA(trimmed); ok { + return value // Return raw content between CDATA brackets + } + + // 2. If no CDATA, we still want to be robust. + // We unescape standard HTML entities (like < > &) + // but we DON'T recursively strip tags unless they are actually valid XML tags + // at the start/end (which should have been handled by the outer matcher anyway). + + // If it contains what looks like a single tag and no other text, it might be nested XML + // but for KV objects we usually want the value. + return html.UnescapeString(inner) +} + +func extractStandaloneCDATA(inner string) (string, bool) { + trimmed := strings.TrimSpace(inner) + if openLen := toolCDATAOpenLenAt(trimmed, 0); openLen > 0 { + if closeStart := findTrailingToolCDATACloseStart(trimmed); closeStart >= openLen { + return trimmed[openLen:closeStart], true + } + if end := findToolCDATAEnd(trimmed, openLen); end >= 0 { + return trimmed[openLen:end], true + } + return trimmed[openLen:], true + } + return "", false +} + +func parseJSONLiteralValue(raw string) (any, bool) { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return nil, false + } + + switch trimmed[0] { + case '{', '[', '"', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 't', 'f', 'n': + default: + return nil, false + } + + var parsed any + if err := json.Unmarshal([]byte(trimmed), &parsed); err != nil { + return nil, false + } + return parsed, true +} + +// SanitizeLooseCDATA repairs malformed trailing CDATA openings just enough for +// final parsing and flush-time recovery. Properly closed CDATA blocks are left +// untouched; an unclosed opener is stripped so the remaining text can still be +// parsed as part of the surrounding tool markup. +func SanitizeLooseCDATA(text string) string { + if text == "" { + return "" + } + + var b strings.Builder + b.Grow(len(text)) + changed := false + pos := 0 + for pos < len(text) { + start := indexToolCDATAOpen(text, pos) + if start < 0 { + b.WriteString(text[pos:]) + break + } + openLen := toolCDATAOpenLenAt(text, start) + contentStart := start + openLen + b.WriteString(text[pos:start]) + + if endRel := findToolCDATAEnd(text, contentStart); endRel >= 0 { + end := endRel + toolCDATACloseLenAt(text, endRel) + b.WriteString(text[start:end]) + pos = end + continue + } + + changed = true + b.WriteString(text[contentStart:]) + pos = len(text) + } + + if !changed { + return text + } + return b.String() +} diff --git a/internal/toolcall/toolcalls_parse.go b/internal/toolcall/toolcalls_parse.go new file mode 100644 index 0000000000000000000000000000000000000000..04bc2133fe058fac07256f03b0e90b8f59f2fd56 --- /dev/null +++ b/internal/toolcall/toolcalls_parse.go @@ -0,0 +1,316 @@ +package toolcall + +import ( + "strings" +) + +type ParsedToolCall struct { + Name string `json:"name"` + Input map[string]any `json:"input"` +} + +type ToolCallParseResult struct { + Calls []ParsedToolCall + SawToolCallSyntax bool + RejectedByPolicy bool + RejectedToolNames []string +} + +func ParseToolCalls(text string, availableToolNames []string) []ParsedToolCall { + return ParseToolCallsDetailed(text, availableToolNames).Calls +} + +func ParseToolCallsDetailed(text string, availableToolNames []string) ToolCallParseResult { + return parseToolCallsDetailedXMLOnly(text) +} + +func ParseStandaloneToolCalls(text string, availableToolNames []string) []ParsedToolCall { + return ParseStandaloneToolCallsDetailed(text, availableToolNames).Calls +} + +func ParseStandaloneToolCallsDetailed(text string, availableToolNames []string) ToolCallParseResult { + return parseToolCallsDetailedXMLOnly(text) +} + +func ParseAssistantToolCallsDetailed(text, thinking string, availableToolNames []string) ToolCallParseResult { + textParsed := ParseStandaloneToolCallsDetailed(text, availableToolNames) + if len(textParsed.Calls) > 0 { + return textParsed + } + if strings.TrimSpace(text) != "" { + return textParsed + } + thinkingParsed := ParseStandaloneToolCallsDetailed(thinking, availableToolNames) + if len(thinkingParsed.Calls) > 0 { + return thinkingParsed + } + return textParsed +} + +func parseToolCallsDetailedXMLOnly(text string) ToolCallParseResult { + result := ToolCallParseResult{} + trimmed := strings.TrimSpace(text) + if trimmed == "" { + return result + } + trimmed = stripFencedCodeBlocks(trimmed) + trimmed = strings.TrimSpace(trimmed) + if trimmed == "" { + return result + } + + normalized, ok := normalizeDSMLToolCallMarkup(trimmed) + if !ok { + return result + } + result.SawToolCallSyntax = looksLikeToolCallSyntax(normalized) || hasRepairableXMLToolCallsWrapper(normalized) + parsed := parseXMLToolCalls(normalized) + if len(parsed) == 0 && indexToolCDATAOpen(normalized, 0) >= 0 { + recovered := SanitizeLooseCDATA(normalized) + if recovered != normalized { + parsed = parseXMLToolCalls(recovered) + } + } + if len(parsed) == 0 { + return result + } + + result.SawToolCallSyntax = true + calls, rejectedNames := filterToolCallsDetailed(parsed) + result.Calls = calls + result.RejectedToolNames = rejectedNames + result.RejectedByPolicy = len(rejectedNames) > 0 && len(calls) == 0 + return result +} + +func filterToolCallsDetailed(parsed []ParsedToolCall) ([]ParsedToolCall, []string) { + out := make([]ParsedToolCall, 0, len(parsed)) + for _, tc := range parsed { + if tc.Name == "" { + continue + } + if tc.Input == nil { + tc.Input = map[string]any{} + } + out = append(out, tc) + } + return out, nil +} + +func looksLikeToolCallSyntax(text string) bool { + hasDSML, hasCanonical := ContainsToolCallWrapperSyntaxOutsideIgnored(text) + return hasDSML || hasCanonical +} + +func stripFencedCodeBlocks(text string) string { + if text == "" { + return "" + } + var b strings.Builder + b.Grow(len(text)) + + lines := strings.SplitAfter(text, "\n") + inFence := false + fenceMarker := "" + inCDATA := false + cdataFenceMarker := "" + // Track builder length when a fence opens so we can preserve content + // collected before the unclosed fence. + beforeFenceLen := 0 + for _, line := range lines { + if inCDATA || cdataStartsBeforeFence(line) { + b.WriteString(line) + inCDATA, cdataFenceMarker = updateCDATAStateForStrip(inCDATA, cdataFenceMarker, line) + continue + } + trimmed := strings.TrimLeft(line, " \t") + if !inFence { + if marker, ok := parseFenceOpen(trimmed); ok { + inFence = true + fenceMarker = marker + beforeFenceLen = b.Len() + continue + } + b.WriteString(line) + continue + } + + if isFenceClose(trimmed, fenceMarker) { + inFence = false + fenceMarker = "" + } + } + + if inFence { + // Unclosed fence: preserve content that was collected before the + // fence started rather than dropping everything. + result := b.String() + if beforeFenceLen > 0 && beforeFenceLen <= len(result) { + return result[:beforeFenceLen] + } + return "" + } + return b.String() +} + +func markdownCodeSpanEnd(text string, start int) (int, bool) { + if start < 0 || start >= len(text) || text[start] != '`' { + return start, false + } + count := countLeadingFenceChars(text[start:], '`') + if count == 0 { + return start, false + } + search := start + count + for search < len(text) { + if text[search] != '`' { + search++ + continue + } + run := countLeadingFenceChars(text[search:], '`') + if run == count { + return search + run, true + } + search += run + } + return start, false +} + +func cdataStartsBeforeFence(line string) bool { + cdataIdx := indexToolCDATAOpen(line, 0) + if cdataIdx < 0 { + return false + } + fenceIdx := firstFenceMarkerIndex(line) + return fenceIdx < 0 || cdataIdx < fenceIdx +} + +func firstFenceMarkerIndex(line string) int { + idxBacktick := strings.Index(line, "```") + idxTilde := strings.Index(line, "~~~") + switch { + case idxBacktick < 0: + return idxTilde + case idxTilde < 0: + return idxBacktick + case idxBacktick < idxTilde: + return idxBacktick + default: + return idxTilde + } +} + +func updateCDATAStateForStrip(inCDATA bool, cdataFenceMarker, line string) (bool, string) { + pos := 0 + state := inCDATA + fenceMarker := cdataFenceMarker + lineForFence := line + if !state { + start := indexToolCDATAOpen(line, pos) + if start < 0 { + return false, "" + } + pos = start + toolCDATAOpenLenAt(line, start) + if pos > len(line) { + pos = len(line) + } + state = true + lineForFence = line[pos:] + } + if !state { + return false, "" + } + + trimmed := strings.TrimLeft(lineForFence, " \t") + if fenceMarker == "" { + if marker, ok := parseFenceOpen(trimmed); ok { + fenceMarker = marker + } + } else if isFenceClose(trimmed, fenceMarker) { + fenceMarker = "" + } + + for pos < len(line) { + endPos := -1 + closeLen := 0 + for search := pos; search < len(line); search++ { + if foundLen := toolCDATACloseLenAt(line, search); foundLen > 0 { + endPos = search + closeLen = foundLen + break + } + } + if endPos < 0 { + return true, fenceMarker + } + pos = endPos + closeLen + if pos > len(line) { + pos = len(line) + } + if fenceMarker != "" { + continue + } + if cdataEndLooksStructural(line, pos) || strings.TrimSpace(line[pos:]) == "" { + state = false + for pos < len(line) { + start := indexToolCDATAOpen(line, pos) + if start < 0 { + return false, "" + } + pos = start + toolCDATAOpenLenAt(line, start) + if pos > len(line) { + pos = len(line) + } + state = true + trimmedTail := strings.TrimLeft(line[pos:], " \t") + if marker, ok := parseFenceOpen(trimmedTail); ok { + fenceMarker = marker + } else { + fenceMarker = "" + } + break + } + continue + } + } + return state, fenceMarker +} + +func parseFenceOpen(line string) (string, bool) { + if len(line) < 3 { + return "", false + } + ch := line[0] + if ch != '`' && ch != '~' { + return "", false + } + count := countLeadingFenceChars(line, ch) + if count < 3 { + return "", false + } + return strings.Repeat(string(ch), count), true +} + +func isFenceClose(line, marker string) bool { + if marker == "" { + return false + } + ch := marker[0] + if line == "" || line[0] != ch { + return false + } + count := countLeadingFenceChars(line, ch) + if count < len(marker) { + return false + } + rest := strings.TrimSpace(line[count:]) + return rest == "" +} + +func countLeadingFenceChars(line string, ch byte) int { + count := 0 + for count < len(line) && line[count] == ch { + count++ + } + return count +} diff --git a/internal/toolcall/toolcalls_parse_markup.go b/internal/toolcall/toolcalls_parse_markup.go new file mode 100644 index 0000000000000000000000000000000000000000..eab642e7d3bcaf9b5249b0dc328b5fc08a3f97ad --- /dev/null +++ b/internal/toolcall/toolcalls_parse_markup.go @@ -0,0 +1,683 @@ +package toolcall + +import ( + "encoding/json" + "encoding/xml" + "html" + "regexp" + "strings" + "unicode/utf8" +) + +var xmlAttrPattern = regexp.MustCompile(`(?is)\b([a-z0-9_:-]+)\s*=\s*("([^"]*)"|'([^']*)')`) +var cdataBRSeparatorPattern = regexp.MustCompile(`(?i)`) + +func parseXMLToolCalls(text string) []ParsedToolCall { + wrappers := findToolCallElementBlocksOutsideIgnored(text) + if len(wrappers) == 0 { + repaired := repairMissingXMLToolCallsOpeningWrapper(text) + if repaired != text { + wrappers = findToolCallElementBlocksOutsideIgnored(repaired) + } + } + if len(wrappers) == 0 { + return nil + } + out := make([]ParsedToolCall, 0, len(wrappers)) + for _, wrapper := range wrappers { + for _, block := range findXMLElementBlocks(wrapper.Body, "invoke") { + call, ok := parseSingleXMLToolCall(block) + if !ok { + continue + } + out = append(out, call) + } + } + if len(out) == 0 { + return nil + } + return out +} + +func findToolCallElementBlocksOutsideIgnored(text string) []xmlElementBlock { + if text == "" { + return nil + } + var out []xmlElementBlock + for searchFrom := 0; searchFrom < len(text); { + tag, ok := FindToolMarkupTagOutsideIgnored(text, searchFrom) + if !ok { + break + } + if tag.Closing || tag.Name != "tool_calls" { + searchFrom = tag.End + 1 + continue + } + closeTag, ok := FindMatchingToolMarkupClose(text, tag) + if !ok { + searchFrom = tag.End + 1 + continue + } + attrsEnd := tag.End + 1 + if delimLen := xmlTagEndDelimiterLenEndingAt(text, tag.End); delimLen > 0 { + attrsEnd = tag.End + 1 - delimLen + } + out = append(out, xmlElementBlock{ + Attrs: text[tag.NameEnd:attrsEnd], + Body: text[tag.End+1 : closeTag.Start], + Start: tag.Start, + End: closeTag.End + 1, + }) + searchFrom = closeTag.End + 1 + } + return out +} + +func repairMissingXMLToolCallsOpeningWrapper(text string) string { + if _, ok := firstToolMarkupTagByName(text, "tool_calls", false); ok { + return text + } + + invokeTag, ok := firstToolMarkupTagByName(text, "invoke", false) + if !ok { + return text + } + closeTag, ok := lastToolMarkupTagByName(text, "tool_calls", true) + if !ok || invokeTag.Start >= closeTag.Start { + return text + } + + return text[:invokeTag.Start] + "" + text[invokeTag.Start:closeTag.Start] + "" + text[closeTag.End+1:] +} + +func firstToolMarkupTagByName(text, name string, closing bool) (ToolMarkupTag, bool) { + for searchFrom := 0; searchFrom < len(text); { + tag, ok := FindToolMarkupTagOutsideIgnored(text, searchFrom) + if !ok { + break + } + if tag.Name == name && tag.Closing == closing { + return tag, true + } + searchFrom = tag.End + 1 + } + return ToolMarkupTag{}, false +} + +func lastToolMarkupTagByName(text, name string, closing bool) (ToolMarkupTag, bool) { + var last ToolMarkupTag + found := false + for searchFrom := 0; searchFrom < len(text); { + tag, ok := FindToolMarkupTagOutsideIgnored(text, searchFrom) + if !ok { + break + } + if tag.Name == name && tag.Closing == closing { + last = tag + found = true + } + searchFrom = tag.End + 1 + } + if !found { + return ToolMarkupTag{}, false + } + return last, true +} + +func parseSingleXMLToolCall(block xmlElementBlock) (ParsedToolCall, bool) { + attrs := parseXMLTagAttributes(block.Attrs) + name := strings.TrimSpace(html.UnescapeString(attrs["name"])) + if name == "" { + return ParsedToolCall{}, false + } + + inner := strings.TrimSpace(block.Body) + if strings.HasPrefix(inner, "{") { + var payload map[string]any + if err := json.Unmarshal([]byte(inner), &payload); err == nil { + input := map[string]any{} + if params, ok := payload["input"].(map[string]any); ok { + input = params + } + if len(input) == 0 { + if params, ok := payload["parameters"].(map[string]any); ok { + input = params + } + } + return ParsedToolCall{Name: name, Input: input}, true + } + } + + input := map[string]any{} + for _, paramMatch := range findXMLElementBlocks(inner, "parameter") { + paramAttrs := parseXMLTagAttributes(paramMatch.Attrs) + paramName := strings.TrimSpace(html.UnescapeString(paramAttrs["name"])) + if paramName == "" { + continue + } + value := parseInvokeParameterValue(paramName, paramMatch.Body) + appendMarkupValue(input, paramName, value) + } + + if len(input) == 0 { + if strings.TrimSpace(inner) != "" { + return ParsedToolCall{}, false + } + return ParsedToolCall{Name: name, Input: map[string]any{}}, true + } + return ParsedToolCall{Name: name, Input: input}, true +} + +type xmlElementBlock struct { + Attrs string + Body string + Start int + End int +} + +func findXMLElementBlocks(text, tag string) []xmlElementBlock { + if text == "" || tag == "" { + return nil + } + var out []xmlElementBlock + pos := 0 + for pos < len(text) { + start, bodyStart, attrs, ok := findXMLStartTagOutsideCDATA(text, tag, pos) + if !ok { + break + } + closeStart, closeEnd, ok := findMatchingXMLEndTagOutsideCDATA(text, tag, bodyStart) + if !ok { + pos = bodyStart + continue + } + out = append(out, xmlElementBlock{ + Attrs: attrs, + Body: text[bodyStart:closeStart], + Start: start, + End: closeEnd, + }) + pos = closeEnd + } + return out +} + +func findXMLStartTagOutsideCDATA(text, tag string, from int) (start, bodyStart int, attrs string, ok bool) { + target := "<" + strings.ToLower(tag) + for i := maxInt(from, 0); i < len(text); { + next, advanced, blocked := skipXMLIgnoredSection(text, i) + if blocked { + return -1, -1, "", false + } + if advanced { + i = next + continue + } + if hasASCIIPrefixFoldAt(text, i, target) && hasXMLTagBoundary(text, i+len(target)) { + end := findXMLTagEnd(text, i+len(target)) + if end < 0 { + return -1, -1, "", false + } + return i, end + 1, text[i+len(target) : end], true + } + i++ + } + return -1, -1, "", false +} + +func findMatchingXMLEndTagOutsideCDATA(text, tag string, from int) (closeStart, closeEnd int, ok bool) { + openTarget := "<" + strings.ToLower(tag) + closeTarget := "= len(text) { + return i, false, false + } + if bodyStart, ok := matchToolCDATAOpenAt(text, i); ok { + end := findToolCDATAEnd(text, bodyStart) + if end < 0 { + return 0, false, true + } + return end + toolCDATACloseLenAt(text, end), true, false + } + switch { + case strings.HasPrefix(text[i:], "") + if end < 0 { + return 0, false, true + } + return i + len(""), true, false + default: + return i, false, false + } +} + +func matchToolCDATAOpenAt(text string, start int) (int, bool) { + openLen := toolCDATAOpenLenAt(text, start) + if openLen > 0 { + return start + openLen, true + } + return start, false +} + +func hasASCIIPrefixFoldAt(text string, start int, prefix string) bool { + _, ok := matchASCIIPrefixFoldAt(text, start, prefix) + return ok +} + +func matchASCIIPrefixFoldAt(text string, start int, prefix string) (int, bool) { + if start < 0 || start >= len(text) && prefix != "" { + return 0, false + } + idx := start + for j := 0; j < len(prefix); j++ { + if idx >= len(text) { + return 0, false + } + ch, size := normalizedASCIIAt(text, idx) + if size <= 0 || asciiLower(ch) != asciiLower(prefix[j]) { + return 0, false + } + idx += size + } + return idx - start, true +} + +func asciiLower(b byte) byte { + if b >= 'A' && b <= 'Z' { + return b + ('a' - 'A') + } + return b +} + +func findToolCDATAEnd(text string, from int) int { + if from < 0 || from >= len(text) { + return -1 + } + firstNonFenceEnd := -1 + for searchFrom := from; searchFrom < len(text); { + end := indexToolCDATAClose(text, searchFrom) + if end < 0 { + break + } + closeLen := toolCDATACloseLenAt(text, end) + searchFrom = end + closeLen + if cdataOffsetIsInsideMarkdownFence(text[from:end]) { + continue + } + if cdataEndLooksStructural(text, searchFrom) { + return end + } + if firstNonFenceEnd < 0 { + firstNonFenceEnd = end + } + } + return firstNonFenceEnd +} + +func indexToolCDATAClose(text string, from int) int { + if from < 0 { + from = 0 + } + asciiIdx := strings.Index(text[from:], "]]>") + fullIdx := strings.Index(text[from:], "]]>") + cjkIdx := strings.Index(text[from:], "]]〉") + if asciiIdx < 0 && fullIdx < 0 && cjkIdx < 0 { + return -1 + } + best := -1 + for _, idx := range []int{asciiIdx, fullIdx, cjkIdx} { + if idx >= 0 && (best < 0 || idx < best) { + best = idx + } + } + return from + best +} + +func toolCDATACloseLenAt(text string, idx int) int { + if idx < 0 || idx >= len(text) { + return 0 + } + if strings.HasPrefix(text[idx:], "]]〉") { + return len("]]〉") + } + if strings.HasPrefix(text[idx:], "]]>") { + return len("]]>") + } + if strings.HasPrefix(text[idx:], "]]>") { + return len("]]>") + } + return 0 +} + +func cdataEndLooksStructural(text string, after int) bool { + for after < len(text) { + switch { + case text[after] == ' ' || text[after] == '\t' || text[after] == '\r' || text[after] == '\n': + after++ + case after+1 < len(text) && text[after] == '<' && text[after+1] == '/': + return true + default: + return false + } + } + return false +} + +func cdataOffsetIsInsideMarkdownFence(fragment string) bool { + if fragment == "" { + return false + } + lines := strings.SplitAfter(fragment, "\n") + inFence := false + fenceMarker := "" + for _, line := range lines { + trimmed := strings.TrimLeft(line, " \t") + if !inFence { + if marker, ok := parseFenceOpen(trimmed); ok { + inFence = true + fenceMarker = marker + } + continue + } + if isFenceClose(trimmed, fenceMarker) { + inFence = false + fenceMarker = "" + } + } + return inFence +} + +func findXMLTagEnd(text string, from int) int { + quote := rune(0) + for i := maxInt(from, 0); i < len(text); { + r, size := utf8.DecodeRuneInString(text[i:]) + if r == utf8.RuneError && size == 0 { + break + } + ch := normalizeFullwidthASCII(r) + if quote != 0 { + if ch == quote { + quote = 0 + } + i += size + continue + } + if ch == '"' || ch == '\'' { + quote = ch + i += size + continue + } + if ch == '>' { + return i + size - 1 + } + i += size + } + return -1 +} + +func hasXMLTagBoundary(text string, idx int) bool { + if idx >= len(text) { + return true + } + switch text[idx] { + case ' ', '\t', '\n', '\r', '>', '/': + return true + default: + r, _ := utf8.DecodeRuneInString(text[idx:]) + return normalizeFullwidthASCII(r) == '>' + } +} + +func isSelfClosingXMLTag(startTag string) bool { + return strings.HasSuffix(strings.TrimSpace(startTag), "/") +} + +func maxInt(a, b int) int { + if a > b { + return a + } + return b +} + +func parseXMLTagAttributes(raw string) map[string]string { + if strings.TrimSpace(raw) == "" { + return map[string]string{} + } + out := map[string]string{} + for _, m := range xmlAttrPattern.FindAllStringSubmatch(raw, -1) { + if len(m) < 5 { + continue + } + key := strings.ToLower(strings.TrimSpace(m[1])) + if key == "" { + continue + } + value := m[3] + if value == "" { + value = m[4] + } + out[key] = value + } + return out +} + +func parseInvokeParameterValue(paramName, raw string) any { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return "" + } + if value, ok := extractStandaloneCDATA(trimmed); ok { + if parsed, ok := parseJSONLiteralValue(value); ok { + if parsedArray, ok := coerceArrayValue(parsed, paramName); ok { + return parsedArray + } + return parsed + } + if parsed, ok := parseStructuredCDATAParameterValue(paramName, value); ok { + return parsed + } + if parsed, ok := parseLooseJSONArrayValue(value, paramName); ok { + return parsed + } + return value + } + decoded := html.UnescapeString(extractRawTagValue(trimmed)) + if strings.Contains(decoded, "<") && strings.Contains(decoded, ">") { + if parsedValue, ok := parseXMLFragmentValue(decoded); ok { + switch v := parsedValue.(type) { + case map[string]any: + if len(v) > 0 { + if parsedArray, ok := coerceArrayValue(v, paramName); ok { + return parsedArray + } + return v + } + case []any: + return v + case string: + text := strings.TrimSpace(v) + if text == "" { + return "" + } + if parsedText, ok := parseJSONLiteralValue(text); ok { + if parsedArray, ok := coerceArrayValue(parsedText, paramName); ok { + return parsedArray + } + return parsedText + } + if parsedText, ok := parseLooseJSONArrayValue(text, paramName); ok { + return parsedText + } + return v + default: + return v + } + } + if parsed := parseStructuredToolCallInput(decoded); len(parsed) > 0 { + if len(parsed) == 1 { + if rawValue, ok := parsed["_raw"].(string); ok { + if parsedText, ok := parseLooseJSONArrayValue(rawValue, paramName); ok { + return parsedText + } + return rawValue + } + } + if parsedArray, ok := coerceArrayValue(parsed, paramName); ok { + return parsedArray + } + return parsed + } + } + if parsed, ok := parseJSONLiteralValue(decoded); ok { + if parsedArray, ok := coerceArrayValue(parsed, paramName); ok { + return parsedArray + } + return parsed + } + if parsed, ok := parseLooseJSONArrayValue(decoded, paramName); ok { + return parsed + } + return decoded +} + +func parseStructuredCDATAParameterValue(paramName, raw string) (any, bool) { + if preservesCDATAStringParameter(paramName) { + return nil, false + } + normalized := normalizeCDATAForStructuredParse(raw) + if !strings.Contains(normalized, "<") || !strings.Contains(normalized, ">") { + return nil, false + } + if !cdataFragmentLooksExplicitlyStructured(normalized) { + return nil, false + } + parsed, ok := parseXMLFragmentValue(normalized) + if !ok { + return nil, false + } + switch v := parsed.(type) { + case []any: + return v, true + case map[string]any: + if len(v) == 0 { + return nil, false + } + return v, true + default: + return nil, false + } +} + +func normalizeCDATAForStructuredParse(raw string) string { + if raw == "" { + return "" + } + normalized := cdataBRSeparatorPattern.ReplaceAllString(raw, "\n") + return html.UnescapeString(strings.TrimSpace(normalized)) +} + +// Preserve flat CDATA fragments as strings. Only recover structure when the +// fragment clearly encodes a data shape: multiple sibling elements, nested +// child elements, or an explicit item list. +func cdataFragmentLooksExplicitlyStructured(raw string) bool { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return false + } + + dec := xml.NewDecoder(strings.NewReader("" + trimmed + "")) + tok, err := dec.Token() + if err != nil { + return false + } + start, ok := tok.(xml.StartElement) + if !ok || !strings.EqualFold(start.Name.Local, "root") { + return false + } + + depth := 0 + directChildren := 0 + firstChildName := "" + firstChildHasNested := false + + for { + tok, err := dec.Token() + if err != nil { + return false + } + switch t := tok.(type) { + case xml.StartElement: + if depth == 0 { + directChildren++ + if directChildren == 1 { + firstChildName = strings.ToLower(strings.TrimSpace(t.Name.Local)) + } else { + return true + } + } else if directChildren == 1 && depth == 1 { + firstChildHasNested = true + } + depth++ + case xml.EndElement: + if strings.EqualFold(t.Name.Local, "root") { + if directChildren != 1 { + return false + } + if firstChildName == "item" { + return true + } + return firstChildHasNested + } + if depth > 0 { + depth-- + } + } + } +} + +func preservesCDATAStringParameter(name string) bool { + switch strings.ToLower(strings.TrimSpace(name)) { + case "content", "file_content", "text", "prompt", "query", "command", "cmd", "script", "code", "old_string", "new_string", "pattern", "path", "file_path": + return true + default: + return false + } +} diff --git a/internal/toolcall/toolcalls_scan.go b/internal/toolcall/toolcalls_scan.go new file mode 100644 index 0000000000000000000000000000000000000000..363e1ea9b512c88529f3430dd260db92d915f540 --- /dev/null +++ b/internal/toolcall/toolcalls_scan.go @@ -0,0 +1,584 @@ +package toolcall + +import ( + "strings" + "unicode" + "unicode/utf8" +) + +type toolMarkupNameAlias struct { + raw string + canonical string + dsmlOnly bool +} + +var toolMarkupNames = []toolMarkupNameAlias{ + {raw: "tool_calls", canonical: "tool_calls"}, + {raw: "tool-calls", canonical: "tool_calls", dsmlOnly: true}, + {raw: "toolcalls", canonical: "tool_calls", dsmlOnly: true}, + {raw: "invoke", canonical: "invoke"}, + {raw: "parameter", canonical: "parameter"}, +} + +type ToolMarkupTag struct { + Start int + End int + NameStart int + NameEnd int + Name string + Closing bool + SelfClosing bool + DSMLLike bool + Canonical bool +} + +func ContainsToolMarkupSyntaxOutsideIgnored(text string) (hasDSML, hasCanonical bool) { + for i := 0; i < len(text); { + next, advanced, blocked := skipXMLIgnoredSection(text, i) + if blocked { + return hasDSML, hasCanonical + } + if advanced { + i = next + continue + } + if end, ok := markdownCodeSpanEnd(text, i); ok { + i = end + continue + } + if tag, ok := scanToolMarkupTagAt(text, i); ok { + if tag.DSMLLike { + hasDSML = true + } else { + hasCanonical = true + } + if hasDSML && hasCanonical { + return true, true + } + i = tag.End + 1 + continue + } + i++ + } + return hasDSML, hasCanonical +} + +func ContainsToolCallWrapperSyntaxOutsideIgnored(text string) (hasDSML, hasCanonical bool) { + for i := 0; i < len(text); { + next, advanced, blocked := skipXMLIgnoredSection(text, i) + if blocked { + return hasDSML, hasCanonical + } + if advanced { + i = next + continue + } + if end, ok := markdownCodeSpanEnd(text, i); ok { + i = end + continue + } + if tag, ok := scanToolMarkupTagAt(text, i); ok { + if tag.Name != "tool_calls" { + i = tag.End + 1 + continue + } + if tag.DSMLLike { + hasDSML = true + } else { + hasCanonical = true + } + if hasDSML && hasCanonical { + return true, true + } + i = tag.End + 1 + continue + } + i++ + } + return hasDSML, hasCanonical +} + +func FindToolMarkupTagOutsideIgnored(text string, start int) (ToolMarkupTag, bool) { + for i := maxInt(start, 0); i < len(text); { + next, advanced, blocked := skipXMLIgnoredSection(text, i) + if blocked { + return ToolMarkupTag{}, false + } + if advanced { + i = next + continue + } + if end, ok := markdownCodeSpanEnd(text, i); ok { + i = end + continue + } + if tag, ok := scanToolMarkupTagAt(text, i); ok { + return tag, true + } + i++ + } + return ToolMarkupTag{}, false +} + +func FindMatchingToolMarkupClose(text string, open ToolMarkupTag) (ToolMarkupTag, bool) { + if text == "" || open.Name == "" || open.Closing || open.End >= len(text) { + return ToolMarkupTag{}, false + } + depth := 1 + for pos := open.End + 1; pos < len(text); { + tag, ok := FindToolMarkupTagOutsideIgnored(text, pos) + if !ok { + return ToolMarkupTag{}, false + } + if tag.Name != open.Name { + pos = tag.End + 1 + continue + } + if tag.Closing { + depth-- + if depth == 0 { + return tag, true + } + } else if !tag.SelfClosing { + depth++ + } + pos = tag.End + 1 + } + return ToolMarkupTag{}, false +} + +func scanToolMarkupTagAt(text string, start int) (ToolMarkupTag, bool) { + next, ok := consumeToolMarkupLessThan(text, start) + if !ok { + return ToolMarkupTag{}, false + } + i := next + for { + next, ok := consumeToolMarkupLessThan(text, i) + if !ok { + break + } + i = next + } + closing := false + if next, ok := consumeToolMarkupClosingSlash(text, i); ok { + closing = true + i = next + } + prefixStart := i + i, dsmlLike := consumeToolMarkupNamePrefix(text, i) + name, nameLen := matchToolMarkupName(text, i, dsmlLike) + if nameLen == 0 { + fallbackName, fallbackStart, fallbackLen, ok := matchToolMarkupNameAfterArbitraryPrefix(text, prefixStart) + if !ok { + return ToolMarkupTag{}, false + } + if !closing && toolMarkupPrefixContainsSlash(text[prefixStart:fallbackStart]) { + closing = true + } + name = fallbackName + i = fallbackStart + nameLen = fallbackLen + dsmlLike = true + } + nameEnd := i + nameLen + nameEndBeforeSeparators := nameEnd + for next, ok := consumeToolMarkupSeparator(text, nameEnd); ok; next, ok = consumeToolMarkupSeparator(text, nameEnd) { + nameEnd = next + } + hasTrailingSeparator := nameEnd > nameEndBeforeSeparators + if !hasToolMarkupBoundary(text, nameEnd) { + return ToolMarkupTag{}, false + } + end := findXMLTagEnd(text, nameEnd) + if end < 0 { + if !hasTrailingSeparator { + return ToolMarkupTag{}, false + } + end = nameEnd - 1 + } + if hasTrailingSeparator { + if nextLT := strings.IndexByte(text[nameEnd:], '<'); nextLT >= 0 && end >= nameEnd+nextLT { + end = nameEnd - 1 + } + } + trimmed := strings.TrimSpace(text[start : end+1]) + return ToolMarkupTag{ + Start: start, + End: end, + NameStart: i, + NameEnd: nameEnd, + Name: name, + Closing: closing, + SelfClosing: strings.HasSuffix(trimmed, "/>"), + DSMLLike: dsmlLike, + Canonical: !dsmlLike, + }, true +} + +func IsPartialToolMarkupTagPrefix(text string) bool { + if text == "" || text[0] != '<' || strings.Contains(text, ">") || strings.Contains(text, ">") { + return false + } + i := 1 + for i < len(text) && text[i] == '<' { + i++ + } + if i >= len(text) { + return true + } + if text[i] == '/' { + i++ + } + for i <= len(text) { + if i == len(text) { + return true + } + if hasToolMarkupNamePrefix(text, i) { + return true + } + if hasASCIIPartialPrefixFoldAt(text, i, "dsml") { + return true + } + if hasPartialToolMarkupNameAfterArbitraryPrefix(text, i) { + return true + } + next, ok := consumeToolMarkupNamePrefixOnce(text, i) + if !ok { + return false + } + i = next + } + return false +} + +func consumeToolMarkupNamePrefix(text string, idx int) (int, bool) { + dsmlLike := false + for { + next, ok := consumeToolMarkupNamePrefixOnce(text, idx) + if !ok { + return idx, dsmlLike + } + idx = next + dsmlLike = true + } +} + +func consumeToolMarkupNamePrefixOnce(text string, idx int) (int, bool) { + idx = skipToolMarkupIgnorables(text, idx) + if next, ok := consumeToolMarkupSeparator(text, idx); ok { + return next, true + } + if spacingLen := toolMarkupWhitespaceLikeLenAt(text, idx); spacingLen > 0 { + return idx + spacingLen, true + } + if next, ok := consumeToolKeyword(text, idx, "dsml"); ok { + if dashLen := toolMarkupDashLenAt(text, next); dashLen > 0 { + next += dashLen + } else if underscoreLen := toolMarkupUnderscoreLenAt(text, next); underscoreLen > 0 { + next += underscoreLen + } + return next, true + } + if next, ok := consumeArbitraryToolMarkupNamePrefix(text, idx); ok { + return next, true + } + return idx, false +} + +func consumeArbitraryToolMarkupNamePrefix(text string, idx int) (int, bool) { + nextSegment, ok := consumeToolMarkupPrefixSegment(text, idx) + if !ok { + return idx, false + } + j := nextSegment + for { + nextSegment, ok = consumeToolMarkupPrefixSegment(text, j) + if !ok { + break + } + j = nextSegment + } + k := j + for k < len(text) && (text[k] == ' ' || text[k] == '\t' || text[k] == '\r' || text[k] == '\n') { + k++ + } + next, ok := consumeToolMarkupSeparator(text, k) + if !ok { + if sep, size := normalizedASCIIAt(text, k); sep == '_' || sep == '-' { + next = k + size + ok = true + } + } + if !ok { + return idx, false + } + for next < len(text) && (text[next] == ' ' || text[next] == '\t' || text[next] == '\r' || text[next] == '\n') { + next++ + } + if !hasToolMarkupNamePrefix(text, next) { + return idx, false + } + return next, true +} + +func consumeToolMarkupPrefixSegment(text string, idx int) (int, bool) { + ch, size := normalizedASCIIAt(text, idx) + if size <= 0 { + return idx, false + } + if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') { + return idx + size, true + } + return idx, false +} + +func hasASCIIPartialPrefixFoldAt(text string, start int, prefix string) bool { + if start < 0 || start >= len(text) { + return false + } + idx := start + matched := 0 + for matched < len(prefix) && idx < len(text) { + ch, size := normalizedASCIIAt(text, idx) + if size <= 0 || asciiLower(ch) != asciiLower(prefix[matched]) { + return false + } + idx += size + matched++ + } + return matched > 0 && matched < len(prefix) && idx == len(text) +} + +func hasToolMarkupNamePrefix(text string, start int) bool { + for _, name := range toolMarkupNames { + if hasASCIIPrefixFoldAt(text, start, name.raw) { + return true + } + if hasASCIIPartialPrefixFoldAt(text, start, name.raw) { + return true + } + } + return false +} + +func matchToolMarkupName(text string, start int, dsmlLike bool) (string, int) { + for _, name := range toolMarkupNames { + if name.dsmlOnly && !dsmlLike { + continue + } + if next, ok := consumeToolKeyword(text, start, name.raw); ok { + return name.canonical, next - start + } + } + return "", 0 +} + +func matchToolMarkupNameAfterArbitraryPrefix(text string, start int) (string, int, int, bool) { + for idx := start; idx < len(text); { + if isToolMarkupTagTerminator(text, idx) { + return "", 0, 0, false + } + for _, name := range toolMarkupNames { + next, ok := consumeToolKeyword(text, idx, name.raw) + if !ok { + continue + } + if !toolMarkupPrefixAllowsLocalNameAt(text, start, idx) { + continue + } + return name.canonical, idx, next - idx, true + } + _, size := utf8.DecodeRuneInString(text[idx:]) + if size <= 0 { + size = 1 + } + idx += size + } + return "", 0, 0, false +} + +func hasPartialToolMarkupNameAfterArbitraryPrefix(text string, start int) bool { + for idx := start; idx < len(text); { + if isToolMarkupTagTerminator(text, idx) { + return false + } + if toolMarkupPrefixAllowsLocalNameAt(text, start, idx) && hasToolMarkupNamePrefix(text, idx) { + return true + } + if toolMarkupPrefixAllowsLocalNameAt(text, start, idx) && hasDSMLNamePrefixOrPartial(text, idx) { + return true + } + _, size := utf8.DecodeRuneInString(text[idx:]) + if size <= 0 { + size = 1 + } + idx += size + } + return toolMarkupPrefixAllowsLocalName(text[start:]) +} + +func toolMarkupPrefixAllowsLocalNameAt(text string, start, localStart int) bool { + if start < 0 || localStart <= start || localStart > len(text) { + return false + } + prefix := text[start:localStart] + if toolMarkupPrefixAllowsLocalName(prefix) { + return true + } + if strings.ContainsAny(prefix, "=\"'") { + return false + } + prev, prevSize := utf8.DecodeLastRuneInString(prefix) + next, _ := utf8.DecodeRuneInString(text[localStart:]) + if prevSize <= 0 || next == utf8.RuneError { + return false + } + return isASCIIAlphaNumeric(normalizeFullwidthASCII(prev)) && isASCIIUpper(normalizeFullwidthASCII(next)) +} + +func hasDSMLNamePrefixOrPartial(text string, start int) bool { + return hasASCIIPrefixFoldAt(text, start, "dsml") || hasASCIIPartialPrefixFoldAt(text, start, "dsml") +} + +func toolMarkupPrefixAllowsLocalName(prefix string) bool { + if prefix == "" { + return false + } + if strings.Contains(normalizedASCIILowerString(prefix), "dsml") { + return true + } + if strings.ContainsAny(prefix, "=\"'") { + return false + } + r, _ := utf8.DecodeLastRuneInString(prefix) + r = normalizeFullwidthASCII(r) + return (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') && (r < '0' || r > '9') +} + +func normalizedASCIILowerString(text string) string { + var b strings.Builder + b.Grow(len(text)) + for _, r := range text { + r = normalizeFullwidthASCII(r) + if r >= 'A' && r <= 'Z' { + r += 'a' - 'A' + } + if r <= 0x7f { + b.WriteRune(r) + } + } + return b.String() +} + +func isASCIIAlphaNumeric(r rune) bool { + return (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') +} + +func isASCIIUpper(r rune) bool { + return r >= 'A' && r <= 'Z' +} + +func isToolMarkupTagTerminator(text string, idx int) bool { + if idx >= len(text) { + return false + } + if text[idx] == '>' { + return true + } + r, _ := utf8.DecodeRuneInString(text[idx:]) + return normalizeFullwidthASCII(r) == '>' +} + +func consumeToolMarkupSeparator(text string, idx int) (int, bool) { + idx = skipToolMarkupIgnorables(text, idx) + if idx >= len(text) { + return idx, false + } + r, size := utf8.DecodeRuneInString(text[idx:]) + if size <= 0 || !isToolMarkupSeparator(r) { + return idx, false + } + return idx + size, true +} + +func isToolMarkupSeparator(r rune) bool { + ch := normalizeFullwidthASCII(r) + if ch == 0 || ch == '<' || ch == '>' || ch == '/' || ch == '=' || ch == '"' || ch == '\'' || ch == '[' { + return false + } + if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' { + return false + } + if r == '▁' || unicode.IsSpace(r) { + return false + } + if (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9') { + return false + } + return true +} + +func consumeToolMarkupLessThan(text string, idx int) (int, bool) { + idx = skipToolMarkupIgnorables(text, idx) + ch, size := normalizedASCIIAt(text, idx) + if size <= 0 || ch != '<' { + return idx, false + } + return idx + size, true +} + +func hasToolMarkupBoundary(text string, idx int) bool { + idx = skipToolMarkupIgnorables(text, idx) + if idx >= len(text) { + return true + } + if toolMarkupWhitespaceLikeLenAt(text, idx) > 0 { + return true + } + if _, ok := consumeToolMarkupClosingSlash(text, idx); ok { + return true + } + return xmlTagEndDelimiterLenAt(text, idx) > 0 +} + +func normalizedASCIIAt(text string, idx int) (byte, int) { + if idx < 0 || idx >= len(text) { + return 0, 0 + } + r, size := utf8.DecodeRuneInString(text[idx:]) + if r == utf8.RuneError && size == 0 { + return 0, 0 + } + normalized := normalizeFullwidthASCII(r) + if normalized > 0x7f { + return 0, 0 + } + return byte(normalized), size +} + +func normalizeFullwidthASCII(r rune) rune { + switch r { + case '〈': + return '<' + case '〉': + return '>' + case '“', '”': + return '"' + case '‘', '’': + return '\'' + } + if r >= '!' && r <= '~' { + return r - 0xFEE0 + } + return r +} + +func toolMarkupPrefixContainsSlash(prefix string) bool { + for _, r := range prefix { + if normalizeFullwidthASCII(r) == '/' { + return true + } + } + return false +} diff --git a/internal/toolcall/toolcalls_schema_normalize.go b/internal/toolcall/toolcalls_schema_normalize.go new file mode 100644 index 0000000000000000000000000000000000000000..65a27c25b2c06706be45be406a6b1a4eb93cb95a --- /dev/null +++ b/internal/toolcall/toolcalls_schema_normalize.go @@ -0,0 +1,282 @@ +package toolcall + +import ( + "encoding/json" + "strings" +) + +func NormalizeParsedToolCallsForSchemas(calls []ParsedToolCall, toolsRaw any) []ParsedToolCall { + if len(calls) == 0 { + return calls + } + schemas := buildToolSchemaIndex(toolsRaw) + if len(schemas) == 0 { + return calls + } + + var changedAny bool + out := make([]ParsedToolCall, len(calls)) + for i, call := range calls { + out[i] = call + schema, ok := schemas[strings.ToLower(strings.TrimSpace(call.Name))] + if !ok || call.Input == nil { + continue + } + normalized, changed := normalizeToolValueWithSchema(call.Input, schema) + if !changed { + continue + } + changedAny = true + if input, ok := normalized.(map[string]any); ok { + out[i].Input = input + } + } + if !changedAny { + return calls + } + return out +} + +func buildToolSchemaIndex(toolsRaw any) map[string]any { + tools, ok := toolsRaw.([]any) + if !ok || len(tools) == 0 { + return nil + } + out := make(map[string]any, len(tools)) + for _, item := range tools { + tool, ok := item.(map[string]any) + if !ok { + continue + } + name, _, schema := ExtractToolMeta(tool) + if name == "" || schema == nil { + continue + } + out[strings.ToLower(name)] = schema + } + if len(out) == 0 { + return nil + } + return out +} + +func ExtractToolMeta(tool map[string]any) (string, string, any) { + name := strings.TrimSpace(asStringValue(tool["name"])) + desc := strings.TrimSpace(asStringValue(tool["description"])) + schema := firstNonNil( + tool["parameters"], + tool["input_schema"], + tool["inputSchema"], + tool["schema"], + ) + if fn, ok := tool["function"].(map[string]any); ok { + if name == "" { + name = strings.TrimSpace(asStringValue(fn["name"])) + } + if desc == "" { + desc = strings.TrimSpace(asStringValue(fn["description"])) + } + schema = firstNonNil( + schema, + fn["parameters"], + fn["input_schema"], + fn["inputSchema"], + fn["schema"], + ) + } + return name, desc, schema +} + +func normalizeToolValueWithSchema(value any, schema any) (any, bool) { + if value == nil || schema == nil { + return value, false + } + schemaMap, ok := schema.(map[string]any) + if !ok || len(schemaMap) == 0 { + return value, false + } + if shouldCoerceSchemaToString(schemaMap) { + return stringifySchemaValue(value) + } + if looksLikeObjectSchema(schemaMap) { + obj, ok := value.(map[string]any) + if !ok || len(obj) == 0 { + return value, false + } + properties, _ := schemaMap["properties"].(map[string]any) + additional := schemaMap["additionalProperties"] + changed := false + out := make(map[string]any, len(obj)) + for key, current := range obj { + next := current + var fieldChanged bool + if propSchema, ok := properties[key]; ok { + next, fieldChanged = normalizeToolValueWithSchema(current, propSchema) + } else if additional != nil { + next, fieldChanged = normalizeToolValueWithSchema(current, additional) + } + out[key] = next + changed = changed || fieldChanged + } + if !changed { + return value, false + } + return out, true + } + if looksLikeArraySchema(schemaMap) { + arr, ok := value.([]any) + if !ok || len(arr) == 0 { + return value, false + } + itemsSchema := schemaMap["items"] + if itemsSchema == nil { + return value, false + } + changed := false + out := make([]any, len(arr)) + switch itemSchemas := itemsSchema.(type) { + case []any: + for i, item := range arr { + if i >= len(itemSchemas) { + out[i] = item + continue + } + next, itemChanged := normalizeToolValueWithSchema(item, itemSchemas[i]) + out[i] = next + changed = changed || itemChanged + } + default: + for i, item := range arr { + next, itemChanged := normalizeToolValueWithSchema(item, itemsSchema) + out[i] = next + changed = changed || itemChanged + } + } + if !changed { + return value, false + } + return out, true + } + return value, false +} + +func shouldCoerceSchemaToString(schema map[string]any) bool { + if schema == nil { + return false + } + if isStringConst(schema["const"]) { + return true + } + if isStringEnum(schema["enum"]) { + return true + } + switch v := schema["type"].(type) { + case string: + return strings.EqualFold(strings.TrimSpace(v), "string") + case []any: + return isOnlyStringLikeTypes(v) + case []string: + items := make([]any, 0, len(v)) + for _, item := range v { + items = append(items, item) + } + return isOnlyStringLikeTypes(items) + default: + return false + } +} + +func looksLikeObjectSchema(schema map[string]any) bool { + if schema == nil { + return false + } + if typ, ok := schema["type"].(string); ok && strings.EqualFold(strings.TrimSpace(typ), "object") { + return true + } + if _, ok := schema["properties"].(map[string]any); ok { + return true + } + _, hasAdditional := schema["additionalProperties"] + return hasAdditional +} + +func looksLikeArraySchema(schema map[string]any) bool { + if schema == nil { + return false + } + if typ, ok := schema["type"].(string); ok && strings.EqualFold(strings.TrimSpace(typ), "array") { + return true + } + _, hasItems := schema["items"] + return hasItems +} + +func isOnlyStringLikeTypes(values []any) bool { + if len(values) == 0 { + return false + } + hasString := false + for _, item := range values { + typ, ok := item.(string) + if !ok { + return false + } + switch strings.ToLower(strings.TrimSpace(typ)) { + case "string": + hasString = true + case "null": + continue + default: + return false + } + } + return hasString +} + +func isStringConst(v any) bool { + _, ok := v.(string) + return ok +} + +func isStringEnum(v any) bool { + values, ok := v.([]any) + if !ok || len(values) == 0 { + return false + } + for _, item := range values { + if _, ok := item.(string); !ok { + return false + } + } + return true +} + +func stringifySchemaValue(value any) (any, bool) { + if value == nil { + return value, false + } + if s, ok := value.(string); ok { + return s, false + } + b, err := json.Marshal(value) + if err != nil { + return value, false + } + return string(b), true +} + +func asStringValue(v any) string { + if s, ok := v.(string); ok { + return s + } + return "" +} + +func firstNonNil(values ...any) any { + for _, value := range values { + if value != nil { + return value + } + } + return nil +} diff --git a/internal/toolcall/toolcalls_schema_normalize_test.go b/internal/toolcall/toolcalls_schema_normalize_test.go new file mode 100644 index 0000000000000000000000000000000000000000..7dac106ff62813db571e09de5e7d7fd7bc8aaad9 --- /dev/null +++ b/internal/toolcall/toolcalls_schema_normalize_test.go @@ -0,0 +1,161 @@ +package toolcall + +import ( + "reflect" + "testing" +) + +func TestNormalizeParsedToolCallsForSchemasCoercesDeclaredStringFieldsRecursively(t *testing.T) { + toolsRaw := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "TaskUpdate", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "taskId": map[string]any{"type": "string"}, + "payload": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + "tags": map[string]any{ + "type": "array", + "items": map[string]any{"type": "string"}, + }, + "count": map[string]any{"type": "number"}, + }, + }, + }, + }, + }, + }, + } + calls := []ParsedToolCall{{ + Name: "TaskUpdate", + Input: map[string]any{ + "taskId": 1, + "payload": map[string]any{ + "content": map[string]any{"text": "hello"}, + "tags": []any{1, true, map[string]any{"k": "v"}}, + "count": 2, + }, + }, + }} + + got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + if len(got) != 1 { + t.Fatalf("expected one normalized call, got %#v", got) + } + if got[0].Input["taskId"] != "1" { + t.Fatalf("expected taskId coerced to string, got %#v", got[0].Input["taskId"]) + } + payload, ok := got[0].Input["payload"].(map[string]any) + if !ok { + t.Fatalf("expected payload object, got %#v", got[0].Input["payload"]) + } + if payload["content"] != `{"text":"hello"}` { + t.Fatalf("expected nested content coerced to json string, got %#v", payload["content"]) + } + if payload["count"] != 2 { + t.Fatalf("expected non-string count unchanged, got %#v", payload["count"]) + } + tags, ok := payload["tags"].([]any) + if !ok { + t.Fatalf("expected tags slice, got %#v", payload["tags"]) + } + wantTags := []any{"1", "true", `{"k":"v"}`} + if !reflect.DeepEqual(tags, wantTags) { + t.Fatalf("unexpected normalized tags: got %#v want %#v", tags, wantTags) + } +} + +func TestNormalizeParsedToolCallsForSchemasSupportsDirectToolSchemaShape(t *testing.T) { + toolsRaw := []any{ + map[string]any{ + "name": "Write", + "input_schema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + }, + }, + }, + } + calls := []ParsedToolCall{{Name: "Write", Input: map[string]any{"content": []any{"a", 1}}}} + got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + if got[0].Input["content"] != `["a",1]` { + t.Fatalf("expected direct-schema content coerced to string, got %#v", got[0].Input["content"]) + } +} + +func TestNormalizeParsedToolCallsForSchemasLeavesAmbiguousUnionUnchanged(t *testing.T) { + toolsRaw := []any{ + map[string]any{ + "type": "function", + "function": map[string]any{ + "name": "TaskUpdate", + "parameters": map[string]any{ + "type": "object", + "properties": map[string]any{ + "taskId": map[string]any{"type": []any{"string", "integer"}}, + }, + }, + }, + }, + } + calls := []ParsedToolCall{{Name: "TaskUpdate", Input: map[string]any{"taskId": 1}}} + got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + if got[0].Input["taskId"] != 1 { + t.Fatalf("expected ambiguous union to stay unchanged, got %#v", got[0].Input["taskId"]) + } +} + +func TestNormalizeParsedToolCallsForSchemasSupportsCamelCaseInputSchema(t *testing.T) { + toolsRaw := []any{ + map[string]any{ + "name": "Write", + "inputSchema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + }, + }, + }, + } + calls := []ParsedToolCall{{Name: "Write", Input: map[string]any{"content": map[string]any{"message": "hi"}}}} + got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + if got[0].Input["content"] != `{"message":"hi"}` { + t.Fatalf("expected camelCase inputSchema content coercion, got %#v", got[0].Input["content"]) + } +} + +func TestNormalizeParsedToolCallsForSchemasPreservesArrayWhenSchemaSaysArray(t *testing.T) { + toolsRaw := []any{ + map[string]any{ + "name": "todowrite", + "inputSchema": map[string]any{ + "type": "object", + "properties": map[string]any{ + "todos": map[string]any{ + "type": "array", + "items": map[string]any{ + "type": "object", + "properties": map[string]any{ + "content": map[string]any{"type": "string"}, + "status": map[string]any{"type": "string"}, + "priority": map[string]any{"type": "string"}, + }, + }, + }, + }, + }, + }, + } + todos := []any{map[string]any{"content": "x", "status": "pending", "priority": "high"}} + calls := []ParsedToolCall{{Name: "todowrite", Input: map[string]any{"todos": todos}}} + got := NormalizeParsedToolCallsForSchemas(calls, toolsRaw) + if !reflect.DeepEqual(got[0].Input["todos"], todos) { + t.Fatalf("expected todos array preserved, got %#v want %#v", got[0].Input["todos"], todos) + } +} diff --git a/internal/toolcall/toolcalls_test.go b/internal/toolcall/toolcalls_test.go new file mode 100644 index 0000000000000000000000000000000000000000..28ff91082f5f481402806d964c1ad9964fa20c85 --- /dev/null +++ b/internal/toolcall/toolcalls_test.go @@ -0,0 +1,1308 @@ +package toolcall + +import ( + "strings" + "testing" +) + +func TestFormatOpenAIToolCalls(t *testing.T) { + formatted := FormatOpenAIToolCalls([]ParsedToolCall{{Name: "search", Input: map[string]any{"q": "x"}}}, nil) + if len(formatted) != 1 { + t.Fatalf("expected 1, got %d", len(formatted)) + } + fn, _ := formatted[0]["function"].(map[string]any) + if fn["name"] != "search" { + t.Fatalf("unexpected function name: %#v", fn) + } +} + +func TestParseToolCallsSupportsToolCallsWrapper(t *testing.T) { + text := `pwdshow cwd` + calls := ParseToolCalls(text, []string{"bash"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if calls[0].Name != "Bash" { + t.Fatalf("expected original tool name Bash, got %q", calls[0].Name) + } + if calls[0].Input["command"] != "pwd" { + t.Fatalf("expected command argument, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsSupportsDSMLShell(t *testing.T) { + text := `<|DSML|tool_calls><|DSML|invoke name="Bash"><|DSML|parameter name="command">` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected 1 DSML call, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "pwd" { + t.Fatalf("unexpected DSML parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsSupportsHyphenatedDSMLShellWithHereDocCDATA(t *testing.T) { + text := ` + + + + +` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected 1 hyphenated DSML call, got %#v", calls) + } + if calls[0].Name != "Bash" { + t.Fatalf("expected Bash tool, got %#v", calls[0]) + } + command, _ := calls[0].Input["command"].(string) + if !strings.Contains(command, `git commit -m "$(cat <<'EOF'`) || !strings.Contains(command, "Co-Authored-By: Claude Opus 4.7") { + t.Fatalf("expected here-doc CDATA command to be preserved, got %q", command) + } + if calls[0].Input["description"] != "Create commit with architecture doc updates" { + t.Fatalf("expected description parameter, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsSupportsUnderscoredDSMLShell(t *testing.T) { + text := ` + + + + + + + +` + calls := ParseToolCalls(text, []string{"search_web", "eval_javascript"}) + if len(calls) != 2 { + t.Fatalf("expected two underscored DSML calls, got %#v", calls) + } + if calls[0].Name != "search_web" || calls[0].Input["query"] != "2026年5月 热点事件" || calls[0].Input["topic"] != "news" { + t.Fatalf("unexpected first underscored DSML call: %#v", calls[0]) + } + if calls[1].Name != "eval_javascript" || calls[1].Input["code"] != "1 + 1" { + t.Fatalf("unexpected second underscored DSML call: %#v", calls[1]) + } +} + +func TestParseToolCallsSupportsArbitraryPrefixedToolMarkup(t *testing.T) { + cases := []string{ + `README.md`, + `README.md`, + `README.md`, + } + for _, text := range cases { + calls := ParseToolCalls(text, []string{"Read"}) + if len(calls) != 1 { + t.Fatalf("expected one arbitrary-prefixed tool call for %q, got %#v", text, calls) + } + if calls[0].Name != "Read" || calls[0].Input["file_path"] != "README.md" { + t.Fatalf("unexpected arbitrary-prefixed parse result: %#v", calls[0]) + } + } +} + +func TestParseToolCallsSupportsCamelPrefixedToolMarkup(t *testing.T) { + text := `` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected one camel-prefixed tool call, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "git push" || calls[0].Input["description"] != "Push dev branch to origin" { + t.Fatalf("unexpected camel-prefixed tool call: %#v", calls[0]) + } +} + +func TestParseToolCallsRejectsCamelPrefixedToolMarkupLookalike(t *testing.T) { + text := `git push` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 0 { + t.Fatalf("expected camel-prefixed lookalike to be ignored, got %#v", calls) + } +} + +func TestParseToolCallsSupportsFullwidthDSMLShell(t *testing.T) { + text := `<dSML|tool_calls> + <dSML|invoke name="Read"> + <dSML|parameter name="file_path"> + + <dSML|invoke name="Read"> + <dSML|parameter name="file_path"> + +` + calls := ParseToolCalls(text, []string{"Read"}) + if len(calls) != 2 { + t.Fatalf("expected two fullwidth DSML calls, got %#v", calls) + } + if calls[0].Name != "Read" || calls[0].Input["file_path"] != "/Users/aq/Desktop/myproject/Personal_Blog/README.md" { + t.Fatalf("unexpected first fullwidth DSML call: %#v", calls[0]) + } + if calls[1].Name != "Read" || calls[1].Input["file_path"] != "/Users/aq/Desktop/myproject/Personal_Blog/index.html" { + t.Fatalf("unexpected second fullwidth DSML call: %#v", calls[1]) + } +} + +func TestParseToolCallsSupportsCJKAngleDSMDrift(t *testing.T) { + text := ` + +〈![CDATA[Show commits on local dev not on origin/dev]]〉〈/DSM|parameter〉 +〈![CDATA[git log --oneline origin/dev..dev]]〉〈/DSM|parameter〉 +〈/DSM|invoke〉 + +〈![CDATA[Show commits on origin/dev not on local dev]]〉〈/DSM|parameter〉 +〈![CDATA[git log --oneline dev..origin/dev]]〉〈/DSM|parameter〉 +〈/DSM|invoke〉 + +〈![CDATA[Check tracking branch status]]〉〈/DSM|parameter〉 +〈![CDATA[git status -b --short]]〉〈/DSM|parameter〉 +〈/DSM|invoke〉 +〈/DSM|tool_calls〉` + + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 3 { + t.Fatalf("expected three CJK-angle DSM drift calls, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "git log --oneline origin/dev..dev" { + t.Fatalf("unexpected first CJK-angle DSM drift call: %#v", calls[0]) + } + if calls[1].Name != "Bash" || calls[1].Input["description"] != "Show commits on origin/dev not on local dev" { + t.Fatalf("unexpected second CJK-angle DSM drift call: %#v", calls[1]) + } + if calls[2].Name != "Bash" || calls[2].Input["command"] != "git status -b --short" { + t.Fatalf("unexpected third CJK-angle DSM drift call: %#v", calls[2]) + } +} + +func TestParseToolCallsSupportsFullwidthBangDSMLDrift(t *testing.T) { + text := `<!DSML!tool_calls> + <!DSML!invoke name=“Bash”> + <!DSML!parameter name=“command”><![CDATA[lsof -i :4321 -t]]><!/DSML!parameter> + <!DSML!parameter name=“description”><![CDATA[Verify port 4321 is free]]><!/DSML!parameter> + <!/DSML!invoke> + <!/DSML!tool_calls>` + + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected one fullwidth-bang DSML drift call, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "lsof -i :4321 -t" || calls[0].Input["description"] != "Verify port 4321 is free" { + t.Fatalf("unexpected fullwidth-bang DSML drift call: %#v", calls[0]) + } +} + +func TestParseToolCallsSupportsIdeographicCommaDSMLDrift(t *testing.T) { + text := `<、DSML、tool_calls> + <、DSML、invoke name="Bash"> + <、DSML、parameter name="command"><、[CDATA[git commit -m "$(cat <<'EOF' +feat: expand fullwidth bang separator and curly quote tolerance in DSML tool parsing + +Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com +EOF +)"]]><、/DSML、parameter> + <、DSML、parameter name="description"><、[CDATA[Create commit with staged changes]]><、/DSML、parameter> + <、/DSML、invoke> +<、/DSML、tool_calls>` + + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected one ideographic-comma DSML drift call, got %#v", calls) + } + command, _ := calls[0].Input["command"].(string) + if calls[0].Name != "Bash" || !strings.Contains(command, `git commit -m "$(cat <<'EOF'`) || !strings.Contains(command, "Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com") { + t.Fatalf("unexpected ideographic-comma DSML drift call: %#v", calls[0]) + } + if calls[0].Input["description"] != "Create commit with staged changes" { + t.Fatalf("unexpected ideographic-comma description: %#v", calls[0]) + } +} + +func TestParseToolCallsIgnoresBareHyphenatedToolCallsLookalike(t *testing.T) { + text := `pwd` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 0 { + t.Fatalf("expected bare hyphenated lookalike to be ignored, got %#v", calls) + } +} + +func TestParseToolCallsToleratesDSMLTrailingPipeTagTerminator(t *testing.T) { + text := strings.Join([]string{ + `<|DSML|tool_calls| `, + ` <|DSML|invoke name="terminal">`, + ` <|DSML|parameter name="command">`, + ` <|DSML|parameter name="timeout">`, + ` `, + ``, + }, "\n") + calls := ParseToolCalls(text, []string{"terminal"}) + if len(calls) != 1 { + t.Fatalf("expected one trailing-pipe DSML call, got %#v", calls) + } + if calls[0].Name != "terminal" { + t.Fatalf("expected terminal tool, got %#v", calls[0]) + } + if calls[0].Input["command"] != `find "/home" -type d` { + t.Fatalf("expected command argument, got %#v", calls[0].Input) + } + if calls[0].Input["timeout"] != float64(10) { + t.Fatalf("expected numeric timeout, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsToleratesDSMLTrailingNovelSeparatorTagTerminator(t *testing.T) { + text := strings.Join([]string{ + ``, + ` `, + ` `, + ` `, + ``, + }, "\n") + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected one trailing-separator DSML call, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "pwd" { + t.Fatalf("unexpected trailing-separator DSML parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsToleratesExtraLeadingLessThanBeforeDSML(t *testing.T) { + text := `<<|DSML|tool_calls><<|DSML|invoke name="Bash"><<|DSML|parameter name="command">` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected one extra-leading-less-than DSML call, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "pwd" { + t.Fatalf("unexpected extra-leading-less-than DSML parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsToleratesRepeatedDSMLPrefixNoise(t *testing.T) { + text := `<<<` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected one repeated-prefix DSML call, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "git status" { + t.Fatalf("unexpected repeated-prefix DSML parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsSupportsDSMLShellWithCanonicalExampleInCDATA(t *testing.T) { + content := `x` + text := `<|DSML|tool_calls><|DSML|invoke name="Write"><|DSML|parameter name="file_path">notes.md<|DSML|parameter name="content">` + calls := ParseToolCalls(text, []string{"Write"}) + if len(calls) != 1 { + t.Fatalf("expected 1 DSML call with XML-looking CDATA, got %#v", calls) + } + if calls[0].Name != "Write" || calls[0].Input["content"] != content { + t.Fatalf("unexpected DSML CDATA parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsKeepsHereDocCDATAWithFencedDSMLAndLiteralCDATAEnd(t *testing.T) { + command := strings.Join([]string{ + "cat > docs/project-value.md << 'ENDOFFILE'", + "# DS2API project value", + "", + "```xml", + `<|DSML|tool_calls>`, + ` <|DSML|invoke name="Bash">`, + ` <|DSML|parameter name="command">&1]]>`, + ` `, + ``, + "```", + "", + "Only the literal `]]>` needs special handling.", + "", + "ENDOFFILE", + `echo "Done. Lines: $(wc -l < docs/project-value.md)"`, + }, "\n") + text := `<|DSML|tool_calls><|DSML|invoke name="Bash"><|DSML|parameter name="command"><|DSML|parameter name="description">` + + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected one DSML call with extreme heredoc CDATA, got %#v", calls) + } + got, _ := calls[0].Input["command"].(string) + if got != command { + t.Fatalf("expected full heredoc command to survive, got:\n%q\nwant:\n%q", got, command) + } + if calls[0].Input["description"] != "Write project value doc" { + t.Fatalf("expected sibling parameter after command, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsKeepsCompactCDATAWithImmediateFencedDSML(t *testing.T) { + content := strings.Join([]string{ + "```xml", + `<|DSML|tool_calls>`, + ` <|DSML|invoke name="Bash">`, + ` <|DSML|parameter name="command">`, + ` `, + ``, + "```", + "tail", + }, "\n") + text := `` + + calls := ParseToolCalls(text, []string{"Write"}) + if len(calls) != 1 { + t.Fatalf("expected one compact CDATA call, got %#v", calls) + } + if calls[0].Input["content"] != content { + t.Fatalf("expected compact CDATA content to survive, got %#v", calls[0].Input["content"]) + } +} + +func TestParseToolCallsPreservesSimpleCDATAInlineMarkupAsText(t *testing.T) { + text := `urgent]]>` + calls := ParseToolCalls(text, []string{"Write"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + got, ok := calls[0].Input["description"].(string) + if !ok { + t.Fatalf("expected description to remain a string, got %#v", calls[0].Input["description"]) + } + if got != "urgent" { + t.Fatalf("expected inline markup CDATA to stay raw, got %q", got) + } +} + +func TestParseToolCallsTreatsUnclosedCDATAAsText(t *testing.T) { + text := `` + res := ParseToolCallsDetailed(text, []string{"Write"}) + if len(res.Calls) != 1 { + t.Fatalf("expected unclosed CDATA to still parse via outer wrapper, got %#v", res.Calls) + } + got, _ := res.Calls[0].Input["content"].(string) + if got != "hello world" { + t.Fatalf("expected recovered CDATA payload, got %q", got) + } +} + +func TestParseToolCallsNormalizesMixedDSMLAndCanonicalToolTags(t *testing.T) { + // Models commonly mix DSML wrapper tags with canonical inner tags. + // These should be normalized and parsed, not rejected. + text := `<|DSML|tool_calls><|DSML|parameter name="command">pwd` + calls := ParseToolCalls(text, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected mixed DSML/XML tool tags to be normalized and parsed, got %#v", calls) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "pwd" { + t.Fatalf("unexpected mixed DSML parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsSupportsStandaloneToolWithMultilineCDATAAndRepeatedXMLTags(t *testing.T) { + text := `script.shfirstsecond` + calls := ParseToolCalls(text, []string{"write_file"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if calls[0].Name != "write_file" { + t.Fatalf("expected tool name write_file, got %q", calls[0].Name) + } + if calls[0].Input["path"] != "script.sh" { + t.Fatalf("expected path argument, got %#v", calls[0].Input) + } + content, _ := calls[0].Input["content"].(string) + if !strings.Contains(content, "#!/bin/bash") || !strings.Contains(content, "echo \"hello\"") { + t.Fatalf("expected multiline CDATA content to be preserved, got %#v", calls[0].Input["content"]) + } + items, ok := calls[0].Input["item"].([]any) + if !ok || len(items) != 2 { + t.Fatalf("expected repeated XML tags to become an array, got %#v", calls[0].Input["item"]) + } +} + +func TestParseToolCallsKeepsToolSyntaxInsideCDATAAsParameterText(t *testing.T) { + payload := strings.Join([]string{ + "# Release notes", + "", + "```xml", + "", + " ", + " x", + " ", + "", + "```", + }, "\n") + text := `DS2API-4.0-Release-Notes.md` + calls := ParseToolCalls(text, []string{"Write"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + content, _ := calls[0].Input["content"].(string) + if content != payload { + t.Fatalf("expected CDATA payload with nested tool syntax to survive intact, got %q", content) + } + if calls[0].Input["file_path"] != "DS2API-4.0-Release-Notes.md" { + t.Fatalf("expected file_path parameter, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsSupportsInvokeParameters(t *testing.T) { + text := `beijingc` + calls := ParseToolCalls(text, []string{"get_weather"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if calls[0].Name != "get_weather" { + t.Fatalf("expected tool name get_weather, got %q", calls[0].Name) + } + if calls[0].Input["city"] != "beijing" || calls[0].Input["unit"] != "c" { + t.Fatalf("expected parsed json parameters, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsSupportsJSONScalarParameters(t *testing.T) { + text := `123true` + calls := ParseToolCalls(text, []string{"configure"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if got, ok := calls[0].Input["count"].(float64); !ok || got != 123 { + t.Fatalf("expected numeric count, got %#v", calls[0].Input["count"]) + } + if got, ok := calls[0].Input["max_tokens"].(float64); !ok || got != 256 { + t.Fatalf("expected numeric max_tokens, got %#v", calls[0].Input["max_tokens"]) + } + if got, ok := calls[0].Input["enabled"].(bool); !ok || !got { + t.Fatalf("expected boolean enabled, got %#v", calls[0].Input["enabled"]) + } +} + +func TestParseToolCallsTreatsItemOnlyParameterBodyAsArray(t *testing.T) { + text := strings.Join([]string{ + `<|DSML|tool_calls>`, + `<|DSML|invoke name="AskUserQuestion">`, + `<|DSML|parameter name="questions">`, + ``, + ``, + `
`, + ``, + ``, + ``, + ``, + `false`, + `
`, + ``, + ``, + ``, + }, "\n") + calls := ParseToolCalls(text, []string{"AskUserQuestion"}) + if len(calls) != 1 { + t.Fatalf("expected one AskUserQuestion call, got %#v", calls) + } + questions, ok := calls[0].Input["questions"].([]any) + if !ok || len(questions) != 1 { + t.Fatalf("expected questions to parse as array, got %#v", calls[0].Input["questions"]) + } + first, ok := questions[0].(map[string]any) + if !ok { + t.Fatalf("expected first question object, got %#v", questions[0]) + } + if first["question"] != "What would you like to do next?" || first["header"] != "Next step" || first["multiSelect"] != false { + t.Fatalf("unexpected question payload: %#v", first) + } + options, ok := first["options"].([]any) + if !ok || len(options) != 2 { + t.Fatalf("expected options to parse as array, got %#v", first["options"]) + } +} + +func TestParseToolCallsTreatsCDATAItemOnlyBodyAsArray(t *testing.T) { + todos := `

Testing EnterWorktree tool
Test EnterWorktree tool
in_progress


Testing TodoWrite tool
Test TodoWrite tool
completed

` + text := `<|DSML|tool_calls><|DSML|invoke name="TodoWrite"><|DSML|parameter name="todos">` + calls := ParseToolCalls(text, []string{"TodoWrite"}) + if len(calls) != 1 { + t.Fatalf("expected one TodoWrite call, got %#v", calls) + } + items, ok := calls[0].Input["todos"].([]any) + if !ok || len(items) != 2 { + t.Fatalf("expected todos CDATA item body to parse as array, got %#v", calls[0].Input["todos"]) + } + first, ok := items[0].(map[string]any) + if !ok { + t.Fatalf("expected first todo object, got %#v", items[0]) + } + if first["activeForm"] != "Testing EnterWorktree tool" || first["content"] != "Test EnterWorktree tool" || first["status"] != "in_progress" { + t.Fatalf("unexpected first todo: %#v", first) + } +} + +func TestParseToolCallsTreatsSingleItemCDATAAsArray(t *testing.T) { + text := `one
]]>
` + calls := ParseToolCalls(text, []string{"TodoWrite"}) + if len(calls) != 1 { + t.Fatalf("expected one TodoWrite call, got %#v", calls) + } + items, ok := calls[0].Input["todos"].([]any) + if !ok || len(items) != 1 { + t.Fatalf("expected single-item CDATA body to parse as array, got %#v", calls[0].Input["todos"]) + } + if got, ok := items[0].(string); !ok || got != "one" { + t.Fatalf("expected single item value to stay intact, got %#v", items[0]) + } +} + +func TestParseToolCallsTreatsLooseJSONListAsArray(t *testing.T) { + tests := []struct { + name string + body string + }{ + { + name: "plain text", + body: `{"content":"Test TodoWrite tool","status":"completed"}, {"content":"Another task","status":"pending"}`, + }, + { + name: "cdata", + body: ``, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + text := `` + tt.body + `` + calls := ParseToolCalls(text, []string{"TodoWrite"}) + if len(calls) != 1 { + t.Fatalf("expected one TodoWrite call, got %#v", calls) + } + items, ok := calls[0].Input["todos"].([]any) + if !ok || len(items) != 2 { + t.Fatalf("expected loose JSON list to parse as array, got %#v", calls[0].Input["todos"]) + } + first, ok := items[0].(map[string]any) + if !ok { + t.Fatalf("expected first todo object, got %#v", items[0]) + } + if first["content"] != "Test TodoWrite tool" || first["status"] != "completed" { + t.Fatalf("unexpected first todo: %#v", first) + } + }) + } +} + +func TestParseToolCallsKeepsPreservedTextParametersAsText(t *testing.T) { + text := `` + calls := ParseToolCalls(text, []string{"Write"}) + if len(calls) != 1 { + t.Fatalf("expected one Write call, got %#v", calls) + } + got, ok := calls[0].Input["content"].(string) + if !ok { + t.Fatalf("expected content to stay a string, got %#v", calls[0].Input["content"]) + } + want := `{"content":"Test TodoWrite tool","status":"completed"}, {"content":"Another task","status":"pending"}` + if got != want { + t.Fatalf("expected content to stay raw, got %q", got) + } +} + +func TestParseToolCallsTreatsCDATAObjectFragmentAsObject(t *testing.T) { + payload := `` + text := `` + calls := ParseToolCalls(text, []string{"AskUserQuestion"}) + if len(calls) != 1 { + t.Fatalf("expected one AskUserQuestion call, got %#v", calls) + } + question, ok := calls[0].Input["questions"].(map[string]any) + if !ok { + t.Fatalf("expected CDATA XML object fragment to parse as object, got %#v", calls[0].Input["questions"]) + } + options, ok := question["options"].([]any) + if question["question"] != "Pick one" || !ok || len(options) != 2 { + t.Fatalf("unexpected parsed question: %#v", question) + } +} + +func TestParseToolCallsPreservesRawMalformedParams(t *testing.T) { + text := `cd /root && git status` + calls := ParseToolCalls(text, []string{"execute_command"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if calls[0].Name != "execute_command" { + t.Fatalf("expected tool name execute_command, got %q", calls[0].Name) + } + raw, ok := calls[0].Input["command"].(string) + if !ok { + t.Fatalf("expected raw command tracking, got %#v", calls[0].Input) + } + if raw != "cd /root && git status" { + t.Fatalf("expected raw arguments to be preserved, got %q", raw) + } +} + +func TestParseToolCallsSupportsParamsJSONWithAmpersandCommand(t *testing.T) { + text := `sshpass -p 'xxx' ssh -o StrictHostKeyChecking=no -p 1111 root@111.111.111.111 'cd /root && git clone https://github.com/ericc-ch/copilot-api.git'` + calls := ParseToolCalls(text, []string{"execute_command"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if calls[0].Name != "execute_command" { + t.Fatalf("expected tool name execute_command, got %q", calls[0].Name) + } + cmd, _ := calls[0].Input["command"].(string) + if !strings.Contains(cmd, "&& git clone") { + t.Fatalf("expected command to keep && segment, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsDoesNotTreatParamsNameTagAsToolName(t *testing.T) { + text := `file.txtpwd` + calls := ParseToolCalls(text, []string{"execute_command"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if calls[0].Name != "execute_command" { + t.Fatalf("expected tool name execute_command, got %q", calls[0].Name) + } + if calls[0].Input["tool_name"] != "file.txt" { + t.Fatalf("expected parameter name preserved, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsDetailedMarksToolCallsSyntax(t *testing.T) { + text := `pwd` + res := ParseToolCallsDetailed(text, []string{"bash"}) + if !res.SawToolCallSyntax { + t.Fatalf("expected SawToolCallSyntax=true, got %#v", res) + } + if len(res.Calls) != 1 { + t.Fatalf("expected one parsed call, got %#v", res) + } +} + +func TestParseToolCallsAllowsAllEmptyParameterPayload(t *testing.T) { + text := ` ` + res := ParseToolCallsDetailed(text, []string{"Bash"}) + if !res.SawToolCallSyntax { + t.Fatalf("expected tool syntax to be detected, got %#v", res) + } + if len(res.Calls) != 1 { + t.Fatalf("expected all-empty payload to be parsed, got %#v", res.Calls) + } + if res.Calls[0].Input["command"] != "" || res.Calls[0].Input["description"] != "" || res.Calls[0].Input["timeout"] != "" { + t.Fatalf("expected empty parameters to be preserved, got %#v", res.Calls[0].Input) + } +} + +func TestParseToolCallsPreservesExplicitZeroArgToolCall(t *testing.T) { + text := `` + res := ParseToolCallsDetailed(text, []string{"noop"}) + if len(res.Calls) != 1 { + t.Fatalf("expected zero-arg tool call to remain valid, got %#v", res.Calls) + } + if len(res.Calls[0].Input) != 0 { + t.Fatalf("expected empty input map for zero-arg tool call, got %#v", res.Calls[0].Input) + } +} + +func TestParseToolCallsSupportsInlineJSONToolObject(t *testing.T) { + text := `{"input":{"command":"pwd","description":"show cwd"}}` + calls := ParseToolCalls(text, []string{"bash"}) + if len(calls) != 1 { + t.Fatalf("expected 1 call, got %#v", calls) + } + if calls[0].Name != "Bash" { + t.Fatalf("expected original tool name Bash, got %q", calls[0].Name) + } + if calls[0].Input["command"] != "pwd" { + t.Fatalf("expected command argument, got %#v", calls[0].Input) + } +} + +func TestParseToolCallsDoesNotAcceptMismatchedMarkupTags(t *testing.T) { + text := `README.md` + calls := ParseToolCalls(text, []string{"read_file"}) + if len(calls) != 0 { + t.Fatalf("expected mismatched tags to be rejected, got %#v", calls) + } +} + +func TestParseToolCallsDoesNotTreatNameInsideParamsAsToolName(t *testing.T) { + text := `README.md` + calls := ParseToolCalls(text, []string{"read_file"}) + if len(calls) != 0 { + t.Fatalf("expected no tool call when name appears only under params, got %#v", calls) + } +} + +func TestParseToolCallsRejectsLegacyToolsWrapper(t *testing.T) { + text := `read_file{"path":"README.md"}` + calls := ParseToolCalls(text, []string{"read_file"}) + if len(calls) != 0 { + t.Fatalf("expected legacy tools wrapper to be rejected, got %#v", calls) + } +} + +func TestParseToolCallsRejectsBareInvokeWithoutToolCallsWrapper(t *testing.T) { + text := `README.md` + res := ParseToolCallsDetailed(text, []string{"read_file"}) + if len(res.Calls) != 0 { + t.Fatalf("expected bare invoke to be rejected, got %#v", res.Calls) + } + if res.SawToolCallSyntax { + t.Fatalf("expected bare invoke to no longer count as supported syntax, got %#v", res) + } +} + +func TestParseToolCallsRepairsMissingOpeningToolCallsWrapperWhenClosingTagExists(t *testing.T) { + text := `Before tool call +README.md +
+after` + res := ParseToolCallsDetailed(text, []string{"read_file"}) + if len(res.Calls) != 1 { + t.Fatalf("expected repaired wrapper to parse exactly one call, got %#v", res) + } + if res.Calls[0].Name != "read_file" { + t.Fatalf("expected repaired wrapper to preserve tool name, got %#v", res.Calls[0]) + } + if got, _ := res.Calls[0].Input["path"].(string); got != "README.md" { + t.Fatalf("expected repaired wrapper to preserve args, got %#v", res.Calls[0].Input) + } + if !res.SawToolCallSyntax { + t.Fatalf("expected repaired wrapper to mark tool syntax seen, got %#v", res) + } +} + +func TestParseToolCallsRejectsLegacyCanonicalBody(t *testing.T) { + text := `read_file{"path":"README.md"}` + calls := ParseToolCalls(text, []string{"read_file"}) + if len(calls) != 0 { + t.Fatalf("expected legacy canonical body to be rejected, got %#v", calls) + } +} + +func TestRepairInvalidJSONBackslashes(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {`{"path": "C:\Users\name"}`, `{"path": "C:\\Users\name"}`}, + {`{"cmd": "cd D:\git_codes"}`, `{"cmd": "cd D:\\git_codes"}`}, + {`{"text": "line1\nline2"}`, `{"text": "line1\nline2"}`}, + {`{"path": "D:\\back\\slash"}`, `{"path": "D:\\back\\slash"}`}, + {`{"unicode": "\u2705"}`, `{"unicode": "\u2705"}`}, + {`{"invalid_u": "\u123"}`, `{"invalid_u": "\\u123"}`}, + } + + for _, tt := range tests { + got := repairInvalidJSONBackslashes(tt.input) + if got != tt.expected { + t.Errorf("repairInvalidJSONBackslashes(%s) = %s; want %s", tt.input, got, tt.expected) + } + } +} + +func TestRepairLooseJSON(t *testing.T) { + tests := []struct { + input string + expected string + }{ + {`{tool_calls: [{"name": "search", "input": {"q": "go"}}]}`, `{"tool_calls": [{"name": "search", "input": {"q": "go"}}]}`}, + {`{name: "search", input: {q: "go"}}`, `{"name": "search", "input": {"q": "go"}}`}, + } + + for _, tt := range tests { + got := RepairLooseJSON(tt.input) + if got != tt.expected { + t.Errorf("RepairLooseJSON(%s) = %s; want %s", tt.input, got, tt.expected) + } + } +} + +func TestParseToolCallInputRepairsControlCharsInPath(t *testing.T) { + in := `{"path":"D:\tmp\new\readme.txt","content":"line1\nline2"}` + parsed := parseToolCallInput(in) + + path, ok := parsed["path"].(string) + if !ok { + t.Fatalf("expected path string in parsed input, got %#v", parsed["path"]) + } + if path != `D:\tmp\new\readme.txt` { + t.Fatalf("expected repaired windows path, got %q", path) + } + + content, ok := parsed["content"].(string) + if !ok { + t.Fatalf("expected content string in parsed input, got %#v", parsed["content"]) + } + if content != "line1\nline2" { + t.Fatalf("expected non-path field to keep decoded escapes, got %q", content) + } +} + +func TestRepairLooseJSONWithNestedObjects(t *testing.T) { + // 测试嵌套对象的修复:DeepSeek 幻觉输出,每个元素内部包含嵌套 {} + // 注意:正则只支持单层嵌套,不支持更深层次的嵌套 + tests := []struct { + name string + input string + expected string + }{ + // 1. 单层嵌套对象(核心修复目标) + { + name: "单层嵌套 - 2个元素", + input: `"todos": {"content": "研究算法", "input": {"q": "8 queens"}}, {"content": "实现", "input": {"path": "queens.py"}}`, + expected: `"todos": [{"content": "研究算法", "input": {"q": "8 queens"}}, {"content": "实现", "input": {"path": "queens.py"}}]`, + }, + // 2. 3个单层嵌套对象 + { + name: "3个单层嵌套对象", + input: `"items": {"a": {"x":1}}, {"b": {"y":2}}, {"c": {"z":3}}`, + expected: `"items": [{"a": {"x":1}}, {"b": {"y":2}}, {"c": {"z":3}}]`, + }, + // 3. 混合嵌套:有些字段是对象,有些是原始值 + { + name: "混合嵌套 - 对象和原始值混合", + input: `"items": {"name": "test", "config": {"timeout": 30}}, {"name": "test2", "config": {"timeout": 60}}`, + expected: `"items": [{"name": "test", "config": {"timeout": 30}}, {"name": "test2", "config": {"timeout": 60}}]`, + }, + // 4. 4个嵌套对象(边界测试) + { + name: "4个嵌套对象", + input: `"todos": {"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}`, + expected: `"todos": [{"id": 1}, {"id": 2}, {"id": 3}, {"id": 4}]`, + }, + // 5. DeepSeek 典型幻觉:无空格逗号分隔 + { + name: "无空格逗号分隔", + input: `"results": {"name": "a"}, {"name": "b"}, {"name": "c"}`, + expected: `"results": [{"name": "a"}, {"name": "b"}, {"name": "c"}]`, + }, + // 6. 嵌套数组(数组在对象内,不是深层嵌套) + { + name: "对象内包含数组", + input: `"data": {"items": [1,2,3]}, {"items": [4,5,6]}`, + expected: `"data": [{"items": [1,2,3]}, {"items": [4,5,6]}]`, + }, + // 7. 真实的 DeepSeek 8皇后问题输出 + { + name: "DeepSeek 8皇后真实输出", + input: `"todos": {"content": "研究8皇后算法", "status": "pending"}, {"content": "实现Python脚本", "status": "pending"}, {"content": "验证结果", "status": "pending"}`, + expected: `"todos": [{"content": "研究8皇后算法", "status": "pending"}, {"content": "实现Python脚本", "status": "pending"}, {"content": "验证结果", "status": "pending"}]`, + }, + // 8. 简单无嵌套对象(回归测试) + { + name: "简单无嵌套对象", + input: `"items": {"a": 1}, {"b": 2}`, + expected: `"items": [{"a": 1}, {"b": 2}]`, + }, + // 9. 更复杂的单层嵌套 + { + name: "复杂单层嵌套", + input: `"functions": {"name": "execute", "input": {"command": "ls"}}, {"name": "read", "input": {"file": "a.txt"}}`, + expected: `"functions": [{"name": "execute", "input": {"command": "ls"}}, {"name": "read", "input": {"file": "a.txt"}}]`, + }, + // 10. 5个嵌套对象 + { + name: "5个嵌套对象", + input: `"tasks": {"id":1}, {"id":2}, {"id":3}, {"id":4}, {"id":5}`, + expected: `"tasks": [{"id":1}, {"id":2}, {"id":3}, {"id":4}, {"id":5}]`, + }, + } + + for _, tt := range tests { + got := RepairLooseJSON(tt.input) + if got != tt.expected { + t.Errorf("[%s] RepairLooseJSON with nested objects:\n input: %s\n got: %s\n expected: %s", tt.name, tt.input, got, tt.expected) + } + } +} + +func TestParseToolCallsUnescapesHTMLEntityArguments(t *testing.T) { + text := `echo a > out.txt` + calls := ParseToolCalls(text, []string{"bash"}) + if len(calls) != 1 { + t.Fatalf("expected one call, got %#v", calls) + } + cmd, _ := calls[0].Input["command"].(string) + if cmd != "echo a > out.txt" { + t.Fatalf("expected html entities to be unescaped in command, got %q", cmd) + } +} + +func TestParseToolCallsIgnoresXMLInsideFencedCodeBlock(t *testing.T) { + text := "Here is an example:\n```xml\nREADME.md\n```\nDo not execute it." + res := ParseToolCallsDetailed(text, []string{"read_file"}) + if len(res.Calls) != 0 { + t.Fatalf("expected no parsed calls for fenced example, got %#v", res.Calls) + } +} + +func TestParseToolCallsParsesOnlyNonFencedXMLToolCall(t *testing.T) { + text := "```xml\nREADME.md\n```\ngolang" + res := ParseToolCallsDetailed(text, []string{"read_file", "search"}) + if len(res.Calls) != 1 { + t.Fatalf("expected exactly one parsed call outside fence, got %#v", res.Calls) + } + if res.Calls[0].Name != "search" { + t.Fatalf("expected non-fenced tool call to be parsed, got %#v", res.Calls[0]) + } +} + +func TestParseToolCallsParsesAfterFourBacktickFence(t *testing.T) { + text := "````markdown\n```xml\nREADME.md\n```\n````\noutside" + res := ParseToolCallsDetailed(text, []string{"read_file", "search"}) + if len(res.Calls) != 1 { + t.Fatalf("expected exactly one parsed call outside four-backtick fence, got %#v", res.Calls) + } + if res.Calls[0].Name != "search" { + t.Fatalf("expected non-fenced tool call to be parsed, got %#v", res.Calls[0]) + } +} + +func TestParseToolCallsToleratesDSMLSpaceSeparatorTypo(t *testing.T) { + text := strings.Join([]string{ + "<|DSML tool_calls>", + "<|DSML invoke name=\"Read\">", + "<|DSML parameter name=\"file_path\">", + "", + "", + }, "\n") + calls := ParseToolCalls(text, []string{"Read"}) + if len(calls) != 1 { + t.Fatalf("expected one call from DSML space-separator typo, got %#v", calls) + } + if calls[0].Name != "Read" { + t.Fatalf("expected Read call, got %#v", calls[0]) + } + if got, _ := calls[0].Input["file_path"].(string); got != "/tmp/input.txt" { + t.Fatalf("expected file_path to parse, got %q", got) + } +} + +func TestParseToolCallsDoesNotAcceptDSMLSpaceLookalikeTagName(t *testing.T) { + text := strings.Join([]string{ + "<|DSML tool_calls_extra>", + "<|DSML invoke name=\"Read\">", + "<|DSML parameter name=\"file_path\">/tmp/input.txt", + "", + "", + }, "\n") + calls := ParseToolCalls(text, []string{"Read"}) + if len(calls) != 0 { + t.Fatalf("expected no calls from lookalike tag, got %#v", calls) + } +} + +func TestParseToolCallsToleratesDSMLCollapsedTagNames(t *testing.T) { + todos := `[x] 检查 toolcalls_format.go 格式化逻辑 +[x] 检查 toolcalls_parse.go 解析逻辑 +[x] 检查 toolcalls_xml.go 和 toolcalls_dsml.go +[x] 检查 toolcalls_markup.go 和 toolcalls_json_repair.go +[x] 检查 prompt/tool_calls.go 注入逻辑 +[x] 检查 toolstream 流式解析 +[x] 查看测试文件确认预期行为 +[x] 给出调查结论` + text := strings.Join([]string{ + "[]", + "", + "", + "", + "", + "", + }, "\n") + calls := ParseToolCalls(text, []string{"update_todo_list"}) + if len(calls) != 1 { + t.Fatalf("expected one call from collapsed DSML tags, got %#v", calls) + } + if calls[0].Name != "update_todo_list" { + t.Fatalf("expected update_todo_list call, got %#v", calls[0]) + } + if got, _ := calls[0].Input["todos"].(string); got != todos { + t.Fatalf("expected todos to round-trip, got %q", got) + } +} + +func TestParseToolCallsDoesNotAcceptDSMLCollapsedLookalikeTagName(t *testing.T) { + text := strings.Join([]string{ + "", + "", + "x", + "", + "", + }, "\n") + calls := ParseToolCalls(text, []string{"update_todo_list"}) + if len(calls) != 0 { + t.Fatalf("expected no calls from collapsed lookalike tag, got %#v", calls) + } +} + +func TestParseToolCallsSkipsProseMentionOfSameWrapperVariant(t *testing.T) { + text := strings.Join([]string{ + "Summary: support canonical and DSML <|DSML|tool_calls> wrappers.", + "", + "<|DSML|tool_calls>", + "<|DSML|invoke name=\"Bash\">", + "<|DSML|parameter name=\"command\">", + "", + "", + }, "\n") + res := ParseToolCallsDetailed(text, []string{"Bash"}) + if len(res.Calls) != 1 { + t.Fatalf("expected one parsed call after prose mention, got %#v", res.Calls) + } + if res.Calls[0].Name != "Bash" { + t.Fatalf("expected Bash call, got %#v", res.Calls[0]) + } + if got, _ := res.Calls[0].Input["command"].(string); got != "git status" { + t.Fatalf("expected command to parse, got %q", got) + } +} + +func TestTurkishILowercaseMapping(t *testing.T) { + tests := []struct { + name string + text string + start int + wantOk bool + wantName string + }{ + {"turkish_i_at_name_start", "İ", 0, false, ""}, + {"turkish_i_at_name_end", "", 0, false, ""}, + {"turkish_i_before_tag", "İ", 0, false, ""}, + {"normal_tool_calls", "", 0, true, "tool_calls"}, + {"normal_invoke", "", 0, true, "invoke"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, ok := FindToolMarkupTagOutsideIgnored(tt.text, tt.start) + if ok != tt.wantOk { + t.Errorf("FindToolMarkupTagOutsideIgnored(%q, %d) ok = %v, want %v", tt.text, tt.start, ok, tt.wantOk) + return + } + if ok && got.Name != tt.wantName { + t.Errorf("FindToolMarkupTagOutsideIgnored(%q, %d) name = %q, want %q", tt.text, tt.start, got.Name, tt.wantName) + } + }) + } +} + +func TestSkipXMLIgnoredSectionBoundaryConditions(t *testing.T) { + text := "hello" + + tests := []struct { + name string + i int + wantNext int + wantAdv bool + wantBlk bool + }{ + {"valid_index", 2, 2, false, false}, + {"at_end_equal_len", 5, 5, false, false}, + {"beyond_end", 6, 6, false, false}, + {"negative", -1, -1, false, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + next, adv, blk := skipXMLIgnoredSection(text, tt.i) + if next != tt.wantNext || adv != tt.wantAdv || blk != tt.wantBlk { + t.Errorf("skipXMLIgnoredSection(%q, %d) = (%d, %v, %v), want (%d, %v, %v)", + text, tt.i, next, adv, blk, tt.wantNext, tt.wantAdv, tt.wantBlk) + } + }) + } +} + +func TestSkipXMLIgnoredSectionCommentWithUnicodeKeepsByteOffset(t *testing.T) { + text := "x" + + next, adv, blk := skipXMLIgnoredSection(text, 0) + if blk || !adv { + t.Fatalf("skipXMLIgnoredSection() = (%d, %v, %v), want advanced unblocked comment", next, adv, blk) + } + if want := len(""); next != want { + t.Fatalf("skipXMLIgnoredSection() next = %d, want %d", next, want) + } +} + +func TestSkipXMLIgnoredSectionMatchesCDATAWithoutAllocatingTail(t *testing.T) { + text := "]]>" + + next, adv, blk := skipXMLIgnoredSection(text, 0) + if blk || !adv { + t.Fatalf("skipXMLIgnoredSection() = (%d, %v, %v), want advanced unblocked CDATA", next, adv, blk) + } + if want := len("]]>"); next != want { + t.Fatalf("skipXMLIgnoredSection() next = %d, want %d", next, want) + } + + tag, ok := FindToolMarkupTagOutsideIgnored(text, 0) + if !ok { + t.Fatal("expected tool tag after skipped CDATA") + } + if tag.Start != next { + t.Fatalf("FindToolMarkupTagOutsideIgnored() start = %d, want %d", tag.Start, next) + } +} + +func TestFindToolCDATAEndBoundaryConditions(t *testing.T) { + text := "" + + tests := []struct { + name string + from int + wantResult int + }{ + {"valid", 12, 14}, + {"at_end", 17, -1}, + {"beyond_end", 18, -1}, + {"negative", -1, -1}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := findToolCDATAEnd(text, tt.from) + if got != tt.wantResult { + t.Errorf("findToolCDATAEnd(%q, %d) = %d, want %d", + text, tt.from, got, tt.wantResult) + } + }) + } +} + +func TestFindMatchingToolMarkupCloseBoundaryConditions(t *testing.T) { + tests := []struct { + name string + text string + open ToolMarkupTag + wantOk bool + }{ + {"empty_text", "", ToolMarkupTag{Name: "tool_calls", End: 0}, false}, + {"open_end_beyond_text", "hello", ToolMarkupTag{Name: "tool_calls", End: 100}, false}, + {"open_end_equals_len", "hello", ToolMarkupTag{Name: "tool_calls", End: 5}, false}, + {"valid_simple", "", ToolMarkupTag{Name: "tool_calls", End: 11}, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, ok := FindMatchingToolMarkupClose(tt.text, tt.open) + if ok != tt.wantOk { + t.Errorf("FindMatchingToolMarkupClose(%q, %+v) ok = %v, want %v", tt.text, tt.open, ok, tt.wantOk) + } + }) + } +} + +func TestParseToolCallsSupportsDSMLShellWithFullwidthClosingSlash(t *testing.T) { + text := `<|DSML|tool_calls><|DSML|invoke name="execute_code"><|DSML|parameter name="code"></DSML|tool_calls>` + calls := ParseToolCalls(text, []string{"execute_code"}) + if len(calls) != 1 { + t.Fatalf("expected 1 DSML call with fullwidth closing slash, got %#v", calls) + } + if calls[0].Name != "execute_code" || calls[0].Input["code"] != `print("hi")` { + t.Fatalf("unexpected fullwidth-closing-slash DSML parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsSupportsDSMLShellWithSentencePieceSeparatorAndFullwidthGT(t *testing.T) { + text := `<|DSML▁tool_calls|><|DSML▁invoke▁name="execute_code"><|DSML▁parameter▁name="code"></DSML|parameter></DSML|invoke></DSML|tool_calls>` + calls := ParseToolCalls(text, []string{"execute_code"}) + if len(calls) != 1 { + t.Fatalf("expected 1 DSML call with fullwidth opening delimiter and Unicode attribute confusables, got %#v", calls) + } + if calls[0].Name != "execute_code" || calls[0].Input["code"] != `print("hi")` { + t.Fatalf("unexpected fullwidth-opening/Unicode-attr DSML parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsCanonicalizesConfusableCandidateShellOnly(t *testing.T) { + text := "<|\u200b\uff24\u0405\u039cL|to\u03bfl\uff3fcalls>" + + "<|\ufeffDSML|inv\u03bfk\u0435 n\u0430me\uff1d\u201cexecute_code\u201d>" + + "<|\u200bDSML|par\u0430meter n\u0430me\uff1d\u201ccode\u201d>" + + "" + calls := ParseToolCalls(text, []string{"execute_code"}) + if len(calls) != 1 { + t.Fatalf("expected one confusable-shell call, got %#v", calls) + } + if calls[0].Name != "execute_code" || calls[0].Input["code"] != `print("hi")` { + t.Fatalf("unexpected confusable-shell parse result: %#v", calls[0]) + } +} + +func TestParseToolCallsKeepsConfusableMarkupInsideCDATAAsText(t *testing.T) { + value := "literal" + text := "" + calls := ParseToolCalls(text, []string{"Write"}) + if len(calls) != 1 { + t.Fatalf("expected one Write call, got %#v", calls) + } + if got, _ := calls[0].Input["description"].(string); got != value { + t.Fatalf("expected confusable markup example inside CDATA to stay raw, got %q", got) + } +} + +func TestParseToolCallsRepairsMissingOpeningWrapperWithConfusableShell(t *testing.T) { + text := "Before tool call\n" + + "\n" + + "\n" + + "after" + res := ParseToolCallsDetailed(text, []string{"read_file"}) + if len(res.Calls) != 1 { + t.Fatalf("expected repaired confusable wrapper to parse one call, got %#v", res) + } + if got, _ := res.Calls[0].Input["path"].(string); got != "README.md" { + t.Fatalf("expected repaired confusable wrapper to preserve args, got %#v", res.Calls[0].Input) + } + if !res.SawToolCallSyntax { + t.Fatalf("expected repaired confusable wrapper to mark tool syntax seen, got %#v", res) + } +} + +func TestParseToolCallsDoesNotAcceptConfusableNearMissTagName(t *testing.T) { + text := "pwd" + calls := ParseToolCalls(text, []string{"execute_code"}) + if len(calls) != 0 { + t.Fatalf("expected confusable near-miss tag name to remain non-executable, got %#v", calls) + } +} + +func TestFindMatchingToolMarkupCloseBoundaryConditionsSupportsConfusableDelimiters(t *testing.T) { + tests := []struct { + name string + text string + open ToolMarkupTag + wantOk bool + }{ + {"valid_fullwidth_closing_slash", "</tool_calls>", ToolMarkupTag{Name: "tool_calls", End: 11}, true}, + {"valid_fullwidth_opening_delimiter", "<tool_calls></tool_calls>", ToolMarkupTag{Name: "tool_calls", End: len("<tool_calls>") - 1}, true}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + _, ok := FindMatchingToolMarkupClose(tt.text, tt.open) + if ok != tt.wantOk { + t.Errorf("FindMatchingToolMarkupClose(%q, %+v) ok = %v, want %v", tt.text, tt.open, ok, tt.wantOk) + } + }) + } +} diff --git a/internal/toolcall/toolcalls_xml.go b/internal/toolcall/toolcalls_xml.go new file mode 100644 index 0000000000000000000000000000000000000000..c29dec0b51e8a43d37458ad66a11b921cdc55bbc --- /dev/null +++ b/internal/toolcall/toolcalls_xml.go @@ -0,0 +1,175 @@ +package toolcall + +import ( + "encoding/xml" + "html" + "strings" +) + +func parseStructuredToolCallInput(raw string) map[string]any { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return map[string]any{} + } + + if strings.HasPrefix(trimmed, "<") { + if parsed, ok := parseXMLFragmentValue(trimmed); ok { + switch v := parsed.(type) { + case map[string]any: + if len(v) > 0 { + return v + } + return map[string]any{} + case string: + text := strings.TrimSpace(v) + if text == "" { + return map[string]any{} + } + if parsedText := parseToolCallInput(text); len(parsedText) > 0 { + if isOnlyRawValue(parsedText, text) { + // Plain text content, keep it as raw text. + } else { + return parsedText + } + } + return map[string]any{"_raw": v} + } + } + + if kv := parseMarkupKVObject(trimmed); len(kv) > 0 { + return kv + } + } + + if kv := parseMarkupKVObject(trimmed); len(kv) > 0 { + return kv + } + + if parsed := parseToolCallInput(trimmed); len(parsed) > 0 { + return parsed + } + + return map[string]any{"_raw": html.UnescapeString(trimmed)} +} + +func parseXMLFragmentValue(raw string) (any, bool) { + trimmed := strings.TrimSpace(raw) + if trimmed == "" { + return "", true + } + + dec := xml.NewDecoder(strings.NewReader("" + trimmed + "")) + tok, err := dec.Token() + if err != nil { + return nil, false + } + start, ok := tok.(xml.StartElement) + if !ok || !strings.EqualFold(start.Name.Local, "root") { + return nil, false + } + + value, err := parseXMLNodeValue(dec, start) + if err != nil { + return nil, false + } + return value, true +} + +func parseXMLNodeValue(dec *xml.Decoder, start xml.StartElement) (any, error) { + children := map[string]any{} + var text strings.Builder + hasChild := false + + for { + tok, err := dec.Token() + if err != nil { + return nil, err + } + switch t := tok.(type) { + case xml.CharData: + s := string([]byte(t)) + if hasChild && strings.TrimSpace(s) == "" { + continue + } + text.WriteString(s) + case xml.StartElement: + if !hasChild && strings.TrimSpace(text.String()) == "" { + text.Reset() + } + hasChild = true + child, err := parseXMLNodeValue(dec, t) + if err != nil { + return nil, err + } + appendXMLChildValue(children, t.Name.Local, child) + case xml.EndElement: + if t.Name.Local != start.Name.Local { + return nil, errXMLMismatch(start.Name.Local, t.Name.Local) + } + if len(children) == 0 { + if parsed, ok := parseJSONLiteralValue(text.String()); ok { + return parsed, nil + } + return text.String(), nil + } + if txt := text.String(); strings.TrimSpace(txt) != "" { + if parsed, ok := parseJSONLiteralValue(txt); ok { + children["_text"] = parsed + } else { + children["_text"] = txt + } + } + if len(children) == 1 { + if items, ok := children["item"]; ok { + switch v := items.(type) { + case []any: + return v, nil + default: + return []any{v}, nil + } + } + } + return children, nil + } + } +} + +func appendXMLChildValue(dst map[string]any, key string, value any) { + if key == "" { + return + } + if existing, ok := dst[key]; ok { + switch current := existing.(type) { + case []any: + dst[key] = append(current, value) + default: + dst[key] = []any{current, value} + } + return + } + dst[key] = value +} + +func isOnlyRawValue(m map[string]any, raw string) bool { + if len(m) != 1 { + return false + } + v, ok := m["_raw"].(string) + if !ok { + return false + } + return strings.TrimSpace(v) == strings.TrimSpace(raw) +} + +type xmlMismatchError struct { + want string + got string +} + +func (e xmlMismatchError) Error() string { + return "mismatched xml end tag: want " + e.want + ", got " + e.got +} + +func errXMLMismatch(want, got string) error { + return xmlMismatchError{want: want, got: got} +} diff --git a/internal/toolstream/complex_edge_test.go b/internal/toolstream/complex_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5e400d00f6d05a67481bf79c7ff1a8b8bdf5fc91 --- /dev/null +++ b/internal/toolstream/complex_edge_test.go @@ -0,0 +1,727 @@ +package toolstream + +import ( + "strings" + "testing" +) + +// ---- 错位工具块 ---- + +// 只有 没有 +func TestSieve_MismatchedClose_OnlyClosingTag(t *testing.T) { + var state State + chunks := []string{ + "一些正文内容\n", + "\n", + "后续内容", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var text strings.Builder + tc := 0 + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + if tc != 0 { + t.Fatalf("孤立闭合标签不应触发工具调用,got %d", tc) + } + if !strings.Contains(text.String(), "一些正文") || !strings.Contains(text.String(), "后续内容") { + t.Fatalf("应保留所有文本, got %q", text.String()) + } +} + +// 打开后跟的不是 而是普通文本 +func TestSieve_ToolCallsWrapperWithNoInvoke(t *testing.T) { + var state State + chunks := []string{ + "\n", + "这里没有 invoke 标签\n", + "\n", + "后续内容", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var text strings.Builder + tc := 0 + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + if tc != 0 { + t.Fatalf("无 invoke 不应触发工具调用,got %d", tc) + } +} + +// 两个连续工具调用块 +func TestSieve_TwoConsecutiveToolCallBlocks(t *testing.T) { + var state State + chunks := []string{ + `a.txt`, + "\n", + `b.txt`, + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + tc := 0 + for _, e := range events { + tc += len(e.ToolCalls) + } + if tc != 2 { + t.Fatalf("应解析出两个工具调用,got %d, events=%#v", tc, events) + } +} + +// ---- 围栏内的工具调用不应触发 ---- + +// 反引号围栏内有完整工具调用 + 围栏外有真正的工具调用 +func TestSieve_FencedExampleThenRealToolCall(t *testing.T) { + var state State + chunks := []string{ + "示例:\n```xml\n", + `1`, + "\n```\n", + `real.txt`, + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file", "fake"})...) + } + events = append(events, Flush(&state, []string{"read_file", "fake"})...) + + var text strings.Builder + tc := 0 + var names []string + for _, e := range events { + text.WriteString(e.Content) + for _, call := range e.ToolCalls { + tc++ + names = append(names, call.Name) + } + } + if tc != 1 { + t.Fatalf("应只触发围栏外的工具调用,got %d, names=%v", tc, names) + } + if names[0] != "read_file" { + t.Fatalf("应触发 read_file,got %v", names) + } + if !strings.Contains(text.String(), "示例") { + t.Fatalf("围栏前文本应保留, got %q", text.String()) + } +} + +// 波浪线围栏包裹工具调用 +func TestSieve_TildeFencedToolCallIgnored(t *testing.T) { + var state State + chunks := []string{ + "~~~\n", + `x`, + "\n~~~\n", + "结束", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + tc := 0 + var text strings.Builder + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + if tc != 0 { + t.Fatalf("波浪线围栏内工具调用不应触发,got %d", tc) + } + if !strings.Contains(text.String(), "结束") { + t.Fatalf("围栏后文本应保留, got %q", text.String()) + } +} + +// 4 反引号嵌套 3 反引号,内含工具标签 +func TestSieve_FourBacktickNestedThreeWithToolCall(t *testing.T) { + var state State + chunks := []string{ + "````markdown\n", + "```xml\n", + `x`, + "\n```\n", + "````\n", + "外部文本", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + tc := 0 + var text strings.Builder + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + if tc != 0 { + t.Fatalf("4反引号嵌套内的工具调用不应触发,got %d", tc) + } + if !strings.Contains(text.String(), "外部文本") { + t.Fatalf("围栏外文本应保留, got %q", text.String()) + } +} + +// ---- DSML 变体在围栏内不触发 ---- + +func TestSieve_DSMLInsideFenceIgnored(t *testing.T) { + var state State + chunks := []string{ + "```\n", + "<|DSML|tool_calls>\n", + `<|DSML|invoke name="read_file">`, + `<|DSML|parameter name="path">x`, + "\n", + "\n", + "```\n", + "结束", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + tc := 0 + for _, e := range events { + tc += len(e.ToolCalls) + } + if tc != 0 { + t.Fatalf("围栏内的 DSML 工具调用不应触发,got %d", tc) + } +} + +// ---- 工具调用前后有丰富文本 ---- + +func TestSieve_RichTextAroundToolCall(t *testing.T) { + var state State + chunks := []string{ + "我来帮你查看文件内容。\n\n", + "首先读取 README:\n", + `README.md`, + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var text strings.Builder + tc := 0 + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + if tc != 1 { + t.Fatalf("应有一个工具调用,got %d", tc) + } + if !strings.Contains(text.String(), "帮你查看") { + t.Fatalf("前置文本丢失, got %q", text.String()) + } + if strings.Contains(text.String(), "\n", + `` + "\n", + `test.md` + "\n", + `` + "\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"write_file"})...) + } + events = append(events, Flush(&state, []string{"write_file"})...) + + var text strings.Builder + tc := 0 + var gotContent any + for _, e := range events { + text.WriteString(e.Content) + if len(e.ToolCalls) > 0 { + tc += len(e.ToolCalls) + gotContent = e.ToolCalls[0].Input["content"] + } + } + if tc != 1 { + t.Fatalf("应有一个工具调用,got %d", tc) + } + content, _ := gotContent.(string) + if content != payload { + t.Fatalf("CDATA 内围栏内容应完整保留,got %q want %q", content, payload) + } + if text.Len() != 0 { + t.Fatalf("不应有文本泄漏, got %q", text.String()) + } +} + +// ---- 极端 token 拆分 ---- + +// 工具标签被拆成单字符流式到达 +func TestSieve_CharByCharToolCall(t *testing.T) { + var state State + full := `go.mod` + var events []Event + for _, ch := range full { + events = append(events, ProcessChunk(&state, string(ch), []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var text strings.Builder + tc := 0 + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + if tc != 1 { + t.Fatalf("单字符流式应解析出工具调用,got %d", tc) + } + if strings.Contains(text.String(), "invoke") { + t.Fatalf("标签泄漏, got %q", text.String()) + } +} + +// ---- 混合格式变体 ---- + +// 全宽竖线 wrapper + DSML invoke +func TestSieve_FullwidthPipeWrapperDSMLInvoke(t *testing.T) { + var state State + chunks := []string{ + "<|tool_calls>\n", + "<|DSML|invoke name=\"read_file\">\n", + "<|DSML|parameter name=\"path\">README.md\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var text strings.Builder + tc := 0 + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + if tc != 1 { + t.Fatalf("全宽+DSML混合应解析成功,got %d", tc) + } + if strings.Contains(strings.ToLower(text.String()), "dsml") { + t.Fatalf("DSML 标签泄漏, got %q", text.String()) + } +} + +// ---- 未闭合工具块应回退为文本 ---- + +func TestSieve_UnclosedToolCallBlockFallsBack(t *testing.T) { + var state State + chunks := []string{ + "\n", + `` + "\n", + `README.md` + "\n", + // 缺少 + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var text strings.Builder + tc := 0 + for _, e := range events { + text.WriteString(e.Content) + tc += len(e.ToolCalls) + } + // 未闭合的应回退为文本,不应丢失 + if text.String() == "" { + t.Fatalf("未闭合工具块不应丢失所有内容") + } + if tc != 0 { + t.Fatalf("未闭合工具块不应解析出工具调用,got %d", tc) + } +} + +// ---- 文本中 mention 标签变体名 + 真正的工具调用 ---- + +// 模型输出 commit message 文本中包含 等 mention, +// 紧随其后是真正的 DSML 工具调用。mention 的变体和实际工具调用变体不同。 +func TestSieve_TagMentionInTextThenRealToolCall(t *testing.T) { + var state State + chunks := []string{ + "建议的 commit message:\n\nfeat: expand DSML alias support\n\n", + "Add support for , ", + "<|tool_calls> (pipe alias),\n", + "and <|tool_calls> wrapper variants.\n\n", + "<|DSML|tool_calls>\n", + "<|DSML|invoke name=\"Bash\">\n", + "<|DSML|parameter name=\"command\">\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var text strings.Builder + tc := 0 + var names []string + for _, e := range events { + text.WriteString(e.Content) + for _, call := range e.ToolCalls { + tc++ + names = append(names, call.Name) + } + } + + if tc != 1 { + t.Fatalf("应解析出 1 个工具调用,got %d, text=%q", tc, text.String()) + } + if names[0] != "Bash" { + t.Fatalf("应解析出 Bash,got %v", names) + } + if !strings.Contains(text.String(), "commit message") { + t.Fatalf("前置文本应保留, got %q", text.String()) + } +} + +func TestSieve_SameVariantTagMentionInTextThenRealToolCall(t *testing.T) { + var state State + chunks := []string{ + "Summary: support canonical and DSML <|DSML|tool_calls> wrappers.\n\n", + "<|DSML|tool_calls>\n", + "<|DSML|invoke name=\"Bash\">\n", + "<|DSML|parameter name=\"command\">\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var text strings.Builder + var callName string + var command string + callCount := 0 + for _, e := range events { + text.WriteString(e.Content) + for _, call := range e.ToolCalls { + callCount++ + callName = call.Name + command, _ = call.Input["command"].(string) + } + } + + if callCount != 1 { + t.Fatalf("应解析出 1 个工具调用,got %d, text=%q", callCount, text.String()) + } + if callName != "Bash" { + t.Fatalf("应解析出 Bash,got %q", callName) + } + if command != "git status" { + t.Fatalf("应解析出 command,got %q", command) + } + if !strings.Contains(text.String(), "Summary:") { + t.Fatalf("前置文本应保留, got %q", text.String()) + } +} + +func TestSieve_ReviewSampleWithAliasMentionsPreservesBodyAndToolCalls(t *testing.T) { + var state State + chunks := []string{ + "Done reviewing the diff. Here's my analysis before we commit:\n\n", + "Summary of Changes\n", + "DSML wrapper variant support — recognize aliases (, <|tool_calls>) alongside canonical and <|DSML|tool_calls> wrappers.\n\n", + "<|DSML|tool_calls>\n", + "<|DSML|invoke name=\"Bash\">\n", + "<|DSML|parameter name=\"command\">\n", + "<|DSML|parameter name=\"description\">\n", + "\n", + "<|DSML|invoke name=\"Bash\">\n", + "<|DSML|parameter name=\"command\"> and <|tool_calls> alongside existing canonical wrappers.\nEOF\n)\"]]>\n", + "<|DSML|parameter name=\"description\">\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var text strings.Builder + var commands []string + for _, e := range events { + text.WriteString(e.Content) + for _, call := range e.ToolCalls { + if call.Name == "Bash" { + cmd, _ := call.Input["command"].(string) + commands = append(commands, cmd) + } + } + } + + if len(commands) != 2 { + t.Fatalf("应解析出 2 个 Bash 工具调用,got %d, text=%q", len(commands), text.String()) + } + if !strings.Contains(text.String(), "<|DSML|tool_calls> wrappers") { + t.Fatalf("正文中的 DSML mention 应保留, got %q", text.String()) + } + if !strings.Contains(text.String(), "Summary of Changes") { + t.Fatalf("前置正文应完整保留, got %q", text.String()) + } + if strings.Contains(text.String(), "git add docs/toolcall-semantics.md") { + t.Fatalf("真实工具参数不应泄漏到正文, got %q", text.String()) + } + if !strings.Contains(commands[0], "git add") || !strings.Contains(commands[1], "git commit") { + t.Fatalf("工具参数解析不符合预期, got %#v", commands) + } +} + +func TestSieve_ChineseReviewSamplePreservesInlineDSMLMention(t *testing.T) { + var state State + chunks := []string{ + "# Context from my IDE setup:\n\n## My request for Codex:\n", + "基于我的审查,这是工作区更改的总结和提交。\n\n## 审查报告\n\n### 文档\n\nAPI.md 中的工具调用部分缺少针对新 DSML 别名的更新——它只提到了 `", + "<|DSML|tool_calls>` 和 canonical ``。由于这涉及 API 兼容性和文档准确性,需要在下游进行记录。\n\n", + "### 代码\n\n所有更改现在一致地处理四个 DSML wrapper 变体。\n\n现在提交已暂存的更改。\n\n", + "<|DSML|tool_calls>\n", + " <|DSML|invoke name=\"Bash\">\n", + " <|DSML|parameter name=\"command\">\n", + " <|DSML|parameter name=\"description\">\n", + " \n", + "\n\n补充", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var text strings.Builder + callCount := 0 + for _, e := range events { + text.WriteString(e.Content) + callCount += len(e.ToolCalls) + } + + if callCount != 1 { + t.Fatalf("应解析出 1 个工具调用,got %d, text=%q", callCount, text.String()) + } + want := "它只提到了 `<|DSML|tool_calls>` 和 canonical ``。由于这涉及 API 兼容性" + if !strings.Contains(text.String(), want) { + t.Fatalf("正文不应在 inline DSML mention 处截断, want contains %q, got %q", want, text.String()) + } + if !strings.Contains(text.String(), "补充") { + t.Fatalf("工具块后的正文应保留, got %q", text.String()) + } + if strings.Contains(text.String(), "<|DSML|invoke") { + t.Fatalf("真实工具块不应泄漏到正文, got %q", text.String()) + } +} + +func TestSieve_HyphenatedDSMLShellWithHereDocCDATA(t *testing.T) { + var state State + chunks := []string{ + "\n", + "\n", + "\n", + "\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var text strings.Builder + var command string + callCount := 0 + for _, e := range events { + text.WriteString(e.Content) + for _, call := range e.ToolCalls { + callCount++ + command, _ = call.Input["command"].(string) + } + } + + if callCount != 1 { + t.Fatalf("应解析出 1 个 hyphenated DSML 工具调用,got %d, text=%q", callCount, text.String()) + } + if !strings.Contains(command, `git commit -m "$(cat <<'EOF'`) || !strings.Contains(command, "Co-Authored-By: Claude Opus 4.7") { + t.Fatalf("here-doc command 未完整保留, got %q", command) + } + if strings.Contains(text.String(), "dsml-tool-calls") || strings.Contains(text.String(), "git commit -m") { + t.Fatalf("真实工具块不应泄漏到正文, got %q", text.String()) + } +} + +func TestSieve_ToleratesDSMLSpaceSeparatorTypo(t *testing.T) { + var state State + chunks := []string{ + "准备读取文件。\n", + "<|DSML tool_calls>\n", + "<|DSML invoke name=\"Read\">\n", + "<|DSML parameter name=\"file_path\">\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Read"})...) + } + events = append(events, Flush(&state, []string{"Read"})...) + + var text strings.Builder + var filePath string + callCount := 0 + for _, e := range events { + text.WriteString(e.Content) + for _, call := range e.ToolCalls { + callCount++ + filePath, _ = call.Input["file_path"].(string) + } + } + + if callCount != 1 { + t.Fatalf("应解析出 1 个工具调用,got %d, text=%q", callCount, text.String()) + } + if filePath != "/tmp/input.txt" { + t.Fatalf("应解析出 file_path,got %q", filePath) + } + if !strings.Contains(text.String(), "准备读取文件") { + t.Fatalf("前置正文应保留, got %q", text.String()) + } + if strings.Contains(text.String(), "<|DSML invoke") { + t.Fatalf("真实工具块不应泄漏到正文, got %q", text.String()) + } +} + +func TestSieve_DSMLSpaceLookalikeTagNameStaysText(t *testing.T) { + var state State + input := "<|DSML tool_calls_extra><|DSML invoke name=\"Read\"><|DSML parameter name=\"file_path\">/tmp/input.txt" + events := ProcessChunk(&state, input, []string{"Read"}) + events = append(events, Flush(&state, []string{"Read"})...) + + var text strings.Builder + callCount := 0 + for _, e := range events { + text.WriteString(e.Content) + callCount += len(e.ToolCalls) + } + if callCount != 0 { + t.Fatalf("相似标签名不应触发工具调用,got %d", callCount) + } + if text.String() != input { + t.Fatalf("相似标签名应作为正文透传, got %q", text.String()) + } +} + +func TestSieve_DSMLCollapsedTagNamesWithPrefixText(t *testing.T) { + var state State + todos := `[x] 检查 toolcalls_format.go 格式化逻辑 +[x] 检查 toolcalls_parse.go 解析逻辑 +[x] 检查 toolcalls_xml.go 和 toolcalls_dsml.go +[x] 检查 toolcalls_markup.go 和 toolcalls_json_repair.go +[x] 检查 prompt/tool_calls.go 注入逻辑 +[x] 检查 toolstream 流式解析 +[x] 查看测试文件确认预期行为 +[x] 给出调查结论` + chunks := []string{ + "[]\n", + "\n", + "\n", + "\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"update_todo_list"})...) + } + events = append(events, Flush(&state, []string{"update_todo_list"})...) + + var text strings.Builder + var gotTodos string + callCount := 0 + for _, e := range events { + text.WriteString(e.Content) + for _, call := range e.ToolCalls { + callCount++ + gotTodos, _ = call.Input["todos"].(string) + } + } + if callCount != 1 { + t.Fatalf("应解析出 1 个工具调用,got %d, text=%q", callCount, text.String()) + } + if gotTodos != todos { + t.Fatalf("todos 应完整保留,got %q", gotTodos) + } + if text.String() != "[]\n" { + t.Fatalf("前置正文应完整保留且不泄漏工具块, got %q", text.String()) + } +} + +func TestSieve_DSMLCollapsedLookalikeTagNameStaysText(t *testing.T) { + var state State + input := "x" + events := ProcessChunk(&state, input, []string{"update_todo_list"}) + events = append(events, Flush(&state, []string{"update_todo_list"})...) + + var text strings.Builder + callCount := 0 + for _, e := range events { + text.WriteString(e.Content) + callCount += len(e.ToolCalls) + } + if callCount != 0 { + t.Fatalf("相似 collapsed 标签名不应触发工具调用,got %d", callCount) + } + if text.String() != input { + t.Fatalf("相似 collapsed 标签名应作为正文透传, got %q", text.String()) + } +} diff --git a/internal/toolstream/fence_edge_sieve_test.go b/internal/toolstream/fence_edge_sieve_test.go new file mode 100644 index 0000000000000000000000000000000000000000..035108d081e42ad28938bd9631df2b8073010f80 --- /dev/null +++ b/internal/toolstream/fence_edge_sieve_test.go @@ -0,0 +1,179 @@ +package toolstream + +import ( + "strings" + "testing" +) + +// 波浪线围栏内的工具调用标签不应触发工具调用 +func TestProcessToolSieveTildeFenceDoesNotTriggerToolCall(t *testing.T) { + var state State + chunks := []string{ + "示例:\n~~~xml\n", + "README.md\n", + "~~~\n", + "完毕。", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected tilde-fenced tool example to stay text, got %d tool calls", toolCalls) + } + if !strings.Contains(textContent.String(), "示例") || !strings.Contains(textContent.String(), "完毕") { + t.Fatalf("expected surrounding text preserved, got %q", textContent.String()) + } +} + +// 4 反引号嵌套 3 反引号(内含工具标签)不应触发 +func TestProcessToolSieveNestedFourBacktickFenceDoesNotTrigger(t *testing.T) { + var state State + input := "说明:\n````xml\n```\nx\n```\n````\n结束。" + chunks := strings.SplitAfter(input, "\n") + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected 4-backtick fenced example to stay text, got %d tool calls", toolCalls) + } +} + +func TestProcessToolSieveMarkdownDocumentationExamplesDoNotTrigger(t *testing.T) { + var state State + chunks := []string{ + "解析器支持多种工具调用格式。\n\n", + "入口函数 `ParseToolCalls(text, availableToolNames)` 会返回调用列表。\n\n", + "核心流程会解析 XML 格式的 `` / `` 标记。\n\n", + "### 标准 XML 结构\n", + "```xml\n", + "\n", + " \n", + " config.json\n", + " \n", + "\n", + "```\n\n", + "DSML 风格形如 `...`,也可能提到 `` 包裹。\n", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected markdown documentation examples to stay text, got %d tool calls", toolCalls) + } + if !strings.Contains(textContent.String(), "标准 XML 结构") || !strings.Contains(textContent.String(), "DSML 风格") { + t.Fatalf("expected documentation text preserved, got %q", textContent.String()) + } +} + +func TestProcessToolSieveInlineMarkdownToolCallSplitAcrossChunksDoesNotTrigger(t *testing.T) { + var state State + chunks := []string{ + "示例:`", + "README.md", + "` 完毕。", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected split inline markdown tool example to stay text, got %d tool calls", toolCalls) + } + if !strings.Contains(textContent.String(), "") || !strings.Contains(textContent.String(), "完毕") { + t.Fatalf("expected inline example text preserved, got %q", textContent.String()) + } +} + +func TestProcessToolSieveUnclosedInlineMarkdownBeforeToolDoesTrigger(t *testing.T) { + var state State + input := "note with stray ` before real call " + + "real.md" + + var events []Event + events = append(events, ProcessChunk(&state, input, []string{"read_file"})...) + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + var calls []string + for _, evt := range events { + textContent.WriteString(evt.Content) + for _, call := range evt.ToolCalls { + if path, _ := call.Input["path"].(string); path != "" { + calls = append(calls, path) + } + } + } + + if len(calls) != 1 || calls[0] != "real.md" { + t.Fatalf("expected real tool call after stray backtick, got %#v from events %#v", calls, events) + } + if !strings.Contains(textContent.String(), "stray ` before real call") { + t.Fatalf("expected stray-backtick prefix preserved, got %q", textContent.String()) + } +} + +func TestProcessToolSieveUnclosedInlineMarkdownBeforeSplitToolDoesTriggerOnFlush(t *testing.T) { + var state State + chunks := []string{ + "note with stray ` before real call ", + "real.md", + } + + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var calls []string + for _, evt := range events { + for _, call := range evt.ToolCalls { + if path, _ := call.Input["path"].(string); path != "" { + calls = append(calls, path) + } + } + } + + if len(calls) != 1 || calls[0] != "real.md" { + t.Fatalf("expected split real tool call after stray backtick, got %#v from events %#v", calls, events) + } +} diff --git a/internal/toolstream/tool_sieve_core.go b/internal/toolstream/tool_sieve_core.go new file mode 100644 index 0000000000000000000000000000000000000000..a0791f4da8105d774cad7c9dff66a2236ba711c9 --- /dev/null +++ b/internal/toolstream/tool_sieve_core.go @@ -0,0 +1,310 @@ +package toolstream + +import "ds2api/internal/toolcall" + +func ProcessChunk(state *State, chunk string, toolNames []string) []Event { + if state == nil { + return nil + } + if chunk != "" { + state.pending.WriteString(chunk) + } + events := make([]Event, 0, 2) + if len(state.pendingToolCalls) > 0 { + events = append(events, Event{ToolCalls: state.pendingToolCalls}) + state.pendingToolRaw = "" + state.pendingToolCalls = nil + } + + for { + if state.capturing { + if state.pending.Len() > 0 { + state.capture.WriteString(state.pending.String()) + state.pending.Reset() + } + prefix, calls, suffix, ready := consumeToolCapture(state, toolNames) + if !ready { + break + } + captured := state.capture.String() + state.capture.Reset() + state.capturing = false + state.resetIncrementalToolState() + if len(calls) > 0 { + if prefix != "" { + state.noteText(prefix) + events = append(events, Event{Content: prefix}) + } + if suffix != "" { + state.pending.WriteString(suffix) + } + _ = captured + state.pendingToolCalls = calls + continue + } + if prefix != "" { + state.noteText(prefix) + events = append(events, Event{Content: prefix}) + } + if suffix != "" { + state.pending.WriteString(suffix) + } + continue + } + + pending := state.pending.String() + if pending == "" { + break + } + start := findToolSegmentStart(state, pending) + if start == holdToolSegmentStart { + break + } + if start >= 0 { + prefix := pending[:start] + if prefix != "" { + resetMarkdownSpan := shouldResetUnclosedMarkdownPrefix(state, prefix, pending[start:]) + state.noteText(prefix) + if resetMarkdownSpan { + state.markdownCodeSpanTicks = 0 + } + events = append(events, Event{Content: prefix}) + } + state.pending.Reset() + state.capture.WriteString(pending[start:]) + state.capturing = true + state.resetIncrementalToolState() + continue + } + + safe, hold := splitSafeContentForToolDetection(state, pending) + if safe == "" { + break + } + state.pending.Reset() + state.pending.WriteString(hold) + state.noteText(safe) + events = append(events, Event{Content: safe}) + } + + return events +} + +func Flush(state *State, toolNames []string) []Event { + if state == nil { + return nil + } + events := ProcessChunk(state, "", toolNames) + if state.pending.Len() > 0 && state.markdownCodeSpanTicks > 0 { + // At end of stream, an unmatched backtick is literal Markdown text. + // Re-scan pending content so a real tool call after that stray + // backtick is not permanently hidden by inline-code state. + state.markdownCodeSpanTicks = 0 + events = append(events, ProcessChunk(state, "", toolNames)...) + } + if len(state.pendingToolCalls) > 0 { + events = append(events, Event{ToolCalls: state.pendingToolCalls}) + state.pendingToolRaw = "" + state.pendingToolCalls = nil + } + if state.capturing { + consumedPrefix, consumedCalls, consumedSuffix, ready := consumeToolCapture(state, toolNames) + if ready { + if consumedPrefix != "" { + state.noteText(consumedPrefix) + events = append(events, Event{Content: consumedPrefix}) + } + if len(consumedCalls) > 0 { + events = append(events, Event{ToolCalls: consumedCalls}) + } + if consumedSuffix != "" { + state.noteText(consumedSuffix) + events = append(events, Event{Content: consumedSuffix}) + } + } else { + content := state.capture.String() + if content != "" { + recovered := toolcall.SanitizeLooseCDATA(content) + if recovered != content { + if prefix, calls, suffix, recoveredReady := consumeXMLToolCapture(recovered, toolNames); recoveredReady && len(calls) > 0 { + if prefix != "" { + state.noteText(prefix) + events = append(events, Event{Content: prefix}) + } + events = append(events, Event{ToolCalls: calls}) + if suffix != "" { + state.noteText(suffix) + events = append(events, Event{Content: suffix}) + } + } else { + // If capture never resolved into a real tool call, release + // the buffered text instead of swallowing it. + state.noteText(content) + events = append(events, Event{Content: content}) + } + } else { + // If capture never resolved into a real tool call, release the + // buffered text instead of swallowing it. + state.noteText(content) + events = append(events, Event{Content: content}) + } + } + } + state.capture.Reset() + state.capturing = false + state.resetIncrementalToolState() + } + if state.pending.Len() > 0 { + content := state.pending.String() + // If pending never resolved into a real tool call, release it as text. + state.noteText(content) + events = append(events, Event{Content: content}) + state.pending.Reset() + } + return events +} + +func splitSafeContentForToolDetection(state *State, s string) (safe, hold string) { + if s == "" { + return "", "" + } + if xmlIdx := findPartialXMLToolTagStart(s); xmlIdx >= 0 { + if insideCodeFenceWithState(state, s[:xmlIdx]) { + return s, "" + } + markdown := markdownCodeSpanStateAt(state, s[:xmlIdx]) + if markdown.ticks > 0 { + if markdownCodeSpanCloses(s[xmlIdx:], markdown.ticks) { + return s, "" + } + if markdown.fromPrior { + return "", s + } + } + if xmlIdx > 0 { + return s[:xmlIdx], s[xmlIdx:] + } + return "", s + } + return s, "" +} + +const holdToolSegmentStart = -2 + +func findToolSegmentStart(state *State, s string) int { + if s == "" { + return -1 + } + offset := 0 + for { + tag, ok := toolcall.FindToolMarkupTagOutsideIgnored(s, offset) + if !ok { + return -1 + } + start := includeDuplicateLeadingLessThan(s, tag.Start) + if insideCodeFenceWithState(state, s[:start]) { + offset = tag.End + 1 + continue + } + markdown := markdownCodeSpanStateAt(state, s[:start]) + if markdown.ticks == 0 { + return start + } + if markdownCodeSpanCloses(s[start:], markdown.ticks) { + offset = tag.End + 1 + continue + } + if markdown.fromPrior { + return holdToolSegmentStart + } + return start + } +} + +type markdownCodeSpanScan struct { + ticks int + fromPrior bool +} + +func markdownCodeSpanStateAt(state *State, text string) markdownCodeSpanScan { + ticks := 0 + fromPrior := false + if state != nil && state.markdownCodeSpanTicks > 0 { + ticks = state.markdownCodeSpanTicks + fromPrior = true + } + for i := 0; i < len(text); { + if text[i] != '`' { + i++ + continue + } + run := countBacktickRun(text, i) + if ticks == 0 { + if run >= 3 && atMarkdownFenceLineStart(text, i) { + i += run + continue + } + if state != nil && insideCodeFenceWithState(state, text[:i]) { + i += run + continue + } + ticks = run + fromPrior = false + } else if run == ticks { + ticks = 0 + fromPrior = false + } + i += run + } + return markdownCodeSpanScan{ticks: ticks, fromPrior: fromPrior} +} + +func markdownCodeSpanCloses(text string, ticks int) bool { + if ticks <= 0 { + return false + } + for i := 0; i < len(text); { + if text[i] != '`' { + i++ + continue + } + run := countBacktickRun(text, i) + if run == ticks { + return true + } + i += run + } + return false +} + +func shouldResetUnclosedMarkdownPrefix(state *State, prefix, suffix string) bool { + markdown := markdownCodeSpanStateAt(state, prefix) + return markdown.ticks > 0 && !markdown.fromPrior && !markdownCodeSpanCloses(suffix, markdown.ticks) +} + +func includeDuplicateLeadingLessThan(s string, idx int) int { + for idx > 0 && s[idx-1] == '<' { + idx-- + } + return idx +} + +func consumeToolCapture(state *State, toolNames []string) (prefix string, calls []toolcall.ParsedToolCall, suffix string, ready bool) { + captured := state.capture.String() + if captured == "" { + return "", nil, "", false + } + + // XML tool call extraction only. + if xmlPrefix, xmlCalls, xmlSuffix, xmlReady := consumeXMLToolCapture(captured, toolNames); xmlReady { + return xmlPrefix, xmlCalls, xmlSuffix, true + } + // If XML tags are present but block is incomplete, keep buffering. + if hasOpenXMLToolTag(captured) { + return "", nil, "", false + } + if shouldKeepBareInvokeCapture(captured) { + return "", nil, "", false + } + return captured, nil, "", true +} diff --git a/internal/toolstream/tool_sieve_jsonscan.go b/internal/toolstream/tool_sieve_jsonscan.go new file mode 100644 index 0000000000000000000000000000000000000000..d9e9593b809f0b91e661ced75a303eea0856dd15 --- /dev/null +++ b/internal/toolstream/tool_sieve_jsonscan.go @@ -0,0 +1,27 @@ +package toolstream + +import "strings" + +func trimWrappingJSONFence(prefix, suffix string) (string, string) { + trimmedPrefix := strings.TrimRight(prefix, " \t\r\n") + fenceIdx := strings.LastIndex(trimmedPrefix, "```") + if fenceIdx < 0 { + return prefix, suffix + } + // Only strip when the trailing fence in prefix behaves like an opening fence. + // A legitimate closing fence before a standalone tool JSON must be preserved. + if strings.Count(trimmedPrefix[:fenceIdx+3], "```")%2 == 0 { + return prefix, suffix + } + fenceHeader := strings.TrimSpace(trimmedPrefix[fenceIdx+3:]) + if fenceHeader != "" && !strings.EqualFold(fenceHeader, "json") { + return prefix, suffix + } + + trimmedSuffix := strings.TrimLeft(suffix, " \t\r\n") + if !strings.HasPrefix(trimmedSuffix, "```") { + return prefix, suffix + } + consumedLeading := len(suffix) - len(trimmedSuffix) + return trimmedPrefix[:fenceIdx], suffix[consumedLeading+3:] +} diff --git a/internal/toolstream/tool_sieve_state.go b/internal/toolstream/tool_sieve_state.go new file mode 100644 index 0000000000000000000000000000000000000000..2c1711cbacaceed6bcd90045bb7bb2b82af389dc --- /dev/null +++ b/internal/toolstream/tool_sieve_state.go @@ -0,0 +1,257 @@ +package toolstream + +import ( + "ds2api/internal/toolcall" + "strings" +) + +type State struct { + pending strings.Builder + capture strings.Builder + capturing bool + codeFenceStack []int + codeFencePendingTicks int + codeFencePendingTildes int + codeFenceNotLineStart bool // inverted: zero-value false means "at line start" + markdownCodeSpanTicks int + pendingToolRaw string + pendingToolCalls []toolcall.ParsedToolCall + disableDeltas bool + toolNameSent bool + toolName string + toolArgsStart int + toolArgsSent int + toolArgsString bool + toolArgsDone bool +} + +type Event struct { + Content string + ToolCalls []toolcall.ParsedToolCall + ToolCallDeltas []ToolCallDelta +} + +type ToolCallDelta struct { + Index int + Name string + Arguments string +} + +func (s *State) resetIncrementalToolState() { + s.disableDeltas = false + s.toolNameSent = false + s.toolName = "" + s.toolArgsStart = -1 + s.toolArgsSent = -1 + s.toolArgsString = false + s.toolArgsDone = false +} + +func (s *State) noteText(content string) { + if !hasMeaningfulText(content) { + return + } + updateMarkdownCodeSpanState(s, content) + updateCodeFenceState(s, content) +} + +func hasMeaningfulText(text string) bool { + return strings.TrimSpace(text) != "" +} + +func insideCodeFenceWithState(state *State, text string) bool { + if state == nil { + return insideCodeFence(text) + } + simulated := simulateCodeFenceState( + state.codeFenceStack, + state.codeFencePendingTicks, + state.codeFencePendingTildes, + !state.codeFenceNotLineStart, + text, + ) + return len(simulated.stack) > 0 +} + +func insideCodeFence(text string) bool { + if text == "" { + return false + } + return len(simulateCodeFenceState(nil, 0, 0, true, text).stack) > 0 +} + +func updateMarkdownCodeSpanState(state *State, text string) { + if state == nil || !hasMeaningfulText(text) { + return + } + state.markdownCodeSpanTicks = simulateMarkdownCodeSpanTicks(state, state.markdownCodeSpanTicks, text) +} + +func simulateMarkdownCodeSpanTicks(state *State, initialTicks int, text string) int { + ticks := initialTicks + for i := 0; i < len(text); { + if text[i] != '`' { + i++ + continue + } + run := countBacktickRun(text, i) + if ticks == 0 { + if run >= 3 && atMarkdownFenceLineStart(text, i) { + i += run + continue + } + if state != nil && insideCodeFenceWithState(state, text[:i]) { + i += run + continue + } + ticks = run + } else if run == ticks { + ticks = 0 + } + i += run + } + return ticks +} + +func countBacktickRun(text string, start int) int { + count := 0 + for start+count < len(text) && text[start+count] == '`' { + count++ + } + return count +} + +func atMarkdownFenceLineStart(text string, idx int) bool { + for i := idx - 1; i >= 0; i-- { + switch text[i] { + case ' ', '\t': + continue + case '\n', '\r': + return true + default: + return false + } + } + return true +} + +func updateCodeFenceState(state *State, text string) { + if state == nil || !hasMeaningfulText(text) { + return + } + next := simulateCodeFenceState( + state.codeFenceStack, + state.codeFencePendingTicks, + state.codeFencePendingTildes, + !state.codeFenceNotLineStart, + text, + ) + state.codeFenceStack = next.stack + state.codeFencePendingTicks = next.pendingTicks + state.codeFencePendingTildes = next.pendingTildes + state.codeFenceNotLineStart = !next.lineStart +} + +type codeFenceSimulation struct { + stack []int + pendingTicks int + pendingTildes int + lineStart bool +} + +func simulateCodeFenceState(stack []int, pendingTicks, pendingTildes int, lineStart bool, text string) codeFenceSimulation { + chunk := text + nextStack := append([]int(nil), stack...) + ticks := pendingTicks + tildes := pendingTildes + atLineStart := lineStart + + flushPending := func() { + if ticks > 0 { + if atLineStart && ticks >= 3 { + applyFenceMarker(&nextStack, ticks) // positive = backtick + } + atLineStart = false + ticks = 0 + } + if tildes > 0 { + if atLineStart && tildes >= 3 { + applyFenceMarker(&nextStack, -tildes) // negative = tilde + } + atLineStart = false + tildes = 0 + } + } + + for i := 0; i < len(chunk); i++ { + ch := chunk[i] + if ch == '`' { + if tildes > 0 { + // Mixed chars — flush tildes first. + flushPending() + } + ticks++ + continue + } + if ch == '~' { + if ticks > 0 { + flushPending() + } + tildes++ + continue + } + flushPending() + switch ch { + case '\n', '\r': + atLineStart = true + case ' ', '\t': + if atLineStart { + continue + } + atLineStart = false + default: + atLineStart = false + } + } + + return codeFenceSimulation{ + stack: nextStack, + pendingTicks: ticks, + pendingTildes: tildes, + lineStart: atLineStart, + } +} + +// applyFenceMarker pushes or pops a fence marker on the stack. +// Positive values represent backtick fences, negative represent tilde fences. +// A closing marker must match the sign (type) of the opening marker. +func applyFenceMarker(stack *[]int, marker int) { + if stack == nil || marker == 0 { + return + } + if len(*stack) == 0 { + *stack = append(*stack, marker) + return + } + top := (*stack)[len(*stack)-1] + // Signs must match: backtick closes backtick, tilde closes tilde. + sameType := (top > 0 && marker > 0) || (top < 0 && marker < 0) + if !sameType { + // Different fence type — treat as nested. + *stack = append(*stack, marker) + return + } + absMarker := marker + absTop := top + if absMarker < 0 { + absMarker = -absMarker + } + if absTop < 0 { + absTop = -absTop + } + if absMarker >= absTop { + *stack = (*stack)[:len(*stack)-1] + return + } + *stack = append(*stack, marker) +} diff --git a/internal/toolstream/tool_sieve_xml.go b/internal/toolstream/tool_sieve_xml.go new file mode 100644 index 0000000000000000000000000000000000000000..ccb09a60daad623aa3389c489a1570162452e720 --- /dev/null +++ b/internal/toolstream/tool_sieve_xml.go @@ -0,0 +1,178 @@ +package toolstream + +import ( + "ds2api/internal/toolcall" + "strings" +) + +// consumeXMLToolCapture tries to extract complete XML tool call blocks from captured text. +func consumeXMLToolCapture(captured string, toolNames []string) (prefix string, calls []toolcall.ParsedToolCall, suffix string, ready bool) { + anyOpenFound := false + type candidate struct { + start int + prefix string + calls []toolcall.ParsedToolCall + suffix string + } + type rejectedBlock struct { + start int + prefix string + suffix string + } + var best *candidate + var rejected *rejectedBlock + + // Scan every recognized tool tag occurrence. Prose can mention a wrapper + // tag before the actual tool block, including the same variant as the real + // block. We only accept complete tool_calls wrappers that parse cleanly. + for searchFrom := 0; searchFrom < len(captured); { + tag, ok := toolcall.FindToolMarkupTagOutsideIgnored(captured, searchFrom) + if !ok { + break + } + if tag.Closing || tag.Name != "tool_calls" { + searchFrom = tag.End + 1 + continue + } + closeTag, ok := toolcall.FindMatchingToolMarkupClose(captured, tag) + if !ok { + anyOpenFound = true + searchFrom = tag.End + 1 + continue + } + + xmlBlock := captured[tag.Start : closeTag.End+1] + prefixPart := captured[:tag.Start] + suffixPart := captured[closeTag.End+1:] + parsed := toolcall.ParseStandaloneToolCallsDetailed(xmlBlock, toolNames) + if len(parsed.Calls) > 0 { + prefixPart, suffixPart = trimWrappingJSONFence(prefixPart, suffixPart) + if best == nil || tag.Start < best.start { + best = &candidate{start: tag.Start, prefix: prefixPart, calls: parsed.Calls, suffix: suffixPart} + } + break + } + if parsed.SawToolCallSyntax { + if rejected == nil || tag.Start < rejected.start { + rejected = &rejectedBlock{start: tag.Start, prefix: prefixPart + xmlBlock, suffix: suffixPart} + } + searchFrom = tag.End + 1 + continue + } + if rejected == nil || tag.Start < rejected.start { + rejected = &rejectedBlock{start: tag.Start, prefix: prefixPart + xmlBlock, suffix: suffixPart} + } + searchFrom = tag.End + 1 + } + if best != nil { + return best.prefix, best.calls, best.suffix, true + } + if anyOpenFound { + // At least one opening tag was found but none had a matching close tag. + // Keep buffering until a closing tag arrives. + return "", nil, "", false + } + if rejected != nil { + // If this block failed to become a tool call, pass it through as text. + return rejected.prefix, nil, rejected.suffix, true + } + if invokeTag, ok := findFirstToolMarkupTagByName(captured, 0, "invoke"); ok { + if wrapperOpen, ok := findFirstToolMarkupTagByName(captured, 0, "tool_calls"); !ok || wrapperOpen.Start > invokeTag.Start { + if closeTag, ok := findFirstToolMarkupTagByNameFrom(captured, invokeTag.Start+1, "tool_calls", true); ok && closeTag.Start > invokeTag.Start { + xmlBlock := "" + captured[invokeTag.Start:closeTag.End+1] + prefixPart := captured[:invokeTag.Start] + suffixPart := captured[closeTag.End+1:] + parsed := toolcall.ParseStandaloneToolCallsDetailed(xmlBlock, toolNames) + if len(parsed.Calls) > 0 { + prefixPart, suffixPart = trimWrappingJSONFence(prefixPart, suffixPart) + return prefixPart, parsed.Calls, suffixPart, true + } + if parsed.SawToolCallSyntax { + return prefixPart + captured[invokeTag.Start:closeTag.End+1], nil, suffixPart, true + } + return prefixPart + captured[invokeTag.Start:closeTag.End+1], nil, suffixPart, true + } + } + } + return "", nil, "", false +} + +// hasOpenXMLToolTag returns true if captured text contains an XML tool opening tag +// whose SPECIFIC closing tag has not appeared yet. +func hasOpenXMLToolTag(captured string) bool { + for searchFrom := 0; searchFrom < len(captured); { + tag, ok := toolcall.FindToolMarkupTagOutsideIgnored(captured, searchFrom) + if !ok { + return false + } + if tag.Closing || tag.Name != "tool_calls" { + searchFrom = tag.End + 1 + continue + } + if _, ok := toolcall.FindMatchingToolMarkupClose(captured, tag); !ok { + return true + } + searchFrom = tag.End + 1 + } + return false +} + +func shouldKeepBareInvokeCapture(captured string) bool { + invokeTag, ok := findFirstToolMarkupTagByName(captured, 0, "invoke") + if !ok { + return false + } + if wrapperOpen, ok := findFirstToolMarkupTagByName(captured, 0, "tool_calls"); ok && wrapperOpen.Start <= invokeTag.Start { + return false + } + if closeTag, ok := findFirstToolMarkupTagByNameFrom(captured, invokeTag.Start+1, "tool_calls", true); ok && closeTag.Start > invokeTag.Start { + return true + } + startEnd := invokeTag.End + if startEnd < 0 { + return true + } + body := captured[startEnd+1:] + trimmedBody := strings.TrimLeft(body, " \t\r\n") + if trimmedBody == "" { + return true + } + + if invokeCloseTag, ok := findFirstToolMarkupTagByNameFrom(captured, startEnd+1, "invoke", true); ok { + return strings.TrimSpace(captured[invokeCloseTag.End+1:]) == "" + } + if paramTag, ok := findFirstToolMarkupTagByName(body, 0, "parameter"); ok && strings.TrimSpace(body[:paramTag.Start]) == "" { + return true + } + + trimmedLower := strings.ToLower(trimmedBody) + return strings.HasPrefix(trimmedLower, "") || strings.Contains(tail, ">") { + return -1 + } + if toolcall.IsPartialToolMarkupTagPrefix(tail) { + return start + } + return -1 +} + +func lastToolMarkupStartDelimiterIndex(s string) int { + asciiIdx := strings.LastIndex(s, "<") + fullwidthIdx := strings.LastIndex(s, "<") + if asciiIdx > fullwidthIdx { + return asciiIdx + } + return fullwidthIdx +} diff --git a/internal/toolstream/tool_sieve_xml_scan.go b/internal/toolstream/tool_sieve_xml_scan.go new file mode 100644 index 0000000000000000000000000000000000000000..faaea84553bac8bde48f655f07ef257d1f07b141 --- /dev/null +++ b/internal/toolstream/tool_sieve_xml_scan.go @@ -0,0 +1,28 @@ +package toolstream + +import "ds2api/internal/toolcall" + +func findFirstToolMarkupTagByName(s string, start int, name string) (toolcall.ToolMarkupTag, bool) { + return findFirstToolMarkupTagByNameFrom(s, start, name, false) +} + +func findFirstToolMarkupTagByNameFrom(s string, start int, name string, closing bool) (toolcall.ToolMarkupTag, bool) { + for pos := maxInt(start, 0); pos < len(s); { + tag, ok := toolcall.FindToolMarkupTagOutsideIgnored(s, pos) + if !ok { + return toolcall.ToolMarkupTag{}, false + } + if tag.Name == name && tag.Closing == closing { + return tag, true + } + pos = tag.End + 1 + } + return toolcall.ToolMarkupTag{}, false +} + +func maxInt(a, b int) int { + if a > b { + return a + } + return b +} diff --git a/internal/toolstream/tool_sieve_xml_test.go b/internal/toolstream/tool_sieve_xml_test.go new file mode 100644 index 0000000000000000000000000000000000000000..418c81200b4f4a4b520c8e9edf06eb00dc4d643c --- /dev/null +++ b/internal/toolstream/tool_sieve_xml_test.go @@ -0,0 +1,1500 @@ +package toolstream + +import ( + "ds2api/internal/toolcall" + "strings" + "testing" +) + +func TestProcessToolSieveInterceptsXMLToolCallWithoutLeak(t *testing.T) { + var state State + // Simulate a model producing XML tool call output chunk by chunk. + chunks := []string{ + "\n", + ` ` + "\n", + ` README.MD` + "\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent string + var toolCalls int + for _, evt := range events { + if evt.Content != "" { + textContent += evt.Content + } + toolCalls += len(evt.ToolCalls) + } + + if strings.Contains(textContent, "\n", + ` <|DSML|invoke name="read_file">` + "\n", + ` <|DSML|parameter name="path">README.MD` + "\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent string + var toolCalls int + for _, evt := range events { + textContent += evt.Content + toolCalls += len(evt.ToolCalls) + } + + if strings.Contains(strings.ToLower(textContent), "dsml") || strings.Contains(textContent, "read_file") { + t.Fatalf("DSML tool call content leaked to text: %q", textContent) + } + if toolCalls != 1 { + t.Fatalf("expected one DSML tool call, got %d events=%#v", toolCalls, events) + } +} + +func TestProcessToolSieveInterceptsDSMLTrailingPipeToolCallWithoutLeak(t *testing.T) { + var state State + chunks := []string{ + "<|DSML|tool_calls| \n", + ` <|DSML|invoke name="terminal">` + "\n", + ` <|DSML|parameter name="command">` + "\n", + ` <|DSML|parameter name="timeout">` + "\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"terminal"})...) + } + events = append(events, Flush(&state, []string{"terminal"})...) + + var textContent strings.Builder + var calls []any + for _, evt := range events { + textContent.WriteString(evt.Content) + for _, call := range evt.ToolCalls { + calls = append(calls, call) + } + } + if text := textContent.String(); strings.Contains(strings.ToLower(text), "dsml") || strings.Contains(text, "terminal") { + t.Fatalf("trailing-pipe DSML tool call leaked to text: %q events=%#v", text, events) + } + if len(calls) != 1 { + t.Fatalf("expected one trailing-pipe DSML tool call, got %d events=%#v", len(calls), events) + } +} + +func TestProcessToolSieveInterceptsDSMLControlSeparatorWithoutLeak(t *testing.T) { + for _, tc := range []struct { + name string + sep string + }{ + {name: "control_picture", sep: "␂"}, + {name: "raw_stx", sep: "\x02"}, + } { + t.Run(tc.name, func(t *testing.T) { + sep := tc.sep + var state State + chunks := []string{ + "\n", + ` ` + "\n", + ` ` + "\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Read"})...) + } + events = append(events, Flush(&state, []string{"Read"})...) + + var textContent strings.Builder + var calls []any + for _, evt := range events { + textContent.WriteString(evt.Content) + for _, call := range evt.ToolCalls { + calls = append(calls, call) + } + } + if text := textContent.String(); strings.Contains(strings.ToLower(text), "dsml") || strings.Contains(text, "Read") || strings.Contains(text, sep) { + t.Fatalf("control-separator DSML tool call leaked to text: %q events=%#v", text, events) + } + if len(calls) != 1 { + t.Fatalf("expected one control-separator DSML tool call, got %d events=%#v", len(calls), events) + } + }) + } +} + +func TestProcessToolSieveInterceptsArbitraryPrefixedToolTagsWithoutLeak(t *testing.T) { + var state State + chunks := []string{ + "\n", + ` ` + "\n", + ` ` + "\n", + " \n", + "
", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Read"})...) + } + events = append(events, Flush(&state, []string{"Read"})...) + + var textContent strings.Builder + var calls []any + for _, evt := range events { + textContent.WriteString(evt.Content) + for _, call := range evt.ToolCalls { + calls = append(calls, call) + } + } + if text := textContent.String(); strings.Contains(text, "proto") || strings.Contains(text, "Read") || strings.Contains(text, "💥") { + t.Fatalf("arbitrary-prefixed tool call leaked to text: %q events=%#v", text, events) + } + if len(calls) != 1 { + t.Fatalf("expected one arbitrary-prefixed tool call, got %d events=%#v", len(calls), events) + } +} + +func TestProcessToolSieveEmitsEmptyDSMLControlSeparatorBlockWithoutLeak(t *testing.T) { + sep := "␂" + chunks := []string{ + "\n", + ` ` + "\n", + ` ` + "\n", + " \n", + "", + } + calls := collectToolCallsForChunks(t, chunks, []string{"Read"}) + if len(calls) != 1 { + t.Fatalf("expected empty control-separator block to produce one call, got %#v", calls) + } + if calls[0].Name != "Read" || calls[0].Input["file_path"] != "" { + t.Fatalf("expected empty file_path parameter to be preserved, got %#v", calls) + } +} + +func TestProcessToolSieveInterceptsExtraLeadingLessThanDSMLToolCallWithoutLeak(t *testing.T) { + var state State + chunks := []string{ + "<<|DSML|tool_calls>\n", + ` <<|DSML|invoke name="Bash">` + "\n", + ` <<|DSML|parameter name="command">` + "\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + if text := textContent.String(); strings.Contains(text, "<") || strings.Contains(text, "Bash") { + t.Fatalf("extra-leading-less-than DSML tool call leaked to text: %q events=%#v", text, events) + } + if toolCalls != 1 { + t.Fatalf("expected one extra-leading-less-than DSML tool call, got %d events=%#v", toolCalls, events) + } +} + +func TestProcessToolSieveInterceptsRepeatedDSMLPrefixNoiseWithoutLeak(t *testing.T) { + var state State + chunks := []string{ + "<\n", + ` <` + "\n", + ` <` + "\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + if text := textContent.String(); strings.Contains(strings.ToLower(text), "dsml") || strings.Contains(text, "Bash") { + t.Fatalf("repeated-prefix DSML tool call leaked to text: %q events=%#v", text, events) + } + if toolCalls != 1 { + t.Fatalf("expected one repeated-prefix DSML tool call, got %d events=%#v", toolCalls, events) + } +} + +func TestProcessToolSieveHandlesLongXMLToolCall(t *testing.T) { + var state State + const toolName = "write_to_file" + payload := strings.Repeat("x", 4096) + splitAt := len(payload) / 2 + chunks := []string{ + "\n \n \n \n", + } + + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{toolName})...) + } + events = append(events, Flush(&state, []string{toolName})...) + + var textContent strings.Builder + toolCalls := 0 + var gotPayload any + for _, evt := range events { + if evt.Content != "" { + textContent.WriteString(evt.Content) + } + if len(evt.ToolCalls) > 0 && gotPayload == nil { + gotPayload = evt.ToolCalls[0].Input["content"] + } + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 1 { + t.Fatalf("expected one long XML tool call, got %d events=%#v", toolCalls, events) + } + if textContent.Len() != 0 { + t.Fatalf("expected no leaked text for long XML tool call, got %q", textContent.String()) + } + got, _ := gotPayload.(string) + if got != payload { + t.Fatalf("expected long XML payload to survive intact, got len=%d want=%d", len(got), len(payload)) + } +} + +func TestProcessToolSieveKeepsCDATAEmbeddedToolClosingBuffered(t *testing.T) { + var state State + payload := strings.Join([]string{ + "# DS2API 4.0 更新内容", + "", + strings.Repeat("x", 4096), + "```xml", + "", + " ", + " x", + " ", + "", + "```", + "tail", + }, "\n") + innerClose := strings.Index(payload, "
") + len("
") + chunks := []string{ + "\n \n \n DS2API-4.0-Release-Notes.md\n \n", + } + + var events []Event + for i, c := range chunks { + next := ProcessChunk(&state, c, []string{"Write"}) + if i <= 1 { + for _, evt := range next { + if evt.Content != "" || len(evt.ToolCalls) > 0 { + t.Fatalf("expected no events before outer closing tag, chunk=%d events=%#v", i, next) + } + } + } + events = append(events, next...) + } + events = append(events, Flush(&state, []string{"Write"})...) + + var textContent strings.Builder + var gotPayload string + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + if len(evt.ToolCalls) > 0 { + toolCalls += len(evt.ToolCalls) + gotPayload, _ = evt.ToolCalls[0].Input["content"].(string) + } + } + + if toolCalls != 1 { + t.Fatalf("expected one parsed tool call, got %d events=%#v", toolCalls, events) + } + if textContent.Len() != 0 { + t.Fatalf("expected no leaked text, got %q", textContent.String()) + } + if gotPayload != payload { + t.Fatalf("expected full CDATA payload to survive intact, got len=%d want=%d", len(gotPayload), len(payload)) + } +} + +func TestProcessToolSieveKeepsExtremeHereDocCDATAUntilOuterClose(t *testing.T) { + var state State + command := strings.Join([]string{ + "cat > docs/project-value.md << 'ENDOFFILE'", + "# DS2API project value", + "", + "```xml", + `<|DSML|tool_calls>`, + ` <|DSML|invoke name="Bash">`, + ` <|DSML|parameter name="command">&1]]>`, + ` `, + ``, + "```", + "", + "Only the literal `]]>` needs special handling.", + "", + "ENDOFFILE", + `echo "Done. Lines: $(wc -l < docs/project-value.md)"`, + }, "\n") + innerClose := strings.Index(command, ``) + len(``) + chunks := []string{ + `<|DSML|tool_calls>` + "\n", + `<|DSML|invoke name="Bash">` + "\n", + `<|DSML|parameter name="command">` + "\n", + `<|DSML|parameter name="description">` + "\n", + `` + "\n", + ``, + } + + var events []Event + for i, c := range chunks { + next := ProcessChunk(&state, c, []string{"Bash"}) + if i <= 2 { + for _, evt := range next { + if evt.Content != "" || len(evt.ToolCalls) > 0 { + t.Fatalf("expected no events before outer close, chunk=%d events=%#v", i, next) + } + } + } + events = append(events, next...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var textContent strings.Builder + var gotCommand string + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + if len(evt.ToolCalls) > 0 { + toolCalls += len(evt.ToolCalls) + gotCommand, _ = evt.ToolCalls[0].Input["command"].(string) + } + } + if toolCalls != 1 { + t.Fatalf("expected one parsed tool call, got %d events=%#v", toolCalls, events) + } + if textContent.Len() != 0 { + t.Fatalf("expected no leaked text, got %q", textContent.String()) + } + if gotCommand != command { + t.Fatalf("expected full heredoc command to survive, got len=%d want=%d", len(gotCommand), len(command)) + } +} + +func TestProcessToolSieveKeepsCompactCDATAWithImmediateFencedDSML(t *testing.T) { + var state State + content := strings.Join([]string{ + "```xml", + `<|DSML|tool_calls>`, + ` <|DSML|invoke name="Bash">`, + ` <|DSML|parameter name="command">`, + ` `, + ``, + "```", + "tail", + }, "\n") + chunks := []string{ + ``, + } + + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Write"})...) + } + events = append(events, Flush(&state, []string{"Write"})...) + + var textContent strings.Builder + var gotContent string + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + if len(evt.ToolCalls) > 0 { + toolCalls += len(evt.ToolCalls) + gotContent, _ = evt.ToolCalls[0].Input["content"].(string) + } + } + if toolCalls != 1 { + t.Fatalf("expected one compact CDATA tool call, got %d events=%#v", toolCalls, events) + } + if textContent.Len() != 0 { + t.Fatalf("expected no leaked text, got %q", textContent.String()) + } + if gotContent != content { + t.Fatalf("expected compact CDATA content to survive, got len=%d want=%d", len(gotContent), len(content)) + } +} + +func TestProcessToolSieveFallsBackWhenCDATANeverCloses(t *testing.T) { + var state State + chunks := []string{ + "\n \n \n \n", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Write"})...) + } + events = append(events, Flush(&state, []string{"Write"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + if evt.Content != "" { + textContent.WriteString(evt.Content) + } + toolCalls += len(evt.ToolCalls) + if len(evt.ToolCalls) > 0 { + if got, _ := evt.ToolCalls[0].Input["content"].(string); got != "hello world" { + t.Fatalf("expected recovered CDATA payload, got %q", got) + } + } + } + + if toolCalls != 1 { + t.Fatalf("expected unclosed CDATA payload to still parse, got %d tool calls events=%#v", toolCalls, events) + } + if textContent.Len() != 0 { + t.Fatalf("expected no leaked text, got %q", textContent.String()) + } +} + +func TestProcessToolSieveXMLWithLeadingText(t *testing.T) { + var state State + // Model outputs some prose then an XML tool call. + chunks := []string{ + "Let me check the file.\n", + "\n \n", + ` go.mod` + "\n \n", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent string + var toolCalls int + for _, evt := range events { + if evt.Content != "" { + textContent += evt.Content + } + toolCalls += len(evt.ToolCalls) + } + + // Leading text should be emitted. + if !strings.Contains(textContent, "Let me check the file.") { + t.Fatalf("expected leading text to be emitted, got %q", textContent) + } + // The XML itself should NOT leak. + if strings.Contains(textContent, "示例 XMLplain text xml payload` + events := ProcessChunk(&state, chunk, []string{"read_file"}) + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + if toolCalls != 0 { + t.Fatalf("expected no tool calls for plain XML payload, got %d events=%#v", toolCalls, events) + } + if textContent.String() != chunk { + t.Fatalf("expected XML payload to pass through unchanged, got %q", textContent.String()) + } +} + +func TestProcessToolSieveNonToolXMLKeepsSuffixForToolParsing(t *testing.T) { + var state State + chunk := `plain xmlREADME.MD` + events := ProcessChunk(&state, chunk, []string{"read_file"}) + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + if !strings.Contains(textContent.String(), `plain xml`) { + t.Fatalf("expected leading non-tool XML to be preserved, got %q", textContent.String()) + } + if strings.Contains(textContent.String(), `{"path":"README.md"}` + events := ProcessChunk(&state, chunk, []string{"read_file"}) + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected malformed executable-looking XML not to become a tool call, got %d events=%#v", toolCalls, events) + } + if textContent.String() != chunk { + t.Fatalf("expected malformed executable-looking XML to be released as text, got %q", textContent.String()) + } +} + +func TestProcessToolSieveEmitsAllEmptyDSMLToolBlock(t *testing.T) { + chunk := strings.Join([]string{ + `<|DSML|tool_calls>`, + `<|DSML|invoke name="Bash">`, + `<|DSML|parameter name="command">`, + `<|DSML|parameter name="description"> `, + `<|DSML|parameter name="timeout">`, + ``, + ``, + }, "\n") + calls := collectToolCallsForChunks(t, []string{chunk}, []string{"Bash"}) + if len(calls) != 1 { + t.Fatalf("expected all-empty DSML block to produce one tool call, got %#v", calls) + } + if calls[0].Input["command"] != "" || calls[0].Input["description"] != "" || calls[0].Input["timeout"] != "" { + t.Fatalf("expected empty parameters to be preserved, got %#v", calls[0].Input) + } +} + +func TestProcessToolSieveEmitsChunkedAllEmptyArbitraryPrefixedToolBlock(t *testing.T) { + chunk := strings.Join([]string{ + ``, + ` `, + ` `, + ` `, + ` `, + ` `, + ` `, + }, "\n") + calls := collectToolCallsForChunks(t, splitEveryNRBytes(chunk, 8), []string{"TaskOutput"}) + if len(calls) != 1 { + t.Fatalf("expected chunked all-empty arbitrary-prefixed block to produce one tool call, got %#v", calls) + } + if calls[0].Name != "TaskOutput" || calls[0].Input["task_id"] != "" || calls[0].Input["block"] != "" || calls[0].Input["timeout"] != "" { + t.Fatalf("expected empty TaskOutput parameters to be preserved, got %#v", calls) + } +} + +func collectToolCallsForChunks(t *testing.T, chunks []string, toolNames []string) []toolcall.ParsedToolCall { + t.Helper() + var state State + var events []Event + for _, chunk := range chunks { + events = append(events, ProcessChunk(&state, chunk, toolNames)...) + } + events = append(events, Flush(&state, toolNames)...) + + var textContent strings.Builder + var calls []toolcall.ParsedToolCall + for _, evt := range events { + textContent.WriteString(evt.Content) + calls = append(calls, evt.ToolCalls...) + } + if textContent.Len() != 0 { + t.Fatalf("expected tool block not to leak as text, got %q", textContent.String()) + } + return calls +} + +func splitEveryNRBytes(s string, n int) []string { + if n <= 0 { + return []string{s} + } + out := make([]string, 0, len(s)/n+1) + for len(s) > 0 { + if len(s) <= n { + out = append(out, s) + break + } + out = append(out, s[:n]) + s = s[n:] + } + return out +} + +func TestProcessToolSievePassesThroughFencedXMLToolCallExamples(t *testing.T) { + var state State + input := strings.Join([]string{ + "Before first example.\n```", + "xml\nREADME.md\n```\n", + "Between examples.\n```xml\n", + "golang\n", + "```\nAfter examples.", + }, "") + + chunks := []string{ + "Before first example.\n```", + "xml\nREADME.md\n```\n", + "Between examples.\n```xml\n", + "golang\n", + "```\nAfter examples.", + } + + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file", "search"})...) + } + events = append(events, Flush(&state, []string{"read_file", "search"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + if evt.Content != "" { + textContent.WriteString(evt.Content) + } + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected fenced XML examples to stay text, got %d tool calls events=%#v", toolCalls, events) + } + if textContent.String() != input { + t.Fatalf("expected fenced XML examples to pass through unchanged, got %q", textContent.String()) + } +} + +func TestProcessToolSieveKeepsPartialXMLTagInsideFencedExample(t *testing.T) { + var state State + input := strings.Join([]string{ + "Example:\n```xml\nREADME.md
\n```\n", + "Done.", + }, "") + + chunks := []string{ + "Example:\n```xml\nREADME.md\n```\n", + "Done.", + } + + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + if evt.Content != "" { + textContent.WriteString(evt.Content) + } + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected partial fenced XML to stay text, got %d tool calls events=%#v", toolCalls, events) + } + if textContent.String() != input { + t.Fatalf("expected partial fenced XML to pass through unchanged, got %q", textContent.String()) + } +} + +func TestProcessToolSievePartialXMLTagHeldBack(t *testing.T) { + var state State + // Chunk ends with a partial XML tool tag. + events := ProcessChunk(&state, "Hello \n", 10}, + {"dsml_trailing_pipe_tag", "some text <|DSML|tool_calls| \n", 10}, + {"dsml_extra_leading_less_than", "some text <<|DSML|tool_calls>\n", 10}, + {"invoke_tag_missing_wrapper", "some text \n", 10}, + {"bare_tool_call_text", "prefix \n", -1}, + {"xml_inside_code_fence", "```xml\n\n```", -1}, + {"no_xml", "just plain text", -1}, + {"gemini_json_no_detect", `some text {"functionCall":{"name":"search"}}`, -1}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := findToolSegmentStart(nil, tc.input) + if got != tc.want { + t.Fatalf("findToolSegmentStart(%q) = %d, want %d", tc.input, got, tc.want) + } + }) + } +} + +func TestFindPartialXMLToolTagStart(t *testing.T) { + cases := []struct { + name string + input string + want int + }{ + {"partial_tool_calls", "Hello done", -1}, + {"no_lt", "plain text", -1}, + {"closed_lt", "a < b > c", -1}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + got := findPartialXMLToolTagStart(tc.input) + if got != tc.want { + t.Fatalf("findPartialXMLToolTagStart(%q) = %d, want %d", tc.input, got, tc.want) + } + }) + } +} + +func TestHasOpenXMLToolTag(t *testing.T) { + if !hasOpenXMLToolTag("\n") { + t.Fatal("should detect open XML tool tag without closing tag") + } + if hasOpenXMLToolTag("\n\n") { + t.Fatal("should return false when closing tag is present") + } + if hasOpenXMLToolTag("plain text without any XML") { + t.Fatal("should return false for plain text") + } +} + +// Test the EXACT scenario the user reports: token-by-token streaming where +// tag arrives in small pieces. +func TestProcessToolSieveTokenByTokenXMLNoLeak(t *testing.T) { + var state State + // Simulate DeepSeek model generating tokens one at a time. + chunks := []string{ + "<", + "tool", + "_ca", + "lls", + ">\n", + " ` + "\n", + " `, + "README.MD", + "\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent string + var toolCalls int + for _, evt := range events { + if evt.Content != "" { + textContent += evt.Content + } + toolCalls += len(evt.ToolCalls) + } + + if strings.Contains(textContent, "") { + t.Fatalf("closing tag fragment leaked to text: %q", textContent) + } + if strings.Contains(textContent, "read_file") { + t.Fatalf("tool name leaked to text: %q", textContent) + } + if toolCalls == 0 { + t.Fatal("expected tool calls to be extracted, got none") + } +} + +// Test that Flush on incomplete XML falls back to raw text. +func TestFlushToolSieveIncompleteXMLFallsBackToText(t *testing.T) { + var state State + // XML block starts but stream ends before completion. + chunks := []string{ + "\n", + " \n", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + // Stream ends abruptly - flush should NOT dump raw XML. + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent string + for _, evt := range events { + if evt.Content != "" { + textContent += evt.Content + } + } + + if textContent != strings.Join(chunks, "") { + t.Fatalf("expected incomplete XML to fall back to raw text, got %q", textContent) + } +} + +// Test that the opening tag "\n " is NOT emitted as text content. +func TestOpeningXMLTagNotLeakedAsContent(t *testing.T) { + var state State + // First chunk is the opening tag - should be held, not emitted. + evts1 := ProcessChunk(&state, "\n ", []string{"read_file"}) + for _, evt := range evts1 { + if strings.Contains(evt.Content, "") { + t.Fatalf("opening tag leaked on first chunk: %q", evt.Content) + } + } + + // Remaining content arrives. + evts2 := ProcessChunk(&state, "\n README.MD\n \n", []string{"read_file"}) + evts2 = append(evts2, Flush(&state, []string{"read_file"})...) + + var textContent string + var toolCalls int + allEvents := append(evts1, evts2...) + for _, evt := range allEvents { + if evt.Content != "" { + textContent += evt.Content + } + toolCalls += len(evt.ToolCalls) + } + + if strings.Contains(textContent, "\n", + " Here is the answer\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"attempt_completion"})...) + } + events = append(events, Flush(&state, []string{"attempt_completion"})...) + + var textContent string + for _, evt := range events { + if evt.Content != "" { + textContent += evt.Content + } + } + + if !strings.Contains(textContent, "Done with task.\n") { + t.Fatalf("expected leading text to be emitted, got %q", textContent) + } + + if textContent != strings.Join(chunks, "") { + t.Fatalf("expected agent XML to fall back to raw text, got %q", textContent) + } +} + +func TestProcessToolSievePassesThroughBareToolCallAsText(t *testing.T) { + var state State + chunk := `README.md` + events := ProcessChunk(&state, chunk, []string{"read_file"}) + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected bare invoke to remain text, got %d events=%#v", toolCalls, events) + } + if textContent.String() != chunk { + t.Fatalf("expected bare invoke to pass through unchanged, got %q", textContent.String()) + } +} + +func TestProcessToolSieveBareInvokeInlineProseDoesNotStall(t *testing.T) { + var state State + chunk := "Use `` as plain documentation text." + events := ProcessChunk(&state, chunk, []string{"read_file"}) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected inline invoke prose to remain text, got %d events=%#v", toolCalls, events) + } + if textContent.String() != chunk { + t.Fatalf("expected inline invoke prose to stream immediately, got %q", textContent.String()) + } + if state.capturing { + t.Fatal("expected inline invoke prose not to leave stream capture open") + } +} + +func TestProcessToolSieveBareInvokeExampleReleasesWhenNotRepairable(t *testing.T) { + var state State + chunks := []string{ + `Example: README.md`, + " then continue.", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 0 { + t.Fatalf("expected non-repairable bare invoke to remain text, got %d events=%#v", toolCalls, events) + } + if textContent.String() != strings.Join(chunks, "") { + t.Fatalf("expected non-repairable bare invoke to pass through, got %q", textContent.String()) + } + if state.capturing { + t.Fatal("expected non-repairable bare invoke not to leave stream capture open") + } +} + +func TestProcessToolSieveRepairsMissingOpeningWrapperWithoutLeakingInvokeText(t *testing.T) { + var state State + chunks := []string{ + "\n", + " README.md\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + + if toolCalls != 1 { + t.Fatalf("expected repaired missing-wrapper stream to emit one tool call, got %d events=%#v", toolCalls, events) + } + if strings.Contains(textContent.String(), "") { + t.Fatalf("expected repaired missing-wrapper stream not to leak xml text, got %q", textContent.String()) + } +} + +// Test escaped U+FF5C pipe variant: <\uff5ctool_calls> should be buffered and parsed. +func TestProcessToolSieveFullwidthPipeVariantDoesNotLeak(t *testing.T) { + var state State + chunks := []string{ + "<\uff5ctool_calls>\n", + "\n", + "git status\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"execute_command"})...) + } + events = append(events, Flush(&state, []string{"execute_command"})...) + + var textContent string + var toolCalls int + for _, evt := range events { + textContent += evt.Content + toolCalls += len(evt.ToolCalls) + } + + if strings.Contains(textContent, "invoke") || strings.Contains(textContent, "execute_command") { + t.Fatalf("escaped U+FF5C pipe variant leaked to text: %q", textContent) + } + if toolCalls != 1 { + t.Fatalf("expected one tool call from escaped U+FF5C pipe variant, got %d events=%#v", toolCalls, events) + } +} + +// Test <|DSML|tool_calls> with DSML invoke/parameter tags should buffer the +// wrapper instead of leaking it before the block is complete. +func TestProcessToolSieveFullwidthDSMLPrefixVariantDoesNotLeak(t *testing.T) { + var state State + chunks := []string{ + "<|DSML|tool", + "_calls>\n", + "<|DSML|invoke name=\"Bash\">\n", + "<|DSML|parameter name=\"command\">\n", + "<|DSML|parameter name=\"description\">\n", + "\n", + "<|DSML|invoke name=\"Bash\">\n", + "<|DSML|parameter name=\"command\">/dev/null || echo \"No package.json found\"]]>\n", + "<|DSML|parameter name=\"description\">\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var textContent strings.Builder + var toolCalls int + var names []string + for _, evt := range events { + textContent.WriteString(evt.Content) + for _, call := range evt.ToolCalls { + toolCalls++ + names = append(names, call.Name) + } + } + + if toolCalls != 2 { + t.Fatalf("expected two tool calls from fullwidth DSML prefix variant, got %d events=%#v", toolCalls, events) + } + if len(names) != 2 || names[0] != "Bash" || names[1] != "Bash" { + t.Fatalf("expected two Bash tool calls, got %v", names) + } + if textContent.Len() != 0 { + t.Fatalf("expected fullwidth DSML prefix variant not to leak text, got %q", textContent.String()) + } +} + +// Test with <|DSML|invoke> (DSML prefix without leading pipe on wrapper). +func TestProcessToolSieveDSMLPrefixVariantDoesNotLeak(t *testing.T) { + var state State + chunks := []string{ + "\n", + " <|DSML|invoke name=\"execute_command\">\n", + " <|DSML|parameter name=\"command\">\n", + " \n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"execute_command"})...) + } + events = append(events, Flush(&state, []string{"execute_command"})...) + + var textContent string + var toolCalls int + for _, evt := range events { + textContent += evt.Content + toolCalls += len(evt.ToolCalls) + } + + if strings.Contains(strings.ToLower(textContent), "dsml") || strings.Contains(textContent, "execute_command") { + t.Fatalf("DSML prefix variant leaked to text: %q", textContent) + } + if toolCalls != 1 { + t.Fatalf("expected one tool call from DSML prefix variant, got %d events=%#v", toolCalls, events) + } +} + +// Test with (no pipe anywhere) should be buffered and parsed. +func TestProcessToolSieveDSMLBarePrefixVariantDoesNotLeak(t *testing.T) { + var state State + chunks := []string{ + "\n", + "\n", + "\n", + "\n", + "", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"execute_command"})...) + } + events = append(events, Flush(&state, []string{"execute_command"})...) + + var textContent string + var toolCalls int + for _, evt := range events { + textContent += evt.Content + toolCalls += len(evt.ToolCalls) + } + + if strings.Contains(strings.ToLower(textContent), "dsml") || strings.Contains(textContent, "execute_command") { + t.Fatalf("DSML bare prefix variant leaked to text: %q", textContent) + } + if toolCalls != 1 { + t.Fatalf("expected one tool call from DSML bare prefix variant, got %d events=%#v", toolCalls, events) + } +} + +func TestProcessToolSieveCJKAngleDSMDriftDoesNotLeak(t *testing.T) { + var state State + chunks := []string{ + "\n", + "\n", + "〈![CDATA[Check tracking branch status]]〉〈/DSM|parameter〉\n", + "〈![CDATA[git status -b --short]]〉〈/DSM|parameter〉\n", + "〈/DSM|invoke〉\n", + "〈/DSM|tool_calls〉", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var textContent string + var calls []toolcall.ParsedToolCall + for _, evt := range events { + textContent += evt.Content + calls = append(calls, evt.ToolCalls...) + } + + if strings.Contains(textContent, "DSM") || strings.Contains(textContent, "git status") { + t.Fatalf("CJK-angle DSM drift leaked to text: %q events=%#v", textContent, events) + } + if len(calls) != 1 { + t.Fatalf("expected one CJK-angle DSM drift tool call, got %d events=%#v", len(calls), events) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "git status -b --short" { + t.Fatalf("unexpected CJK-angle DSM drift call: %#v", calls[0]) + } +} + +func TestProcessToolSieveFullwidthBangDSMLDriftDoesNotLeak(t *testing.T) { + var state State + chunks := []string{ + "<!DSML!tool_calls>\n", + " <!DSML!invoke name=“Bash”>\n", + " <!DSML!parameter name=“command”><![CDATA[lsof -i :4321 -t]]><!/DSML!parameter>\n", + " <!DSML!parameter name=“description”><![CDATA[Verify port 4321 is free]]><!/DSML!parameter>\n", + " <!/DSML!invoke>\n", + " <!/DSML!tool_calls>", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var textContent string + var calls []toolcall.ParsedToolCall + for _, evt := range events { + textContent += evt.Content + calls = append(calls, evt.ToolCalls...) + } + + if strings.Contains(textContent, "DSML") || strings.Contains(textContent, "lsof") { + t.Fatalf("fullwidth-bang DSML drift leaked to text: %q events=%#v", textContent, events) + } + if len(calls) != 1 { + t.Fatalf("expected one fullwidth-bang DSML drift tool call, got %d events=%#v", len(calls), events) + } + if calls[0].Name != "Bash" || calls[0].Input["command"] != "lsof -i :4321 -t" { + t.Fatalf("unexpected fullwidth-bang DSML drift call: %#v", calls[0]) + } +} + +func TestProcessToolSieveIdeographicCommaDSMLDriftDoesNotLeak(t *testing.T) { + var state State + chunks := []string{ + "<、DSML、tool_calls>\n", + " <、DSML、invoke name=\"Bash\">\n", + " <、DSML、parameter name=\"command\"><、[CDATA[git commit -m \"$(cat <<'EOF'\n", + "feat: expand fullwidth bang separator and curly quote tolerance in DSML tool parsing\n\n", + "Co-Authored-By: Claude Opus 4.6 noreply@anthropic.com\n", + "EOF\n", + ")\"]]><、/DSML、parameter>\n", + " <、DSML、parameter name=\"description\"><、[CDATA[Create commit with staged changes]]><、/DSML、parameter>\n", + " <、/DSML、invoke>\n", + "<、/DSML、tool_calls>", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"Bash"})...) + } + events = append(events, Flush(&state, []string{"Bash"})...) + + var textContent string + var calls []toolcall.ParsedToolCall + for _, evt := range events { + textContent += evt.Content + calls = append(calls, evt.ToolCalls...) + } + + if strings.Contains(textContent, "DSML") || strings.Contains(textContent, "git commit") { + t.Fatalf("ideographic-comma DSML drift leaked to text: %q events=%#v", textContent, events) + } + if len(calls) != 1 { + t.Fatalf("expected one ideographic-comma DSML drift tool call, got %d events=%#v", len(calls), events) + } + command, _ := calls[0].Input["command"].(string) + if calls[0].Name != "Bash" || !strings.Contains(command, "git commit -m") { + t.Fatalf("unexpected ideographic-comma DSML drift call: %#v", calls[0]) + } +} + +func TestProcessToolSieveParsesFullwidthClosingSlashAndKeepsSuffixText(t *testing.T) { + var state State + chunk := `<|DSML|tool_calls><|DSML|invoke name="execute_code"><|DSML|parameter name="code"></DSML|tool_calls> sao cụm này lại đc trả là 1 message` + events := ProcessChunk(&state, chunk, []string{"execute_code"}) + events = append(events, Flush(&state, []string{"execute_code"})...) + + var textContent strings.Builder + toolCalls := 0 + var parsed Event + for _, evt := range events { + textContent.WriteString(evt.Content) + if len(evt.ToolCalls) > 0 { + parsed = evt + } + toolCalls += len(evt.ToolCalls) + } + if toolCalls != 1 { + t.Fatalf("expected exactly one parsed tool call from fullwidth closing slash block, got %d events=%#v", toolCalls, events) + } + if parsed.ToolCalls[0].Name != "execute_code" || parsed.ToolCalls[0].Input["code"] != `print("hi")` { + t.Fatalf("unexpected parsed call from fullwidth closing slash block: %#v", parsed.ToolCalls[0]) + } + if got := textContent.String(); got != " sao cụm này lại đc trả là 1 message" { + t.Fatalf("expected suffix text to be preserved, got %q", got) + } +} + +func TestProcessToolSieveParsesSentencePieceSeparatorAndFullwidthTerminator(t *testing.T) { + var state State + chunk := `<|DSML▁tool_calls|><|DSML▁invoke▁name="execute_code"><|DSML▁parameter▁name="code"> 0 { + parsed = evt + } + toolCalls += len(evt.ToolCalls) + } + if toolCalls != 1 { + t.Fatalf("expected exactly one parsed tool call from sentencepiece/fullwidth-terminator block, got %d events=%#v", toolCalls, events) + } + if parsed.ToolCalls[0].Name != "execute_code" || parsed.ToolCalls[0].Input["code"] != `print("hi")` { + t.Fatalf("unexpected parsed call from sentencepiece/fullwidth-terminator block: %#v", parsed.ToolCalls[0]) + } + if got := textContent.String(); got != " suffix" { + t.Fatalf("expected suffix text to be preserved, got %q", got) + } +} + +func TestProcessToolSieveParsesFullwidthOpeningDelimiterAndUnicodeAttributes(t *testing.T) { + var state State + chunk := `<|DSML tool_calls><|DSML invoke name=“execute_code”><|DSML parameter name=“code”></DSML|parameter></DSML|invoke></DSML|tool_calls> suffix` + events := ProcessChunk(&state, chunk, []string{"execute_code"}) + events = append(events, Flush(&state, []string{"execute_code"})...) + + var textContent strings.Builder + toolCalls := 0 + var parsed Event + for _, evt := range events { + textContent.WriteString(evt.Content) + if len(evt.ToolCalls) > 0 { + parsed = evt + } + toolCalls += len(evt.ToolCalls) + } + if toolCalls != 1 { + t.Fatalf("expected exactly one parsed tool call from fullwidth-opening/Unicode-attr block, got %d events=%#v", toolCalls, events) + } + if parsed.ToolCalls[0].Name != "execute_code" || parsed.ToolCalls[0].Input["code"] != `print("hi")` { + t.Fatalf("unexpected parsed call from fullwidth-opening/Unicode-attr block: %#v", parsed.ToolCalls[0]) + } + if got := textContent.String(); got != " suffix" { + t.Fatalf("expected suffix text to be preserved, got %q", got) + } +} + +func TestProcessToolSieveParsesConfusableCandidateShellAndKeepsSuffixText(t *testing.T) { + var state State + chunk := "<|\u200b\uff24\u0405\u039cL|to\u03bfl\uff3fcalls><|\ufeffDSML|inv\u03bfk\u0435 n\u0430me\uff1d\u201cexecute_code\u201d><|\u200bDSML|par\u0430meter n\u0430me\uff1d\u201ccode\u201d> suffix" + events := ProcessChunk(&state, chunk, []string{"execute_code"}) + events = append(events, Flush(&state, []string{"execute_code"})...) + + var textContent strings.Builder + toolCalls := 0 + var parsed Event + for _, evt := range events { + textContent.WriteString(evt.Content) + if len(evt.ToolCalls) > 0 { + parsed = evt + } + toolCalls += len(evt.ToolCalls) + } + if toolCalls != 1 { + t.Fatalf("expected exactly one parsed tool call from confusable-shell block, got %d events=%#v", toolCalls, events) + } + if parsed.ToolCalls[0].Name != "execute_code" || parsed.ToolCalls[0].Input["code"] != `print("hi")` { + t.Fatalf("unexpected parsed call from confusable-shell block: %#v", parsed.ToolCalls[0]) + } + if got := textContent.String(); got != " suffix" { + t.Fatalf("expected suffix text to be preserved, got %q", got) + } +} + +func TestProcessToolSieveRepairsConfusableMissingWrapperAndKeepsSuffixText(t *testing.T) { + var state State + chunks := []string{ + "\n", + " \n", + "\n", + " trailing prose", + } + var events []Event + for _, c := range chunks { + events = append(events, ProcessChunk(&state, c, []string{"read_file"})...) + } + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + var parsed Event + for _, evt := range events { + textContent.WriteString(evt.Content) + if len(evt.ToolCalls) > 0 { + parsed = evt + } + toolCalls += len(evt.ToolCalls) + } + if toolCalls != 1 { + t.Fatalf("expected repaired confusable missing-wrapper stream to emit one tool call, got %d events=%#v", toolCalls, events) + } + if parsed.ToolCalls[0].Name != "read_file" || parsed.ToolCalls[0].Input["path"] != "README.md" { + t.Fatalf("unexpected parsed call from repaired confusable missing-wrapper block: %#v", parsed.ToolCalls[0]) + } + if got := textContent.String(); got != " trailing prose" { + t.Fatalf("expected suffix prose to be preserved, got %q", got) + } +} + +func TestProcessToolSieveKeepsConfusableNearMissWrapperAsText(t *testing.T) { + var state State + chunk := "README.md" + events := ProcessChunk(&state, chunk, []string{"read_file"}) + events = append(events, Flush(&state, []string{"read_file"})...) + + var textContent strings.Builder + toolCalls := 0 + for _, evt := range events { + textContent.WriteString(evt.Content) + toolCalls += len(evt.ToolCalls) + } + if toolCalls != 0 { + t.Fatalf("expected confusable near-miss wrapper to remain text, got %d events=%#v", toolCalls, events) + } + if got := textContent.String(); got != chunk { + t.Fatalf("expected confusable near-miss wrapper to pass through unchanged, got %q", got) + } +} diff --git a/internal/translatorcliproxy/bridge.go b/internal/translatorcliproxy/bridge.go new file mode 100644 index 0000000000000000000000000000000000000000..c5d67417f851299716b6a250fa92448caacbe492 --- /dev/null +++ b/internal/translatorcliproxy/bridge.go @@ -0,0 +1,127 @@ +package translatorcliproxy + +import ( + "bytes" + "context" + "encoding/json" + "strings" + + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" + _ "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator/builtin" +) + +func ToOpenAI(from sdktranslator.Format, model string, raw []byte, stream bool) []byte { + return sdktranslator.TranslateRequest(from, sdktranslator.FormatOpenAI, model, raw, stream) +} + +func FromOpenAINonStream(to sdktranslator.Format, model string, originalReq, translatedReq, raw []byte) []byte { + var param any + converted := sdktranslator.TranslateNonStream(context.Background(), sdktranslator.FormatOpenAI, to, model, originalReq, translatedReq, raw, ¶m) + usage, ok := extractOpenAIUsageFromJSON(raw) + if !ok { + return converted + } + return injectNonStreamUsageMetadata(converted, to, usage) +} + +func FromOpenAIStream(to sdktranslator.Format, model string, originalReq, translatedReq, streamBody []byte) []byte { + var out bytes.Buffer + var param any + for _, line := range bytes.Split(streamBody, []byte("\n")) { + trimmed := strings.TrimSpace(string(line)) + if trimmed == "" { + continue + } + payload := append([]byte(nil), line...) + if !bytes.HasPrefix(payload, []byte("data:")) { + continue + } + chunks := sdktranslator.TranslateStream(context.Background(), sdktranslator.FormatOpenAI, to, model, originalReq, translatedReq, payload, ¶m) + for i := range chunks { + out.Write(chunks[i]) + if !bytes.HasSuffix(chunks[i], []byte("\n")) { + out.WriteByte('\n') + } + } + } + return out.Bytes() +} + +func ParseFormat(name string) sdktranslator.Format { + switch strings.ToLower(strings.TrimSpace(name)) { + case "openai", "openai-chat", "chat", "chat-completions": + return sdktranslator.FormatOpenAI + case "openai-response", "responses", "openai-responses": + return sdktranslator.FormatOpenAIResponse + case "claude", "anthropic": + return sdktranslator.FormatClaude + case "gemini", "google": + return sdktranslator.FormatGemini + case "gemini-cli", "geminicli": + return sdktranslator.FormatGeminiCLI + case "codex", "openai-codex": + return sdktranslator.FormatCodex + case "antigravity": + return sdktranslator.FormatAntigravity + default: + return sdktranslator.FromString(name) + } +} + +func ToOpenAIByName(formatName, model string, raw []byte, stream bool) []byte { + return ToOpenAI(ParseFormat(formatName), model, raw, stream) +} + +func extractOpenAIUsageFromJSON(raw []byte) (openAIUsage, bool) { + payload := map[string]any{} + if err := json.Unmarshal(raw, &payload); err != nil { + return openAIUsage{}, false + } + usageObj, _ := payload["usage"].(map[string]any) + if usageObj == nil { + return openAIUsage{}, false + } + p := toInt(usageObj["prompt_tokens"]) + c := toInt(usageObj["completion_tokens"]) + t := toInt(usageObj["total_tokens"]) + if p <= 0 { + p = toInt(usageObj["input_tokens"]) + } + if c <= 0 { + c = toInt(usageObj["output_tokens"]) + } + if t <= 0 { + t = p + c + } + if p <= 0 && c <= 0 && t <= 0 { + return openAIUsage{}, false + } + return openAIUsage{PromptTokens: p, CompletionTokens: c, TotalTokens: t}, true +} + +func injectNonStreamUsageMetadata(converted []byte, target sdktranslator.Format, usage openAIUsage) []byte { + obj := map[string]any{} + if err := json.Unmarshal(converted, &obj); err != nil { + return converted + } + switch target { + case sdktranslator.FormatClaude: + obj["usage"] = map[string]any{ + "input_tokens": usage.PromptTokens, + "output_tokens": usage.CompletionTokens, + } + case sdktranslator.FormatGemini: + obj["usageMetadata"] = map[string]any{ + "promptTokenCount": usage.PromptTokens, + "candidatesTokenCount": usage.CompletionTokens, + "totalTokenCount": usage.TotalTokens, + } + default: + return converted + } + out, err := json.Marshal(obj) + if err != nil { + return converted + } + return out +} diff --git a/internal/translatorcliproxy/bridge_test.go b/internal/translatorcliproxy/bridge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..3370360f8bdcfd68655bbcf62d83bf644526417c --- /dev/null +++ b/internal/translatorcliproxy/bridge_test.go @@ -0,0 +1,116 @@ +package translatorcliproxy + +import ( + "strings" + "testing" + + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" +) + +func TestToOpenAIClaude(t *testing.T) { + raw := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":false}`) + got := ToOpenAI(sdktranslator.FormatClaude, "claude-sonnet-4-5", raw, false) + s := string(got) + if !strings.Contains(s, `"messages"`) || !strings.Contains(s, `"model"`) { + t.Fatalf("unexpected translated request: %s", s) + } +} + +func TestToOpenAIGeminiThinkingBudgetZeroDisablesReasoning(t *testing.T) { + raw := []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}],"generationConfig":{"thinkingConfig":{"thinkingBudget":0}}}`) + got := string(ToOpenAI(sdktranslator.FormatGemini, "gemini-2.5-flash", raw, false)) + if !strings.Contains(got, `"reasoning_effort":"none"`) { + t.Fatalf("expected Gemini thinkingBudget=0 to translate to reasoning_effort none, got: %s", got) + } +} + +func TestFromOpenAINonStreamClaude(t *testing.T) { + original := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":false}`) + translatedReq := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":false}`) + openaibody := []byte(`{"id":"chatcmpl_1","object":"chat.completion","created":1,"model":"claude-sonnet-4-5","choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":1,"completion_tokens":1,"total_tokens":2}}`) + got := FromOpenAINonStream(sdktranslator.FormatClaude, "claude-sonnet-4-5", original, translatedReq, openaibody) + if !strings.Contains(string(got), `"type":"message"`) { + t.Fatalf("expected claude response format, got: %s", string(got)) + } +} + +func TestFromOpenAINonStreamClaudePreservesUsageFromOpenAI(t *testing.T) { + original := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":false}`) + translatedReq := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":false}`) + openaibody := []byte(`{"id":"chatcmpl_1","object":"chat.completion","created":1,"model":"claude-sonnet-4-5","choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":11,"completion_tokens":29,"total_tokens":40}}`) + got := string(FromOpenAINonStream(sdktranslator.FormatClaude, "claude-sonnet-4-5", original, translatedReq, openaibody)) + if !strings.Contains(got, `"input_tokens":11`) || !strings.Contains(got, `"output_tokens":29`) { + t.Fatalf("expected claude usage to preserve prompt/completion tokens, got: %s", got) + } +} + +func TestFromOpenAINonStreamGeminiPreservesUsageFromOpenAI(t *testing.T) { + original := []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`) + translatedReq := []byte(`{"model":"gemini-2.5-pro","messages":[{"role":"user","content":"hi"}],"stream":false}`) + openaibody := []byte(`{"id":"chatcmpl_1","object":"chat.completion","created":1,"model":"gemini-2.5-pro","choices":[{"index":0,"message":{"role":"assistant","content":"hello"},"finish_reason":"stop"}],"usage":{"prompt_tokens":11,"completion_tokens":29,"total_tokens":40}}`) + got := string(FromOpenAINonStream(sdktranslator.FormatGemini, "gemini-2.5-pro", original, translatedReq, openaibody)) + if !strings.Contains(got, `"promptTokenCount":11`) || !strings.Contains(got, `"candidatesTokenCount":29`) || !strings.Contains(got, `"totalTokenCount":40`) { + t.Fatalf("expected gemini usageMetadata to preserve prompt/completion tokens, got: %s", got) + } +} + +func TestFromOpenAINonStreamPreservesResponsesUsageShape(t *testing.T) { + original := []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`) + translatedReq := []byte(`{"model":"gemini-2.5-pro","messages":[{"role":"user","content":"hi"}],"stream":false}`) + openaibody := []byte(`{"id":"resp_1","object":"response","model":"gemini-2.5-pro","usage":{"input_tokens":"11","output_tokens":"29","total_tokens":"40"}}`) + gotGemini := string(FromOpenAINonStream(sdktranslator.FormatGemini, "gemini-2.5-pro", original, translatedReq, openaibody)) + if !strings.Contains(gotGemini, `"promptTokenCount":11`) || !strings.Contains(gotGemini, `"candidatesTokenCount":29`) || !strings.Contains(gotGemini, `"totalTokenCount":40`) { + t.Fatalf("expected gemini usageMetadata from input/output usage fields, got: %s", gotGemini) + } + + origClaude := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":false}`) + gotClaude := string(FromOpenAINonStream(sdktranslator.FormatClaude, "claude-sonnet-4-5", origClaude, origClaude, openaibody)) + if !strings.Contains(gotClaude, `"input_tokens":11`) || !strings.Contains(gotClaude, `"output_tokens":29`) { + t.Fatalf("expected claude usage from input/output usage fields, got: %s", gotClaude) + } +} + +func TestParseFormatAliases(t *testing.T) { + cases := map[string]sdktranslator.Format{ + "responses": sdktranslator.FormatOpenAIResponse, + "anthropic": sdktranslator.FormatClaude, + "geminicli": sdktranslator.FormatGeminiCLI, + "openai-codex": sdktranslator.FormatCodex, + "antigravity": sdktranslator.FormatAntigravity, + "chat-completions": sdktranslator.FormatOpenAI, + } + for in, want := range cases { + if got := ParseFormat(in); got != want { + t.Fatalf("ParseFormat(%q)=%q want %q", in, got, want) + } + } +} + +func TestToOpenAIByNameAllSupportedFormats(t *testing.T) { + tests := []struct { + name string + format string + model string + body string + }{ + {name: "openai", format: "openai", model: "gpt-4.1", body: `{"model":"gpt-4.1","messages":[{"role":"user","content":"hi"}],"stream":false}`}, + {name: "responses", format: "responses", model: "gpt-4.1", body: `{"model":"gpt-4.1","input":"hello","stream":false}`}, + {name: "claude", format: "claude", model: "claude-sonnet-4-5", body: `{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hello"}],"stream":false}`}, + {name: "gemini", format: "gemini", model: "gemini-2.5-pro", body: `{"contents":[{"role":"user","parts":[{"text":"hello"}]}]}`}, + {name: "gemini-cli", format: "gemini-cli", model: "gemini-2.5-pro", body: `{"model":"gemini-2.5-pro","messages":[{"role":"user","content":"hello"}],"stream":false}`}, + {name: "codex", format: "codex", model: "gpt-5-codex", body: `{"model":"gpt-5-codex","messages":[{"role":"user","content":"hello"}],"stream":false}`}, + {name: "antigravity", format: "antigravity", model: "gpt-4.1", body: `{"model":"gpt-4.1","messages":[{"role":"user","content":"hello"}],"stream":false}`}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + got := ToOpenAIByName(tc.format, tc.model, []byte(tc.body), false) + if len(got) == 0 { + t.Fatalf("expected non-empty conversion result for format=%s", tc.format) + } + if !strings.Contains(string(got), `"model"`) { + t.Fatalf("expected model field in converted payload, got=%s", string(got)) + } + }) + } +} diff --git a/internal/translatorcliproxy/stream_writer.go b/internal/translatorcliproxy/stream_writer.go new file mode 100644 index 0000000000000000000000000000000000000000..ac7fc416f66475c111e6453b271dc9ef40b7944c --- /dev/null +++ b/internal/translatorcliproxy/stream_writer.go @@ -0,0 +1,240 @@ +package translatorcliproxy + +import ( + "bytes" + "context" + "encoding/json" + "net/http" + "strconv" + "strings" + + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" +) + +// OpenAIStreamTranslatorWriter translates OpenAI SSE output to another client format in real-time. +type OpenAIStreamTranslatorWriter struct { + dst http.ResponseWriter + target sdktranslator.Format + model string + originalReq []byte + translatedReq []byte + param any + statusCode int + headersSent bool + lineBuf bytes.Buffer +} + +func NewOpenAIStreamTranslatorWriter(dst http.ResponseWriter, target sdktranslator.Format, model string, originalReq, translatedReq []byte) *OpenAIStreamTranslatorWriter { + return &OpenAIStreamTranslatorWriter{ + dst: dst, + target: target, + model: model, + originalReq: originalReq, + translatedReq: translatedReq, + statusCode: http.StatusOK, + } +} + +func (w *OpenAIStreamTranslatorWriter) Header() http.Header { + return w.dst.Header() +} + +func (w *OpenAIStreamTranslatorWriter) WriteHeader(statusCode int) { + if w.headersSent { + return + } + w.statusCode = statusCode + w.headersSent = true + w.dst.WriteHeader(statusCode) +} + +func (w *OpenAIStreamTranslatorWriter) Write(p []byte) (int, error) { + if !w.headersSent { + w.WriteHeader(http.StatusOK) + } + if w.statusCode < 200 || w.statusCode >= 300 { + return w.dst.Write(p) + } + w.lineBuf.Write(p) + for { + line, ok := w.readOneLine() + if !ok { + break + } + trimmed := bytes.TrimSpace(line) + if len(trimmed) == 0 { + continue + } + if bytes.HasPrefix(trimmed, []byte(":")) { + if _, err := w.dst.Write(trimmed); err != nil { + return len(p), err + } + if _, err := w.dst.Write([]byte("\n\n")); err != nil { + return len(p), err + } + if f, ok := w.dst.(http.Flusher); ok { + f.Flush() + } + continue + } + if !bytes.HasPrefix(trimmed, []byte("data:")) { + continue + } + usage, hasUsage := extractOpenAIUsage(trimmed) + chunks := sdktranslator.TranslateStream(context.Background(), sdktranslator.FormatOpenAI, w.target, w.model, w.originalReq, w.translatedReq, trimmed, &w.param) + if hasUsage { + for i := range chunks { + chunks[i] = injectStreamUsageMetadata(chunks[i], w.target, usage) + } + } + for i := range chunks { + if len(chunks[i]) == 0 { + continue + } + if _, err := w.dst.Write(chunks[i]); err != nil { + return len(p), err + } + if !bytes.HasSuffix(chunks[i], []byte("\n")) { + if _, err := w.dst.Write([]byte("\n")); err != nil { + return len(p), err + } + } + } + if f, ok := w.dst.(http.Flusher); ok { + f.Flush() + } + } + return len(p), nil +} + +func (w *OpenAIStreamTranslatorWriter) Flush() { + if f, ok := w.dst.(http.Flusher); ok { + f.Flush() + } +} + +func (w *OpenAIStreamTranslatorWriter) Unwrap() http.ResponseWriter { + return w.dst +} + +func (w *OpenAIStreamTranslatorWriter) readOneLine() ([]byte, bool) { + b := w.lineBuf.Bytes() + idx := bytes.IndexByte(b, '\n') + if idx < 0 { + return nil, false + } + line := append([]byte(nil), b[:idx]...) + w.lineBuf.Next(idx + 1) + return line, true +} + +type openAIUsage struct { + PromptTokens int + CompletionTokens int + TotalTokens int +} + +func extractOpenAIUsage(line []byte) (openAIUsage, bool) { + raw := strings.TrimSpace(strings.TrimPrefix(string(line), "data:")) + if raw == "" || raw == "[DONE]" { + return openAIUsage{}, false + } + var payload map[string]any + if err := json.Unmarshal([]byte(raw), &payload); err != nil { + return openAIUsage{}, false + } + usageObj, _ := payload["usage"].(map[string]any) + if usageObj == nil { + return openAIUsage{}, false + } + p := toInt(usageObj["prompt_tokens"]) + c := toInt(usageObj["completion_tokens"]) + t := toInt(usageObj["total_tokens"]) + if p <= 0 { + p = toInt(usageObj["input_tokens"]) + } + if c <= 0 { + c = toInt(usageObj["output_tokens"]) + } + if p <= 0 && c <= 0 && t <= 0 { + return openAIUsage{}, false + } + if t <= 0 { + t = p + c + } + return openAIUsage{PromptTokens: p, CompletionTokens: c, TotalTokens: t}, true +} + +func injectStreamUsageMetadata(chunk []byte, target sdktranslator.Format, usage openAIUsage) []byte { + if target != sdktranslator.FormatGemini { + return chunk + } + suffix := "" + switch { + case bytes.HasSuffix(chunk, []byte("\n\n")): + suffix = "\n\n" + case bytes.HasSuffix(chunk, []byte("\n")): + suffix = "\n" + } + text := strings.TrimSpace(string(chunk)) + if text == "" { + return chunk + } + var ( + hasDataPrefix bool + jsonText = text + ) + if strings.HasPrefix(jsonText, "data:") { + hasDataPrefix = true + jsonText = strings.TrimSpace(strings.TrimPrefix(jsonText, "data:")) + } + if jsonText == "" || jsonText == "[DONE]" { + return chunk + } + obj := map[string]any{} + if err := json.Unmarshal([]byte(jsonText), &obj); err != nil { + return chunk + } + if _, ok := obj["candidates"]; !ok { + return chunk + } + obj["usageMetadata"] = map[string]any{ + "promptTokenCount": usage.PromptTokens, + "candidatesTokenCount": usage.CompletionTokens, + "totalTokenCount": usage.TotalTokens, + } + b, err := json.Marshal(obj) + if err != nil { + return chunk + } + if hasDataPrefix { + return []byte("data: " + string(b) + suffix) + } + if suffix != "" { + return append(b, []byte(suffix)...) + } + return b +} + +func toInt(v any) int { + switch x := v.(type) { + case int: + return x + case int32: + return int(x) + case int64: + return int(x) + case float64: + return int(x) + case float32: + return int(x) + case string: + n, err := strconv.Atoi(strings.TrimSpace(x)) + if err != nil { + return 0 + } + return n + default: + return 0 + } +} diff --git a/internal/translatorcliproxy/stream_writer_test.go b/internal/translatorcliproxy/stream_writer_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f4758d47adc9f1fc8a341a2876b71cf7d230d3a9 --- /dev/null +++ b/internal/translatorcliproxy/stream_writer_test.go @@ -0,0 +1,88 @@ +package translatorcliproxy + +import ( + "net/http/httptest" + "strings" + "testing" + + sdktranslator "github.com/router-for-me/CLIProxyAPI/v6/sdk/translator" +) + +func TestOpenAIStreamTranslatorWriterClaude(t *testing.T) { + original := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":true}`) + translated := []byte(`{"model":"claude-sonnet-4-5","messages":[{"role":"user","content":"hi"}],"stream":true}`) + + rec := httptest.NewRecorder() + w := NewOpenAIStreamTranslatorWriter(rec, sdktranslator.FormatClaude, "claude-sonnet-4-5", original, translated) + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(200) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl_1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"claude-sonnet-4-5\",\"choices\":[{\"index\":0,\"delta\":{\"role\":\"assistant\"},\"finish_reason\":null}]}\n\n")) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl_1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"claude-sonnet-4-5\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hi\"},\"finish_reason\":null}]}\n\n")) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl_1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"claude-sonnet-4-5\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":11,\"completion_tokens\":29,\"total_tokens\":40}}\n\n")) + _, _ = w.Write([]byte("data: [DONE]\n\n")) + + body := rec.Body.String() + if !strings.Contains(body, "event: message_start") { + t.Fatalf("expected claude message_start event, got: %s", body) + } + if !strings.Contains(body, `"output_tokens":29`) { + t.Fatalf("expected claude stream usage to preserve output tokens, got: %s", body) + } +} + +func TestOpenAIStreamTranslatorWriterGemini(t *testing.T) { + original := []byte(`{"contents":[{"role":"user","parts":[{"text":"hi"}]}]}`) + translated := []byte(`{"model":"gemini-2.5-pro","messages":[{"role":"user","content":"hi"}],"stream":true}`) + + rec := httptest.NewRecorder() + w := NewOpenAIStreamTranslatorWriter(rec, sdktranslator.FormatGemini, "gemini-2.5-pro", original, translated) + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(200) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl_1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"gemini-2.5-pro\",\"choices\":[{\"index\":0,\"delta\":{\"content\":\"hi\"},\"finish_reason\":null}]}\n\n")) + _, _ = w.Write([]byte("data: {\"id\":\"chatcmpl_1\",\"object\":\"chat.completion.chunk\",\"created\":1,\"model\":\"gemini-2.5-pro\",\"choices\":[{\"index\":0,\"delta\":{},\"finish_reason\":\"stop\"}],\"usage\":{\"prompt_tokens\":11,\"completion_tokens\":29,\"total_tokens\":40}}\n\n")) + _, _ = w.Write([]byte("data: [DONE]\n\n")) + + body := rec.Body.String() + if !strings.Contains(body, "candidates") { + t.Fatalf("expected gemini stream payload, got: %s", body) + } + if !strings.Contains(body, `"promptTokenCount":11`) || !strings.Contains(body, `"candidatesTokenCount":29`) { + t.Fatalf("expected gemini stream usageMetadata to preserve usage, got: %s", body) + } +} + +func TestOpenAIStreamTranslatorWriterPreservesKeepAliveComment(t *testing.T) { + rec := httptest.NewRecorder() + w := NewOpenAIStreamTranslatorWriter(rec, sdktranslator.FormatGemini, "gemini-2.5-pro", []byte(`{}`), []byte(`{}`)) + w.Header().Set("Content-Type", "text/event-stream") + w.WriteHeader(200) + _, _ = w.Write([]byte(": keep-alive\n\n")) + + body := rec.Body.String() + if !strings.Contains(body, ": keep-alive\n\n") { + t.Fatalf("expected keep-alive comment passthrough, got %q", body) + } +} + +func TestInjectStreamUsageMetadataPreservesSSEFrameTerminator(t *testing.T) { + chunk := []byte("data: {\"candidates\":[{\"index\":0}],\"model\":\"gemini-2.5-pro\"}\n\n") + usage := openAIUsage{PromptTokens: 11, CompletionTokens: 29, TotalTokens: 40} + got := injectStreamUsageMetadata(chunk, sdktranslator.FormatGemini, usage) + if !strings.HasSuffix(string(got), "\n\n") { + t.Fatalf("expected injected chunk to preserve \\n\\n frame terminator, got %q", string(got)) + } + if !strings.Contains(string(got), `"usageMetadata"`) { + t.Fatalf("expected usageMetadata injected, got %q", string(got)) + } +} + +func TestExtractOpenAIUsageSupportsResponsesUsageFields(t *testing.T) { + line := []byte(`data: {"usage":{"input_tokens":"11","output_tokens":"29","total_tokens":"40"}}`) + got, ok := extractOpenAIUsage(line) + if !ok { + t.Fatal("expected usage extracted from input/output usage fields") + } + if got.PromptTokens != 11 || got.CompletionTokens != 29 || got.TotalTokens != 40 { + t.Fatalf("unexpected usage extracted: %#v", got) + } +} diff --git a/internal/util/helpers.go b/internal/util/helpers.go new file mode 100644 index 0000000000000000000000000000000000000000..15e6de7f748066c3c75072e22d5a05bdd0d95db3 --- /dev/null +++ b/internal/util/helpers.go @@ -0,0 +1,37 @@ +package util + +import ( + "encoding/json" + "net/http" +) + +// WriteJSON writes a JSON response with the given status code. +// This is a shared helper to avoid duplicate writeJSON functions +// in openai, claude, and admin packages. +func WriteJSON(w http.ResponseWriter, status int, payload any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(payload) +} + +// ToBool loosely converts an interface value to bool. +func ToBool(v any) bool { + if b, ok := v.(bool); ok { + return b + } + return false +} + +// IntFrom converts a JSON-decoded numeric value (float64, int, int64) to int. +func IntFrom(v any) int { + switch n := v.(type) { + case float64: + return int(n) + case int: + return n + case int64: + return int(n) + default: + return 0 + } +} diff --git a/internal/util/messages.go b/internal/util/messages.go new file mode 100644 index 0000000000000000000000000000000000000000..3a43f2441c64d26892f1f1d5b3a0ff9d5896aece --- /dev/null +++ b/internal/util/messages.go @@ -0,0 +1,51 @@ +package util + +import ( + "ds2api/internal/claudeconv" + "ds2api/internal/config" + "ds2api/internal/prompt" +) + +const ClaudeDefaultModel = "claude-sonnet-4-6" + +type Message struct { + Role string `json:"role"` + Content any `json:"content"` +} + +func MessagesPrepare(messages []map[string]any) string { + return prompt.MessagesPrepare(messages) +} + +func normalizeContent(v any) string { + return prompt.NormalizeContent(v) +} + +func ConvertClaudeToDeepSeek(claudeReq map[string]any, store *config.Store) map[string]any { + return claudeconv.ConvertClaudeToDeepSeek(claudeReq, store, ClaudeDefaultModel) +} + +// EstimateTokens provides a rough token count approximation. +// For ASCII text (English, code, etc.) we use ~4 chars per token. +// For non-ASCII text (Chinese, Japanese, Korean, etc.) we use ~1.3 chars per token, +// which better reflects typical BPE tokenizer behavior for CJK scripts. +func EstimateTokens(text string) int { + if text == "" { + return 0 + } + asciiChars := 0 + nonASCIIChars := 0 + for _, r := range text { + if r < 128 { + asciiChars++ + } else { + nonASCIIChars++ + } + } + // ASCII: ~4 chars per token; non-ASCII (CJK): ~1.3 chars per token + n := asciiChars/4 + (nonASCIIChars*10+7)/13 + if n < 1 { + return 1 + } + return n +} diff --git a/internal/util/messages_test.go b/internal/util/messages_test.go new file mode 100644 index 0000000000000000000000000000000000000000..fef909db3f207c8b4d893bb56b6ec477c3bcee9d --- /dev/null +++ b/internal/util/messages_test.go @@ -0,0 +1,143 @@ +package util + +import ( + "strings" + "testing" + + "ds2api/internal/config" +) + +func TestMessagesPrepareBasic(t *testing.T) { + messages := []map[string]any{{"role": "user", "content": "Hello"}} + got := MessagesPrepare(messages) + if got == "" { + t.Fatal("expected non-empty prompt") + } + if !strings.HasPrefix(got, "User: ") { + t.Fatalf("expected prompt to start with user block, got %q", got) + } + if !strings.Contains(got, "Hello") || !strings.HasSuffix(got, "Assistant: ") { + t.Fatalf("unexpected prompt: %q", got) + } +} + +func TestMessagesPrepareRoles(t *testing.T) { + messages := []map[string]any{ + {"role": "system", "content": "You are helper"}, + {"role": "user", "content": "Hi"}, + {"role": "assistant", "content": "Hello"}, + {"role": "tool", "content": "Search results"}, + {"role": "user", "content": "How are you"}, + } + got := MessagesPrepare(messages) + if !contains(got, "You are helper") || !contains(got, "User: Hi") { + t.Fatalf("expected system/user content in %q", got) + } + if !contains(got, "User: HiAssistant: Hello\n") { + t.Fatalf("expected user/assistant separation in %q", got) + } + if !contains(got, "Assistant: Hello\nTool: Search results\n") { + t.Fatalf("expected assistant/tool separation in %q", got) + } + if !contains(got, "Tool: Search results\nUser: How are you") { + t.Fatalf("expected tool/user separation in %q", got) + } + if !contains(got, "Assistant: ") { + t.Fatalf("expected assistant marker in %q", got) + } + if !contains(got, "System: ") { + t.Fatalf("expected system marker in %q", got) + } + if !contains(got, "User: ") { + t.Fatalf("expected user marker in %q", got) + } + if !contains(got, "Tool: ") { + t.Fatalf("expected tool marker in %q", got) + } +} + +func TestMessagesPrepareObjectContent(t *testing.T) { + messages := []map[string]any{ + {"role": "user", "content": map[string]any{"temp": 18, "ok": true}}, + } + got := MessagesPrepare(messages) + if !contains(got, `"temp":18`) || !contains(got, `"ok":true`) { + t.Fatalf("expected serialized object content, got %q", got) + } +} + +func TestMessagesPrepareArrayTextVariants(t *testing.T) { + messages := []map[string]any{ + { + "role": "user", + "content": []any{ + map[string]any{"type": "output_text", "text": "line1"}, + map[string]any{"type": "input_text", "text": "line2"}, + map[string]any{"type": "image_url", "image_url": "https://example.com/a.png"}, + }, + }, + } + got := MessagesPrepare(messages) + if !contains(got, "line1\nline2") { + t.Fatalf("unexpected content from text variants: %q", got) + } +} + +func TestConvertClaudeToDeepSeek(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "model": "claude-opus-4-6", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + "system": "You are helpful", + "stream": true, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["model"] == "" { + t.Fatal("expected mapped model") + } + msgs, ok := out["messages"].([]any) + if !ok || len(msgs) == 0 { + t.Fatal("expected messages") + } + first, _ := msgs[0].(map[string]any) + if first["role"] != "system" { + t.Fatalf("expected first message system, got %#v", first) + } +} + +func TestConvertClaudeToDeepSeekUsesGlobalAliasResolution(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "model": "claude-3-5-sonnet-latest", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["model"] != "deepseek-v4-flash" { + t.Fatalf("expected global alias resolution, got model=%q", out["model"]) + } +} + +func TestConvertClaudeToDeepSeekUsesNoThinkingAliasResolution(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-6-nothinking", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["model"] != "deepseek-v4-flash-nothinking" { + t.Fatalf("expected noThinking alias resolution, got model=%q", out["model"]) + } +} + +func contains(s, sub string) bool { + return len(s) >= len(sub) && (s == sub || len(sub) == 0 || (len(s) > 0 && (indexOf(s, sub) >= 0))) +} + +func indexOf(s, sub string) int { + for i := 0; i+len(sub) <= len(s); i++ { + if s[i:i+len(sub)] == sub { + return i + } + } + return -1 +} diff --git a/internal/util/render.go b/internal/util/render.go new file mode 100644 index 0000000000000000000000000000000000000000..801d2f116736192cd9cb928dd84747e02c2c67d0 --- /dev/null +++ b/internal/util/render.go @@ -0,0 +1,147 @@ +package util + +import ( + "ds2api/internal/toolcall" + "fmt" + "strings" + "time" + + "github.com/google/uuid" +) + +// BuildOpenAIChatCompletion is kept for backward compatibility. +// Prefer internal/format/openai.BuildChatCompletion for new code. +func BuildOpenAIChatCompletion(completionID, model, finalPrompt, finalThinking, finalText string, toolNames []string) map[string]any { + detected := toolcall.ParseToolCalls(finalText, toolNames) + finishReason := "stop" + messageObj := map[string]any{"role": "assistant", "content": finalText} + if strings.TrimSpace(finalThinking) != "" { + messageObj["reasoning_content"] = finalThinking + } + if len(detected) > 0 { + finishReason = "tool_calls" + messageObj["tool_calls"] = toolcall.FormatOpenAIToolCalls(detected, nil) + messageObj["content"] = nil + } + promptTokens := CountPromptTokens(finalPrompt, model) + reasoningTokens := CountOutputTokens(finalThinking, model) + completionTokens := CountOutputTokens(finalText, model) + + return map[string]any{ + "id": completionID, + "object": "chat.completion", + "created": time.Now().Unix(), + "model": model, + "choices": []map[string]any{{"index": 0, "message": messageObj, "finish_reason": finishReason}}, + "usage": map[string]any{ + "prompt_tokens": promptTokens, + "completion_tokens": reasoningTokens + completionTokens, + "total_tokens": promptTokens + reasoningTokens + completionTokens, + "completion_tokens_details": map[string]any{ + "reasoning_tokens": reasoningTokens, + }, + }, + } +} + +// BuildOpenAIResponseObject is kept for backward compatibility. +// Prefer internal/format/openai.BuildResponseObject for new code. +func BuildOpenAIResponseObject(responseID, model, finalPrompt, finalThinking, finalText string, toolNames []string) map[string]any { + detected := toolcall.ParseToolCalls(finalText, toolNames) + exposedOutputText := finalText + output := make([]any, 0, 2) + if len(detected) > 0 { + // Keep structured tool output only; avoid leaking raw tool-call JSON + // into response.output_text for clients reading completed responses. + exposedOutputText = "" + toolCalls := make([]any, 0, len(detected)) + for _, tc := range detected { + toolCalls = append(toolCalls, map[string]any{ + "type": "tool_call", + "name": tc.Name, + "arguments": tc.Input, + }) + } + output = append(output, map[string]any{ + "type": "tool_calls", + "tool_calls": toolCalls, + }) + } else { + content := []any{ + map[string]any{ + "type": "output_text", + "text": finalText, + }, + } + if finalThinking != "" { + content = append([]any{map[string]any{ + "type": "reasoning", + "text": finalThinking, + }}, content...) + } + output = append(output, map[string]any{ + "type": "message", + "id": "msg_" + strings.ReplaceAll(uuid.NewString(), "-", ""), + "role": "assistant", + "content": content, + }) + } + promptTokens := CountPromptTokens(finalPrompt, model) + reasoningTokens := CountOutputTokens(finalThinking, model) + completionTokens := CountOutputTokens(finalText, model) + return map[string]any{ + "id": responseID, + "type": "response", + "object": "response", + "created_at": time.Now().Unix(), + "status": "completed", + "model": model, + "output": output, + "output_text": exposedOutputText, + "usage": map[string]any{ + "input_tokens": promptTokens, + "output_tokens": reasoningTokens + completionTokens, + "total_tokens": promptTokens + reasoningTokens + completionTokens, + }, + } +} + +// BuildClaudeMessageResponse is kept for backward compatibility. +// Prefer internal/format/claude.BuildMessageResponse for new code. +func BuildClaudeMessageResponse(messageID, model string, normalizedMessages []any, finalThinking, finalText string, toolNames []string) map[string]any { + detected := toolcall.ParseToolCalls(finalText, toolNames) + content := make([]map[string]any, 0, 4) + if finalThinking != "" { + content = append(content, map[string]any{"type": "thinking", "thinking": finalThinking}) + } + stopReason := "end_turn" + if len(detected) > 0 { + stopReason = "tool_use" + for i, tc := range detected { + content = append(content, map[string]any{ + "type": "tool_use", + "id": fmt.Sprintf("toolu_%d_%d", time.Now().Unix(), i), + "name": tc.Name, + "input": tc.Input, + }) + } + } else { + if finalText == "" { + finalText = "抱歉,没有生成有效的响应内容。" + } + content = append(content, map[string]any{"type": "text", "text": finalText}) + } + return map[string]any{ + "id": messageID, + "type": "message", + "role": "assistant", + "model": model, + "content": content, + "stop_reason": stopReason, + "stop_sequence": nil, + "usage": map[string]any{ + "input_tokens": CountPromptTokens(fmt.Sprintf("%v", normalizedMessages), model), + "output_tokens": CountOutputTokens(finalThinking, model) + CountOutputTokens(finalText, model), + }, + } +} diff --git a/internal/util/render_test.go b/internal/util/render_test.go new file mode 100644 index 0000000000000000000000000000000000000000..f596f3998dc59481a6c89e66191f5225e0ef0da8 --- /dev/null +++ b/internal/util/render_test.go @@ -0,0 +1,25 @@ +package util + +import "testing" + +func TestBuildOpenAIResponseObjectWithText(t *testing.T) { + out := BuildOpenAIResponseObject( + "resp_1", + "gpt-4o", + "prompt", + "reasoning", + "text", + nil, + ) + if out["object"] != "response" { + t.Fatalf("unexpected object: %#v", out["object"]) + } + output, _ := out["output"].([]any) + if len(output) == 0 { + t.Fatalf("expected output entries") + } + first, _ := output[0].(map[string]any) + if first["type"] != "message" { + t.Fatalf("expected first output type message, got %#v", first["type"]) + } +} diff --git a/internal/util/text.go b/internal/util/text.go new file mode 100644 index 0000000000000000000000000000000000000000..1beae5ba5248e08ce13f9f72118ed5c9826b93fa --- /dev/null +++ b/internal/util/text.go @@ -0,0 +1,46 @@ +package util + +import "unicode/utf8" + +// TruncateRunes trims a string to at most limit Unicode code points. +func TruncateRunes(text string, limit int) (string, bool) { + if limit < 0 { + return text, false + } + if limit == 0 { + return "", text != "" + } + + count := 0 + for i := range text { + if count == limit { + return text[:i], true + } + count++ + } + return text, false +} + +// TruncateUTF8Bytes trims a string to fit within limit bytes without cutting +// through a UTF-8 code point boundary. +func TruncateUTF8Bytes(text string, limit int) (string, bool) { + if limit < 0 { + return text, false + } + if len(text) <= limit { + return text, false + } + if limit == 0 { + return "", true + } + + raw := []byte(text) + cut := limit + if cut > len(raw) { + cut = len(raw) + } + for cut > 0 && cut < len(raw) && !utf8.RuneStart(raw[cut]) { + cut-- + } + return string(raw[:cut]), true +} diff --git a/internal/util/thinking.go b/internal/util/thinking.go new file mode 100644 index 0000000000000000000000000000000000000000..6fa101c0a259e7b4fb0ff48afb5384ad863a2375 --- /dev/null +++ b/internal/util/thinking.go @@ -0,0 +1,92 @@ +package util + +import "strings" + +func ResolveThinkingEnabled(req map[string]any, defaultEnabled bool) bool { + if enabled, ok := ResolveThinkingOverride(req); ok { + return enabled + } + return defaultEnabled +} + +func ResolveThinkingOverride(req map[string]any) (bool, bool) { + if req == nil { + return false, false + } + if enabled, ok := parseThinkingSetting(req["thinking"]); ok { + return enabled, true + } + if enabled, ok := parseReasoningSetting(req["reasoning"]); ok { + return enabled, true + } + if extraBody, ok := req["extra_body"].(map[string]any); ok { + if enabled, ok := parseThinkingSetting(extraBody["thinking"]); ok { + return enabled, true + } + if enabled, ok := parseReasoningSetting(extraBody["reasoning"]); ok { + return enabled, true + } + if enabled, ok := parseReasoningEffort(extraBody["reasoning_effort"]); ok { + return enabled, true + } + } + if enabled, ok := parseReasoningEffort(req["reasoning_effort"]); ok { + return enabled, true + } + return false, false +} + +func parseThinkingSetting(raw any) (bool, bool) { + switch v := raw.(type) { + case bool: + return v, true + case string: + switch strings.ToLower(strings.TrimSpace(v)) { + case "enabled", "enable", "on", "true": + return true, true + case "disabled", "disable", "off", "false", "none": + return false, true + default: + return false, false + } + case map[string]any: + if typ, ok := v["type"]; ok { + return parseThinkingSetting(typ) + } + } + return false, false +} + +func parseReasoningSetting(raw any) (bool, bool) { + switch v := raw.(type) { + case bool: + return v, true + case string: + return parseReasoningEffort(v) + case map[string]any: + for _, key := range []string{"effort", "type", "enabled"} { + if enabled, ok := parseReasoningSetting(v[key]); ok { + return enabled, true + } + } + } + return false, false +} + +func parseReasoningEffort(raw any) (bool, bool) { + switch strings.ToLower(strings.TrimSpace(toString(raw))) { + case "minimal", "low", "medium", "high", "xhigh": + return true, true + case "none", "disabled", "disable", "off", "false": + return false, true + default: + return false, false + } +} + +func toString(raw any) string { + if s, ok := raw.(string); ok { + return s + } + return "" +} diff --git a/internal/util/thinking_test.go b/internal/util/thinking_test.go new file mode 100644 index 0000000000000000000000000000000000000000..003fb5b2c2ed54b91e32dfe8873115767aff293c --- /dev/null +++ b/internal/util/thinking_test.go @@ -0,0 +1,55 @@ +package util + +import "testing" + +func TestResolveThinkingEnabledPriority(t *testing.T) { + req := map[string]any{ + "thinking": map[string]any{"type": "disabled"}, + "extra_body": map[string]any{ + "thinking": map[string]any{"type": "enabled"}, + }, + "reasoning_effort": "high", + } + if got := ResolveThinkingEnabled(req, true); got { + t.Fatalf("expected top-level thinking to win, got enabled=%v", got) + } +} + +func TestResolveThinkingEnabledUsesExtraBodyFallback(t *testing.T) { + req := map[string]any{ + "extra_body": map[string]any{ + "thinking": map[string]any{"type": "disabled"}, + }, + } + if got := ResolveThinkingEnabled(req, true); got { + t.Fatalf("expected extra_body thinking to disable, got enabled=%v", got) + } +} + +func TestResolveThinkingEnabledMapsReasoningEffortToEnabled(t *testing.T) { + for _, effort := range []string{"minimal", "low", "medium", "high", "xhigh"} { + if got := ResolveThinkingEnabled(map[string]any{"reasoning_effort": effort}, false); !got { + t.Fatalf("expected reasoning_effort=%s to enable thinking", effort) + } + } +} + +func TestResolveThinkingEnabledMapsReasoningObject(t *testing.T) { + req := map[string]any{"reasoning": map[string]any{"effort": "none"}} + if got := ResolveThinkingEnabled(req, true); got { + t.Fatalf("expected reasoning.effort=none to disable thinking") + } + req = map[string]any{"reasoning": map[string]any{"effort": "medium"}} + if got := ResolveThinkingEnabled(req, false); !got { + t.Fatalf("expected reasoning.effort=medium to enable thinking") + } +} + +func TestResolveThinkingEnabledDefaultsWhenUnset(t *testing.T) { + if !ResolveThinkingEnabled(nil, true) { + t.Fatal("expected default thinking=true when unset") + } + if ResolveThinkingEnabled(nil, false) { + t.Fatal("expected default thinking=false when unset") + } +} diff --git a/internal/util/token_count.go b/internal/util/token_count.go new file mode 100644 index 0000000000000000000000000000000000000000..09fe31b97242bb74adc54999de59b86bc53ff76e --- /dev/null +++ b/internal/util/token_count.go @@ -0,0 +1,46 @@ +package util + +const ( + defaultTokenizerModel = "gpt-4o" + claudeTokenizerModel = "claude" +) + +func CountPromptTokens(text, model string) int { + base := maxTokenCount( + EstimateTokens(text), + countWithTokenizer(text, model), + ) + if base <= 0 { + return 0 + } + return base + conservativePromptPadding(base) +} + +func CountOutputTokens(text, model string) int { + base := maxTokenCount( + EstimateTokens(text), + countWithTokenizer(text, model), + ) + if base <= 0 { + return 0 + } + return base +} + +func conservativePromptPadding(base int) int { + padding := base / 50 + if padding < 4 { + padding = 4 + } + return padding +} + +func maxTokenCount(values ...int) int { + best := 0 + for _, v := range values { + if v > best { + best = v + } + } + return best +} diff --git a/internal/util/token_count_heuristic.go b/internal/util/token_count_heuristic.go new file mode 100644 index 0000000000000000000000000000000000000000..8839111d20472ee86f23839e3d9db3bc7335563a --- /dev/null +++ b/internal/util/token_count_heuristic.go @@ -0,0 +1,7 @@ +//go:build 386 || arm || mips || mipsle || wasm + +package util + +func countWithTokenizer(_, _ string) int { + return 0 +} diff --git a/internal/util/token_count_tiktoken.go b/internal/util/token_count_tiktoken.go new file mode 100644 index 0000000000000000000000000000000000000000..92e48e13226b429c3ac025ee3a0b69efba496499 --- /dev/null +++ b/internal/util/token_count_tiktoken.go @@ -0,0 +1,98 @@ +//go:build !386 && !arm && !mips && !mipsle && !wasm + +package util + +import ( + "strings" + "sync" + + tiktoken "github.com/hupe1980/go-tiktoken" +) + +var ( + tokenEncodingPools sync.Map + tokenEncodingUnsupported sync.Map +) + +func countWithTokenizer(text, model string) int { + text = strings.TrimSpace(text) + if text == "" { + return 0 + } + encoding, release := tokenizerEncodingForCount(tokenizerModelForCount(model)) + if encoding == nil { + return 0 + } + defer release() + ids, _, err := encoding.Encode(text, nil, nil) + if err != nil { + return 0 + } + return len(ids) +} + +func tokenizerEncodingForCount(model string) (*tiktoken.Encoding, func()) { + model = strings.TrimSpace(model) + if model == "" { + model = defaultTokenizerModel + } + if _, ok := tokenEncodingUnsupported.Load(model); ok { + return nil, func() {} + } + if rawPool, ok := tokenEncodingPools.Load(model); ok { + pool, _ := rawPool.(*sync.Pool) + return getEncodingFromPool(pool) + } + + encoding, err := tiktoken.NewEncodingForModel(model) + if err != nil { + tokenEncodingUnsupported.Store(model, struct{}{}) + return nil, func() {} + } + pool := &sync.Pool{ + New: func() any { + encoding, err := tiktoken.NewEncodingForModel(model) + if err != nil { + return nil + } + return encoding + }, + } + actualPool, _ := tokenEncodingPools.LoadOrStore(model, pool) + pool, _ = actualPool.(*sync.Pool) + return encoding, func() { + pool.Put(encoding) + } +} + +func getEncodingFromPool(pool *sync.Pool) (*tiktoken.Encoding, func()) { + if pool == nil { + return nil, func() {} + } + encoding, _ := pool.Get().(*tiktoken.Encoding) + if encoding == nil { + return nil, func() {} + } + return encoding, func() { + pool.Put(encoding) + } +} + +func tokenizerModelForCount(model string) string { + model = strings.ToLower(strings.TrimSpace(model)) + if model == "" { + return defaultTokenizerModel + } + switch { + case strings.HasPrefix(model, "claude"): + return claudeTokenizerModel + case strings.HasPrefix(model, "gpt-4"), strings.HasPrefix(model, "gpt-5"), strings.HasPrefix(model, "o1"), strings.HasPrefix(model, "o3"), strings.HasPrefix(model, "o4"): + return defaultTokenizerModel + case strings.HasPrefix(model, "deepseek-v4"): + return defaultTokenizerModel + case strings.HasPrefix(model, "deepseek"): + return defaultTokenizerModel + default: + return defaultTokenizerModel + } +} diff --git a/internal/util/token_count_tiktoken_test.go b/internal/util/token_count_tiktoken_test.go new file mode 100644 index 0000000000000000000000000000000000000000..811c03d4a8399de4a855fc7609c7ac7ff6274e0a --- /dev/null +++ b/internal/util/token_count_tiktoken_test.go @@ -0,0 +1,35 @@ +//go:build !386 && !arm && !mips && !mipsle && !wasm + +package util + +import "testing" + +func TestTokenizerEncodingForCountCachesSupportedModel(t *testing.T) { + encoding, release := tokenizerEncodingForCount(defaultTokenizerModel) + if encoding == nil { + t.Fatalf("expected tokenizer encoding for %q", defaultTokenizerModel) + } + release() + + if _, ok := tokenEncodingPools.Load(defaultTokenizerModel); !ok { + t.Fatalf("expected tokenizer encoding pool for %q", defaultTokenizerModel) + } + + encoding, release = tokenizerEncodingForCount(defaultTokenizerModel) + if encoding == nil { + t.Fatalf("expected cached tokenizer encoding for %q", defaultTokenizerModel) + } + release() +} + +func TestTokenizerEncodingForCountCachesUnsupportedModel(t *testing.T) { + const model = "__ds2api_unsupported_tokenizer_model__" + encoding, release := tokenizerEncodingForCount(model) + release() + if encoding != nil { + t.Fatalf("expected nil encoding for unsupported model %q", model) + } + if _, ok := tokenEncodingUnsupported.Load(model); !ok { + t.Fatalf("expected unsupported tokenizer model to be cached") + } +} diff --git a/internal/util/util_edge_test.go b/internal/util/util_edge_test.go new file mode 100644 index 0000000000000000000000000000000000000000..80ba0ca76fff6a69edafe2f6a8f726e5d4d14684 --- /dev/null +++ b/internal/util/util_edge_test.go @@ -0,0 +1,374 @@ +package util + +import ( + "encoding/json" + "net/http/httptest" + "strings" + "testing" + + "ds2api/internal/config" +) + +// ─── EstimateTokens edge cases ─────────────────────────────────────── + +func TestEstimateTokensEmpty(t *testing.T) { + if got := EstimateTokens(""); got != 0 { + t.Fatalf("expected 0 for empty string, got %d", got) + } +} + +func TestEstimateTokensShortASCII(t *testing.T) { + got := EstimateTokens("ab") + if got != 1 { + t.Fatalf("expected 1 for 2 ascii chars, got %d", got) + } +} + +func TestEstimateTokensLongASCII(t *testing.T) { + got := EstimateTokens(strings.Repeat("x", 100)) + if got != 25 { + t.Fatalf("expected 25 for 100 ascii chars, got %d", got) + } +} + +func TestEstimateTokensChinese(t *testing.T) { + got := EstimateTokens("你好世界") + if got < 1 { + t.Fatalf("expected at least 1 token for Chinese text, got %d", got) + } +} + +func TestEstimateTokensMixed(t *testing.T) { + got := EstimateTokens("Hello 你好世界") + if got < 2 { + t.Fatalf("expected at least 2 tokens for mixed text, got %d", got) + } +} + +func TestEstimateTokensSingleByte(t *testing.T) { + got := EstimateTokens("x") + if got != 1 { + t.Fatalf("expected 1 for single char (minimum), got %d", got) + } +} + +func TestEstimateTokensSingleChinese(t *testing.T) { + got := EstimateTokens("你") + if got != 1 { + t.Fatalf("expected 1 for single Chinese char, got %d", got) + } +} + +// ─── ToBool edge cases ─────────────────────────────────────────────── + +func TestToBoolTrue(t *testing.T) { + if !ToBool(true) { + t.Fatal("expected true") + } +} + +func TestToBoolFalse(t *testing.T) { + if ToBool(false) { + t.Fatal("expected false") + } +} + +func TestToBoolNonBool(t *testing.T) { + if ToBool("true") { + t.Fatal("expected false for string 'true'") + } + if ToBool(1) { + t.Fatal("expected false for int 1") + } + if ToBool(nil) { + t.Fatal("expected false for nil") + } +} + +// ─── IntFrom edge cases ───────────────────────────────────────────── + +func TestIntFromFloat64(t *testing.T) { + if got := IntFrom(float64(42.5)); got != 42 { + t.Fatalf("expected 42 for float64(42.5), got %d", got) + } +} + +func TestIntFromInt(t *testing.T) { + if got := IntFrom(int(42)); got != 42 { + t.Fatalf("expected 42, got %d", got) + } +} + +func TestIntFromInt64(t *testing.T) { + if got := IntFrom(int64(42)); got != 42 { + t.Fatalf("expected 42, got %d", got) + } +} + +func TestIntFromString(t *testing.T) { + if got := IntFrom("42"); got != 0 { + t.Fatalf("expected 0 for string, got %d", got) + } +} + +func TestIntFromNil(t *testing.T) { + if got := IntFrom(nil); got != 0 { + t.Fatalf("expected 0 for nil, got %d", got) + } +} + +// ─── WriteJSON ─────────────────────────────────────────────────────── + +func TestWriteJSON(t *testing.T) { + rec := httptest.NewRecorder() + WriteJSON(rec, 200, map[string]any{"key": "value"}) + if rec.Code != 200 { + t.Fatalf("expected 200, got %d", rec.Code) + } + if ct := rec.Header().Get("Content-Type"); ct != "application/json" { + t.Fatalf("expected application/json content type, got %q", ct) + } + var body map[string]any + if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { + t.Fatalf("decode error: %v", err) + } + if body["key"] != "value" { + t.Fatalf("unexpected body: %#v", body) + } +} + +func TestWriteJSONStatusCodes(t *testing.T) { + for _, code := range []int{200, 201, 400, 404, 500} { + rec := httptest.NewRecorder() + WriteJSON(rec, code, map[string]any{"status": code}) + if rec.Code != code { + t.Fatalf("expected %d, got %d", code, rec.Code) + } + } +} + +// ─── MessagesPrepare edge cases ────────────────────────────────────── + +func TestMessagesPrepareEmpty(t *testing.T) { + got := MessagesPrepare(nil) + if got != "" { + t.Fatalf("expected empty for nil messages, got %q", got) + } +} + +func TestMessagesPrepareMergesConsecutiveSameRole(t *testing.T) { + messages := []map[string]any{ + {"role": "user", "content": "Hello"}, + {"role": "user", "content": "World"}, + } + got := MessagesPrepare(messages) + if !strings.Contains(got, "Hello") || !strings.Contains(got, "World") { + t.Fatalf("expected both messages, got %q", got) + } + // Should be merged into a single user turn with one marker at the start. + count := strings.Count(got, "User: ") + if count != 1 { + t.Fatalf("expected one User marker for the merged pair, got %d occurrences", count) + } +} +} + +func TestMessagesPrepareAssistantMarkers(t *testing.T) { + messages := []map[string]any{ + {"role": "user", "content": "Hi"}, + {"role": "assistant", "content": "Hello!"}, + } + got := MessagesPrepare(messages) + if !strings.Contains(got, "Assistant: ") { + t.Fatalf("expected assistant marker, got %q", got) + } + if !strings.Contains(got, "Assistant: Hello!\n") { + t.Fatalf("expected assistant EOS suffix, got %q", got) + } + if strings.Contains(got, "") || strings.Contains(got, "") { + t.Fatalf("did not expect think tags in prompt, got %q", got) + } + if strings.Contains(got, "") { + t.Fatalf("did not expect legacy system marker, got %q", got) + } +} + +func TestMessagesPrepareUnknownRole(t *testing.T) { + messages := []map[string]any{ + {"role": "user", "content": "Hello"}, + {"role": "unknown_role", "content": "Unknown"}, + } + got := MessagesPrepare(messages) + if !strings.Contains(got, "Unknown") { + t.Fatalf("expected unknown role content, got %q", got) + } +} + +func TestMessagesPrepareMarkdownImageReplaced(t *testing.T) { + messages := []map[string]any{ + {"role": "user", "content": "Look at this: ![alt](https://example.com/img.png)"}, + } + got := MessagesPrepare(messages) + if strings.Contains(got, "![alt]") { + t.Fatalf("expected markdown image to be replaced, got %q", got) + } +} + +func TestMessagesPrepareNilContent(t *testing.T) { + messages := []map[string]any{ + {"role": "user", "content": nil}, + } + got := MessagesPrepare(messages) + if got != "null" { + t.Logf("nil content handled as: %q", got) + } +} + +// ─── normalizeContent edge cases ───────────────────────────────────── + +func TestNormalizeContentString(t *testing.T) { + got := normalizeContent("hello") + if got != "hello" { + t.Fatalf("expected 'hello', got %q", got) + } +} + +func TestNormalizeContentArray(t *testing.T) { + got := normalizeContent([]any{ + map[string]any{"type": "text", "text": "line1"}, + map[string]any{"type": "text", "text": "line2"}, + }) + if got != "line1\nline2" { + t.Fatalf("expected 'line1\\nline2', got %q", got) + } +} + +func TestNormalizeContentArrayWithContentField(t *testing.T) { + got := normalizeContent([]any{ + map[string]any{"type": "text", "content": "from-content"}, + }) + if got != "from-content" { + t.Fatalf("expected 'from-content', got %q", got) + } +} + +func TestNormalizeContentArraySkipsImage(t *testing.T) { + got := normalizeContent([]any{ + map[string]any{"type": "image_url", "image_url": "https://example.com/img.png"}, + map[string]any{"type": "text", "text": "caption"}, + }) + if strings.Contains(got, "image") { + t.Fatalf("expected image skipped, got %q", got) + } + if got != "caption" { + t.Fatalf("expected 'caption', got %q", got) + } +} + +func TestNormalizeContentArrayNonMapItems(t *testing.T) { + got := normalizeContent([]any{"string item", 42}) + if got != "" { + t.Fatalf("expected empty for non-map items, got %q", got) + } +} + +func TestNormalizeContentJSON(t *testing.T) { + got := normalizeContent(map[string]any{"key": "value"}) + if !strings.Contains(got, `"key":"value"`) { + t.Fatalf("expected JSON serialized, got %q", got) + } +} + +// ─── ConvertClaudeToDeepSeek edge cases ────────────────────────────── + +func TestConvertClaudeToDeepSeekDefaultModel(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["model"] == "" { + t.Fatal("expected default model") + } +} + +func TestConvertClaudeToDeepSeekWithStopSequences(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + "stop_sequences": []any{"\n\n"}, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["stop"] == nil { + t.Fatal("expected stop field from stop_sequences") + } +} + +func TestConvertClaudeToDeepSeekWithTemperature(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + "temperature": 0.7, + "top_p": 0.9, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["temperature"] != 0.7 { + t.Fatalf("expected temperature 0.7, got %v", out["temperature"]) + } + if out["top_p"] != 0.9 { + t.Fatalf("expected top_p 0.9, got %v", out["top_p"]) + } +} + +func TestConvertClaudeToDeepSeekNoSystem(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-5", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + } + out := ConvertClaudeToDeepSeek(req, store) + msgs, _ := out["messages"].([]any) + if len(msgs) != 1 { + t.Fatalf("expected 1 message without system, got %d", len(msgs)) + } +} + +func TestConvertClaudeToDeepSeekOpusUsesGlobalAlias(t *testing.T) { + store := config.LoadStore() + req := map[string]any{ + "model": "claude-opus-4-6", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["model"] != "deepseek-v4-pro" { + t.Fatalf("expected opus to use global alias, got %q", out["model"]) + } +} + +func TestConvertClaudeToDeepSeekUsesExplicitModelAlias(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":[],"accounts":[],"model_aliases":{"claude-sonnet-4-6":"deepseek-v4-pro-search"}}`) + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-6", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["model"] != "deepseek-v4-pro-search" { + t.Fatalf("expected explicit alias override, got %q", out["model"]) + } +} + +func TestConvertClaudeToDeepSeekUsesExplicitNoThinkingModelAlias(t *testing.T) { + t.Setenv("DS2API_CONFIG_JSON", `{"keys":[],"accounts":[],"model_aliases":{"claude-sonnet-4-6":"deepseek-v4-pro-search"}}`) + store := config.LoadStore() + req := map[string]any{ + "model": "claude-sonnet-4-6-nothinking", + "messages": []any{map[string]any{"role": "user", "content": "Hi"}}, + } + out := ConvertClaudeToDeepSeek(req, store) + if out["model"] != "deepseek-v4-pro-search-nothinking" { + t.Fatalf("expected explicit alias override with nothinking suffix, got %q", out["model"]) + } +} diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 0000000000000000000000000000000000000000..03542b904091e6ec737e37296e1a303fde59f831 --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,185 @@ +package version + +import ( + "os" + "path/filepath" + "runtime" + "strconv" + "strings" + "sync" +) + +// BuildVersion can be injected at build time via -ldflags. +// In release builds it should come from Git tag (e.g. v2.3.5). +var BuildVersion = "" + +var ( + currentOnce sync.Once + currentVal string + sourceVal string +) + +func Current() (value string, source string) { + currentOnce.Do(func() { + if build := strings.TrimSpace(BuildVersion); build != "" { + currentVal = normalize(build) + sourceVal = "build-ldflags" + return + } + if fv := readVersionFile(); fv != "" { + currentVal = normalize(fv) + sourceVal = "file:VERSION" + return + } + + if vv := versionFromVercelEnv(); vv != "" { + currentVal = vv + sourceVal = "env:vercel" + return + } + currentVal = "dev" + sourceVal = "default" + }) + return currentVal, sourceVal +} + +func readVersionFile() string { + candidates := []string{"VERSION"} + if wd, err := os.Getwd(); err == nil { + candidates = append(candidates, filepath.Join(wd, "VERSION")) + } + if _, file, _, ok := runtime.Caller(0); ok { + repoRoot := filepath.Clean(filepath.Join(filepath.Dir(file), "../..")) + candidates = append(candidates, filepath.Join(repoRoot, "VERSION")) + } + seen := map[string]struct{}{} + for _, c := range candidates { + c = filepath.Clean(strings.TrimSpace(c)) + if c == "" { + continue + } + if _, ok := seen[c]; ok { + continue + } + seen[c] = struct{}{} + b, err := os.ReadFile(c) + if err != nil { + continue + } + if v := strings.TrimSpace(string(b)); v != "" { + return v + } + } + return "" +} + +func normalize(v string) string { + v = strings.TrimSpace(v) + if v == "" { + return "" + } + return strings.TrimPrefix(v, "v") +} + +func Tag(v string) string { + v = normalize(v) + if v == "" || v == "dev" { + return v + } + if v[0] < '0' || v[0] > '9' { + return v + } + return "v" + v +} + +func versionFromVercelEnv() string { + if tag := normalize(strings.TrimSpace(os.Getenv("VERCEL_GIT_COMMIT_TAG"))); tag != "" { + return tag + } + ref := strings.TrimSpace(os.Getenv("VERCEL_GIT_COMMIT_REF")) + sha := strings.TrimSpace(os.Getenv("VERCEL_GIT_COMMIT_SHA")) + if len(sha) > 7 { + sha = sha[:7] + } + ref = sanitizeVersionLabel(ref) + sha = sanitizeVersionLabel(sha) + if ref == "" && sha == "" { + return "" + } + if ref != "" && sha != "" { + return "preview-" + ref + "." + sha + } + if ref != "" { + return "preview-" + ref + } + return "preview-" + sha +} + +func sanitizeVersionLabel(in string) string { + in = strings.TrimSpace(strings.ToLower(in)) + if in == "" { + return "" + } + var b strings.Builder + b.Grow(len(in)) + prevDash := false + for i := 0; i < len(in); i++ { + c := in[i] + if (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') { + b.WriteByte(c) + prevDash = false + continue + } + if !prevDash { + b.WriteByte('-') + prevDash = true + } + } + out := strings.Trim(b.String(), "-") + return out +} + +func Compare(a, b string) int { + pa := parse(normalize(a)) + pb := parse(normalize(b)) + for i := 0; i < 3; i++ { + if pa[i] < pb[i] { + return -1 + } + if pa[i] > pb[i] { + return 1 + } + } + return 0 +} + +func parse(v string) [3]int { + var out [3]int + parts := strings.SplitN(v, ".", 4) + for i := 0; i < 3 && i < len(parts); i++ { + n := readLeadingInt(parts[i]) + out[i] = n + } + return out +} + +func readLeadingInt(s string) int { + s = strings.TrimSpace(s) + if s == "" { + return 0 + } + i := 0 + for ; i < len(s); i++ { + if s[i] < '0' || s[i] > '9' { + break + } + } + if i == 0 { + return 0 + } + n, err := strconv.Atoi(s[:i]) + if err != nil { + return 0 + } + return n +} diff --git a/internal/version/version_test.go b/internal/version/version_test.go new file mode 100644 index 0000000000000000000000000000000000000000..03f7e95255d97acc11afe11aad25d4b588c4c11d --- /dev/null +++ b/internal/version/version_test.go @@ -0,0 +1,39 @@ +package version + +import "testing" + +func TestNormalizeAndTag(t *testing.T) { + if got := normalize("v2.3.5"); got != "2.3.5" { + t.Fatalf("normalize failed: %q", got) + } + if got := Tag("2.3.5"); got != "v2.3.5" { + t.Fatalf("tag failed: %q", got) + } +} + +func TestCompare(t *testing.T) { + if Compare("2.3.5", "2.3.5") != 0 { + t.Fatal("expected equal") + } + if Compare("2.3.5", "2.3.6") >= 0 { + t.Fatal("expected less") + } + if Compare("v2.10.0", "2.3.9") <= 0 { + t.Fatal("expected greater") + } +} + +func TestTagKeepsPreviewStyle(t *testing.T) { + if got := Tag("preview-dev.abcd123"); got != "preview-dev.abcd123" { + t.Fatalf("expected preview tag unchanged, got %q", got) + } +} + +func TestVersionFromVercelEnv(t *testing.T) { + t.Setenv("VERCEL_GIT_COMMIT_TAG", "") + t.Setenv("VERCEL_GIT_COMMIT_REF", "dev") + t.Setenv("VERCEL_GIT_COMMIT_SHA", "abcdef123456") + if got := versionFromVercelEnv(); got != "preview-dev.abcdef1" { + t.Fatalf("unexpected vercel preview version: %q", got) + } +} diff --git a/internal/webui/build.go b/internal/webui/build.go new file mode 100644 index 0000000000000000000000000000000000000000..e1b603062bc2001bd40b63ebe88b49d0c86ae3a8 --- /dev/null +++ b/internal/webui/build.go @@ -0,0 +1,103 @@ +package webui + +import ( + "context" + "errors" + "fmt" + "os" + "os/exec" + "path/filepath" + "strings" + "time" + + "ds2api/internal/config" +) + +const ( + defaultBuildTimeout = 5 * time.Minute +) + +func EnsureBuiltOnStartup() { + if !shouldAutoBuild() { + return + } + staticDir := resolveStaticAdminDir(config.StaticAdminDir()) + if hasBuiltUI(staticDir) { + return + } + if err := buildWebUI(staticDir); err != nil { + config.Logger.Warn("[webui] auto build failed", "error", err) + return + } + if hasBuiltUI(staticDir) { + config.Logger.Info("[webui] auto build completed", "dir", staticDir) + return + } + config.Logger.Warn("[webui] auto build finished but output missing", "dir", staticDir) +} + +func shouldAutoBuild() bool { + raw := strings.TrimSpace(os.Getenv("DS2API_AUTO_BUILD_WEBUI")) + if raw == "" { + return !config.IsVercel() + } + switch strings.ToLower(raw) { + case "1", "true", "yes", "on": + return true + case "0", "false", "no", "off": + return false + default: + return !config.IsVercel() + } +} + +func hasBuiltUI(staticDir string) bool { + if strings.TrimSpace(staticDir) == "" { + return false + } + indexPath := filepath.Join(staticDir, "index.html") + st, err := os.Stat(indexPath) + return err == nil && !st.IsDir() +} + +func buildWebUI(staticDir string) error { + if _, err := exec.LookPath("npm"); err != nil { + return fmt.Errorf("npm not found in PATH: %w", err) + } + if strings.TrimSpace(staticDir) == "" { + return errors.New("static admin dir is empty") + } + + config.Logger.Info("[webui] static files missing, running npm build") + ctx, cancel := context.WithTimeout(context.Background(), defaultBuildTimeout) + defer cancel() + + if _, err := os.Stat(filepath.Join("webui", "node_modules")); err != nil { + if !os.IsNotExist(err) { + return err + } + installCmd := exec.CommandContext(ctx, "npm", "ci", "--prefix", "webui") + installCmd.Stdout = os.Stdout + installCmd.Stderr = os.Stderr + if err := installCmd.Run(); err != nil { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + return fmt.Errorf("webui npm ci timed out after %s", defaultBuildTimeout) + } + return err + } + } + + if err := os.MkdirAll(staticDir, 0o755); err != nil { + return err + } + cmd := exec.CommandContext(ctx, "npm", "run", "build", "--prefix", "webui", "--", "--outDir", staticDir, "--emptyOutDir") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + if errors.Is(ctx.Err(), context.DeadlineExceeded) { + return fmt.Errorf("webui build timed out after %s", defaultBuildTimeout) + } + return err + } + return nil +} diff --git a/internal/webui/handler.go b/internal/webui/handler.go new file mode 100644 index 0000000000000000000000000000000000000000..8e777b93ef82109ee59ec29806b73adbedcddb4e --- /dev/null +++ b/internal/webui/handler.go @@ -0,0 +1,177 @@ +package webui + +import ( + "net/http" + "os" + "path/filepath" + "strings" + + "github.com/go-chi/chi/v5" + + "ds2api/internal/config" +) + +const welcomeHTML = ` +DS2API + +

DS2API

DeepSeek to OpenAI & Claude Compatible API

` + +type Handler struct { + StaticDir string +} + +func NewHandler() *Handler { + return &Handler{StaticDir: resolveStaticAdminDir(config.StaticAdminDir())} +} + +func RegisterRoutes(r chi.Router, h *Handler) { + r.Get("/", h.index) + r.Get("/admin", h.admin) +} + +func (h *Handler) HandleAdminFallback(w http.ResponseWriter, r *http.Request) bool { + if r.Method != http.MethodGet { + return false + } + if !strings.HasPrefix(r.URL.Path, "/admin/") { + return false + } + h.admin(w, r) + return true +} + +func (h *Handler) index(w http.ResponseWriter, _ *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusOK) + _, _ = w.Write([]byte(welcomeHTML)) +} + +func (h *Handler) admin(w http.ResponseWriter, r *http.Request) { + staticDir := resolveStaticAdminDir(h.StaticDir) + if fi, err := os.Stat(staticDir); err == nil && fi.IsDir() { + h.serveFromDisk(w, r, staticDir) + return + } + http.Error(w, "WebUI not built. Run `cd webui && npm run build` first.", http.StatusNotFound) +} + +// staticContentTypes pins the Content-Type of common WebUI assets so we do not +// rely on mime.TypeByExtension, which on Windows consults the registry and can +// return the wrong type (e.g. application/xml for .css) when third-party +// software has overwritten HKEY_CLASSES_ROOT entries. Browsers strictly enforce +// stylesheet/script MIME types and will refuse to apply a misidentified asset, +// breaking the /admin page on affected machines. +var staticContentTypes = map[string]string{ + ".css": "text/css; charset=utf-8", + ".js": "text/javascript; charset=utf-8", + ".mjs": "text/javascript; charset=utf-8", + ".html": "text/html; charset=utf-8", + ".htm": "text/html; charset=utf-8", + ".json": "application/json; charset=utf-8", + ".map": "application/json; charset=utf-8", + ".svg": "image/svg+xml", + ".png": "image/png", + ".jpg": "image/jpeg", + ".jpeg": "image/jpeg", + ".gif": "image/gif", + ".webp": "image/webp", + ".ico": "image/x-icon", + ".woff": "font/woff", + ".woff2": "font/woff2", + ".ttf": "font/ttf", + ".otf": "font/otf", + ".txt": "text/plain; charset=utf-8", + ".wasm": "application/wasm", +} + +// setStaticContentType pins the response Content-Type by file extension so that +// http.ServeFile does not fall back to mime.TypeByExtension (which on Windows +// reads the registry and may return an incorrect type). +func setStaticContentType(w http.ResponseWriter, fullPath string) { + ext := strings.ToLower(filepath.Ext(fullPath)) + if ct, ok := staticContentTypes[ext]; ok { + w.Header().Set("Content-Type", ct) + } +} + +func (h *Handler) serveFromDisk(w http.ResponseWriter, r *http.Request, staticDir string) { + root := filepath.Clean(staticDir) + path := strings.TrimPrefix(r.URL.Path, "/admin") + path = strings.TrimPrefix(path, "/") + if path != "" && strings.Contains(path, ".") { + full := filepath.Join(root, filepath.Clean(path)) + if !isPathInsideRoot(full, root) { + http.NotFound(w, r) + return + } + if _, err := os.Stat(full); err == nil { + if strings.HasPrefix(path, "assets/") { + w.Header().Set("Cache-Control", "public, max-age=31536000, immutable") + } else { + w.Header().Set("Cache-Control", "no-store, must-revalidate") + } + setStaticContentType(w, full) + http.ServeFile(w, r, full) + return + } + http.NotFound(w, r) + return + } + index := filepath.Join(root, "index.html") + if _, err := os.Stat(index); err != nil { + http.Error(w, "index.html not found", http.StatusNotFound) + return + } + w.Header().Set("Cache-Control", "no-store, must-revalidate") + setStaticContentType(w, index) + http.ServeFile(w, r, index) +} + +func isPathInsideRoot(path, root string) bool { + cleanPath := filepath.Clean(path) + cleanRoot := filepath.Clean(root) + if cleanPath == cleanRoot { + return true + } + volume := filepath.VolumeName(cleanRoot) + rootWithoutVolume := cleanRoot[len(volume):] + if rootWithoutVolume == string(os.PathSeparator) { + return strings.HasPrefix(cleanPath, cleanRoot) + } + return strings.HasPrefix(cleanPath, cleanRoot+string(os.PathSeparator)) +} + +func resolveStaticAdminDir(preferred string) string { + if strings.TrimSpace(os.Getenv("DS2API_STATIC_ADMIN_DIR")) != "" { + return filepath.Clean(preferred) + } + candidates := []string{preferred} + if wd, err := os.Getwd(); err == nil { + candidates = append(candidates, filepath.Join(wd, "static/admin")) + } + if exe, err := os.Executable(); err == nil { + exeDir := filepath.Dir(exe) + candidates = append(candidates, + filepath.Join(exeDir, "static/admin"), + filepath.Join(filepath.Dir(exeDir), "static/admin"), + ) + } + // Common serverless locations. + candidates = append(candidates, "/var/task/static/admin", "/var/task/user/static/admin") + + seen := map[string]struct{}{} + for _, c := range candidates { + c = filepath.Clean(strings.TrimSpace(c)) + if c == "" { + continue + } + if _, ok := seen[c]; ok { + continue + } + seen[c] = struct{}{} + if fi, err := os.Stat(c); err == nil && fi.IsDir() { + return c + } + } + return filepath.Clean(preferred) +} diff --git a/internal/webui/handler_test.go b/internal/webui/handler_test.go new file mode 100644 index 0000000000000000000000000000000000000000..5e07bf533cd4dc7cdfa5fb486c2d9ad3ec64f8f6 --- /dev/null +++ b/internal/webui/handler_test.go @@ -0,0 +1,148 @@ +package webui + +import ( + "net/http" + "net/http/httptest" + "os" + "path/filepath" + "strings" + "testing" +) + +// TestServeFromDiskPinsContentType ensures static admin assets are returned +// with an explicit, RFC-compliant Content-Type that does not depend on +// mime.TypeByExtension. On Windows mime.TypeByExtension consults the registry +// (HKEY_CLASSES_ROOT) which third-party software can corrupt — for example +// installing certain editors rewrites .css to application/xml — and Chrome +// then refuses to apply a stylesheet whose Content-Type is not text/css, +// breaking the /admin page entirely. Pinning the type by file extension makes +// the response deterministic across operating systems and machine state. +func TestServeFromDiskPinsContentType(t *testing.T) { + staticDir := t.TempDir() + assetsDir := filepath.Join(staticDir, "assets") + if err := os.MkdirAll(assetsDir, 0o755); err != nil { + t.Fatalf("mkdir assets: %v", err) + } + + files := map[string]string{ + "index.html": "", + "assets/index.css": "body{}", + "assets/index.js": "console.log(1)", + "assets/icon.svg": ``, + "assets/source.js.map": `{"version":3}`, + } + for rel, body := range files { + full := filepath.Join(staticDir, filepath.FromSlash(rel)) + if err := os.MkdirAll(filepath.Dir(full), 0o755); err != nil { + t.Fatalf("mkdir %s: %v", rel, err) + } + if err := os.WriteFile(full, []byte(body), 0o644); err != nil { + t.Fatalf("write %s: %v", rel, err) + } + } + + h := &Handler{StaticDir: staticDir} + + cases := []struct { + urlPath string + wantPrefix string + wantCacheCtl string + }{ + {"/admin/assets/index.css", "text/css", "public, max-age=31536000, immutable"}, + {"/admin/assets/index.js", "text/javascript", "public, max-age=31536000, immutable"}, + {"/admin/assets/icon.svg", "image/svg+xml", "public, max-age=31536000, immutable"}, + {"/admin/assets/source.js.map", "application/json", "public, max-age=31536000, immutable"}, + // "/admin/index.html" is intentionally omitted: http.ServeFile redirects + // requests for index.html to "./", matching Go's net/http behavior. The + // route the SPA actually lands on is "/admin/" below. + {"/admin/", "text/html", "no-store, must-revalidate"}, + } + + for _, tc := range cases { + t.Run(tc.urlPath, func(t *testing.T) { + req := httptest.NewRequest(http.MethodGet, tc.urlPath, nil) + rec := httptest.NewRecorder() + h.serveFromDisk(rec, req, staticDir) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d, want 200", rec.Code) + } + ct := rec.Header().Get("Content-Type") + if !strings.HasPrefix(ct, tc.wantPrefix) { + t.Fatalf("Content-Type = %q, want prefix %q", ct, tc.wantPrefix) + } + if got := rec.Header().Get("Cache-Control"); got != tc.wantCacheCtl { + t.Fatalf("Cache-Control = %q, want %q", got, tc.wantCacheCtl) + } + }) + } +} + +func TestServeFromDiskRejectsSiblingDirectoryWithSharedPrefix(t *testing.T) { + parent := t.TempDir() + staticDir := filepath.Join(parent, "admin") + siblingDir := filepath.Join(parent, "admin-leak") + if err := os.MkdirAll(staticDir, 0o755); err != nil { + t.Fatalf("mkdir static dir: %v", err) + } + if err := os.MkdirAll(siblingDir, 0o755); err != nil { + t.Fatalf("mkdir sibling dir: %v", err) + } + if err := os.WriteFile(filepath.Join(siblingDir, "secret.txt"), []byte("secret"), 0o644); err != nil { + t.Fatalf("write sibling secret: %v", err) + } + + h := &Handler{StaticDir: staticDir} + req := httptest.NewRequest(http.MethodGet, "/admin/../admin-leak/secret.txt", nil) + rec := httptest.NewRecorder() + h.serveFromDisk(rec, req, staticDir) + + if rec.Code != http.StatusNotFound { + t.Fatalf("status = %d, want 404", rec.Code) + } + if body := rec.Body.String(); strings.Contains(body, "secret") { + t.Fatal("served content from sibling directory") + } +} + +func TestIsPathInsideRootAllowsFilesystemRootChildren(t *testing.T) { + root := filepath.VolumeName(os.TempDir()) + string(os.PathSeparator) + child := filepath.Join(root, "assets", "index.css") + + if !isPathInsideRoot(child, root) { + t.Fatalf("expected filesystem-root child %q inside %q", child, root) + } +} + +func TestIsPathInsideRootRejectsSharedPrefixSibling(t *testing.T) { + parent := t.TempDir() + root := filepath.Join(parent, "admin") + sibling := filepath.Join(parent, "admin-leak", "secret.txt") + + if isPathInsideRoot(sibling, root) { + t.Fatalf("expected shared-prefix sibling %q outside %q", sibling, root) + } +} + +// TestSetStaticContentTypeUnknownExtensionFallsThrough verifies that unknown +// extensions leave the Content-Type header unset, so http.ServeFile can apply +// its own detection (sniffing or mime.TypeByExtension) for cases the pinned +// table does not cover. +func TestSetStaticContentTypeUnknownExtensionFallsThrough(t *testing.T) { + rec := httptest.NewRecorder() + setStaticContentType(rec, "/tmp/data.unknownext") + if got := rec.Header().Get("Content-Type"); got != "" { + t.Fatalf("Content-Type = %q, want empty for unknown extension", got) + } +} + +// TestSetStaticContentTypeIsCaseInsensitive guards against a regression where +// uppercase extensions (e.g. STYLE.CSS shipped from some build pipelines) +// would bypass the pinned table and fall back to the registry on Windows. +func TestSetStaticContentTypeIsCaseInsensitive(t *testing.T) { + rec := httptest.NewRecorder() + setStaticContentType(rec, "/tmp/STYLE.CSS") + if got := rec.Header().Get("Content-Type"); !strings.HasPrefix(got, "text/css") { + t.Fatalf("Content-Type = %q, want text/css prefix", got) + } +} diff --git a/pow/README.md b/pow/README.md new file mode 100644 index 0000000000000000000000000000000000000000..85c74e137bd6ba0cb56d90d4e19ccbe5baaeea98 --- /dev/null +++ b/pow/README.md @@ -0,0 +1,30 @@ +# DeepSeek PoW 纯算实现 + +当前服务端 PoW 已走纯 Go 实现:`internal/deepseek/pow.go` 负责从上游 challenge map 中取字段,调用 `ds2api/pow` 求解 nonce,并组装 `x-ds-pow-response` header。 + +## 算法 + +DeepSeekHashV1 = SHA3-256 但 **Keccak-f[1600] 跳过 round 0** (只做 rounds 1..23)。其余参数不变: +rate=136, padding=0x06+0x80, output=32 字节。 + +PoW 协议:服务端选 answer ∈ [0, difficulty),计算 `challenge = hash(prefix + str(answer))`。 +客户端遍历 [0, difficulty) 找到匹配的 nonce。 + +``` +prefix = salt + "_" + str(expire_at) + "_" +input = (prefix + str(nonce)).encode("utf-8") +hash = DeepSeekHashV1(input) → 32 bytes +header = base64(json({algorithm, challenge, salt, answer, signature, target_path})) +``` + +## 主要入口 + +- `pow/deepseek_hash.go`:DeepSeekHashV1 / Keccak-f[1600] rounds 1..23。 +- `pow/deepseek_pow.go`:`SolvePow`、`BuildPowHeader`、`SolveAndBuildHeader`。 +- `internal/deepseek/pow.go`:服务侧适配层,校验 `algorithm == DeepSeekHashV1` 并调用 `pow.SolvePow`。 + +## 测试 + +```bash +cd pow && go test -v ./... && go test -bench=. -benchmem +``` diff --git a/pow/deepseek_hash.go b/pow/deepseek_hash.go new file mode 100644 index 0000000000000000000000000000000000000000..e4cfdc99c586b65a61f97f4452ad3dc3bad8780e --- /dev/null +++ b/pow/deepseek_hash.go @@ -0,0 +1,153 @@ +// Package pow 提供 DeepSeekHashV1 纯 Go 实现。 +// DeepSeekHashV1 = SHA3-256 但跳过 Keccak-f[1600] round 0 (只做 rounds 1..23)。 +package pow + +import "encoding/binary" + +var rc = [24]uint64{ + 0x0000000000000001, 0x0000000000008082, 0x800000000000808A, 0x8000000080008000, + 0x000000000000808B, 0x0000000080000001, 0x8000000080008081, 0x8000000000008009, + 0x000000000000008A, 0x0000000000000088, 0x0000000080008009, 0x000000008000000A, + 0x000000008000808B, 0x800000000000008B, 0x8000000000008089, 0x8000000000008003, + 0x8000000000008002, 0x8000000000000080, 0x000000000000800A, 0x800000008000000A, + 0x8000000080008081, 0x8000000000008080, 0x0000000080000001, 0x8000000080008008, +} + +func rotl64(v uint64, k uint) uint64 { return v<>(64-k) } + +func keccakF23(s *[25]uint64) { + a0, a1, a2, a3, a4 := s[0], s[1], s[2], s[3], s[4] + a5, a6, a7, a8, a9 := s[5], s[6], s[7], s[8], s[9] + a10, a11, a12, a13, a14 := s[10], s[11], s[12], s[13], s[14] + a15, a16, a17, a18, a19 := s[15], s[16], s[17], s[18], s[19] + a20, a21, a22, a23, a24 := s[20], s[21], s[22], s[23], s[24] + + for r := 1; r < 24; r++ { + c0 := a0 ^ a5 ^ a10 ^ a15 ^ a20 + c1 := a1 ^ a6 ^ a11 ^ a16 ^ a21 + c2 := a2 ^ a7 ^ a12 ^ a17 ^ a22 + c3 := a3 ^ a8 ^ a13 ^ a18 ^ a23 + c4 := a4 ^ a9 ^ a14 ^ a19 ^ a24 + d0 := c4 ^ rotl64(c1, 1) + d1 := c0 ^ rotl64(c2, 1) + d2 := c1 ^ rotl64(c3, 1) + d3 := c2 ^ rotl64(c4, 1) + d4 := c3 ^ rotl64(c0, 1) + a0 ^= d0 + a5 ^= d0 + a10 ^= d0 + a15 ^= d0 + a20 ^= d0 + a1 ^= d1 + a6 ^= d1 + a11 ^= d1 + a16 ^= d1 + a21 ^= d1 + a2 ^= d2 + a7 ^= d2 + a12 ^= d2 + a17 ^= d2 + a22 ^= d2 + a3 ^= d3 + a8 ^= d3 + a13 ^= d3 + a18 ^= d3 + a23 ^= d3 + a4 ^= d4 + a9 ^= d4 + a14 ^= d4 + a19 ^= d4 + a24 ^= d4 + + b0 := a0 + b10 := rotl64(a1, 1) + b20 := rotl64(a2, 62) + b5 := rotl64(a3, 28) + b15 := rotl64(a4, 27) + b16 := rotl64(a5, 36) + b1 := rotl64(a6, 44) + b11 := rotl64(a7, 6) + b21 := rotl64(a8, 55) + b6 := rotl64(a9, 20) + b7 := rotl64(a10, 3) + b17 := rotl64(a11, 10) + b2 := rotl64(a12, 43) + b12 := rotl64(a13, 25) + b22 := rotl64(a14, 39) + b23 := rotl64(a15, 41) + b8 := rotl64(a16, 45) + b18 := rotl64(a17, 15) + b3 := rotl64(a18, 21) + b13 := rotl64(a19, 8) + b14 := rotl64(a20, 18) + b24 := rotl64(a21, 2) + b9 := rotl64(a22, 61) + b19 := rotl64(a23, 56) + b4 := rotl64(a24, 14) + + a0 = b0 ^ (^b1 & b2) + a1 = b1 ^ (^b2 & b3) + a2 = b2 ^ (^b3 & b4) + a3 = b3 ^ (^b4 & b0) + a4 = b4 ^ (^b0 & b1) + a5 = b5 ^ (^b6 & b7) + a6 = b6 ^ (^b7 & b8) + a7 = b7 ^ (^b8 & b9) + a8 = b8 ^ (^b9 & b5) + a9 = b9 ^ (^b5 & b6) + a10 = b10 ^ (^b11 & b12) + a11 = b11 ^ (^b12 & b13) + a12 = b12 ^ (^b13 & b14) + a13 = b13 ^ (^b14 & b10) + a14 = b14 ^ (^b10 & b11) + a15 = b15 ^ (^b16 & b17) + a16 = b16 ^ (^b17 & b18) + a17 = b17 ^ (^b18 & b19) + a18 = b18 ^ (^b19 & b15) + a19 = b19 ^ (^b15 & b16) + a20 = b20 ^ (^b21 & b22) + a21 = b21 ^ (^b22 & b23) + a22 = b22 ^ (^b23 & b24) + a23 = b23 ^ (^b24 & b20) + a24 = b24 ^ (^b20 & b21) + + a0 ^= rc[r] + } + + s[0], s[1], s[2], s[3], s[4] = a0, a1, a2, a3, a4 + s[5], s[6], s[7], s[8], s[9] = a5, a6, a7, a8, a9 + s[10], s[11], s[12], s[13], s[14] = a10, a11, a12, a13, a14 + s[15], s[16], s[17], s[18], s[19] = a15, a16, a17, a18, a19 + s[20], s[21], s[22], s[23], s[24] = a20, a21, a22, a23, a24 +} + +// DeepSeekHashV1 返回 data 的 32 字节摘要,与 WASM wasm_deepseek_hash_v1 等价。 +func DeepSeekHashV1(data []byte) [32]byte { + const rate = 136 + var s [25]uint64 + + off := 0 + for off+rate <= len(data) { + for i := 0; i < rate/8; i++ { + s[i] ^= binary.LittleEndian.Uint64(data[off+i*8:]) + } + keccakF23(&s) + off += rate + } + + var final [rate]byte + copy(final[:], data[off:]) + final[len(data)-off] = 0x06 + final[rate-1] |= 0x80 + for i := 0; i < rate/8; i++ { + s[i] ^= binary.LittleEndian.Uint64(final[i*8:]) + } + keccakF23(&s) + + var out [32]byte + binary.LittleEndian.PutUint64(out[0:], s[0]) + binary.LittleEndian.PutUint64(out[8:], s[1]) + binary.LittleEndian.PutUint64(out[16:], s[2]) + binary.LittleEndian.PutUint64(out[24:], s[3]) + return out +} diff --git a/pow/deepseek_pow.go b/pow/deepseek_pow.go new file mode 100644 index 0000000000000000000000000000000000000000..bb9b2b4af0c15e2e4611edaf18a2562653e80086 --- /dev/null +++ b/pow/deepseek_pow.go @@ -0,0 +1,147 @@ +package pow + +import ( + "context" + "encoding/base64" + "encoding/binary" + "encoding/hex" + "encoding/json" + "errors" + "strconv" +) + +// Challenge 对应 /api/v0/chat/create_pow_challenge 返回 dem data.biz_data.challenge。 +type Challenge struct { + Algorithm string `json:"algorithm"` + Challenge string `json:"challenge"` + Salt string `json:"salt"` + ExpireAt int64 `json:"expire_at"` + Difficulty int64 `json:"difficulty"` + Signature string `json:"signature"` + TargetPath string `json:"target_path"` +} + +// BuildPrefix: "__" (对应 pow.go:89) +func BuildPrefix(salt string, expireAt int64) string { + return salt + "_" + strconv.FormatInt(expireAt, 10) + "_" +} + +// SolvePow 搜索 nonce ∈ [0, difficulty) 使得 DeepSeekHashV1(prefix+str(nonce)) == challenge。 +// prefix 预吸收进 state,循环内零分配。 +func SolvePow(ctx context.Context, challengeHex, salt string, expireAt, difficulty int64) (int64, error) { + if len(challengeHex) != 64 { + return 0, errors.New("pow: challenge must be 64 hex chars") + } + target, err := hex.DecodeString(challengeHex) + if err != nil { + return 0, err + } + var ta [32]byte + copy(ta[:], target) + t0 := binary.LittleEndian.Uint64(ta[0:]) + t1 := binary.LittleEndian.Uint64(ta[8:]) + t2 := binary.LittleEndian.Uint64(ta[16:]) + t3 := binary.LittleEndian.Uint64(ta[24:]) + + prefix := []byte(BuildPrefix(salt, expireAt)) + const rate = 136 + var baseState [25]uint64 + off := 0 + for off+rate <= len(prefix) { + for i := 0; i < rate/8; i++ { + baseState[i] ^= binary.LittleEndian.Uint64(prefix[off+i*8:]) + } + keccakF23(&baseState) + off += rate + } + tailLen := len(prefix) - off + var tail [rate]byte + copy(tail[:], prefix[off:]) + + var numBuf [20]byte + for n := int64(0); n < difficulty; n++ { + // Periodically check if context is canceled to avoid wasting CPU + if n&0x3FF == 0 { + if err := ctx.Err(); err != nil { + return 0, err + } + } + + v := uint64(n) + pos := 20 + if v == 0 { + pos-- + numBuf[pos] = '0' + } else { + for v > 0 { + pos-- + numBuf[pos] = byte('0' + v%10) + v /= 10 + } + } + numLen := 20 - pos + s := baseState + totalTail := tailLen + numLen + if totalTail < rate { + var buf [rate]byte + copy(buf[:tailLen], tail[:tailLen]) + copy(buf[tailLen:totalTail], numBuf[pos:]) + buf[totalTail] = 0x06 + buf[rate-1] |= 0x80 + for i := 0; i < rate/8; i++ { + s[i] ^= binary.LittleEndian.Uint64(buf[i*8:]) + } + keccakF23(&s) + } else { + var buf [rate]byte + copy(buf[:tailLen], tail[:tailLen]) + copy(buf[tailLen:rate], numBuf[pos:pos+(rate-tailLen)]) + for i := 0; i < rate/8; i++ { + s[i] ^= binary.LittleEndian.Uint64(buf[i*8:]) + } + keccakF23(&s) + var buf2 [rate]byte + rem := totalTail - rate + copy(buf2[:rem], numBuf[pos+(rate-tailLen):pos+(rate-tailLen)+rem]) + buf2[rem] = 0x06 + buf2[rate-1] |= 0x80 + for i := 0; i < rate/8; i++ { + s[i] ^= binary.LittleEndian.Uint64(buf2[i*8:]) + } + keccakF23(&s) + } + if s[0] == t0 && s[1] == t1 && s[2] == t2 && s[3] == t3 { + return n, nil + } + } + return 0, errors.New("pow: no solution within difficulty") +} + +// BuildPowHeader 序列化 {algorithm,challenge,salt,answer,signature,target_path} 为 base64(JSON)。 +// 不含 difficulty/expire_at (对应 pow.go:218)。 +func BuildPowHeader(c *Challenge, answer int64) (string, error) { + b, err := json.Marshal(map[string]any{ + "algorithm": c.Algorithm, "challenge": c.Challenge, "salt": c.Salt, + "answer": answer, "signature": c.Signature, "target_path": c.TargetPath, + }) + if err != nil { + return "", err + } + return base64.StdEncoding.EncodeToString(b), nil +} + +// SolveAndBuildHeader 端到端: Challenge → x-ds-pow-response header string。 +func SolveAndBuildHeader(ctx context.Context, c *Challenge) (string, error) { + if c.Algorithm != "DeepSeekHashV1" { + return "", errors.New("pow: unsupported algorithm: " + c.Algorithm) + } + d := c.Difficulty + if d == 0 { + d = 144000 + } + answer, err := SolvePow(ctx, c.Challenge, c.Salt, c.ExpireAt, d) + if err != nil { + return "", err + } + return BuildPowHeader(c, answer) +} diff --git a/pow/deepseek_pow_test.go b/pow/deepseek_pow_test.go new file mode 100644 index 0000000000000000000000000000000000000000..d2ed773870914e461e1a21eeba2cf271845f28e2 --- /dev/null +++ b/pow/deepseek_pow_test.go @@ -0,0 +1,80 @@ +package pow + +import ( + "context" + "encoding/base64" + "encoding/hex" + "encoding/json" + "strconv" + "testing" +) + +// 测试向量来自直接调用 DeepSeek 官方 WASM。 +func TestDeepSeekHashV1(t *testing.T) { + for _, tc := range []struct{ in, want string }{ + {"", "e594808bc5b7151ac160c6d39a02e0a8e261ed588578403099e3561dc40c26b3"}, + {"testsalt_1700000000_42", "d4a2ea58c89e40887c933484868380c6f803eaa8dc53a3b9df8e431b921a4f09"}, + {"testsalt_1700000000_100000", "abea2f35796b65486e9be1b36f7878c66cab021e96faa473fdf4decd31f9ba30"}, + {"abc123salt_1700000000_12345", "74b3b7452745b70e85eb32ee7f0a9ec0381d42dd5137b695da915e104fc390e1"}, + } { + h := DeepSeekHashV1([]byte(tc.in)) + got := hex.EncodeToString(h[:]) + if got != tc.want { + t.Errorf("hash(%q) = %s, want %s", tc.in, got, tc.want) + } + } +} + +func TestSolvePow(t *testing.T) { + for _, tc := range []struct { + salt string + expire int64 + answer int64 + diff int64 + }{ + {"testsalt", 1700000000, 42, 1000}, + {"testsalt", 1700000000, 500, 2000}, + {"abc123salt", 1700000000, 12345, 20000}, + } { + h := DeepSeekHashV1([]byte(BuildPrefix(tc.salt, tc.expire) + strconv.FormatInt(tc.answer, 10))) + got, err := SolvePow(context.Background(), hex.EncodeToString(h[:]), tc.salt, tc.expire, tc.diff) + if err != nil || got != tc.answer { + t.Errorf("salt=%q answer=%d: got=%d err=%v", tc.salt, tc.answer, got, err) + } + } +} + +func TestSolveAndBuildHeader(t *testing.T) { + t0 := DeepSeekHashV1([]byte("salt_1712345678_777")) + header, err := SolveAndBuildHeader(context.Background(), &Challenge{ + Algorithm: "DeepSeekHashV1", Challenge: hex.EncodeToString(t0[:]), + Salt: "salt", ExpireAt: 1712345678, Difficulty: 2000, + Signature: "sig", TargetPath: "/api/v0/chat/completion", + }) + if err != nil { + t.Fatal(err) + } + raw, _ := base64.StdEncoding.DecodeString(header) + var m map[string]any + if err := json.Unmarshal(raw, &m); err != nil { + t.Fatal(err) + } + if int64(m["answer"].(float64)) != 777 { + t.Errorf("answer = %v, want 777", m["answer"]) + } +} + +func BenchmarkHash(b *testing.B) { + d := []byte("realisticsalt_1712345678_12345") + for i := 0; i < b.N; i++ { + DeepSeekHashV1(d) + } +} + +func BenchmarkSolve(b *testing.B) { + h := DeepSeekHashV1([]byte("realisticsalt_1712345678_72000")) + ch := hex.EncodeToString(h[:]) + for i := 0; i < b.N; i++ { + _, _ = SolvePow(context.Background(), ch, "realisticsalt", 1712345678, 144000) + } +} diff --git a/scripts/build-release-archives.sh b/scripts/build-release-archives.sh new file mode 100644 index 0000000000000000000000000000000000000000..415aab83388eb3f2a927da5aab41fe00106e7306 --- /dev/null +++ b/scripts/build-release-archives.sh @@ -0,0 +1,81 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" +cd "$ROOT_DIR" + +source "${ROOT_DIR}/scripts/release-targets.sh" + +build_one() { + local tag="$1" build_version="$2" goos="$3" goarch="$4" goarm="$5" label="$6" + local pkg stage bin + + pkg="ds2api_${tag}_${label}" + stage="dist/${pkg}" + bin="ds2api" + if [[ "$goos" == "windows" ]]; then + bin="ds2api.exe" + fi + + echo "[release-archives] building ${label}" + rm -rf "$stage" + mkdir -p "${stage}/static" + + if [[ "$goarm" == "-" ]]; then + CGO_ENABLED=0 GOOS="$goos" GOARCH="$goarch" \ + go build -buildvcs=false -trimpath -ldflags="-s -w -X ds2api/internal/version.BuildVersion=${build_version}" -o "${stage}/${bin}" ./cmd/ds2api + else + CGO_ENABLED=0 GOOS="$goos" GOARCH="$goarch" GOARM="$goarm" \ + go build -buildvcs=false -trimpath -ldflags="-s -w -X ds2api/internal/version.BuildVersion=${build_version}" -o "${stage}/${bin}" ./cmd/ds2api + fi + + cp config.example.json .env.example LICENSE README.MD README.en.md "${stage}/" + cp -R static/admin "${stage}/static/admin" + + if [[ "$goos" == "windows" ]]; then + (cd dist && zip -rq "${pkg}.zip" "${pkg}") + else + tar -C dist -czf "dist/${pkg}.tar.gz" "${pkg}" + fi + + rm -rf "$stage" +} + +if [[ "${1:-}" == "--build-one" ]]; then + shift + build_one "$@" + exit 0 +fi + +tag="${RELEASE_TAG:-}" +if [[ -z "$tag" && -f VERSION ]]; then + tag="$(tr -d '[:space:]' < VERSION)" +fi +if [[ -z "$tag" ]]; then + echo "release tag is empty; set RELEASE_TAG or provide VERSION." >&2 + exit 1 +fi + +build_version="${BUILD_VERSION:-$tag}" +jobs="${RELEASE_BUILD_JOBS:-}" +if [[ -z "$jobs" ]]; then + if command -v nproc >/dev/null 2>&1; then + jobs="$(nproc)" + elif command -v sysctl >/dev/null 2>&1; then + jobs="$(sysctl -n hw.ncpu)" + else + jobs="2" + fi +fi + +mkdir -p dist + +if [[ "$jobs" -le 1 ]]; then + for target in "${DS2API_RELEASE_TARGETS[@]}"; do + read -r goos goarch goarm label <<< "$target" + build_one "$tag" "$build_version" "$goos" "$goarch" "$goarm" "$label" + done +else + printf '%s\n' "${DS2API_RELEASE_TARGETS[@]}" \ + | xargs -L 1 -P "$jobs" bash "${ROOT_DIR}/scripts/build-release-archives.sh" --build-one "$tag" "$build_version" +fi diff --git a/scripts/build-webui.sh b/scripts/build-webui.sh new file mode 100644 index 0000000000000000000000000000000000000000..bde077e2580319feb97b6d5128f364270094ff06 --- /dev/null +++ b/scripts/build-webui.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# WebUI 构建脚本 +# 用法: ./scripts/build-webui.sh + +set -e + +echo "🔨 Building WebUI..." + +cd "$(dirname "$0")/../webui" + +# 检查 node_modules +if [ ! -d "node_modules" ]; then + echo "📦 Installing dependencies..." + npm ci --prefer-offline --no-audit +fi + +# 构建 +echo "🏗️ Running build..." +npm run build + +if [ ! -f "../static/admin/index.html" ]; then + echo "❌ WebUI build failed: static/admin/index.html not found" + exit 1 +fi + +echo "✅ WebUI built successfully!" +echo "📁 Output: static/admin/" diff --git a/scripts/lint.sh b/scripts/lint.sh new file mode 100644 index 0000000000000000000000000000000000000000..32eea6a044556a7f20f5d8deebdc3eedb6480ab7 --- /dev/null +++ b/scripts/lint.sh @@ -0,0 +1,109 @@ +#!/usr/bin/env bash +set -euo pipefail + +ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)" +cd "$ROOT_DIR" + +LINT_BIN="${GOLANGCI_LINT_BIN:-golangci-lint}" +BOOTSTRAP_VERSION="${GOLANGCI_LINT_VERSION:-v2.11.4}" +BOOTSTRAP_BIN="${ROOT_DIR}/.tmp/golangci-lint-${BOOTSTRAP_VERSION}" + +export GOCACHE="${GOCACHE:-${ROOT_DIR}/.tmp/go-build-cache}" +export GOLANGCI_LINT_CACHE="${GOLANGCI_LINT_CACHE:-${ROOT_DIR}/.tmp/golangci-lint-cache}" +mkdir -p "$GOCACHE" "$GOLANGCI_LINT_CACHE" + +bootstrap_golangci_lint() { + local version_no_v os arch artifact archive_url tmp_dir + version_no_v="${BOOTSTRAP_VERSION#v}" + os="$(uname -s | tr '[:upper:]' '[:lower:]')" + arch="$(uname -m | tr '[:upper:]' '[:lower:]')" + + case "$os" in + linux|darwin|windows) ;; + *) + echo "unsupported OS for bootstrap: ${os}" >&2 + return 1 + ;; + esac + + case "$arch" in + x86_64|amd64) arch="amd64" ;; + aarch64|arm64) arch="arm64" ;; + *) + echo "unsupported architecture for bootstrap: ${arch}" >&2 + return 1 + ;; + esac + + artifact="${os}-${arch}" + archive_url="https://github.com/golangci/golangci-lint/releases/download/${BOOTSTRAP_VERSION}/golangci-lint-${version_no_v}-${artifact}.tar.gz" + + mkdir -p "${ROOT_DIR}/.tmp" + tmp_dir="$(mktemp -d)" + trap 'rm -rf "${tmp_dir}"' RETURN + + curl -sSfL "${archive_url}" -o "${tmp_dir}/golangci-lint.tar.gz" + tar -xzf "${tmp_dir}/golangci-lint.tar.gz" -C "${tmp_dir}" + cp "${tmp_dir}/golangci-lint-${version_no_v}-${artifact}/golangci-lint" "${BOOTSTRAP_BIN}" + chmod +x "${BOOTSTRAP_BIN}" + + echo "bootstrapped golangci-lint ${BOOTSTRAP_VERSION} to ${BOOTSTRAP_BIN}" >&2 +} + +run_lint() { + local bin="$1" + if [[ "$bin" == *" "* ]]; then + eval "$bin fmt --diff -c .golangci.yml" && eval "$bin run -c .golangci.yml ./..." + else + "$bin" fmt --diff -c .golangci.yml && "$bin" run -c .golangci.yml ./... + fi +} + +is_compatibility_error() { + case "$1" in + *"command not found"*|\ + *"not recognized as an internal or external command"*|\ + *"No such file or directory"*|\ + *"unknown command \"fmt\""*|\ + *"unknown command \"run\""*|\ + *"unknown flag"*|\ + *"no such flag"*|\ + *"unsupported version of the configuration"*|\ + *"can't load config"*) + return 0 + ;; + *) + return 1 + ;; + esac +} + +# v2 separates formatters from linters; enforce both in one entrypoint. +if lint_output="$(run_lint "$LINT_BIN" 2>&1)"; then + [[ -n "$lint_output" ]] && echo "$lint_output" + exit 0 +fi + +if [[ -n "${GOLANGCI_LINT_BIN:-}" ]]; then + echo "$lint_output" >&2 + echo "lint failed with explicit GOLANGCI_LINT_BIN=${GOLANGCI_LINT_BIN}; skip auto-bootstrap." >&2 + exit 1 +fi + +if ! is_compatibility_error "$lint_output"; then + echo "$lint_output" >&2 + exit 1 +fi + +echo "default golangci-lint appears incompatible; bootstrapping ${BOOTSTRAP_VERSION}..." >&2 +if [[ ! -x "${BOOTSTRAP_BIN}" ]]; then + bootstrap_golangci_lint +fi + +if lint_output="$(run_lint "${BOOTSTRAP_BIN}" 2>&1)"; then + [[ -n "$lint_output" ]] && echo "$lint_output" + exit 0 +fi + +echo "$lint_output" >&2 +exit 1 diff --git a/scripts/release-targets.sh b/scripts/release-targets.sh new file mode 100644 index 0000000000000000000000000000000000000000..63a5a7e0d01395b9df87822650bacec3b6a4ade1 --- /dev/null +++ b/scripts/release-targets.sh @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +# goos goarch goarm package-label +DS2API_RELEASE_TARGETS=( + "linux amd64 - linux_amd64" + "linux arm64 - linux_arm64" + "linux arm 7 linux_armv7" + "darwin amd64 - darwin_amd64" + "darwin arm64 - darwin_arm64" + "windows amd64 - windows_amd64" + "windows arm64 - windows_arm64" +) diff --git a/start.mjs b/start.mjs new file mode 100644 index 0000000000000000000000000000000000000000..7f35ff441f6e769899d98aff00a176af32a060a0 --- /dev/null +++ b/start.mjs @@ -0,0 +1,569 @@ +#!/usr/bin/env node +/** + * DS2API 启动脚本 - 交互式菜单 + * + * 使用方法: + * node start.mjs # 显示交互式菜单 + * node start.mjs dev # 开发模式(后端 + 前端热重载) + * node start.mjs prod # 生产模式(编译后运行) + * node start.mjs build # 编译后端二进制 + * node start.mjs webui # 构建前端静态文件 + * node start.mjs install # 安装前端依赖 + * node start.mjs stop # 停止所有服务 + * node start.mjs status # 查看服务状态 + */ + +import { spawn, execSync } from 'child_process'; +import { createInterface } from 'readline'; +import { existsSync } from 'fs'; +import { fileURLToPath } from 'url'; +import { dirname, join } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// 判断是否为 Windows +const isWindows = process.platform === 'win32'; + +// 编译产物路径 +const BINARY = join(__dirname, isWindows ? 'ds2api.exe' : 'ds2api'); + +// 配置(从环境变量读取,与 Go 主程序保持一致) +const CONFIG = { + port: process.env.PORT || '5001', + frontendPort: 5173, + logLevel: process.env.LOG_LEVEL || 'INFO', + adminKey: process.env.DS2API_ADMIN_KEY || 'admin', + webuiDir: join(__dirname, 'webui'), + staticAdminDir: process.env.DS2API_STATIC_ADMIN_DIR || join(__dirname, 'static', 'admin'), +}; + +// 国内镜像配置 +const MIRRORS = { + goproxy: process.env.GOPROXY || 'https://goproxy.cn,direct', + npm: process.env.NPM_REGISTRY || 'https://registry.npmmirror.com', +}; + +// 存储子进程 +const processes = []; + +// 颜色输出 +const colors = { + reset: '\x1b[0m', + bright: '\x1b[1m', + dim: '\x1b[2m', + red: '\x1b[31m', + green: '\x1b[32m', + yellow: '\x1b[33m', + blue: '\x1b[34m', + magenta: '\x1b[35m', + cyan: '\x1b[36m', +}; + +const log = { + info: (msg) => console.log(`${colors.cyan}[INFO]${colors.reset} ${msg}`), + success: (msg) => console.log(`${colors.green}[OK]${colors.reset} ${msg}`), + warn: (msg) => console.log(`${colors.yellow}[WARN]${colors.reset} ${msg}`), + error: (msg) => console.log(`${colors.red}[ERROR]${colors.reset} ${msg}`), + title: (msg) => console.log(`\n${colors.bright}${colors.magenta}${msg}${colors.reset}`), +}; + +// 清理并退出 +function cleanup() { + console.log('\n'); + log.info('正在关闭所有服务...'); + processes.forEach(proc => { + if (proc && !proc.killed) { + proc.kill('SIGTERM'); + } + }); + log.success('已退出'); + process.exit(0); +} + +process.on('SIGINT', cleanup); +process.on('SIGTERM', cleanup); + +// 检查命令是否存在 +function commandExists(cmd) { + try { + execSync(`${isWindows ? 'where' : 'which'} ${cmd}`, { stdio: 'ignore' }); + return true; + } catch { + return false; + } +} + +// 检查 Go 是否安装 +function checkGo() { + return commandExists('go'); +} + +// 获取 Go 版本 +function getGoVersion() { + try { + return execSync('go version', { encoding: 'utf-8' }).trim(); + } catch { + return null; + } +} + +// 检查前端依赖是否已安装 +function checkFrontendDeps() { + if (!existsSync(CONFIG.webuiDir)) return null; + return existsSync(join(CONFIG.webuiDir, 'node_modules')); +} + +// 检查前端是否已构建 +function checkWebuiBuilt() { + return existsSync(join(CONFIG.staticAdminDir, 'index.html')); +} + +// 检查后端二进制是否存在 +function binaryExists() { + return existsSync(BINARY); +} + +// 查找占用端口的进程 PID +function findPidByPort(port) { + const numericPort = parseInt(port, 10); + if (isNaN(numericPort)) return []; + + try { + if (isWindows) { + const output = execSync(`netstat -ano | findstr :${numericPort} | findstr LISTENING`, { + encoding: 'utf-8', + shell: true, + stdio: ['pipe', 'pipe', 'ignore'], + }); + const pids = new Set(); + for (const line of output.trim().split('\n')) { + const parts = line.trim().split(/\s+/); + const pid = parts[parts.length - 1]; + if (pid && pid !== '0') pids.add(pid); + } + return [...pids]; + } else { + const output = execSync(`lsof -ti :${numericPort}`, { + encoding: 'utf-8', + stdio: ['pipe', 'pipe', 'ignore'], + }); + return output.trim().split('\n').filter(Boolean); + } + } catch { + return []; + } +} + +// 获取运行中的服务状态 +function getRunningStatus() { + const backendPids = findPidByPort(CONFIG.port); + const frontendPids = findPidByPort(CONFIG.frontendPort); + return { + backend: backendPids, + frontend: frontendPids, + isRunning: backendPids.length > 0 || frontendPids.length > 0, + }; +} + +// 停止服务 +async function stopServices() { + const running = getRunningStatus(); + + if (!running.isRunning) { + log.warn('没有检测到正在运行的服务'); + return; + } + + log.title('========== 停止服务 =========='); + + const killProcess = async (pid) => { + try { + if (isWindows) { + try { + execSync(`taskkill /PID ${pid}`, { stdio: 'ignore', shell: true }); + } catch { + execSync(`taskkill /F /T /PID ${pid}`, { stdio: 'ignore', shell: true }); + } + } else { + execSync(`kill -15 ${pid}`, { stdio: 'ignore' }); + await new Promise(r => setTimeout(r, 500)); + try { + execSync(`kill -0 ${pid}`, { stdio: 'ignore' }); + execSync(`kill -9 ${pid}`, { stdio: 'ignore' }); + } catch { /* 进程已退出 */ } + } + } catch { /* 进程可能已退出 */ } + }; + + if (running.backend.length > 0) { + log.info(`停止后端服务 (端口 ${CONFIG.port}, PID: ${running.backend.join(', ')})...`); + for (const pid of running.backend) await killProcess(pid); + log.success('后端服务已停止'); + } + + if (running.frontend.length > 0) { + log.info(`停止前端服务 (端口 ${CONFIG.frontendPort}, PID: ${running.frontend.join(', ')})...`); + for (const pid of running.frontend) await killProcess(pid); + log.success('前端服务已停止'); + } +} + +// 安装前端依赖 +async function installFrontendDeps() { + if (!existsSync(CONFIG.webuiDir)) { + log.warn('webui 目录不存在,跳过前端依赖安装'); + return; + } + log.info(`安装前端依赖 (npm ci, registry: ${MIRRORS.npm})...`); + return new Promise((resolve, reject) => { + const proc = spawn('npm', ['ci', '--registry', MIRRORS.npm], { + cwd: CONFIG.webuiDir, + stdio: 'inherit', + shell: isWindows, + }); + proc.on('close', code => code === 0 ? resolve() : reject(new Error('前端依赖安装失败'))); + }); +} + +// 确保前端依赖已安装 +async function ensureFrontendDeps() { + if (checkFrontendDeps() === false) { + log.warn('检测到前端依赖未安装,正在安装...'); + await installFrontendDeps(); + } +} + +// 编译后端二进制 +async function buildBackend() { + if (!checkGo()) throw new Error('未找到 Go,请先安装 Go (https://go.dev/dl/)'); + log.info(`编译后端二进制 (GOPROXY: ${MIRRORS.goproxy})...`); + return new Promise((resolve, reject) => { + const proc = spawn('go', ['build', '-o', BINARY, './cmd/ds2api'], { + cwd: __dirname, + stdio: 'inherit', + shell: isWindows, + env: { ...process.env, GOPROXY: MIRRORS.goproxy }, + }); + proc.on('close', code => code === 0 ? resolve() : reject(new Error('后端编译失败'))); + }); +} + +// 构建前端静态文件 +async function buildWebui() { + if (!existsSync(CONFIG.webuiDir)) { + log.warn('webui 目录不存在'); + return; + } + await ensureFrontendDeps(); + log.info('构建前端静态文件...'); + return new Promise((resolve, reject) => { + const proc = spawn( + 'npm', ['run', 'build', '--', '--outDir', CONFIG.staticAdminDir, '--emptyOutDir'], + { cwd: CONFIG.webuiDir, stdio: 'inherit', shell: isWindows } + ); + proc.on('close', code => code === 0 ? resolve() : reject(new Error('前端构建失败'))); + }); +} + +// 启动后端(开发模式:go run,无需预编译) +async function startBackendDev() { + if (!checkGo()) throw new Error('未找到 Go,请先安装 Go (https://go.dev/dl/)'); + log.info(`启动后端(go run)... 本地 http://127.0.0.1:${CONFIG.port} 绑定 0.0.0.0:${CONFIG.port}`); + const proc = spawn('go', ['run', './cmd/ds2api'], { + cwd: __dirname, + stdio: 'inherit', + shell: isWindows, + env: { ...process.env, + PORT: CONFIG.port, + LOG_LEVEL: CONFIG.logLevel, + DS2API_ADMIN_KEY: CONFIG.adminKey, + GOPROXY: MIRRORS.goproxy, + }, + }); + processes.push(proc); + return proc; +} + +// 启动后端(生产模式:运行编译好的二进制) +async function startBackendProd() { + if (!binaryExists()) { + log.warn('未找到编译产物,正在编译...'); + await buildBackend(); + } + log.info(`启动后端(二进制)... 本地 http://127.0.0.1:${CONFIG.port} 绑定 0.0.0.0:${CONFIG.port}`); + const proc = spawn(BINARY, [], { + cwd: __dirname, + stdio: 'inherit', + shell: false, + env: { + ...process.env, + PORT: CONFIG.port, + LOG_LEVEL: CONFIG.logLevel, + DS2API_ADMIN_KEY: CONFIG.adminKey, + }, + }); + processes.push(proc); + return proc; +} + +// 启动前端开发服务器 +async function startFrontend() { + if (!existsSync(CONFIG.webuiDir)) { + log.warn('webui 目录不存在,跳过前端启动'); + return null; + } + await ensureFrontendDeps(); + log.info(`启动前端开发服务器... http://localhost:${CONFIG.frontendPort}`); + const proc = spawn('npm', ['run', 'dev'], { + cwd: CONFIG.webuiDir, + stdio: 'inherit', + shell: true, + }); + processes.push(proc); + return proc; +} + +// 显示状态信息 +function showStatus() { + console.log('\n' + '─'.repeat(50)); + log.success(`后端 API: http://127.0.0.1:${CONFIG.port}`); + log.success(`管理界面: http://127.0.0.1:${CONFIG.port}/admin`); + log.info(`后端绑定: 0.0.0.0:${CONFIG.port} (可通过局域网 IP 访问)`); + if (existsSync(CONFIG.webuiDir)) { + log.success(`前端 Dev: http://localhost:${CONFIG.frontendPort}`); + } + console.log('─'.repeat(50)); + log.info('按 Ctrl+C 停止所有服务\n'); +} + +// 等待进程退出 +function waitForProcesses() { + return new Promise(resolve => { + const check = setInterval(() => { + const activeCount = processes.filter(proc => proc.exitCode === null && proc.signalCode === null).length; + if (activeCount === 0) { + clearInterval(check); + resolve(); + } + }, 1000); + }); +} + +// 交互式菜单 +async function showMenu() { + const rl = createInterface({ input: process.stdin, output: process.stdout }); + const question = (prompt) => new Promise(resolve => rl.question(prompt, resolve)); + + console.clear(); + log.title('╔══════════════════════════════════════════╗'); + log.title('║ DS2API 启动脚本 (Go) ║'); + log.title('╚══════════════════════════════════════════╝'); + + // 环境状态 + const goVersion = getGoVersion(); + const frontendDeps = checkFrontendDeps(); + const webuiBuilt = checkWebuiBuilt(); + const hasBinary = binaryExists(); + const running = getRunningStatus(); + + const ok = (v) => v ? `${colors.green}✓${colors.reset}` : `${colors.yellow}✗${colors.reset}`; + + console.log(`\n${colors.bright}环境状态:${colors.reset}`); + console.log(` Go: ${goVersion ? `${colors.green}${goVersion}${colors.reset}` : `${colors.red}未安装${colors.reset}`}`); + console.log(` 前端依赖: ${frontendDeps === null ? `${colors.dim}N/A${colors.reset}` : frontendDeps ? `${colors.green}已安装${colors.reset}` : `${colors.yellow}未安装${colors.reset}`}`); + console.log(` 前端构建: ${ok(webuiBuilt)} ${webuiBuilt ? `(${CONFIG.staticAdminDir})` : '未构建'}`); + console.log(` 后端二进制: ${ok(hasBinary)} ${hasBinary ? BINARY : '未编译'}`); + + console.log(`\n${colors.bright}服务状态:${colors.reset}`); + console.log(` 后端 (:${CONFIG.port}): ${running.backend.length > 0 ? `${colors.green}运行中${colors.reset} (PID: ${running.backend.join(', ')})` : `${colors.dim}未运行${colors.reset}`}`); + console.log(` 前端 (:${CONFIG.frontendPort}): ${running.frontend.length > 0 ? `${colors.green}运行中${colors.reset} (PID: ${running.frontend.join(', ')})` : `${colors.dim}未运行${colors.reset}`}`); + + console.log(`\n${colors.bright}环境变量:${colors.reset}`); + console.log(` PORT: ${colors.cyan}${CONFIG.port}${colors.reset}`); + console.log(` LOG_LEVEL: ${colors.cyan}${CONFIG.logLevel}${colors.reset}`); + console.log(` DS2API_ADMIN_KEY: ${colors.cyan}${CONFIG.adminKey}${colors.reset}`); + console.log(` GOPROXY: ${colors.cyan}${MIRRORS.goproxy}${colors.reset}`); + console.log(` NPM_REGISTRY: ${colors.cyan}${MIRRORS.npm}${colors.reset}`); + console.log(`${colors.dim} 自定义: DS2API_ADMIN_KEY=密钥 PORT=5001 node start.mjs${colors.reset}`); + + console.log(` +${colors.bright}请选择操作:${colors.reset} + + ${colors.cyan}1.${colors.reset} 开发模式 (go run + 前端热重载) + ${colors.cyan}2.${colors.reset} 仅后端 (go run,无需编译) + ${colors.cyan}3.${colors.reset} 仅前端 (npm dev) + ${colors.cyan}4.${colors.reset} 生产模式 (编译后运行,前端已嵌入) + ${colors.cyan}5.${colors.reset} 编译后端 (go build) + ${colors.cyan}6.${colors.reset} 构建前端 (npm build → static/admin) + ${colors.cyan}7.${colors.reset} 安装前端依赖 (npm ci) + ${colors.red}8.${colors.reset} 停止所有服务 + ${colors.cyan}0.${colors.reset} 退出 +`); + + const choice = await question(`${colors.yellow}请输入选项 [1]: ${colors.reset}`); + rl.close(); + + switch (choice.trim() || '1') { + case '1': + log.title('========== 开发模式 =========='); + await startBackendDev(); + await new Promise(r => setTimeout(r, 1500)); + await startFrontend(); + showStatus(); + await waitForProcesses(); + break; + + case '2': + log.title('========== 仅后端 (go run) =========='); + await startBackendDev(); + showStatus(); + await waitForProcesses(); + break; + + case '3': + log.title('========== 仅前端 =========='); + await startFrontend(); + showStatus(); + await waitForProcesses(); + break; + + case '4': + log.title('========== 生产模式 =========='); + await startBackendProd(); + showStatus(); + await waitForProcesses(); + break; + + case '5': + log.title('========== 编译后端 =========='); + await buildBackend(); + log.success(`编译完成:${BINARY}`); + break; + + case '6': + log.title('========== 构建前端 =========='); + await buildWebui(); + log.success('前端构建完成!'); + break; + + case '7': + log.title('========== 安装前端依赖 =========='); + await installFrontendDeps(); + log.success('前端依赖安装完成!'); + break; + + case '8': + await stopServices(); + break; + + case '0': + log.info('再见!'); + process.exit(0); + break; + + default: + log.warn('无效选项'); + await showMenu(); + } +} + +// 命令行参数处理 +async function main() { + const cmd = process.argv[2]; + + if (!checkGo() && !['install', 'webui', 'stop', 'status', 'help', '-h', '--help'].includes(cmd)) { + log.error('未找到 Go,请先安装 Go: https://go.dev/dl/'); + if (!cmd) { + // 无 Go 时仍允许进入菜单(可以只操作前端) + } else { + process.exit(1); + } + } + + switch (cmd) { + case 'dev': + log.title('========== 开发模式 =========='); + await startBackendDev(); + await new Promise(r => setTimeout(r, 1500)); + await startFrontend(); + showStatus(); + await waitForProcesses(); + break; + + case 'prod': + log.title('========== 生产模式 =========='); + await startBackendProd(); + showStatus(); + await waitForProcesses(); + break; + + case 'build': + await buildBackend(); + log.success(`编译完成:${BINARY}`); + break; + + case 'webui': + await buildWebui(); + log.success('前端构建完成!'); + break; + + case 'install': + await installFrontendDeps(); + log.success('前端依赖安装完成!'); + break; + + case 'stop': + await stopServices(); + break; + + case 'status': { + const status = getRunningStatus(); + const goVer = getGoVersion(); + console.log(`\n${colors.bright}环境:${colors.reset}`); + console.log(` Go: ${goVer || `${colors.red}未安装${colors.reset}`}`); + console.log(`\n${colors.bright}服务状态:${colors.reset}`); + console.log(` 后端 (:${CONFIG.port}): ${status.backend.length > 0 ? `${colors.green}运行中${colors.reset} (PID: ${status.backend.join(', ')})` : `${colors.dim}未运行${colors.reset}`}`); + console.log(` 前端 (:${CONFIG.frontendPort}): ${status.frontend.length > 0 ? `${colors.green}运行中${colors.reset} (PID: ${status.frontend.join(', ')})` : `${colors.dim}未运行${colors.reset}`}\n`); + break; + } + + case 'help': + case '-h': + case '--help': + console.log(` +${colors.bright}DS2API 启动脚本 (Go)${colors.reset} + +${colors.cyan}使用方法:${colors.reset} + node start.mjs 显示交互式菜单 + node start.mjs dev 开发模式 (go run + 前端热重载) + node start.mjs prod 生产模式 (编译产物,前端已嵌入) + node start.mjs build 编译后端二进制 (go build) + node start.mjs webui 构建前端静态文件 + node start.mjs install 安装前端依赖 (npm ci) + node start.mjs stop 停止所有服务 + node start.mjs status 查看服务状态 + +${colors.cyan}常用环境变量:${colors.reset} + PORT 后端端口 (默认: 5001) + LOG_LEVEL 日志级别: DEBUG|INFO|WARN|ERROR (默认: INFO) + DS2API_ADMIN_KEY 管理员密钥 (默认: admin) + DS2API_CONFIG_PATH 配置文件路径 (默认: config.json) + GOPROXY Go 模块代理 (默认: https://goproxy.cn,direct) + NPM_REGISTRY npm 镜像源 (默认: https://registry.npmmirror.com) + +${colors.cyan}示例:${colors.reset} + DS2API_ADMIN_KEY=mykey PORT=8080 node start.mjs dev + GOPROXY=off NPM_REGISTRY=https://registry.npmjs.org node start.mjs dev +`); + break; + + default: + await showMenu(); + } +} + +main().catch(e => { + log.error(e.message); + process.exit(1); +}); diff --git a/vercel.json b/vercel.json new file mode 100644 index 0000000000000000000000000000000000000000..5cebd5df884145c2238c9b2559da3bb4b87b2db3 --- /dev/null +++ b/vercel.json @@ -0,0 +1,137 @@ +{ + "version": 2, + "buildCommand": "npm ci --prefix webui && npm run build --prefix webui", + "outputDirectory": "static", + "functions": { + "api/chat-stream.js": { + "maxDuration": 300 + }, + "api/index.go": { + "maxDuration": 300 + } + }, + "rewrites": [ + { + "source": "/v1/chat/completions", + "has": [ + { + "type": "query", + "key": "__go" + } + ], + "destination": "/api/index" + }, + { + "source": "/v1/chat/completions", + "destination": "/api/chat-stream" + }, + { + "source": "/admin/login", + "destination": "/api/index" + }, + { + "source": "/admin/verify", + "destination": "/api/index" + }, + { + "source": "/admin/config", + "destination": "/api/index" + }, + { + "source": "/admin/config/(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/settings", + "destination": "/api/index" + }, + { + "source": "/admin/settings/(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/keys(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/accounts(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/queue/status", + "destination": "/api/index" + }, + { + "source": "/admin/import", + "destination": "/api/index" + }, + { + "source": "/admin/test", + "destination": "/api/index" + }, + { + "source": "/admin/vercel/(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/export", + "destination": "/api/index" + }, + { + "source": "/admin/version", + "destination": "/api/index" + }, + { + "source": "/admin/chat-history(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/proxies(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/dev/raw-samples/(.*)", + "destination": "/api/index" + }, + { + "source": "/admin/dev/captures(.*)", + "destination": "/api/index" + }, + { + "source": "/admin", + "destination": "/admin/index.html" + }, + { + "source": "/admin/assets/(.*)", + "destination": "/admin/assets/$1" + }, + { + "source": "/admin/(.*)", + "destination": "/admin/index.html" + }, + { + "source": "/(.*)", + "destination": "/api/index" + } + ], + "headers": [ + { + "source": "/admin/assets/(.*)", + "headers": [ + { + "key": "Cache-Control", + "value": "public, max-age=31536000, immutable" + } + ] + }, + { + "source": "/admin/(.*)", + "headers": [ + { + "key": "Cache-Control", + "value": "no-store, must-revalidate" + } + ] + } + ] +} diff --git a/webui/index.html b/webui/index.html new file mode 100644 index 0000000000000000000000000000000000000000..370d1f153f518ecf56fd3c948ee33be6a61d3238 --- /dev/null +++ b/webui/index.html @@ -0,0 +1,41 @@ + + + + + + + + + DS2API - 管理面板 / Admin Console + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + diff --git a/webui/package-lock.json b/webui/package-lock.json new file mode 100644 index 0000000000000000000000000000000000000000..d11dcd9162d0e548432fa73b519c9dcaa5d09d85 --- /dev/null +++ b/webui/package-lock.json @@ -0,0 +1,2130 @@ +{ + "name": "ds2api-admin", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "ds2api-admin", + "version": "1.0.0", + "dependencies": { + "clsx": "^2.1.1", + "lucide-react": "^0.563.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-router-dom": "^7.13.0", + "tailwind-merge": "^3.4.0", + "uuid": "^14.0.0" + }, + "devDependencies": { + "@vitejs/plugin-react": "^6.0.1", + "autoprefixer": "^10.4.24", + "postcss": "^8.5.10", + "tailwindcss": "^3.4.19", + "vite": "^8.0.5" + } + }, + "node_modules/@alloc/quick-lru": { + "version": "5.2.0", + "resolved": "https://registry.npmjs.org/@alloc/quick-lru/-/quick-lru-5.2.0.tgz", + "integrity": "sha512-UrcABB+4bUrFABwbluTIBErXwvbsU/V7TZWfmbgJfbkwiBuziS9gxdODUyuiecfdGQ85jglMW6juS3+z5TsKLw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/@emnapi/core": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/@emnapi/core/-/core-1.9.2.tgz", + "integrity": "sha512-UC+ZhH3XtczQYfOlu3lNEkdW/p4dsJ1r/bP7H8+rhao3TTTMO1ATq/4DdIi23XuGoFY+Cz0JmCbdVl0hz9jZcA==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "@emnapi/wasi-threads": "1.2.1", + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/runtime": { + "version": "1.9.2", + "resolved": "https://registry.npmjs.org/@emnapi/runtime/-/runtime-1.9.2.tgz", + "integrity": "sha512-3U4+MIWHImeyu1wnmVygh5WlgfYDtyf0k8AbLhMFxOipihf6nrWC4syIm/SwEeec0mNSafiiNnMJwbza/Is6Lw==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@emnapi/wasi-threads": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/@emnapi/wasi-threads/-/wasi-threads-1.2.1.tgz", + "integrity": "sha512-uTII7OYF+/Mes/MrcIOYp5yOtSMLBWSIoLPpcgwipoiKbli6k322tcoFsxoIIxPDqW01SQGAgko4EzZi2BNv2w==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@jridgewell/gen-mapping": { + "version": "0.3.13", + "resolved": "https://registry.npmjs.org/@jridgewell/gen-mapping/-/gen-mapping-0.3.13.tgz", + "integrity": "sha512-2kkt/7niJ6MgEPxF0bYdQ6etZaA+fQvDcLKckhy1yIQOzaoKjBBjSj63/aLVjYE3qhRt5dvM+uUyfCg6UKCBbA==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/sourcemap-codec": "^1.5.0", + "@jridgewell/trace-mapping": "^0.3.24" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.2", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.2.tgz", + "integrity": "sha512-bRISgCIjP20/tbWSPWMEi54QVPRZExkuD9lJL+UIxUKtwVJA8wW1Trb1jMs1RFXo1CBTNZ/5hpC9QvmKWdopKw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.5.5", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.5.5.tgz", + "integrity": "sha512-cYQ9310grqxueWbl+WuIUIaiUaDcj7WOq5fVhEljNVgRfOUhY9fy2zTvfoqWsnebh8Sl70VScFbICvJnLKB0Og==", + "dev": true, + "license": "MIT" + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.31", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.31.tgz", + "integrity": "sha512-zzNR+SdQSDJzc8joaeP8QQoCQr8NuYx2dIIytl1QeBEZHJ9uW6hebsrYgbz8hJwUQao3TWCMtmfV8Nu1twOLAw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/resolve-uri": "^3.1.0", + "@jridgewell/sourcemap-codec": "^1.4.14" + } + }, + "node_modules/@napi-rs/wasm-runtime": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/@napi-rs/wasm-runtime/-/wasm-runtime-1.1.2.tgz", + "integrity": "sha512-sNXv5oLJ7ob93xkZ1XnxisYhGYXfaG9f65/ZgYuAu3qt7b3NadcOEhLvx28hv31PgX8SZJRYrAIPQilQmFpLVw==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@tybys/wasm-util": "^0.10.1" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/Brooooooklyn" + }, + "peerDependencies": { + "@emnapi/core": "^1.7.1", + "@emnapi/runtime": "^1.7.1" + } + }, + "node_modules/@nodelib/fs.scandir": { + "version": "2.1.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz", + "integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==", + "dev": true, + "license": "MIT", + "dependencies": { + "@nodelib/fs.stat": "2.0.5", + "run-parallel": "^1.1.9" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/@nodelib/fs.stat": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz", + "integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 8" + } + }, + "node_modules/@nodelib/fs.walk": { + "version": "1.2.8", + "resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz", + "integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@nodelib/fs.scandir": "2.1.5", + "fastq": "^1.6.0" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/@oxc-project/types": { + "version": "0.122.0", + "resolved": "https://registry.npmjs.org/@oxc-project/types/-/types-0.122.0.tgz", + "integrity": "sha512-oLAl5kBpV4w69UtFZ9xqcmTi+GENWOcPF7FCrczTiBbmC0ibXxCwyvZGbO39rCVEuLGAZM84DH0pUIyyv/YJzA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/Boshen" + } + }, + "node_modules/@rolldown/binding-android-arm64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-android-arm64/-/binding-android-arm64-1.0.0-rc.12.tgz", + "integrity": "sha512-pv1y2Fv0JybcykuiiD3qBOBdz6RteYojRFY1d+b95WVuzx211CRh+ytI/+9iVyWQ6koTh5dawe4S/yRfOFjgaA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-arm64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-arm64/-/binding-darwin-arm64-1.0.0-rc.12.tgz", + "integrity": "sha512-cFYr6zTG/3PXXF3pUO+umXxt1wkRK/0AYT8lDwuqvRC+LuKYWSAQAQZjCWDQpAH172ZV6ieYrNnFzVVcnSflAg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-darwin-x64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-darwin-x64/-/binding-darwin-x64-1.0.0-rc.12.tgz", + "integrity": "sha512-ZCsYknnHzeXYps0lGBz8JrF37GpE9bFVefrlmDrAQhOEi4IOIlcoU1+FwHEtyXGx2VkYAvhu7dyBf75EJQffBw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-freebsd-x64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-freebsd-x64/-/binding-freebsd-x64-1.0.0-rc.12.tgz", + "integrity": "sha512-dMLeprcVsyJsKolRXyoTH3NL6qtsT0Y2xeuEA8WQJquWFXkEC4bcu1rLZZSnZRMtAqwtrF/Ib9Ddtpa/Gkge9Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm-gnueabihf": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm-gnueabihf/-/binding-linux-arm-gnueabihf-1.0.0-rc.12.tgz", + "integrity": "sha512-YqWjAgGC/9M1lz3GR1r1rP79nMgo3mQiiA+Hfo+pvKFK1fAJ1bCi0ZQVh8noOqNacuY1qIcfyVfP6HoyBRZ85Q==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-/I5AS4cIroLpslsmzXfwbe5OmWvSsrFuEw3mwvbQ1kDxJ822hFHIx+vsN/TAzNVyepI/j/GSzrtCIwQPeKCLIg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-arm64-musl": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.0.0-rc.12.tgz", + "integrity": "sha512-V6/wZztnBqlx5hJQqNWwFdxIKN0m38p8Jas+VoSfgH54HSj9tKTt1dZvG6JRHcjh6D7TvrJPWFGaY9UBVOaWPw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-ppc64-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-ppc64-gnu/-/binding-linux-ppc64-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-AP3E9BpcUYliZCxa3w5Kwj9OtEVDYK6sVoUzy4vTOJsjPOgdaJZKFmN4oOlX0Wp0RPV2ETfmIra9x1xuayFB7g==", + "cpu": [ + "ppc64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-s390x-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-s390x-gnu/-/binding-linux-s390x-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-nWwpvUSPkoFmZo0kQazZYOrT7J5DGOJ/+QHHzjvNlooDZED8oH82Yg67HvehPPLAg5fUff7TfWFHQS8IV1n3og==", + "cpu": [ + "s390x" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-gnu": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.0.0-rc.12.tgz", + "integrity": "sha512-RNrafz5bcwRy+O9e6P8Z/OCAJW/A+qtBczIqVYwTs14pf4iV1/+eKEjdOUta93q2TsT/FI0XYDP3TCky38LMAg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-linux-x64-musl": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-linux-x64-musl/-/binding-linux-x64-musl-1.0.0-rc.12.tgz", + "integrity": "sha512-Jpw/0iwoKWx3LJ2rc1yjFrj+T7iHZn2JDg1Yny1ma0luviFS4mhAIcd1LFNxK3EYu3DHWCps0ydXQ5i/rrJ2ig==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-openharmony-arm64": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-openharmony-arm64/-/binding-openharmony-arm64-1.0.0-rc.12.tgz", + "integrity": "sha512-vRugONE4yMfVn0+7lUKdKvN4D5YusEiPilaoO2sgUWpCvrncvWgPMzK00ZFFJuiPgLwgFNP5eSiUlv2tfc+lpA==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "openharmony" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-wasm32-wasi": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-wasm32-wasi/-/binding-wasm32-wasi-1.0.0-rc.12.tgz", + "integrity": "sha512-ykGiLr/6kkiHc0XnBfmFJuCjr5ZYKKofkx+chJWDjitX+KsJuAmrzWhwyOMSHzPhzOHOy7u9HlFoa5MoAOJ/Zg==", + "cpu": [ + "wasm32" + ], + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "@napi-rs/wasm-runtime": "^1.1.1" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/@rolldown/binding-win32-arm64-msvc": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.0.0-rc.12.tgz", + "integrity": "sha512-5eOND4duWkwx1AzCxadcOrNeighiLwMInEADT0YM7xeEOOFcovWZCq8dadXgcRHSf3Ulh1kFo/qvzoFiCLOL1Q==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/binding-win32-x64-msvc": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.0.0-rc.12.tgz", + "integrity": "sha512-PyqoipaswDLAZtot351MLhrlrh6lcZPo2LSYE+VDxbVk24LVKAGOuE4hb8xZQmrPAuEtTZW8E6D2zc5EUZX4Lw==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MIT", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": "^20.19.0 || >=22.12.0" + } + }, + "node_modules/@rolldown/pluginutils": { + "version": "1.0.0-rc.7", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.0-rc.7.tgz", + "integrity": "sha512-qujRfC8sFVInYSPPMLQByRh7zhwkGFS4+tyMQ83srV1qrxL4g8E2tyxVVyxd0+8QeBM1mIk9KbWxkegRr76XzA==", + "dev": true, + "license": "MIT" + }, + "node_modules/@tybys/wasm-util": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/@tybys/wasm-util/-/wasm-util-0.10.1.tgz", + "integrity": "sha512-9tTaPJLSiejZKx+Bmog4uSubteqTvFrVrURwkmHixBo0G4seD0zUxp98E1DzUBJxLQ3NPwXrGKDiVjwx/DpPsg==", + "dev": true, + "license": "MIT", + "optional": true, + "dependencies": { + "tslib": "^2.4.0" + } + }, + "node_modules/@vitejs/plugin-react": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/@vitejs/plugin-react/-/plugin-react-6.0.1.tgz", + "integrity": "sha512-l9X/E3cDb+xY3SWzlG1MOGt2usfEHGMNIaegaUGFsLkb3RCn/k8/TOXBcab+OndDI4TBtktT8/9BwwW8Vi9KUQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@rolldown/pluginutils": "1.0.0-rc.7" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "peerDependencies": { + "@rolldown/plugin-babel": "^0.1.7 || ^0.2.0", + "babel-plugin-react-compiler": "^1.0.0", + "vite": "^8.0.0" + }, + "peerDependenciesMeta": { + "@rolldown/plugin-babel": { + "optional": true + }, + "babel-plugin-react-compiler": { + "optional": true + } + } + }, + "node_modules/any-promise": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/any-promise/-/any-promise-1.3.0.tgz", + "integrity": "sha512-7UvmKalWRt1wgjL1RrGxoSJW/0QZFIegpeGvZG9kjp8vrRu55XTHbwnqq2GpXm9uLbcuhxm3IqX9OB4MZR1b2A==", + "dev": true, + "license": "MIT" + }, + "node_modules/anymatch": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/anymatch/-/anymatch-3.1.3.tgz", + "integrity": "sha512-KMReFUr0B4t+D+OBkjR3KYqvocp2XaSzO55UcB6mgQMd3KbcE+mWTyvVV7D/zsdEbNnV6acZUutkiHQXvTr1Rw==", + "dev": true, + "license": "ISC", + "dependencies": { + "normalize-path": "^3.0.0", + "picomatch": "^2.0.4" + }, + "engines": { + "node": ">= 8" + } + }, + "node_modules/arg": { + "version": "5.0.2", + "resolved": "https://registry.npmjs.org/arg/-/arg-5.0.2.tgz", + "integrity": "sha512-PYjyFOLKQ9y57JvQ6QLo8dAgNqswh8M1RMJYdQduT6xbWSgK36P/Z/v+p888pM69jMMfS8Xd8F6I1kQ/I9HUGg==", + "dev": true, + "license": "MIT" + }, + "node_modules/autoprefixer": { + "version": "10.4.27", + "resolved": "https://registry.npmjs.org/autoprefixer/-/autoprefixer-10.4.27.tgz", + "integrity": "sha512-NP9APE+tO+LuJGn7/9+cohklunJsXWiaWEfV3si4Gi/XHDwVNgkwr1J3RQYFIvPy76GmJ9/bW8vyoU1LcxwKHA==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/autoprefixer" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "browserslist": "^4.28.1", + "caniuse-lite": "^1.0.30001774", + "fraction.js": "^5.3.4", + "picocolors": "^1.1.1", + "postcss-value-parser": "^4.2.0" + }, + "bin": { + "autoprefixer": "bin/autoprefixer" + }, + "engines": { + "node": "^10 || ^12 || >=14" + }, + "peerDependencies": { + "postcss": "^8.1.0" + } + }, + "node_modules/baseline-browser-mapping": { + "version": "2.10.14", + "resolved": "https://registry.npmjs.org/baseline-browser-mapping/-/baseline-browser-mapping-2.10.14.tgz", + "integrity": "sha512-fOVLPAsFTsQfuCkvahZkzq6nf8KvGWanlYoTh0SVA0A/PIUxQGU2AOZAoD95n2gFLVDW/jP6sbGLny95nmEuHA==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "baseline-browser-mapping": "dist/cli.cjs" + }, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/binary-extensions": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.3.0.tgz", + "integrity": "sha512-Ceh+7ox5qe7LJuLHoY0feh3pHuUDHAcRUeyL2VYghZwfpkNIy/+8Ocg0a3UuSoYzavmylwuLWQOf3hl0jjMMIw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/braces": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.3.tgz", + "integrity": "sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==", + "dev": true, + "license": "MIT", + "dependencies": { + "fill-range": "^7.1.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/browserslist": { + "version": "4.28.2", + "resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.28.2.tgz", + "integrity": "sha512-48xSriZYYg+8qXna9kwqjIVzuQxi+KYWp2+5nCYnYKPTr0LvD89Jqk2Or5ogxz0NUMfIjhh2lIUX/LyX9B4oIg==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/browserslist" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "baseline-browser-mapping": "^2.10.12", + "caniuse-lite": "^1.0.30001782", + "electron-to-chromium": "^1.5.328", + "node-releases": "^2.0.36", + "update-browserslist-db": "^1.2.3" + }, + "bin": { + "browserslist": "cli.js" + }, + "engines": { + "node": "^6 || ^7 || ^8 || ^9 || ^10 || ^11 || ^12 || >=13.7" + } + }, + "node_modules/camelcase-css": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/camelcase-css/-/camelcase-css-2.0.1.tgz", + "integrity": "sha512-QOSvevhslijgYwRx6Rv7zKdMF8lbRmx+uQGx2+vDc+KI/eBnsy9kit5aj23AgGu3pa4t9AgwbnXWqS+iOY+2aA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 6" + } + }, + "node_modules/caniuse-lite": { + "version": "1.0.30001785", + "resolved": "https://registry.npmjs.org/caniuse-lite/-/caniuse-lite-1.0.30001785.tgz", + "integrity": "sha512-blhOL/WNR+Km1RI/LCVAvA73xplXA7ZbjzI4YkMK9pa6T/P3F2GxjNpEkyw5repTw9IvkyrjyHpwjnhZ5FOvYQ==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/caniuse-lite" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "CC-BY-4.0" + }, + "node_modules/chokidar": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz", + "integrity": "sha512-7VT13fmjotKpGipCW9JEQAusEPE+Ei8nl6/g4FBAmIm0GOOLMua9NDDo/DWp0ZAxCr3cPq5ZpBqmPAQgDda2Pw==", + "dev": true, + "license": "MIT", + "dependencies": { + "anymatch": "~3.1.2", + "braces": "~3.0.2", + "glob-parent": "~5.1.2", + "is-binary-path": "~2.1.0", + "is-glob": "~4.0.1", + "normalize-path": "~3.0.0", + "readdirp": "~3.6.0" + }, + "engines": { + "node": ">= 8.10.0" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + }, + "optionalDependencies": { + "fsevents": "~2.3.2" + } + }, + "node_modules/chokidar/node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "license": "ISC", + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/clsx": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/clsx/-/clsx-2.1.1.tgz", + "integrity": "sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA==", + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/commander": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/commander/-/commander-4.1.1.tgz", + "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 6" + } + }, + "node_modules/cookie": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-1.1.1.tgz", + "integrity": "sha512-ei8Aos7ja0weRpFzJnEA9UHJ/7XQmqglbRwnf2ATjcB9Wq874VKH9kfjjirM6UhU2/E5fFYadylyhFldcqSidQ==", + "license": "MIT", + "engines": { + "node": ">=18" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/express" + } + }, + "node_modules/cssesc": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/cssesc/-/cssesc-3.0.0.tgz", + "integrity": "sha512-/Tb/JcjK111nNScGob5MNtsntNM1aCNUDipB/TkwZFhyDrrE47SOx/18wF2bbjgc3ZzCSKW1T5nt5EbFoAz/Vg==", + "dev": true, + "license": "MIT", + "bin": { + "cssesc": "bin/cssesc" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/detect-libc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/detect-libc/-/detect-libc-2.1.2.tgz", + "integrity": "sha512-Btj2BOOO83o3WyH59e8MgXsxEQVcarkUOpEYrubB0urwnN10yQ364rsiByU11nZlqWYZm05i/of7io4mzihBtQ==", + "dev": true, + "license": "Apache-2.0", + "engines": { + "node": ">=8" + } + }, + "node_modules/didyoumean": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/didyoumean/-/didyoumean-1.2.2.tgz", + "integrity": "sha512-gxtyfqMg7GKyhQmb056K7M3xszy/myH8w+B4RT+QXBQsvAOdc3XymqDDPHx1BgPgsdAA5SIifona89YtRATDzw==", + "dev": true, + "license": "Apache-2.0" + }, + "node_modules/dlv": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/dlv/-/dlv-1.1.3.tgz", + "integrity": "sha512-+HlytyjlPKnIG8XuRG8WvmBP8xs8P71y+SKKS6ZXWoEgLuePxtDoUEiH7WkdePWrQ5JBpE6aoVqfZfJUQkjXwA==", + "dev": true, + "license": "MIT" + }, + "node_modules/electron-to-chromium": { + "version": "1.5.331", + "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.5.331.tgz", + "integrity": "sha512-IbxXrsTlD3hRodkLnbxAPP4OuJYdWCeM3IOdT+CpcMoIwIoDfCmRpEtSPfwBXxVkg9xmBeY7Lz2Eo2TDn/HC3Q==", + "dev": true, + "license": "ISC" + }, + "node_modules/escalade": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/escalade/-/escalade-3.2.0.tgz", + "integrity": "sha512-WUj2qlxaQtO4g6Pq5c29GTcWGDyd8itL8zTlipgECz3JesAiiOKotd8JU6otB3PACgG6xkJUyVhboMS+bje/jA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=6" + } + }, + "node_modules/fast-glob": { + "version": "3.3.3", + "resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.3.tgz", + "integrity": "sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg==", + "dev": true, + "license": "MIT", + "dependencies": { + "@nodelib/fs.stat": "^2.0.2", + "@nodelib/fs.walk": "^1.2.3", + "glob-parent": "^5.1.2", + "merge2": "^1.3.0", + "micromatch": "^4.0.8" + }, + "engines": { + "node": ">=8.6.0" + } + }, + "node_modules/fast-glob/node_modules/glob-parent": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz", + "integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==", + "dev": true, + "license": "ISC", + "dependencies": { + "is-glob": "^4.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/fastq": { + "version": "1.20.1", + "resolved": "https://registry.npmjs.org/fastq/-/fastq-1.20.1.tgz", + "integrity": "sha512-GGToxJ/w1x32s/D2EKND7kTil4n8OVk/9mycTc4VDza13lOvpUZTGX3mFSCtV9ksdGBVzvsyAVLM6mHFThxXxw==", + "dev": true, + "license": "ISC", + "dependencies": { + "reusify": "^1.0.4" + } + }, + "node_modules/fill-range": { + "version": "7.1.1", + "resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz", + "integrity": "sha512-YsGpe3WHLK8ZYi4tWDg2Jy3ebRz2rXowDxnld4bkQB00cc/1Zw9AWnC0i9ztDJitivtQvaI9KaLyKrc+hBW0yg==", + "dev": true, + "license": "MIT", + "dependencies": { + "to-regex-range": "^5.0.1" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/fraction.js": { + "version": "5.3.4", + "resolved": "https://registry.npmjs.org/fraction.js/-/fraction.js-5.3.4.tgz", + "integrity": "sha512-1X1NTtiJphryn/uLQz3whtY6jK3fTqoE3ohKs0tT+Ujr1W59oopxmoEh7Lu5p6vBaPbgoM0bzveAW4Qi5RyWDQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": "*" + }, + "funding": { + "type": "github", + "url": "https://github.com/sponsors/rawify" + } + }, + "node_modules/fsevents": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz", + "integrity": "sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==", + "dev": true, + "hasInstallScript": true, + "license": "MIT", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": "^8.16.0 || ^10.6.0 || >=11.0.0" + } + }, + "node_modules/function-bind": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", + "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", + "dev": true, + "license": "MIT", + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/glob-parent": { + "version": "6.0.2", + "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", + "integrity": "sha512-XxwI8EOhVQgWp6iDL+3b0r86f4d6AX6zSU55HfB4ydCEuXLXc5FcYeOu+nnGftS4TEju/11rt4KJPTMgbfmv4A==", + "dev": true, + "license": "ISC", + "dependencies": { + "is-glob": "^4.0.3" + }, + "engines": { + "node": ">=10.13.0" + } + }, + "node_modules/hasown": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", + "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "function-bind": "^1.1.2" + }, + "engines": { + "node": ">= 0.4" + } + }, + "node_modules/is-binary-path": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", + "integrity": "sha512-ZMERYes6pDydyuGidse7OsHxtbI7WVeUEozgR/g7rd0xUimYNlvZRE/K2MgZTjWy725IfelLeVcEM97mmtRGXw==", + "dev": true, + "license": "MIT", + "dependencies": { + "binary-extensions": "^2.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/is-core-module": { + "version": "2.16.1", + "resolved": "https://registry.npmjs.org/is-core-module/-/is-core-module-2.16.1.tgz", + "integrity": "sha512-UfoeMA6fIJ8wTYFEUjelnaGI67v6+N7qXJEvQuIGa99l4xsCruSYOVSQ0uPANn4dAzm8lkYPaKLrrijLq7x23w==", + "dev": true, + "license": "MIT", + "dependencies": { + "hasown": "^2.0.2" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/is-extglob": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/is-extglob/-/is-extglob-2.1.1.tgz", + "integrity": "sha512-SbKbANkN603Vi4jEZv49LeVJMn4yGwsbzZworEoyEiutsN3nJYdbO36zfhGJ6QEDpOZIFkDtnq5JRxmvl3jsoQ==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-glob": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", + "integrity": "sha512-xelSayHH36ZgE7ZWhli7pW34hNbNl8Ojv5KVmkJD4hBdD3th8Tfk9vYasLM+mXWOZhFkgZfxhLSnrwRr4elSSg==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-extglob": "^2.1.1" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/is-number": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/is-number/-/is-number-7.0.0.tgz", + "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.12.0" + } + }, + "node_modules/jiti": { + "version": "1.21.7", + "resolved": "https://registry.npmjs.org/jiti/-/jiti-1.21.7.tgz", + "integrity": "sha512-/imKNG4EbWNrVjoNC/1H5/9GFy+tqjGBHCaSsN+P2RnPqjsLmv6UD3Ej+Kj8nBWaRAwyk7kK5ZUc+OEatnTR3A==", + "dev": true, + "license": "MIT", + "bin": { + "jiti": "bin/jiti.js" + } + }, + "node_modules/js-tokens": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/js-tokens/-/js-tokens-4.0.0.tgz", + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "license": "MIT" + }, + "node_modules/lightningcss": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss/-/lightningcss-1.32.0.tgz", + "integrity": "sha512-NXYBzinNrblfraPGyrbPoD19C1h9lfI/1mzgWYvXUTe414Gz/X1FD2XBZSZM7rRTrMA8JL3OtAaGifrIKhQ5yQ==", + "dev": true, + "license": "MPL-2.0", + "dependencies": { + "detect-libc": "^2.0.3" + }, + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + }, + "optionalDependencies": { + "lightningcss-android-arm64": "1.32.0", + "lightningcss-darwin-arm64": "1.32.0", + "lightningcss-darwin-x64": "1.32.0", + "lightningcss-freebsd-x64": "1.32.0", + "lightningcss-linux-arm-gnueabihf": "1.32.0", + "lightningcss-linux-arm64-gnu": "1.32.0", + "lightningcss-linux-arm64-musl": "1.32.0", + "lightningcss-linux-x64-gnu": "1.32.0", + "lightningcss-linux-x64-musl": "1.32.0", + "lightningcss-win32-arm64-msvc": "1.32.0", + "lightningcss-win32-x64-msvc": "1.32.0" + } + }, + "node_modules/lightningcss-android-arm64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-android-arm64/-/lightningcss-android-arm64-1.32.0.tgz", + "integrity": "sha512-YK7/ClTt4kAK0vo6w3X+Pnm0D2cf2vPHbhOXdoNti1Ga0al1P4TBZhwjATvjNwLEBCnKvjJc2jQgHXH0NEwlAg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-arm64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-arm64/-/lightningcss-darwin-arm64-1.32.0.tgz", + "integrity": "sha512-RzeG9Ju5bag2Bv1/lwlVJvBE3q6TtXskdZLLCyfg5pt+HLz9BqlICO7LZM7VHNTTn/5PRhHFBSjk5lc4cmscPQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-darwin-x64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-darwin-x64/-/lightningcss-darwin-x64-1.32.0.tgz", + "integrity": "sha512-U+QsBp2m/s2wqpUYT/6wnlagdZbtZdndSmut/NJqlCcMLTWp5muCrID+K5UJ6jqD2BFshejCYXniPDbNh73V8w==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-freebsd-x64": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-freebsd-x64/-/lightningcss-freebsd-x64-1.32.0.tgz", + "integrity": "sha512-JCTigedEksZk3tHTTthnMdVfGf61Fky8Ji2E4YjUTEQX14xiy/lTzXnu1vwiZe3bYe0q+SpsSH/CTeDXK6WHig==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm-gnueabihf": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm-gnueabihf/-/lightningcss-linux-arm-gnueabihf-1.32.0.tgz", + "integrity": "sha512-x6rnnpRa2GL0zQOkt6rts3YDPzduLpWvwAF6EMhXFVZXD4tPrBkEFqzGowzCsIWsPjqSK+tyNEODUBXeeVHSkw==", + "cpu": [ + "arm" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-gnu": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-gnu/-/lightningcss-linux-arm64-gnu-1.32.0.tgz", + "integrity": "sha512-0nnMyoyOLRJXfbMOilaSRcLH3Jw5z9HDNGfT/gwCPgaDjnx0i8w7vBzFLFR1f6CMLKF8gVbebmkUN3fa/kQJpQ==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-arm64-musl": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-arm64-musl/-/lightningcss-linux-arm64-musl-1.32.0.tgz", + "integrity": "sha512-UpQkoenr4UJEzgVIYpI80lDFvRmPVg6oqboNHfoH4CQIfNA+HOrZ7Mo7KZP02dC6LjghPQJeBsvXhJod/wnIBg==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-gnu": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-gnu/-/lightningcss-linux-x64-gnu-1.32.0.tgz", + "integrity": "sha512-V7Qr52IhZmdKPVr+Vtw8o+WLsQJYCTd8loIfpDaMRWGUZfBOYEJeyJIkqGIDMZPwPx24pUMfwSxxI8phr/MbOA==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-linux-x64-musl": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-linux-x64-musl/-/lightningcss-linux-x64-musl-1.32.0.tgz", + "integrity": "sha512-bYcLp+Vb0awsiXg/80uCRezCYHNg1/l3mt0gzHnWV9XP1W5sKa5/TCdGWaR/zBM2PeF/HbsQv/j2URNOiVuxWg==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-arm64-msvc": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-arm64-msvc/-/lightningcss-win32-arm64-msvc-1.32.0.tgz", + "integrity": "sha512-8SbC8BR40pS6baCM8sbtYDSwEVQd4JlFTOlaD3gWGHfThTcABnNDBda6eTZeqbofalIJhFx0qKzgHJmcPTnGdw==", + "cpu": [ + "arm64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lightningcss-win32-x64-msvc": { + "version": "1.32.0", + "resolved": "https://registry.npmjs.org/lightningcss-win32-x64-msvc/-/lightningcss-win32-x64-msvc-1.32.0.tgz", + "integrity": "sha512-Amq9B/SoZYdDi1kFrojnoqPLxYhQ4Wo5XiL8EVJrVsB8ARoC1PWW6VGtT0WKCemjy8aC+louJnjS7U18x3b06Q==", + "cpu": [ + "x64" + ], + "dev": true, + "license": "MPL-2.0", + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">= 12.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/parcel" + } + }, + "node_modules/lilconfig": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/lilconfig/-/lilconfig-3.1.3.tgz", + "integrity": "sha512-/vlFKAoH5Cgt3Ie+JLhRbwOsCQePABiU3tJ1egGvyQ+33R/vcwM2Zl2QR/LzjsBeItPt3oSVXapn+m4nQDvpzw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=14" + }, + "funding": { + "url": "https://github.com/sponsors/antonk52" + } + }, + "node_modules/lines-and-columns": { + "version": "1.2.4", + "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", + "integrity": "sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg==", + "dev": true, + "license": "MIT" + }, + "node_modules/loose-envify": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/loose-envify/-/loose-envify-1.4.0.tgz", + "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "license": "MIT", + "dependencies": { + "js-tokens": "^3.0.0 || ^4.0.0" + }, + "bin": { + "loose-envify": "cli.js" + } + }, + "node_modules/lucide-react": { + "version": "0.563.0", + "resolved": "https://registry.npmjs.org/lucide-react/-/lucide-react-0.563.0.tgz", + "integrity": "sha512-8dXPB2GI4dI8jV4MgUDGBeLdGk8ekfqVZ0BdLcrRzocGgG75ltNEmWS+gE7uokKF/0oSUuczNDT+g9hFJ23FkA==", + "license": "ISC", + "peerDependencies": { + "react": "^16.5.1 || ^17.0.0 || ^18.0.0 || ^19.0.0" + } + }, + "node_modules/merge2": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz", + "integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 8" + } + }, + "node_modules/micromatch": { + "version": "4.0.8", + "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", + "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", + "dev": true, + "license": "MIT", + "dependencies": { + "braces": "^3.0.3", + "picomatch": "^2.3.1" + }, + "engines": { + "node": ">=8.6" + } + }, + "node_modules/mz": { + "version": "2.7.0", + "resolved": "https://registry.npmjs.org/mz/-/mz-2.7.0.tgz", + "integrity": "sha512-z81GNO7nnYMEhrGh9LeymoE4+Yr0Wn5McHIZMK5cfQCl+NDX08sCZgUc9/6MHni9IWuFLm1Z3HTCXu2z9fN62Q==", + "dev": true, + "license": "MIT", + "dependencies": { + "any-promise": "^1.0.0", + "object-assign": "^4.0.1", + "thenify-all": "^1.0.0" + } + }, + "node_modules/nanoid": { + "version": "3.3.11", + "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.11.tgz", + "integrity": "sha512-N8SpfPUnUp1bK+PMYW8qSWdl9U+wwNWI4QKxOYDy9JAro3WMX7p2OeVRF9v+347pnakNevPmiHhNmZ2HbFA76w==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "bin": { + "nanoid": "bin/nanoid.cjs" + }, + "engines": { + "node": "^10 || ^12 || ^13.7 || ^14 || >=15.0.1" + } + }, + "node_modules/node-releases": { + "version": "2.0.37", + "resolved": "https://registry.npmjs.org/node-releases/-/node-releases-2.0.37.tgz", + "integrity": "sha512-1h5gKZCF+pO/o3Iqt5Jp7wc9rH3eJJ0+nh/CIoiRwjRxde/hAHyLPXYN4V3CqKAbiZPSeJFSWHmJsbkicta0Eg==", + "dev": true, + "license": "MIT" + }, + "node_modules/normalize-path": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/normalize-path/-/normalize-path-3.0.0.tgz", + "integrity": "sha512-6eZs5Ls3WtCisHWp9S2GUy8dqkpGi4BVSz3GaqiE6ezub0512ESztXUwUB6C6IKbQkY2Pnb/mD4WYojCRwcwLA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/object-hash": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/object-hash/-/object-hash-3.0.0.tgz", + "integrity": "sha512-RSn9F68PjH9HqtltsSnqYC1XXoWe9Bju5+213R98cNGttag9q9yAOTzdbsqvIa7aNm5WffBZFpWYr2aWrklWAw==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 6" + } + }, + "node_modules/path-parse": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.7.tgz", + "integrity": "sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==", + "dev": true, + "license": "MIT" + }, + "node_modules/picocolors": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/picocolors/-/picocolors-1.1.1.tgz", + "integrity": "sha512-xceH2snhtb5M9liqDsmEw56le376mTZkEX/jEb/RxNFyegNul7eNslCXP9FDj/Lcu0X8KEyMceP2ntpaHrDEVA==", + "dev": true, + "license": "ISC" + }, + "node_modules/picomatch": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-2.3.2.tgz", + "integrity": "sha512-V7+vQEJ06Z+c5tSye8S+nHUfI51xoXIXjHQ99cQtKUkQqqO1kO/KCJUfZXuB47h/YBlDhah2H3hdUGXn8ie0oA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=8.6" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/pify": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/pify/-/pify-2.3.0.tgz", + "integrity": "sha512-udgsAY+fTnvv7kI7aaxbqwWNb0AHiB0qBO89PZKPkoTmGOgdbrHDKD+0B2X4uTfJ/FT1R09r9gTsjUjNJotuog==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/pirates": { + "version": "4.0.7", + "resolved": "https://registry.npmjs.org/pirates/-/pirates-4.0.7.tgz", + "integrity": "sha512-TfySrs/5nm8fQJDcBDuUng3VOUKsd7S+zqvbOTiGXHfxX4wK31ard+hoNuvkicM/2YFzlpDgABOevKSsB4G/FA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 6" + } + }, + "node_modules/postcss": { + "version": "8.5.10", + "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.10.tgz", + "integrity": "sha512-pMMHxBOZKFU6HgAZ4eyGnwXF/EvPGGqUr0MnZ5+99485wwW41kW91A4LOGxSHhgugZmSChL5AlElNdwlNgcnLQ==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/postcss" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "nanoid": "^3.3.11", + "picocolors": "^1.1.1", + "source-map-js": "^1.2.1" + }, + "engines": { + "node": "^10 || ^12 || >=14" + } + }, + "node_modules/postcss-import": { + "version": "15.1.0", + "resolved": "https://registry.npmjs.org/postcss-import/-/postcss-import-15.1.0.tgz", + "integrity": "sha512-hpr+J05B2FVYUAXHeK1YyI267J/dDDhMU6B6civm8hSY1jYJnBXxzKDKDswzJmtLHryrjhnDjqqp/49t8FALew==", + "dev": true, + "license": "MIT", + "dependencies": { + "postcss-value-parser": "^4.0.0", + "read-cache": "^1.0.0", + "resolve": "^1.1.7" + }, + "engines": { + "node": ">=14.0.0" + }, + "peerDependencies": { + "postcss": "^8.0.0" + } + }, + "node_modules/postcss-js": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/postcss-js/-/postcss-js-4.1.0.tgz", + "integrity": "sha512-oIAOTqgIo7q2EOwbhb8UalYePMvYoIeRY2YKntdpFQXNosSu3vLrniGgmH9OKs/qAkfoj5oB3le/7mINW1LCfw==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "camelcase-css": "^2.0.1" + }, + "engines": { + "node": "^12 || ^14 || >= 16" + }, + "peerDependencies": { + "postcss": "^8.4.21" + } + }, + "node_modules/postcss-load-config": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/postcss-load-config/-/postcss-load-config-6.0.1.tgz", + "integrity": "sha512-oPtTM4oerL+UXmx+93ytZVN82RrlY/wPUV8IeDxFrzIjXOLF1pN+EmKPLbubvKHT2HC20xXsCAH2Z+CKV6Oz/g==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "lilconfig": "^3.1.1" + }, + "engines": { + "node": ">= 18" + }, + "peerDependencies": { + "jiti": ">=1.21.0", + "postcss": ">=8.0.9", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "jiti": { + "optional": true + }, + "postcss": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/postcss-nested": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/postcss-nested/-/postcss-nested-6.2.0.tgz", + "integrity": "sha512-HQbt28KulC5AJzG+cZtj9kvKB93CFCdLvog1WFLf1D+xmMvPGlBstkpTEZfK5+AN9hfJocyBFCNiqyS48bpgzQ==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/postcss/" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "postcss-selector-parser": "^6.1.1" + }, + "engines": { + "node": ">=12.0" + }, + "peerDependencies": { + "postcss": "^8.2.14" + } + }, + "node_modules/postcss-selector-parser": { + "version": "6.1.2", + "resolved": "https://registry.npmjs.org/postcss-selector-parser/-/postcss-selector-parser-6.1.2.tgz", + "integrity": "sha512-Q8qQfPiZ+THO/3ZrOrO0cJJKfpYCagtMUkXbnEfmgUjwXg6z/WBeOyS9APBBPCTSiDV+s4SwQGu8yFsiMRIudg==", + "dev": true, + "license": "MIT", + "dependencies": { + "cssesc": "^3.0.0", + "util-deprecate": "^1.0.2" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/postcss-value-parser": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/postcss-value-parser/-/postcss-value-parser-4.2.0.tgz", + "integrity": "sha512-1NNCs6uurfkVbeXG4S8JFT9t19m45ICnif8zWLd5oPSZ50QnwMfK+H3jv408d4jw/7Bttv5axS5IiHoLaVNHeQ==", + "dev": true, + "license": "MIT" + }, + "node_modules/queue-microtask": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz", + "integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT" + }, + "node_modules/react": { + "version": "18.3.1", + "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", + "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", + "license": "MIT", + "dependencies": { + "loose-envify": "^1.1.0" + }, + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/react-dom": { + "version": "18.3.1", + "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.3.1.tgz", + "integrity": "sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==", + "license": "MIT", + "dependencies": { + "loose-envify": "^1.1.0", + "scheduler": "^0.23.2" + }, + "peerDependencies": { + "react": "^18.3.1" + } + }, + "node_modules/react-router": { + "version": "7.14.0", + "resolved": "https://registry.npmjs.org/react-router/-/react-router-7.14.0.tgz", + "integrity": "sha512-m/xR9N4LQLmAS0ZhkY2nkPA1N7gQ5TUVa5n8TgANuDTARbn1gt+zLPXEm7W0XDTbrQ2AJSJKhoa6yx1D8BcpxQ==", + "license": "MIT", + "dependencies": { + "cookie": "^1.0.1", + "set-cookie-parser": "^2.6.0" + }, + "engines": { + "node": ">=20.0.0" + }, + "peerDependencies": { + "react": ">=18", + "react-dom": ">=18" + }, + "peerDependenciesMeta": { + "react-dom": { + "optional": true + } + } + }, + "node_modules/react-router-dom": { + "version": "7.14.0", + "resolved": "https://registry.npmjs.org/react-router-dom/-/react-router-dom-7.14.0.tgz", + "integrity": "sha512-2G3ajSVSZMEtmTjIklRWlNvo8wICEpLihfD/0YMDxbWK2UyP5EGfnoIn9AIQGnF3G/FX0MRbHXdFcD+rL1ZreQ==", + "license": "MIT", + "dependencies": { + "react-router": "7.14.0" + }, + "engines": { + "node": ">=20.0.0" + }, + "peerDependencies": { + "react": ">=18", + "react-dom": ">=18" + } + }, + "node_modules/read-cache": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/read-cache/-/read-cache-1.0.0.tgz", + "integrity": "sha512-Owdv/Ft7IjOgm/i0xvNDZ1LrRANRfew4b2prF3OWMQLxLfu3bS8FVhCsrSCMK4lR56Y9ya+AThoTpDCTxCmpRA==", + "dev": true, + "license": "MIT", + "dependencies": { + "pify": "^2.3.0" + } + }, + "node_modules/readdirp": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-3.6.0.tgz", + "integrity": "sha512-hOS089on8RduqdbhvQ5Z37A0ESjsqz6qnRcffsMU3495FuTdqSm+7bhJ29JvIOsBDEEnan5DPu9t3To9VRlMzA==", + "dev": true, + "license": "MIT", + "dependencies": { + "picomatch": "^2.2.1" + }, + "engines": { + "node": ">=8.10.0" + } + }, + "node_modules/resolve": { + "version": "1.22.11", + "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.22.11.tgz", + "integrity": "sha512-RfqAvLnMl313r7c9oclB1HhUEAezcpLjz95wFH4LVuhk9JF/r22qmVP9AMmOU4vMX7Q8pN8jwNg/CSpdFnMjTQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-core-module": "^2.16.1", + "path-parse": "^1.0.7", + "supports-preserve-symlinks-flag": "^1.0.0" + }, + "bin": { + "resolve": "bin/resolve" + }, + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/reusify": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.1.0.tgz", + "integrity": "sha512-g6QUff04oZpHs0eG5p83rFLhHeV00ug/Yf9nZM6fLeUrPguBTkTQOdpAWWspMh55TZfVQDPaN3NQJfbVRAxdIw==", + "dev": true, + "license": "MIT", + "engines": { + "iojs": ">=1.0.0", + "node": ">=0.10.0" + } + }, + "node_modules/rolldown": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/rolldown/-/rolldown-1.0.0-rc.12.tgz", + "integrity": "sha512-yP4USLIMYrwpPHEFB5JGH1uxhcslv6/hL0OyvTuY+3qlOSJvZ7ntYnoWpehBxufkgN0cvXxppuTu5hHa/zPh+A==", + "dev": true, + "license": "MIT", + "dependencies": { + "@oxc-project/types": "=0.122.0", + "@rolldown/pluginutils": "1.0.0-rc.12" + }, + "bin": { + "rolldown": "bin/cli.mjs" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "optionalDependencies": { + "@rolldown/binding-android-arm64": "1.0.0-rc.12", + "@rolldown/binding-darwin-arm64": "1.0.0-rc.12", + "@rolldown/binding-darwin-x64": "1.0.0-rc.12", + "@rolldown/binding-freebsd-x64": "1.0.0-rc.12", + "@rolldown/binding-linux-arm-gnueabihf": "1.0.0-rc.12", + "@rolldown/binding-linux-arm64-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-arm64-musl": "1.0.0-rc.12", + "@rolldown/binding-linux-ppc64-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-s390x-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-x64-gnu": "1.0.0-rc.12", + "@rolldown/binding-linux-x64-musl": "1.0.0-rc.12", + "@rolldown/binding-openharmony-arm64": "1.0.0-rc.12", + "@rolldown/binding-wasm32-wasi": "1.0.0-rc.12", + "@rolldown/binding-win32-arm64-msvc": "1.0.0-rc.12", + "@rolldown/binding-win32-x64-msvc": "1.0.0-rc.12" + } + }, + "node_modules/rolldown/node_modules/@rolldown/pluginutils": { + "version": "1.0.0-rc.12", + "resolved": "https://registry.npmjs.org/@rolldown/pluginutils/-/pluginutils-1.0.0-rc.12.tgz", + "integrity": "sha512-HHMwmarRKvoFsJorqYlFeFRzXZqCt2ETQlEDOb9aqssrnVBB1/+xgTGtuTrIk5vzLNX1MjMtTf7W9z3tsSbrxw==", + "dev": true, + "license": "MIT" + }, + "node_modules/run-parallel": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", + "integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "license": "MIT", + "dependencies": { + "queue-microtask": "^1.2.2" + } + }, + "node_modules/scheduler": { + "version": "0.23.2", + "resolved": "https://registry.npmjs.org/scheduler/-/scheduler-0.23.2.tgz", + "integrity": "sha512-UOShsPwz7NrMUqhR6t0hWjFduvOzbtv7toDH1/hIrfRNIDBnnBWd0CwJTGvTpngVlmwGCdP9/Zl/tVrDqcuYzQ==", + "license": "MIT", + "dependencies": { + "loose-envify": "^1.1.0" + } + }, + "node_modules/set-cookie-parser": { + "version": "2.7.2", + "resolved": "https://registry.npmjs.org/set-cookie-parser/-/set-cookie-parser-2.7.2.tgz", + "integrity": "sha512-oeM1lpU/UvhTxw+g3cIfxXHyJRc/uidd3yK1P242gzHds0udQBYzs3y8j4gCCW+ZJ7ad0yctld8RYO+bdurlvw==", + "license": "MIT" + }, + "node_modules/source-map-js": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/source-map-js/-/source-map-js-1.2.1.tgz", + "integrity": "sha512-UXWMKhLOwVKb728IUtQPXxfYU+usdybtUrK/8uGE8CQMvrhOpwvzDBwj0QhSL7MQc7vIsISBG8VQ8+IDQxpfQA==", + "dev": true, + "license": "BSD-3-Clause", + "engines": { + "node": ">=0.10.0" + } + }, + "node_modules/sucrase": { + "version": "3.35.1", + "resolved": "https://registry.npmjs.org/sucrase/-/sucrase-3.35.1.tgz", + "integrity": "sha512-DhuTmvZWux4H1UOnWMB3sk0sbaCVOoQZjv8u1rDoTV0HTdGem9hkAZtl4JZy8P2z4Bg0nT+YMeOFyVr4zcG5Tw==", + "dev": true, + "license": "MIT", + "dependencies": { + "@jridgewell/gen-mapping": "^0.3.2", + "commander": "^4.0.0", + "lines-and-columns": "^1.1.6", + "mz": "^2.7.0", + "pirates": "^4.0.1", + "tinyglobby": "^0.2.11", + "ts-interface-checker": "^0.1.9" + }, + "bin": { + "sucrase": "bin/sucrase", + "sucrase-node": "bin/sucrase-node" + }, + "engines": { + "node": ">=16 || 14 >=14.17" + } + }, + "node_modules/supports-preserve-symlinks-flag": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/supports-preserve-symlinks-flag/-/supports-preserve-symlinks-flag-1.0.0.tgz", + "integrity": "sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">= 0.4" + }, + "funding": { + "url": "https://github.com/sponsors/ljharb" + } + }, + "node_modules/tailwind-merge": { + "version": "3.5.0", + "resolved": "https://registry.npmjs.org/tailwind-merge/-/tailwind-merge-3.5.0.tgz", + "integrity": "sha512-I8K9wewnVDkL1NTGoqWmVEIlUcB9gFriAEkXkfCjX5ib8ezGxtR3xD7iZIxrfArjEsH7F1CHD4RFUtxefdqV/A==", + "license": "MIT", + "funding": { + "type": "github", + "url": "https://github.com/sponsors/dcastil" + } + }, + "node_modules/tailwindcss": { + "version": "3.4.19", + "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.19.tgz", + "integrity": "sha512-3ofp+LL8E+pK/JuPLPggVAIaEuhvIz4qNcf3nA1Xn2o/7fb7s/TYpHhwGDv1ZU3PkBluUVaF8PyCHcm48cKLWQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "@alloc/quick-lru": "^5.2.0", + "arg": "^5.0.2", + "chokidar": "^3.6.0", + "didyoumean": "^1.2.2", + "dlv": "^1.1.3", + "fast-glob": "^3.3.2", + "glob-parent": "^6.0.2", + "is-glob": "^4.0.3", + "jiti": "^1.21.7", + "lilconfig": "^3.1.3", + "micromatch": "^4.0.8", + "normalize-path": "^3.0.0", + "object-hash": "^3.0.0", + "picocolors": "^1.1.1", + "postcss": "^8.4.47", + "postcss-import": "^15.1.0", + "postcss-js": "^4.0.1", + "postcss-load-config": "^4.0.2 || ^5.0 || ^6.0", + "postcss-nested": "^6.2.0", + "postcss-selector-parser": "^6.1.2", + "resolve": "^1.22.8", + "sucrase": "^3.35.0" + }, + "bin": { + "tailwind": "lib/cli.js", + "tailwindcss": "lib/cli.js" + }, + "engines": { + "node": ">=14.0.0" + } + }, + "node_modules/thenify": { + "version": "3.3.1", + "resolved": "https://registry.npmjs.org/thenify/-/thenify-3.3.1.tgz", + "integrity": "sha512-RVZSIV5IG10Hk3enotrhvz0T9em6cyHBLkH/YAZuKqd8hRkKhSfCGIcP2KUY0EPxndzANBmNllzWPwak+bheSw==", + "dev": true, + "license": "MIT", + "dependencies": { + "any-promise": "^1.0.0" + } + }, + "node_modules/thenify-all": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/thenify-all/-/thenify-all-1.6.0.tgz", + "integrity": "sha512-RNxQH/qI8/t3thXJDwcstUO4zeqo64+Uy/+sNVRBx4Xn2OX+OZ9oP+iJnNFqplFra2ZUVeKCSa2oVWi3T4uVmA==", + "dev": true, + "license": "MIT", + "dependencies": { + "thenify": ">= 3.1.0 < 4" + }, + "engines": { + "node": ">=0.8" + } + }, + "node_modules/tinyglobby": { + "version": "0.2.15", + "resolved": "https://registry.npmjs.org/tinyglobby/-/tinyglobby-0.2.15.tgz", + "integrity": "sha512-j2Zq4NyQYG5XMST4cbs02Ak8iJUdxRM0XI5QyxXuZOzKOINmWurp3smXu3y5wDcJrptwpSjgXHzIQxR0omXljQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "fdir": "^6.5.0", + "picomatch": "^4.0.3" + }, + "engines": { + "node": ">=12.0.0" + }, + "funding": { + "url": "https://github.com/sponsors/SuperchupuDev" + } + }, + "node_modules/tinyglobby/node_modules/fdir": { + "version": "6.5.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.5.0.tgz", + "integrity": "sha512-tIbYtZbucOs0BRGqPJkshJUYdL+SDH7dVM8gjy+ERp3WAUjLEFJE+02kanyHtwjWOnwrKYBiwAmM0p4kLJAnXg==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12.0.0" + }, + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/tinyglobby/node_modules/picomatch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", + "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + }, + "node_modules/to-regex-range": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", + "integrity": "sha512-65P7iz6X5yEr1cwcgvQxbbIw7Uk3gOy5dIdtZ4rDveLqhrdJP+Li/Hx6tyK0NEb+2GCyneCMJiGqrADCSNk8sQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "is-number": "^7.0.0" + }, + "engines": { + "node": ">=8.0" + } + }, + "node_modules/ts-interface-checker": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/ts-interface-checker/-/ts-interface-checker-0.1.13.tgz", + "integrity": "sha512-Y/arvbn+rrz3JCKl9C4kVNfTfSm2/mEp5FSz5EsZSANGPSlQrpRI5M4PKF+mJnE52jOO90PnPSc3Ur3bTQw0gA==", + "dev": true, + "license": "Apache-2.0" + }, + "node_modules/tslib": { + "version": "2.8.1", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz", + "integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==", + "dev": true, + "license": "0BSD", + "optional": true + }, + "node_modules/update-browserslist-db": { + "version": "1.2.3", + "resolved": "https://registry.npmjs.org/update-browserslist-db/-/update-browserslist-db-1.2.3.tgz", + "integrity": "sha512-Js0m9cx+qOgDxo0eMiFGEueWztz+d4+M3rGlmKPT+T4IS/jP4ylw3Nwpu6cpTTP8R1MAC1kF4VbdLt3ARf209w==", + "dev": true, + "funding": [ + { + "type": "opencollective", + "url": "https://opencollective.com/browserslist" + }, + { + "type": "tidelift", + "url": "https://tidelift.com/funding/github/npm/browserslist" + }, + { + "type": "github", + "url": "https://github.com/sponsors/ai" + } + ], + "license": "MIT", + "dependencies": { + "escalade": "^3.2.0", + "picocolors": "^1.1.1" + }, + "bin": { + "update-browserslist-db": "cli.js" + }, + "peerDependencies": { + "browserslist": ">= 4.21.0" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "dev": true, + "license": "MIT" + }, + "node_modules/uuid": { + "version": "14.0.0", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-14.0.0.tgz", + "integrity": "sha512-Qo+uWgilfSmAhXCMav1uYFynlQO7fMFiMVZsQqZRMIXp0O7rR7qjkj+cPvBHLgBqi960QCoo/PH2/6ZtVqKvrg==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "license": "MIT", + "bin": { + "uuid": "dist-node/bin/uuid" + } + }, + "node_modules/vite": { + "version": "8.0.5", + "resolved": "https://registry.npmjs.org/vite/-/vite-8.0.5.tgz", + "integrity": "sha512-nmu43Qvq9UopTRfMx2jOYW5l16pb3iDC1JH6yMuPkpVbzK0k+L7dfsEDH4jRgYFmsg0sTAqkojoZgzLMlwHsCQ==", + "dev": true, + "license": "MIT", + "dependencies": { + "lightningcss": "^1.32.0", + "picomatch": "^4.0.4", + "postcss": "^8.5.8", + "rolldown": "1.0.0-rc.12", + "tinyglobby": "^0.2.15" + }, + "bin": { + "vite": "bin/vite.js" + }, + "engines": { + "node": "^20.19.0 || >=22.12.0" + }, + "funding": { + "url": "https://github.com/vitejs/vite?sponsor=1" + }, + "optionalDependencies": { + "fsevents": "~2.3.3" + }, + "peerDependencies": { + "@types/node": "^20.19.0 || >=22.12.0", + "@vitejs/devtools": "^0.1.0", + "esbuild": "^0.27.0 || ^0.28.0", + "jiti": ">=1.21.0", + "less": "^4.0.0", + "sass": "^1.70.0", + "sass-embedded": "^1.70.0", + "stylus": ">=0.54.8", + "sugarss": "^5.0.0", + "terser": "^5.16.0", + "tsx": "^4.8.1", + "yaml": "^2.4.2" + }, + "peerDependenciesMeta": { + "@types/node": { + "optional": true + }, + "@vitejs/devtools": { + "optional": true + }, + "esbuild": { + "optional": true + }, + "jiti": { + "optional": true + }, + "less": { + "optional": true + }, + "sass": { + "optional": true + }, + "sass-embedded": { + "optional": true + }, + "stylus": { + "optional": true + }, + "sugarss": { + "optional": true + }, + "terser": { + "optional": true + }, + "tsx": { + "optional": true + }, + "yaml": { + "optional": true + } + } + }, + "node_modules/vite/node_modules/picomatch": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.4.tgz", + "integrity": "sha512-QP88BAKvMam/3NxH6vj2o21R6MjxZUAd6nlwAS/pnGvN9IVLocLHxGYIzFhg6fUQ+5th6P4dv4eW9jX3DSIj7A==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" + } + } + } +} diff --git a/webui/package.json b/webui/package.json new file mode 100644 index 0000000000000000000000000000000000000000..0703bc811869b6a4bdc21ea0dc6f7d5281a461ab --- /dev/null +++ b/webui/package.json @@ -0,0 +1,27 @@ +{ + "name": "ds2api-admin", + "private": true, + "version": "1.0.0", + "type": "module", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview" + }, + "dependencies": { + "clsx": "^2.1.1", + "lucide-react": "^0.563.0", + "react": "^18.2.0", + "react-dom": "^18.2.0", + "react-router-dom": "^7.13.0", + "tailwind-merge": "^3.4.0", + "uuid": "^14.0.0" + }, + "devDependencies": { + "@vitejs/plugin-react": "^6.0.1", + "autoprefixer": "^10.4.24", + "postcss": "^8.5.10", + "tailwindcss": "^3.4.19", + "vite": "^8.0.5" + } +} diff --git a/webui/postcss.config.js b/webui/postcss.config.js new file mode 100644 index 0000000000000000000000000000000000000000..d41ad63557e97efa6032f82f33c7a7d03bf909fa --- /dev/null +++ b/webui/postcss.config.js @@ -0,0 +1,6 @@ +export default { + plugins: { + tailwindcss: {}, + autoprefixer: {}, + }, +} diff --git a/webui/public/ds2api-favicon.svg b/webui/public/ds2api-favicon.svg new file mode 100644 index 0000000000000000000000000000000000000000..feb9dcd572b80b4ba90f7c3262620d4a4b6f6555 --- /dev/null +++ b/webui/public/ds2api-favicon.svg @@ -0,0 +1,20 @@ + + + + + + + + + + DS + + diff --git a/webui/src/App.jsx b/webui/src/App.jsx new file mode 100644 index 0000000000000000000000000000000000000000..8067b8b80f9c96c5b0c2886ddd304aa698db1c28 --- /dev/null +++ b/webui/src/App.jsx @@ -0,0 +1,3 @@ +import AppRoutes from './app/AppRoutes' + +export default AppRoutes diff --git a/webui/src/app/AppRoutes.jsx b/webui/src/app/AppRoutes.jsx new file mode 100644 index 0000000000000000000000000000000000000000..1795d4e6666ad0b7ae789d45c0577b0cfd9f4cea --- /dev/null +++ b/webui/src/app/AppRoutes.jsx @@ -0,0 +1,84 @@ +import { Navigate, Route, Routes, useLocation, useNavigate } from 'react-router-dom' +import clsx from 'clsx' + +import LandingPage from '../components/LandingPage' +import Login from '../components/Login' +import DashboardShell from '../layout/DashboardShell' +import { useI18n } from '../i18n' +import { useAdminAuth } from './useAdminAuth' +import { useAdminConfig } from './useAdminConfig' + +export default function AppRoutes() { + const { t } = useI18n() + const navigate = useNavigate() + const location = useLocation() + + const isProduction = import.meta.env.MODE === 'production' + const { + token, + authChecking, + message, + isAdminRoute, + isVercel, + showMessage, + handleLogin, + handleLogout, + } = useAdminAuth({ isProduction, location, t }) + + const { + config, + fetchConfig, + } = useAdminConfig({ token, showMessage, t }) + + if (isAdminRoute && authChecking) { + return ( +
+
+
+

{t('auth.checking')}

+
+
+ ) + } + + return ( + + {!isProduction && ( + navigate('/admin')} />} /> + )} + + ) : ( +
+
+
+
+
+ + {message && ( +
+ {message.text} +
+ )} + +
+ ) + } /> + } /> +
+ ) +} diff --git a/webui/src/app/useAdminAuth.js b/webui/src/app/useAdminAuth.js new file mode 100644 index 0000000000000000000000000000000000000000..2da2391ac17f20253039720ce96155524059682c --- /dev/null +++ b/webui/src/app/useAdminAuth.js @@ -0,0 +1,70 @@ +import { useCallback, useEffect, useMemo, useState } from 'react' +import { detectRuntimeEnv } from '../utils/runtimeEnv' + +export function useAdminAuth({ isProduction, location, t }) { + const [message, setMessage] = useState(null) + const [token, setToken] = useState(null) + const [authChecking, setAuthChecking] = useState(true) + + const isAdminRoute = location.pathname.startsWith('/admin') || isProduction + const runtimeEnv = useMemo(() => detectRuntimeEnv(), []) + const isVercel = runtimeEnv.isVercel + + const showMessage = useCallback((type, text) => { + setMessage({ type, text }) + setTimeout(() => setMessage(null), 5000) + }, []) + + const handleLogout = useCallback(() => { + setToken(null) + localStorage.removeItem('ds2api_token') + localStorage.removeItem('ds2api_token_expires') + sessionStorage.removeItem('ds2api_token') + sessionStorage.removeItem('ds2api_token_expires') + }, []) + + const handleLogin = useCallback((newToken) => { + setToken(newToken) + }, []) + + useEffect(() => { + if (!isAdminRoute) { + setAuthChecking(false) + return + } + + const checkAuth = async () => { + const storedToken = localStorage.getItem('ds2api_token') || sessionStorage.getItem('ds2api_token') + const expiresAt = parseInt(localStorage.getItem('ds2api_token_expires') || sessionStorage.getItem('ds2api_token_expires') || '0') + + if (storedToken && expiresAt > Date.now()) { + try { + const res = await fetch('/admin/verify', { + headers: { 'Authorization': `Bearer ${storedToken}` } + }) + if (res.ok) { + setToken(storedToken) + } else { + handleLogout() + } + } catch { + setToken(storedToken) + } + } + setAuthChecking(false) + } + + checkAuth() + }, [handleLogout, isAdminRoute, t]) + + return { + token, + authChecking, + message, + isAdminRoute, + isVercel, + showMessage, + handleLogin, + handleLogout, + } +} diff --git a/webui/src/app/useAdminConfig.js b/webui/src/app/useAdminConfig.js new file mode 100644 index 0000000000000000000000000000000000000000..7b5c64580ce040f608df2f0385800d567b2d9adb --- /dev/null +++ b/webui/src/app/useAdminConfig.js @@ -0,0 +1,50 @@ +import { useCallback, useEffect, useState } from 'react' + +const ENV_DRAFT_KEY = 'ds2api_env_config_draft_v1' + +export function useAdminConfig({ token, showMessage, t }) { + const [config, setConfig] = useState({ keys: [], accounts: [] }) + + const fetchConfig = useCallback(async () => { + if (!token) return + try { + const res = await fetch('/admin/config', { + headers: { 'Authorization': `Bearer ${token}` } + }) + if (res.ok) { + const data = await res.json() + if (data?.env_backed) { + localStorage.setItem(ENV_DRAFT_KEY, JSON.stringify(data)) + } else { + localStorage.removeItem(ENV_DRAFT_KEY) + } + setConfig(data) + } + } catch (e) { + console.error('Failed to fetch config:', e) + showMessage('error', t('errors.fetchConfig', { error: e.message })) + } + }, [showMessage, t, token]) + + useEffect(() => { + if (token) { + const rawDraft = localStorage.getItem(ENV_DRAFT_KEY) + if (rawDraft) { + try { + const draft = JSON.parse(rawDraft) + if (draft?.env_backed) { + setConfig(draft) + } + } catch (_e) { + localStorage.removeItem(ENV_DRAFT_KEY) + } + } + fetchConfig() + } + }, [fetchConfig, token]) + + return { + config, + fetchConfig, + } +} diff --git a/webui/src/components/BatchImport.jsx b/webui/src/components/BatchImport.jsx new file mode 100644 index 0000000000000000000000000000000000000000..e97d29538bcd9e3d93930c2da3c1a0b3a955e5a7 --- /dev/null +++ b/webui/src/components/BatchImport.jsx @@ -0,0 +1,217 @@ +import { useState } from 'react' +import { FileCode, Download, Upload, Copy, Check, AlertTriangle } from 'lucide-react' +import clsx from 'clsx' +import { useI18n } from '../i18n' +import { getBatchImportTemplates } from '../utils/batchImportTemplates' + +export default function BatchImport({ onRefresh, onMessage, authFetch }) { + const { t } = useI18n() + const [jsonInput, setJsonInput] = useState('') + const [loading, setLoading] = useState(false) + const [result, setResult] = useState(null) + const [copied, setCopied] = useState(false) + const [importMode, setImportMode] = useState('merge') + + const apiFetch = authFetch || fetch + const templates = getBatchImportTemplates(t) + + const handleImport = async () => { + if (!jsonInput.trim()) { + onMessage('error', t('batchImport.enterJson')) + return + } + + let config + try { + config = JSON.parse(jsonInput) + } catch (e) { + onMessage('error', t('messages.invalidJson')) + return + } + + setLoading(true) + setResult(null) + try { + const res = await apiFetch(`/admin/config/import?mode=${encodeURIComponent(importMode)}`, { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + body: JSON.stringify({ config, mode: importMode }), + }) + const data = await res.json() + if (res.ok) { + setResult(data) + onMessage('success', t('batchImport.importSuccess', { keys: data.imported_keys, accounts: data.imported_accounts })) + onRefresh() + } else { + onMessage('error', data.detail || t('messages.importFailed')) + } + } catch (e) { + onMessage('error', t('messages.networkError')) + } finally { + setLoading(false) + } + } + + const loadTemplate = (key) => { + const tpl = templates[key] + if (tpl) { + setJsonInput(JSON.stringify(tpl.config, null, 2)) + onMessage('info', t('batchImport.templateLoaded', { name: tpl.name })) + } + } + + const handleExport = async () => { + try { + const res = await apiFetch('/admin/config/export') + if (res.ok) { + const data = await res.json() + setJsonInput(JSON.stringify(JSON.parse(data.json), null, 2)) + onMessage('success', t('batchImport.currentConfigLoaded')) + } + } catch (e) { + onMessage('error', t('batchImport.fetchConfigFailed')) + } + } + + const copyBase64 = async () => { + try { + const res = await apiFetch('/admin/config/export') + if (res.ok) { + const data = await res.json() + await navigator.clipboard.writeText(data.base64) + setCopied(true) + setTimeout(() => setCopied(false), 2000) + onMessage('success', t('batchImport.copySuccess')) + } + } catch (e) { + onMessage('error', t('messages.copyFailed')) + } + } + + return ( +
+ {/* Templates Panel */} +
+
+

+ + {t('batchImport.quickTemplates')} +

+
+ {Object.entries(templates).map(([key, tpl]) => ( + + ))} +
+
+ +
+

+ + {t('batchImport.dataExport')} +

+

+ {t('batchImport.dataExportDesc')} +

+ +

+ {t('batchImport.variableName')}: DS2API_CONFIG_JSON +

+
+
+ + {/* Editor Panel */} +
+
+

+ + {t('batchImport.jsonEditor')} +

+
+
+ {t('batchImport.modeLabel')} + + +
+ {importMode === 'replace' && ( + {t('batchImport.modeReplaceHint')} + )} + + +
+
+ +
+