Compare commits

...
Author SHA1 Message Date
ji ruo f053931a1d fix: require Basic Auth for health checks 2026-11-03 14:56:39 +08:00
ji ruo 5203176364 fix: remove application fingerprints from public responses 2026-11-03 14:48:41 +08:00
RuoJi6 c0b558fa1e Merge pull request #113 from RuoJi6/codex/issue-110
feat: use DeepSeek Search API for web search
2026-11-02 22:16:00 -08:00
RuoJi6 aa1b5635e6 Merge pull request #112 from RuoJi6/codex/issue-111-service-address
fix: show service addresses in task assets
2026-11-02 22:15:32 -08:00
ji ruo 7140d14a17 feat: use DeepSeek Search API for web search 2026-11-03 14:07:32 +08:00
20 changed files with 561 additions and 139 deletions

No files matched your search

+9 -1
View File
@@ -246,7 +246,15 @@ jobs:
trap 'docker logs artex-smoke; docker rm -f artex-smoke' EXIT
curl --fail --retry 30 --retry-connrefused --retry-delay 2 --max-time 3 \
http://127.0.0.1:8787/api/health > /tmp/artex-health.json
jq -e --arg version "${RELEASE_TAG#v}" '.ok == true and .version == $version' /tmp/artex-health.json
jq -e '. == {"ok": true}' /tmp/artex-health.json
curl --fail -H 'Content-Type: application/json' \
--data '{"password":"smoke-test-admin-password"}' \
http://127.0.0.1:8787/api/auth/init > /tmp/artex-auth.json
token=$(jq -er '.token' /tmp/artex-auth.json)
echo "::add-mask::$token"
curl --fail -H "Authorization: Bearer $token" \
http://127.0.0.1:8787/api/version > /tmp/artex-version.json
jq -e --arg version "${RELEASE_TAG#v}" '.version == $version' /tmp/artex-version.json
curl --fail http://127.0.0.1:8787/ > /tmp/artex-index.html
grep -qi '<!doctype html' /tmp/artex-index.html
- name: Push tested architecture image
+3 -1
View File
@@ -320,7 +320,9 @@ CGO_ENABLED=0 go build -tags embedui -o artex ./cmd/artex
在 **系统配置 → HTTP Basic Auth** 中填写独立的验证用户名和密码,打开「启用访问验证」并点击「保存访问验证」。默认关闭,保存后立即生效,无需重启;密码至少 8 位、最多 72 字节,用户名不能包含冒号。
开启后,访客先完成浏览器原生账号密码验证,再进入原有 ARTEX 登录页。网页、静态资源和 API 均受保护,`/api/health` 健康检查保留公开访问;通过访问验证仍需登录 ARTEX 才能使用业务 API。公网部署请通过 HTTPS 访问。
开启后,访客先完成浏览器原生账号密码验证,再进入原有 ARTEX 登录页。网页、静态资源和 API 均受保护,`/api/health` 的 GET/HEAD 健康检查同样需要通过 Basic Auth,未认证时返回 `401`;通过后仅返回 `{"ok":true}`,不暴露应用名称或版本。健康检查不额外要求管理员 JWT;外部探针需要配置相同的 Basic Auth 凭据,例如使用 `curl --fail --user '验证用户名' http://127.0.0.1:8787/api/health` 并按提示输入密码。未启用 Basic Auth 时保留原有的无需凭据探活行为。版本号由需要管理员 JWT 的 `/api/version` 提供,启用访问验证后该接口同时受 Basic Auth 保护。未认证请求的 Basic Auth realm 使用通用名称 `Restricted`;通过访问验证仍需登录 ARTEX 才能使用业务 API。公网部署请通过 HTTPS 访问。
从仍使用 `/api/health` 读取版本的旧版网页升级到此版本时,旧页面可能无法自动判断升级完成;服务重启后手动刷新页面即可。新版网页的版本显示和升级轮询均使用 `/api/version`。
配置持久化到数据库,密码仅保存 bcrypt 哈希,界面不会回显。保存时密码留空表示不修改;关闭后保留凭据,方便再次开启。独立的 HttpOnly 验证 Cookie 有效期为 12 小时,兼容现有 JWT 和 SSE;保存配置会使其他浏览器的旧验证 Cookie 失效,当前管理员浏览器保持访问。浏览器可能缓存 Basic Auth 凭据,可用新的无痕窗口检查验证弹窗。
+12 -15
View File
@@ -134,7 +134,7 @@ func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message strin
Provider: c.prov,
SystemPrompt: system,
DynamicBoundary: boundary,
Tools: tools,
Tools: append(tools, deepSeekTools(ws)...),
DeferredTools: def.Deferred,
UnlockSet: def.Unlock,
PermissionMode: permission.ModeBypass,
@@ -143,20 +143,17 @@ func (c *ChatAgent) Chat(ctx context.Context, agentKey, sessionID, message strin
WebFetchCACert: c.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
EnableWebSearch: ws.Enabled,
WebSearchBackend: ws.Backend,
BraveSearchAPIKey: ws.BraveKey,
TavilySearchAPIKey: ws.TavilyKey,
DeepSeekSearchBaseURL: ws.DeepSeekBaseURL,
DeepSeekSearchAPIKey: ws.DeepSeekAPIKey,
DeepSeekSearchModel: ws.DeepSeekModel,
WebSearchProxy: ws.Proxy,
BashEnv: proxyEnv(proxyAddr, c.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: sessionWorkDir,
MaxTurns: maxTurns,
MaxDuration: maxDuration,
Compaction: compactionConfig(c.window),
Todos: actool.NewTodoStore(),
EnableWebSearch: ws.Enabled && ws.Backend != "deepseek",
WebSearchBackend: ws.Backend,
BraveSearchAPIKey: ws.BraveKey,
TavilySearchAPIKey: ws.TavilyKey,
WebSearchProxy: ws.Proxy,
BashEnv: proxyEnv(proxyAddr, c.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: sessionWorkDir,
MaxTurns: maxTurns,
MaxDuration: maxDuration,
Compaction: compactionConfig(c.window),
Todos: actool.NewTodoStore(),
// large tool output spills to cmd-output/ under the session dir.
// 截断上限用 SDK 默认(tool.Capture 的 30000 字符)。
ToolOutputDir: filepath.Join(sessionWorkDir, "cmd-output"),
+121
View File
@@ -0,0 +1,121 @@
package agent
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/Autumn-27/norma/permission"
actool "github.com/Autumn-27/norma/tool"
)
const deepSeekToolsURL = "https://api.deepseek.com/v1/tools/"
func deepSeekTools(opts WebSearchOpts) []actool.CoreTool {
if !opts.Enabled || opts.Backend != "deepseek" {
return nil
}
key := strings.TrimSpace(opts.DeepSeekAPIKey)
if key == "" {
return nil
}
client := &http.Client{Timeout: 30 * time.Second}
if proxy := strings.TrimSpace(opts.Proxy); proxy != "" {
if parsed, err := url.Parse(proxy); err == nil && parsed.Scheme != "" && parsed.Host != "" {
client.Transport = &http.Transport{Proxy: http.ProxyURL(parsed)}
} else {
return nil
}
}
return []actool.CoreTool{
deepSeekTool("deepseek_search", "Search the web with DeepSeek and return ranked titles, URLs, and snippets.", "search", key, client, map[string]any{
"query": map[string]any{"type": "string", "description": "Search query"},
"count": map[string]any{"type": "integer", "description": "Maximum results (1-10)"},
}, "query"),
deepSeekTool("deepseek_fetch", "Fetch a web page with DeepSeek and return its extracted content.", "fetch", key, client, map[string]any{
"url": map[string]any{"type": "string", "description": "Page URL to fetch"},
}, "url"),
}
}
func DeepSeekSearchProbe(ctx context.Context, key, proxy, query string, count int) (int, error) {
if strings.TrimSpace(key) == "" {
return 0, fmt.Errorf("DeepSeek Search API Key is required")
}
client := &http.Client{Timeout: 30 * time.Second}
if strings.TrimSpace(proxy) != "" {
parsed, err := url.Parse(proxy)
if err != nil || parsed.Scheme == "" || parsed.Host == "" {
return 0, fmt.Errorf("invalid search proxy URL")
}
client.Transport = &http.Transport{Proxy: http.ProxyURL(parsed)}
}
body, _ := json.Marshal(map[string]any{"query": query, "count": count})
req, err := http.NewRequestWithContext(ctx, http.MethodPost, deepSeekToolsURL+"search", strings.NewReader(string(body)))
if err != nil {
return 0, err
}
req.Header.Set("Authorization", "Bearer "+strings.TrimSpace(key))
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
var result map[string]json.RawMessage
if err := json.NewDecoder(io.LimitReader(resp.Body, 2<<20)).Decode(&result); err != nil {
return 0, fmt.Errorf("DeepSeek returned invalid JSON: %w", err)
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return 0, fmt.Errorf("DeepSeek search returned HTTP %d: %s", resp.StatusCode, string(result["error"]))
}
for _, field := range []string{"results", "data", "items"} {
var items []json.RawMessage
if json.Unmarshal(result[field], &items) == nil && len(items) > 0 {
return len(items), nil
}
}
return 0, fmt.Errorf("DeepSeek search response did not contain results")
}
func deepSeekTool(name, description, endpoint, key string, client *http.Client, properties map[string]any, required string) actool.CoreTool {
return actool.Build(actool.Spec{
Name: name, Description: description,
Schema: map[string]any{"type": "object", "properties": properties, "required": []any{required}},
ReadOnly: func(json.RawMessage) bool { return true }, Concurrent: func(json.RawMessage) bool { return true },
Permissions: func(context.Context, json.RawMessage, permission.Context) permission.Decision {
return permission.Allowed()
},
Run: func(ctx context.Context, input json.RawMessage, _ *actool.ToolContext) (actool.Result, error) {
var args map[string]any
if err := json.Unmarshal(input, &args); err != nil {
return actool.Errorf("invalid input"), nil
}
body, _ := json.Marshal(args)
req, err := http.NewRequestWithContext(ctx, http.MethodPost, deepSeekToolsURL+endpoint, strings.NewReader(string(body)))
if err != nil {
return actool.Errorf(err.Error()), nil
}
req.Header.Set("Authorization", "Bearer "+key)
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return actool.Errorf("DeepSeek " + endpoint + " failed: " + err.Error()), nil
}
defer resp.Body.Close()
var result json.RawMessage
if err := json.NewDecoder(io.LimitReader(resp.Body, 2<<20)).Decode(&result); err != nil {
return actool.Errorf("DeepSeek returned invalid JSON"), nil
}
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return actool.Errorf(fmt.Sprintf("DeepSeek %s returned HTTP %d: %s", endpoint, resp.StatusCode, string(result))), nil
}
return actool.Text(string(result)), nil
},
})
}
+51
View File
@@ -0,0 +1,51 @@
package agent
import (
"context"
"io"
"net/http"
"strings"
"testing"
)
type deepSeekRoundTripper func(*http.Request) (*http.Response, error)
func (f deepSeekRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) { return f(r) }
func TestDeepSeekSearchToolUsesConfiguredKeyAndQuery(t *testing.T) {
client := &http.Client{Transport: deepSeekRoundTripper(func(r *http.Request) (*http.Response, error) {
if r.URL.String() != deepSeekToolsURL+"search" {
t.Errorf("request URL = %q", r.URL)
}
if got := r.Header.Get("Authorization"); got != "Bearer test-key" {
t.Errorf("authorization = %q", got)
}
body, _ := io.ReadAll(r.Body)
if string(body) != `{"count":3,"query":"ARTEX"}` {
t.Errorf("request body = %s", body)
}
return &http.Response{StatusCode: http.StatusOK, Body: io.NopCloser(strings.NewReader(`{"results":[{"title":"ARTEX","url":"https://example.test"}]}`)), Header: make(http.Header)}, nil
})}
tool := deepSeekTool("deepseek_search", "", "search", "test-key", client, map[string]any{
"query": map[string]any{"type": "string"}, "count": map[string]any{"type": "integer"},
}, "query")
result, err := tool.Call(context.Background(), []byte(`{"query":"ARTEX","count":3}`), nil)
if err != nil {
t.Fatal(err)
}
if result.IsError || !strings.Contains(result.Flatten(), `"results"`) {
t.Fatalf("unexpected result: %+v", result)
}
}
func TestDeepSeekToolsRespectGlobalAndBackendSettings(t *testing.T) {
for _, opts := range []WebSearchOpts{
{Enabled: false, Backend: "deepseek", DeepSeekAPIKey: "key"},
{Enabled: true, Backend: "ddgs", DeepSeekAPIKey: "key"},
{Enabled: true, Backend: "deepseek"},
} {
if got := deepSeekTools(opts); len(got) != 0 {
t.Fatalf("deepseek tools enabled for opts %+v", opts)
}
}
}
+12 -15
View File
@@ -142,7 +142,7 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
Provider: m.prov,
SystemPrompt: system,
DynamicBoundary: boundary,
Tools: tools,
Tools: append(tools, deepSeekTools(m.webSearch)...),
DeferredTools: def.Deferred,
UnlockSet: def.Unlock,
PermissionMode: permission.ModeBypass,
@@ -151,20 +151,17 @@ func (m *MainAgent) Chat(ctx context.Context, taskID int64, mainSeg int, as *db.
WebFetchCACert: m.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
EnableWebSearch: m.webSearch.Enabled,
WebSearchBackend: m.webSearch.Backend,
BraveSearchAPIKey: m.webSearch.BraveKey,
TavilySearchAPIKey: m.webSearch.TavilyKey,
DeepSeekSearchBaseURL: m.webSearch.DeepSeekBaseURL,
DeepSeekSearchAPIKey: m.webSearch.DeepSeekAPIKey,
DeepSeekSearchModel: m.webSearch.DeepSeekModel,
WebSearchProxy: m.webSearch.Proxy,
BashEnv: proxyEnv(proxyAddr, m.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: mainDir, // 本任务工作目录 <workDir>/tasks/<taskID>
ToolOutputDir: cmdOutDir(mainDir),
MaxTurns: m.maxTurns, // 0 = unlimited (configurable in agent management)
Compaction: compactionConfig(m.compactionWindow()), // long chats stay within the window
Todos: actool.NewTodoStore(), // 会话级临时待办(TodoWrite),纯规划用,退出即丢
EnableWebSearch: m.webSearch.Enabled && m.webSearch.Backend != "deepseek",
WebSearchBackend: m.webSearch.Backend,
BraveSearchAPIKey: m.webSearch.BraveKey,
TavilySearchAPIKey: m.webSearch.TavilyKey,
WebSearchProxy: m.webSearch.Proxy,
BashEnv: proxyEnv(proxyAddr, m.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: mainDir, // 本任务工作目录 <workDir>/tasks/<taskID>
ToolOutputDir: cmdOutDir(mainDir),
MaxTurns: m.maxTurns, // 0 = unlimited (configurable in agent management)
Compaction: compactionConfig(m.compactionWindow()), // long chats stay within the window
Todos: actool.NewTodoStore(), // 会话级临时待办(TodoWrite),纯规划用,退出即丢
// 命中预算(步数)→ SDK 跑收尾:向用户输出一句进展总结。Prompt 与收尾轮数可后台编辑(默认 10 轮)。
Settlement: wrapupSettlement("mainagent", nil),
NonStreaming: m.nonStreaming(), // 该 profile 选非流式时走 Provider.Complete
+12 -15
View File
@@ -412,7 +412,7 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
Provider: p.prov,
SystemPrompt: system,
DynamicBoundary: boundary,
Tools: tools,
Tools: append(tools, deepSeekTools(p.webSearch)...),
DeferredTools: def.Deferred,
UnlockSet: def.Unlock,
PermissionMode: permission.ModeBypass,
@@ -421,20 +421,17 @@ func (p *Planner) Plan(ctx context.Context, taskID int64, as *db.AssetStore, ts
WebFetchCACert: p.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
EnableWebSearch: p.webSearch.Enabled,
WebSearchBackend: p.webSearch.Backend,
BraveSearchAPIKey: p.webSearch.BraveKey,
TavilySearchAPIKey: p.webSearch.TavilyKey,
DeepSeekSearchBaseURL: p.webSearch.DeepSeekBaseURL,
DeepSeekSearchAPIKey: p.webSearch.DeepSeekAPIKey,
DeepSeekSearchModel: p.webSearch.DeepSeekModel,
WebSearchProxy: p.webSearch.Proxy,
BashEnv: proxyEnv(proxyAddr, p.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: taskDir, // 本任务工作目录 <workDir>/tasks/<taskID>
ToolOutputDir: cmdOutDir(taskDir),
MaxTurns: p.maxTurns, // 0 = unlimited (configurable in agent management)
MaxDuration: maxDur, // 0=不限;有 deadline 时=距 deadline 剩余
Compaction: compactionConfig(p.compactionWindow()),
EnableWebSearch: p.webSearch.Enabled && p.webSearch.Backend != "deepseek",
WebSearchBackend: p.webSearch.Backend,
BraveSearchAPIKey: p.webSearch.BraveKey,
TavilySearchAPIKey: p.webSearch.TavilyKey,
WebSearchProxy: p.webSearch.Proxy,
BashEnv: proxyEnv(proxyAddr, p.proxyCACert), // Bash 子命令默认走代理+信任 CA
WorkingDir: taskDir, // 本任务工作目录 <workDir>/tasks/<taskID>
ToolOutputDir: cmdOutDir(taskDir),
MaxTurns: p.maxTurns, // 0 = unlimited (configurable in agent management)
MaxDuration: maxDur, // 0=不限;有 deadline 时=距 deadline 剩余
Compaction: compactionConfig(p.compactionWindow()),
// 跨唤醒共享的规划待办:让串行链在多轮之间保留(session 是新的,store 不是)。
Todos: p.todoFor(ts.ID()),
// 命中【本轮】步数预算→ SDK 跑收尾:把本轮已想清楚的结论落地(该派的 add_intent、
+13 -26
View File
@@ -28,27 +28,17 @@ import (
// WebSearchOpts is the web-search backend selection the server pushes into each
// agent (planner/worker/main). Enabled=false leaves the web_search tool off.
// Backend is "ddgs" (no key), "brave-free" (BraveKey required), "tavily"
// (TavilyKey required), or "deepseek" (DeepSeek* required, filled from the
// active LLM profile). It maps directly onto agentcore.Options.
// (TavilyKey required), or "deepseek" (uses DeepSeek's independent Search API).
// Proxy is a dedicated egress proxy for the search request (http/https/socks5),
// independent of the traffic-recording MITM proxy — set it when the search endpoint
// is only reachable via a VPN/SOCKS proxy. Empty = direct.
//
// 注意 deepseek 后端与其它三个的性质不同:DeepSeek 没有可直接调用的搜索接口,
// 搜索只存在于其 Anthropic 兼容 messages 接口内部(web_search_20250305 server
// tool),因此每次搜索会消耗一次模型调用,且搜索请求由 DeepSeek 服务端发出——
// 不经过本机 Proxy,也不会进流量留痕。
type WebSearchOpts struct {
Enabled bool
Backend string
BraveKey string
TavilyKey string
Proxy string
// DeepSeek* 来自当前激活的 LLM 配置(仅 anthropic 格式的 DeepSeek 官方端点),
// 不单独配置,随 LLM 配置切换而变。
DeepSeekBaseURL string
DeepSeekAPIKey string
DeepSeekModel string
Enabled bool
Backend string
BraveKey string
TavilyKey string
Proxy string
DeepSeekAPIKey string
}
type Worker struct {
@@ -437,7 +427,7 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
Provider: w.prov,
SystemPrompt: system,
DynamicBoundary: boundary,
Tools: tools,
Tools: append(tools, deepSeekTools(w.webSearch)...),
DeferredTools: def.Deferred,
UnlockSet: def.Unlock,
PermissionMode: permission.ModeBypass,
@@ -448,14 +438,11 @@ func (w *Worker) execute(ctx context.Context, name string, taskID int64, as *db.
WebFetchCACert: w.proxyCACert,
// 联网搜索(可选)。ddgs 无需 key;brave-free 需 BraveKey;tavily 需 TavilyKey。
// WebSearchProxy 是独立的出口代理(http/https/socks5),与记录流量的 MITM 代理无关;空则直连。
EnableWebSearch: w.webSearch.Enabled,
WebSearchBackend: w.webSearch.Backend,
BraveSearchAPIKey: w.webSearch.BraveKey,
TavilySearchAPIKey: w.webSearch.TavilyKey,
DeepSeekSearchBaseURL: w.webSearch.DeepSeekBaseURL,
DeepSeekSearchAPIKey: w.webSearch.DeepSeekAPIKey,
DeepSeekSearchModel: w.webSearch.DeepSeekModel,
WebSearchProxy: w.webSearch.Proxy,
EnableWebSearch: w.webSearch.Enabled && w.webSearch.Backend != "deepseek",
WebSearchBackend: w.webSearch.Backend,
BraveSearchAPIKey: w.webSearch.BraveKey,
TavilySearchAPIKey: w.webSearch.TavilyKey,
WebSearchProxy: w.webSearch.Proxy,
// Bash 子命令的 HTTP 默认走记录代理 + 信任其 CA(工具无需 -x/-k)。
BashEnv: proxyEnv(proxyAddr, w.proxyCACert),
WorkingDir: runDir,
+4 -5
View File
@@ -125,10 +125,9 @@ func (s *Server) setBasicAuthCookie(w http.ResponseWriter, r *http.Request, cfg
func (s *Server) requireBasicAuth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cfg := s.basicAuth.snapshot()
// Health probes stay public; CORS preflight contains no credentials and
// the API CORS handler returns 204 without invoking any business handler.
if !cfg.Enabled || (r.URL.Path == "/api/health" && (r.Method == "GET" || r.Method == "HEAD")) ||
(r.Method == "OPTIONS" && strings.HasPrefix(r.URL.Path, "/api/")) {
// CORS preflight contains no credentials; the API CORS handler returns
// 204 without invoking any business handler, including health probes.
if !cfg.Enabled || (r.Method == "OPTIONS" && strings.HasPrefix(r.URL.Path, "/api/")) {
next.ServeHTTP(w, r)
return
}
@@ -140,7 +139,7 @@ func (s *Server) requireBasicAuth(next http.Handler) http.Handler {
username, password, ok := r.BasicAuth()
if !ok || len(password) > maxPasswordBytes || username != cfg.Username ||
bcrypt.CompareHashAndPassword([]byte(cfg.PasswordHash), []byte(password)) != nil {
w.Header().Set("WWW-Authenticate", `Basic realm="ARTEX", charset="UTF-8"`)
w.Header().Set("WWW-Authenticate", `Basic realm="Restricted", charset="UTF-8"`)
writeErr(w, http.StatusUnauthorized, "请先完成 HTTP Basic Auth 验证")
return
}
+10 -4
View File
@@ -115,7 +115,7 @@ func TestBasicAuthBlockedSaveDoesNotBlockRequests(t *testing.T) {
h := s.requireBasicAuth(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusNoContent) }))
for _, path := range []string{"/", "/_next/static/app.js", "/api/tasks", "/api/health"} {
r := httptest.NewRequest(http.MethodGet, path, nil)
if enabled && path != "/api/health" {
if enabled {
r.AddCookie(initial.Result().Cookies()[0])
}
w := httptest.NewRecorder()
@@ -126,7 +126,11 @@ func TestBasicAuthBlockedSaveDoesNotBlockRequests(t *testing.T) {
}
}
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/health", nil))
r := httptest.NewRequest(http.MethodGet, "/api/health", nil)
if enabled {
r.AddCookie(initial.Result().Cookies()[0])
}
s.Handler().ServeHTTP(w, r)
if w.Code != http.StatusOK {
reads <- errors.New("health handler unavailable during save")
return
@@ -247,7 +251,9 @@ func TestBasicAuthGate(t *testing.T) {
{name: "asset challenge", path: "/_next/static/app.js", want: 401},
{name: "auth API challenge", path: "/api/auth/status", want: 401},
{name: "check challenge", path: "/api/basic-auth/check", want: 401},
{name: "health exempt", path: "/api/health", want: 204},
{name: "health challenge", path: "/api/health", want: 401},
{name: "health credentials", path: "/api/health", user: "entry", pass: "密码:test-pass", want: 204},
{name: "health gate cookie", path: "/api/health", cookie: cookie, want: 204},
{name: "health prefix not exempt", path: "/api/health/anything", want: 401},
{name: "wrong password", path: "/login", user: "entry", pass: "wrong", want: 401},
{name: "wrong user", path: "/login", user: "wrong", pass: "密码:test-pass", want: 401},
@@ -273,7 +279,7 @@ func TestBasicAuthGate(t *testing.T) {
if w.Code != tc.want {
t.Fatalf("status=%d want %d: %s", w.Code, tc.want, w.Body.String())
}
if w.Code == 401 && w.Header().Get("WWW-Authenticate") != `Basic realm="ARTEX", charset="UTF-8"` {
if w.Code == 401 && w.Header().Get("WWW-Authenticate") != `Basic realm="Restricted", charset="UTF-8"` {
t.Fatal("missing browser challenge")
}
})
+153
View File
@@ -0,0 +1,153 @@
package server
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestHealthGateAndResponse(t *testing.T) {
for _, tc := range []struct {
name string
gate bool
auth string
want int
}{
{name: "gate disabled", want: http.StatusOK},
{name: "anonymous", gate: true, want: http.StatusUnauthorized},
{name: "wrong credentials", gate: true, auth: "wrong", want: http.StatusUnauthorized},
{name: "valid credentials", gate: true, auth: "basic", want: http.StatusOK},
{name: "valid gate cookie", gate: true, auth: "cookie", want: http.StatusOK},
{name: "admin JWT only", gate: true, auth: "jwt", want: http.StatusUnauthorized},
} {
t.Run(tc.name, func(t *testing.T) {
s, _ := basicAuthTestServer(t)
var cookie *http.Cookie
if tc.gate {
w := saveBasicAuthForTest(s, `{"enabled":true,"username":"entry","password":"test-pass"}`)
if w.Code != http.StatusOK {
t.Fatalf("enable gate: %d %s", w.Code, w.Body.String())
}
cookie = w.Result().Cookies()[0]
}
h := s.Handler()
for _, method := range []string{http.MethodGet, http.MethodHead} {
t.Run(method, func(t *testing.T) {
r := httptest.NewRequest(method, "/api/health", nil)
switch tc.auth {
case "wrong":
r.SetBasicAuth("entry", "wrong-password")
case "basic":
r.SetBasicAuth("entry", "test-pass")
case "cookie":
r.AddCookie(cookie)
case "jwt":
token, err := signJWT(s.jwtKey)
if err != nil {
t.Fatal(err)
}
r.Header.Set("Authorization", "Bearer "+token)
}
w := httptest.NewRecorder()
h.ServeHTTP(w, r)
if w.Code != tc.want {
t.Fatalf("status=%d want=%d: %s", w.Code, tc.want, w.Body.String())
}
if w.Header().Get("Cache-Control") != "no-store" {
t.Fatal("health response must not be cached")
}
var body map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
if tc.want == http.StatusUnauthorized {
if w.Header().Get("WWW-Authenticate") != `Basic realm="Restricted", charset="UTF-8"` {
t.Fatal("health must require Basic Auth")
}
for _, field := range []string{"ok", "service", "version"} {
if _, exists := body[field]; exists {
t.Fatalf("unauthenticated health exposed %s: %s", field, w.Body.String())
}
}
return
}
if w.Header().Get("WWW-Authenticate") != "" || len(body) != 1 || body["ok"] != true {
t.Fatalf("unexpected health response: %v %s", w.Header(), w.Body.String())
}
})
}
})
}
}
func TestVersionRequiresAuthentication(t *testing.T) {
for _, tc := range []struct {
name string
gate bool
basic bool
token string
cookie bool
want int
}{
{name: "anonymous", want: http.StatusUnauthorized},
{name: "invalid JWT", token: "invalid", want: http.StatusUnauthorized},
{name: "valid JWT", token: "valid", want: http.StatusOK},
{name: "valid JWT cookie", token: "valid", cookie: true, want: http.StatusOK},
{name: "gate anonymous", gate: true, want: http.StatusUnauthorized},
{name: "gate Basic Auth only", gate: true, basic: true, want: http.StatusUnauthorized},
{name: "gate JWT only", gate: true, token: "valid", want: http.StatusUnauthorized},
{name: "gate invalid JWT", gate: true, basic: true, token: "invalid", want: http.StatusUnauthorized},
{name: "gate both credentials", gate: true, basic: true, token: "valid", cookie: true, want: http.StatusOK},
} {
t.Run(tc.name, func(t *testing.T) {
s, _ := basicAuthTestServer(t)
if tc.gate {
w := saveBasicAuthForTest(s, `{"enabled":true,"username":"entry","password":"test-pass"}`)
if w.Code != http.StatusOK {
t.Fatalf("enable gate: %d %s", w.Code, w.Body.String())
}
}
r := httptest.NewRequest(http.MethodGet, "/api/version", nil)
if tc.basic {
r.SetBasicAuth("entry", "test-pass")
}
token := tc.token
if token == "valid" {
var err error
token, err = signJWT(s.jwtKey)
if err != nil {
t.Fatal(err)
}
}
if token != "" {
if tc.cookie {
r.AddCookie(&http.Cookie{Name: "artex_token", Value: token})
} else {
r.Header.Set("Authorization", "Bearer "+token)
}
}
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, r)
if w.Code != tc.want {
t.Fatalf("status=%d want=%d: %s", w.Code, tc.want, w.Body.String())
}
var body map[string]any
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatal(err)
}
if tc.want != http.StatusOK {
if _, ok := body["version"]; ok {
t.Fatalf("unauthorized request exposed version: %s", w.Body.String())
}
return
}
if len(body) != 1 || body["version"] != BuildVersion {
t.Fatalf("unexpected authenticated version: %s", w.Body.String())
}
if w.Header().Get("Cache-Control") != "no-store" {
t.Fatal("version response must not be cached")
}
})
}
}
+13 -16
View File
@@ -304,6 +304,7 @@ const (
settingWebSearchBackend = "web_search_backend"
settingBraveKey = "brave_search_api_key"
settingTavilyKey = "tavily_search_api_key"
settingDeepSeekSearchKey = "deepseek_search_api_key"
settingWebSearchProxy = "web_search_proxy"
// settingGlobalProxy is the global egress proxy for all target traffic
// (http/https/socks5). Empty = direct. Distinct from web_search_proxy (which
@@ -328,8 +329,7 @@ const (
settingNoaCompaction = "noa_compaction"
// defaultWebSearchBackend is used when web search is on but no backend was picked.
defaultWebSearchBackend = "ddgs"
// deepSeekWebSearchBackend borrows the active LLM profile instead of its own
// key, so it only works on an anthropic-format profile pointed at DeepSeek.
// deepSeekWebSearchBackend calls DeepSeek's independent web-search API.
deepSeekWebSearchBackend = "deepseek"
// defaultWorkers is the concurrent work-agent count when the setting is unset.
defaultWorkers = 3
@@ -783,26 +783,23 @@ func (m *Manager) WebSearchOpts() agent.WebSearchOpts {
if on && backend == "tavily" && strings.TrimSpace(tavilyKey) == "" {
on = false
}
if on && backend == deepSeekWebSearchBackend && m.deepSeekSearchKey() == "" {
on = false
}
o := agent.WebSearchOpts{Enabled: on, Backend: backend, BraveKey: braveKey, TavilyKey: tavilyKey, Proxy: proxy}
if backend == deepSeekWebSearchBackend {
o.DeepSeekBaseURL, o.DeepSeekAPIKey, o.DeepSeekModel = m.deepSeekSearchCreds()
o.DeepSeekAPIKey = m.deepSeekSearchKey()
}
return o
}
// deepSeekSearchCreds resolves the credentials the "deepseek" search backend
// borrows from the active LLM profile (it has no key of its own). Whether that
// profile can actually drive server-side search — DeepSeek exposes it only on
// the Anthropic-format endpoint — is deliberately NOT validated here: the UI
// states the requirement and the user decides. A profile that can't serve it
// simply fails at search time (or at the settings page's 测试 button), which is
// the same feedback every other backend gives for a bad key.
func (m *Manager) deepSeekSearchCreds() (baseURL, apiKey, model string) {
p, err := m.pg.ActiveProfile()
if err != nil || p == nil {
return "", "", ""
}
return p.BaseURL, p.APIKey, p.Model
func (m *Manager) deepSeekSearchKey() string {
key, _, _ := m.pg.GetSetting(settingDeepSeekSearchKey)
return strings.TrimSpace(key)
}
func (m *Manager) setDeepSeekSearchKey(key string) error {
return m.pg.SetSetting(settingDeepSeekSearchKey, strings.TrimSpace(key))
}
// SetWebSearch persists and applies the web-search settings. braveKey, tavilyKey, and
+42 -14
View File
@@ -34,7 +34,7 @@ import (
// BuildVersion is the backend application version, injected from cmd/artex at
// startup (which in turn gets it from -ldflags "-X main.version=<tag>").
// Defaults to "dev" for local builds. Exposed to the frontend via GET /api/health.
// Defaults to "dev" for local builds. Exposed via the authenticated GET /api/version.
var BuildVersion = "dev"
// Server exposes the ARTEX backend over a JSON HTTP API for the shadcn/ui
@@ -803,6 +803,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("PUT /api/settings/basic-auth", s.saveBasicAuthSettings)
mux.HandleFunc("GET /api/health", s.health)
mux.HandleFunc("GET /api/version", s.version)
mux.HandleFunc("GET /api/stats", s.stats)
mux.HandleFunc("GET /api/system/metrics", s.systemMetrics)
mux.HandleFunc("GET /api/logs", s.getLogs)
@@ -1149,8 +1150,16 @@ func (s *Server) Handler() http.Handler {
// --- handlers ---
// health requires the optional Basic Auth gate but no admin JWT, and exposes no application fingerprints.
func (s *Server) health(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{"ok": true, "service": "artex", "version": BuildVersion})
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, 200, map[string]any{"ok": true})
}
// version is protected by the normal JWT middleware and optional Basic Auth gate.
func (s *Server) version(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
writeJSON(w, 200, map[string]any{"version": BuildVersion})
}
func (s *Server) stats(w http.ResponseWriter, r *http.Request) {
@@ -3668,6 +3677,7 @@ func (s *Server) settingsPayload() map[string]any {
"web_search_backend": backend,
"brave_key_set": strings.TrimSpace(braveKey) != "",
"tavily_key_set": strings.TrimSpace(tavilyKey) != "",
"deepseek_search_key_set": s.m.deepSeekSearchKey() != "",
"web_search_proxy": maskSecrets(proxy), // 独立出口代理(http/https/socks5),空=直连
"global_proxy": maskSecrets(s.m.GlobalProxy()), // 全局出口代理(http/https/socks5),所有目标流量走它,空=直连
"proxy_pool": func() []ProxyPoolEntry { e, _, _ := s.m.ProxyPoolSnapshot(); return e }(),
@@ -3768,6 +3778,7 @@ func (s *Server) putSettings(w http.ResponseWriter, r *http.Request) {
WebSearchBackend *string `json:"web_search_backend"`
BraveKey *string `json:"brave_search_api_key"`
TavilyKey *string `json:"tavily_search_api_key"`
DeepSeekSearchKey *string `json:"deepseek_search_api_key"`
WebSearchProxy *string `json:"web_search_proxy"` // 独立出口代理(http/https/socks5);null=不改,""=清空
GlobalProxy *string `json:"global_proxy"` // 全局出口代理(http/https/socks5);null=不改,""=清空(直连)
ProxyRotateTimed *bool `json:"proxy_rotate_timed_enabled"`
@@ -4055,7 +4066,7 @@ func (s *Server) putSettings(w http.ResponseWriter, r *http.Request) {
return
}
}
if req.WebSearchEnabled != nil || req.WebSearchBackend != nil || req.BraveKey != nil || req.TavilyKey != nil || req.WebSearchProxy != nil {
if req.WebSearchEnabled != nil || req.WebSearchBackend != nil || req.BraveKey != nil || req.TavilyKey != nil || req.DeepSeekSearchKey != nil || req.WebSearchProxy != nil {
// Fill unspecified fields from current state so a partial PUT doesn't reset them.
on, backend, _, _, _ := s.m.WebSearch()
if req.WebSearchEnabled != nil {
@@ -4074,6 +4085,16 @@ func (s *Server) putSettings(w http.ResponseWriter, r *http.Request) {
}
changed = true
}
if req.DeepSeekSearchKey != nil {
if strings.Contains(*req.DeepSeekSearchKey, "***") {
*req.DeepSeekSearchKey = s.m.deepSeekSearchKey()
}
if err := s.m.setDeepSeekSearchKey(*req.DeepSeekSearchKey); err != nil {
writeErr(w, 500, err.Error())
return
}
changed = true
}
if changed {
// rebuild agents so the new proxy/tools/prompt/web-search take hold (only if LLM configured).
s.cfgMu.Lock()
@@ -4129,10 +4150,11 @@ func (s *Server) setReporterBrowserScreenshotVisibility(enabled bool) error {
// not retype them. Always 200 with {ok, error?, count?, backend?}.
func (s *Server) testWebSearch(w http.ResponseWriter, r *http.Request) {
var req struct {
Backend string `json:"web_search_backend"`
Proxy string `json:"web_search_proxy"`
BraveKey string `json:"brave_search_api_key"`
TavilyKey string `json:"tavily_search_api_key"`
Backend string `json:"web_search_backend"`
Proxy string `json:"web_search_proxy"`
BraveKey string `json:"brave_search_api_key"`
TavilyKey string `json:"tavily_search_api_key"`
DeepSeekSearchKey string `json:"deepseek_search_api_key"`
}
// Empty body is fine — fall back entirely to the saved config below.
if err := json.NewDecoder(r.Body).Decode(&req); err != nil && err != io.EOF {
@@ -4155,18 +4177,24 @@ func (s *Server) testWebSearch(w http.ResponseWriter, r *http.Request) {
if strings.TrimSpace(req.TavilyKey) != "" {
tavilyKey = req.TavilyKey
}
deepSeekKey := s.m.deepSeekSearchKey()
if strings.TrimSpace(req.DeepSeekSearchKey) != "" {
deepSeekKey = strings.TrimSpace(req.DeepSeekSearchKey)
}
cfg := actool.WebSearchConfig{Backend: backend, BraveAPIKey: braveKey, TavilyAPIKey: tavilyKey, Proxy: proxy}
// Hard cap so a slow/blocked proxy can't hang the request.
wall := 30 * time.Second
// deepseek 的凭据不在表单里,来自当前激活的 LLM 配置;同时它每次搜索都跑一次
// 模型推理,30s 的通用上限偏紧,单独放宽。这里不预判配置能不能用——测这一下
// 本来就是给用户自己确认的手段,真跑不通时下面的报错比预判更有信息量。
probeQuery := "test"
if strings.TrimSpace(backend) == deepSeekWebSearchBackend {
cfg.DeepSeekBaseURL, cfg.DeepSeekAPIKey, cfg.DeepSeekModel = s.m.deepSeekSearchCreds()
wall = 120 * time.Second
// 搜索词由 DeepSeek 端的模型自行决定,"test" 太空泛会让它跳过搜索直接作答。
probeQuery = "DeepSeek company official website"
ctx, cancel := context.WithTimeout(r.Context(), wall)
defer cancel()
count, err := agent.DeepSeekSearchProbe(ctx, deepSeekKey, proxy, "DeepSeek company official website", 3)
if err != nil {
writeJSON(w, 200, map[string]any{"ok": false, "error": err.Error(), "backend": backend})
return
}
writeJSON(w, 200, map[string]any{"ok": count > 0, "count": count, "backend": backend})
return
}
ctx, cancel := context.WithTimeout(r.Context(), wall)
defer cancel()
@@ -48,7 +48,7 @@ function MainContentInner({ children }: { children: ReactNode }) {
useEffect(() => {
api
.health()
.version()
.then((h) => setVersion((h.version ?? "").replace(/^v(?=\d)/, "")))
.catch(() => setVersion(""));
}, []);
@@ -43,7 +43,7 @@ export function UpdateCard() {
const [info, setInfo] = React.useState<UpdateCheck | null>(null);
const [checking, setChecking] = React.useState(true);
const [progress, setProgress] = React.useState<UpdateProgress | null>(null);
// 与 progress 分开:暂存完成后进程就没了,SSE 会断,此时要切到轮询 /api/health。
// 与 progress 分开:暂存完成后进程就没了,SSE 会断,此时要切到轮询 /api/version。
const [restarting, setRestarting] = React.useState(false);
const [busy, setBusy] = React.useState(false);
@@ -116,7 +116,7 @@ export function UpdateCard() {
}
};
// 轮询 /api/health 直到版本号变化。
// 携带管理员 JWT 轮询 /api/version 直到版本号变化。
//
// 判据必须是"版本变了"而不是"能连上了":换装过程中旧版本会短暂地重新起来一次
// (那一次只负责把 artex.new 换上去然后立刻退出),只看连通性会误判成功。
@@ -126,15 +126,12 @@ export function UpdateCard() {
while (Date.now() < deadline) {
await sleep(2000);
try {
const r = await fetch("/api/health", { cache: "no-store" });
if (r.ok) {
const j = (await r.json()) as { version?: string };
if (j.version && j.version !== fromVersion) {
toast.success(`已更新到 ${j.version},正在重新加载页面`);
await sleep(800);
window.location.reload();
return;
}
const { version } = await api.version();
if (version && version !== fromVersion) {
toast.success(`已更新到 ${version},正在重新加载页面`);
await sleep(800);
window.location.reload();
return;
}
} catch {
// 重启窗口内连不上是预期的,继续轮询。
@@ -172,7 +169,7 @@ export function UpdateCard() {
streamRef.current = es;
es.onerror = () => {
// 进程退出时 SSE 必然断开。如果已经进入等待重启,这属于正常现象,
// 交给 /api/health 轮询继续判定即可。
// 交给 /api/version 轮询继续判定即可。
// EventSource 自动重连,恢复服务端保存的当前进度。
};
return es;
+37 -12
View File
@@ -51,6 +51,9 @@ export default function SystemSettingsPage() {
const [braveKeyInput, setBraveKeyInput] = React.useState("");
const [tavilyKeySet, setTavilyKeySet] = React.useState(false);
const [tavilyKeyInput, setTavilyKeyInput] = React.useState("");
const [deepSeekKeySet, setDeepSeekKeySet] = React.useState(false);
const [deepSeekKeyInput, setDeepSeekKeyInput] = React.useState("");
const [savingDeepSeekKey, setSavingDeepSeekKey] = React.useState(false);
const [savingTavilyKey, setSavingTavilyKey] = React.useState(false);
const [proxyInput, setProxyInput] = React.useState("");
const [savingProxy, setSavingProxy] = React.useState(false);
@@ -124,6 +127,7 @@ export default function SystemSettingsPage() {
setBackend(s.web_search_backend || "ddgs");
setBraveKeySet(!!s.brave_key_set);
setTavilyKeySet(!!s.tavily_key_set);
setDeepSeekKeySet(!!s.deepseek_search_key_set);
setProxyInput(s.web_search_proxy ?? "");
const savedProxy = s.global_proxy ?? "";
setGlobalProxyInput(savedProxy);
@@ -437,6 +441,16 @@ export default function SystemSettingsPage() {
.finally(() => setSavingTavilyKey(false));
};
const saveDeepSeekKey = () => {
setSavingDeepSeekKey(true);
api.setSettings({ deepseek_search_api_key: deepSeekKeyInput }).then((s) => {
apply(s);
setDeepSeekKeyInput("");
toast.success("已保存 DeepSeek Search API Key");
}).catch((e) => toast.error(`保存失败:${(e as Error).message}`))
.finally(() => setSavingDeepSeekKey(false));
};
const saveProxy = () => {
setSavingProxy(true);
api
@@ -608,6 +622,7 @@ export default function SystemSettingsPage() {
web_search_proxy: proxyInput.trim(),
brave_search_api_key: braveKeyInput,
tavily_search_api_key: tavilyKeyInput,
deepseek_search_api_key: deepSeekKeyInput,
})
.then((r) => {
if (r.ok) toast.success(`搜索测试成功 · ${r.backend} 返回 ${r.count} 条结果`);
@@ -1441,7 +1456,7 @@ export default function SystemSettingsPage() {
记录代理,独立于流量捕获。
<br />
来源可选 <b>DuckDuckGo(ddgs)</b>(无需 Key)、<b>Brave(免费版)</b>(需填写 Brave API Key)、{" "}
<b>Tavily</b>(需填写 Tavily API Key)或 <b>DeepSeek</b>(复用当前 LLM 配置)。总开关关闭时,各 Agent
<b>Tavily</b>(需填写 Tavily API Key)或 <b>DeepSeek</b>(需单独配置 Search API Key)。总开关关闭时,各 Agent
的网络搜索开关不可用。
</CardDescription>
</CardHeader>
@@ -1487,21 +1502,31 @@ export default function SystemSettingsPage() {
{webSearch && backend === "deepseek" && (
<div className="border-border/60 bg-muted/30 flex flex-col gap-2 rounded-md border p-3">
<p className="text-sm font-medium">DeepSeek 官方联网搜索</p>
<p className="text-sm font-medium">DeepSeek 联网搜索</p>
<p className="text-muted-foreground text-xs leading-relaxed">
该来源直接复用<b>当前激活的 LLM 配置</b>。因此它
<b>仅支持 DeepSeek 官方模型</b>,且该配置<b>必须使用 anthropic 协议</b>
——DeepSeek 的 OpenAI 协议端点不支持服务端搜索。切换 LLM 配置后此来源可能失效。
通过 DeepSeek 独立 Search / Fetch API 提供搜索与网页提取,使用当前 Agent 选择的模型生成查询和整理结果。
</p>
<p className="text-muted-foreground text-xs leading-relaxed">
与其它来源不同,搜索由 <b>DeepSeek 服务端执行</b>:每次搜索会额外消耗一次模型调用(产生 Token
费用),搜索请求<b>不经过上面的出口代理</b>,也<b>不计入流量留痕</b>;返回结果
<b>只有标题和链接</b>
(无摘要),需要正文时由 WebFetch 抓取。
</p>
<p className="text-muted-foreground text-xs leading-relaxed">
是否满足上述条件由你自行确认,系统不做拦截;可用下方「测试搜索」按钮实际跑一次来验证。
搜索请求由 ARTEX 服务端发往 DeepSeek,可使用上方独立出口代理。API Key 只保存于服务端设置,不会回显。
</p>
<div className="flex items-center gap-2">
<Input
type="password"
autoComplete="off"
placeholder={deepSeekKeySet ? "已配置(留空则不变)" : "DeepSeek Search API Key"}
value={deepSeekKeyInput}
disabled={!loaded || savingDeepSeekKey}
onChange={(e) => setDeepSeekKeyInput(e.target.value)}
/>
<Button
type="button"
onClick={saveDeepSeekKey}
disabled={!loaded || savingDeepSeekKey || deepSeekKeyInput.trim() === ""}
>
保存
</Button>
</div>
{deepSeekKeySet && <span className="text-xs text-emerald-500">API Key 已配置</span>}
</div>
)}
+52
View File
@@ -0,0 +1,52 @@
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");
const test = require("node:test");
const vm = require("node:vm");
const ts = require("typescript");
const compiled = ts.transpileModule(fs.readFileSync(path.join(__dirname, "api.ts"), "utf8"), {
compilerOptions: { target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.CommonJS },
}).outputText;
function client(fetch) {
const exports = {};
vm.runInNewContext(compiled, {
exports,
process: { env: {} },
URLSearchParams,
window: {},
localStorage: { getItem: () => "test-admin-token" },
require: (id) => {
if (id === "@/lib/mock/enabled") return { MOCK: false };
if (id === "@/lib/mock/handler") return { mockHandle: () => assert.fail("unexpected demo request") };
throw new Error(`Unexpected runtime import: ${id}`);
},
fetch,
});
return exports.api;
}
test("version queries carry the admin token and request fresh results after restart", async () => {
const versions = ["v1.0.0", "v1.0.1"];
const api = client(async (url, init) => {
assert.equal(url, "/api/version");
assert.equal(init.headers.Authorization, "Bearer test-admin-token");
assert.equal(init.cache, "no-store");
return { ok: true, status: 200, json: async () => ({ version: versions.shift() }) };
});
assert.equal((await api.version()).version, "v1.0.0");
assert.equal((await api.version()).version, "v1.0.1");
});
test("version queries can resume after a temporary restart connection failure", async () => {
let attempts = 0;
const api = client(async () => {
if (++attempts === 1) throw new TypeError("connection unavailable during restart");
return { ok: true, status: 200, json: async () => ({ version: "v1.0.1" }) };
});
await assert.rejects(api.version(), /connection unavailable/);
assert.equal((await api.version()).version, "v1.0.1");
});
+4 -2
View File
@@ -318,8 +318,9 @@ function schedulePayload(input: ScheduleInput) {
}
export const api = {
// 后端应用版本号(release 时由 ldflags 注入,默认 "dev")。
health: () => get<{ ok: boolean; service: string; version: string }>("/health"),
// 健康检查受可选 Basic Auth 保护且不返回应用名称或版本;版本号仅向已登录管理员提供。
health: () => get<{ ok: boolean }>("/health"),
version: () => http<{ version: string }>("/version", { cache: "no-store" }),
systemMetrics: () => get<SystemMetrics>("/system/metrics"),
// ---- auth ----
@@ -996,6 +997,7 @@ export const api = {
web_search_proxy?: string;
brave_search_api_key?: string;
tavily_search_api_key?: string;
deepseek_search_api_key?: string;
}) => post<{ ok: boolean; error?: string; count?: number; backend?: string }>(`/settings/web-search/test`, patch),
// global_proxy 传空 = 测直连;probe_url 可覆盖默认探测地址(隔离网用)。
+1
View File
@@ -3348,6 +3348,7 @@ export const settings: Settings = {
web_search_backend: "ddgs",
brave_key_set: false,
tavily_key_set: true,
deepseek_search_key_set: false,
web_search_proxy: "",
global_proxy: "",
proxy_pool: [],
+2
View File
@@ -996,9 +996,11 @@ export interface Settings {
web_search_backend: string; // "ddgs" | "brave-free" | "tavily" | "deepseek"
brave_key_set: boolean;
tavily_key_set: boolean;
deepseek_search_key_set: boolean;
// write-only: only sent on PUT to store/clear the key.
brave_search_api_key?: string;
tavily_search_api_key?: string;
deepseek_search_api_key?: string;
// 独立出口代理(http/https/socks5),用于访问搜索端点;与记录流量的 MITM 代理无关。空=直连。
web_search_proxy?: string;
// 全局出口代理(http/https/socks5,可带 user:pass),所有目标流量走它。开启流量捕获时作为