@nite07/websearch-mcp 0.1.4 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +46 -0
- package/README.zh.md +38 -0
- package/dist/live.d.ts +21 -0
- package/dist/live.js +52 -0
- package/dist/main.js +217 -37
- package/dist/sdk.d.ts +21 -0
- package/dist/sdk.js +88 -0
- package/dist/tools.d.ts +2 -1
- package/dist/tools.js +20 -4
- package/dist/ui/embedded.d.ts +20 -0
- package/dist/ui/embedded.js +14 -0
- package/dist/ui/page.d.ts +2 -0
- package/dist/ui/page.html +694 -0
- package/dist/ui/page.js +15 -0
- package/dist/ui/server.d.ts +21 -0
- package/dist/ui/server.js +243 -0
- package/dist/ui/session.d.ts +38 -0
- package/dist/ui/session.js +111 -0
- package/dist/ui/state.d.ts +62 -0
- package/dist/ui/state.js +256 -0
- package/dist/ui/token.d.ts +23 -0
- package/dist/ui/token.js +23 -0
- package/package.json +4 -3
- package/dist/protocol.d.ts +0 -19
- package/dist/protocol.js +0 -125
|
@@ -0,0 +1,694 @@
|
|
|
1
|
+
<!doctype html>
|
|
2
|
+
<html lang="zh-CN">
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="utf-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
6
|
+
<title>websearch-mcp · 设置</title>
|
|
7
|
+
<style>
|
|
8
|
+
:root {
|
|
9
|
+
color-scheme: light dark;
|
|
10
|
+
--bg: #f6f7f9; --panel: #fff; --ink: #16181d; --muted: #626a78; --line: #dfe3ea;
|
|
11
|
+
--accent: #2f6fed; --accent-ink: #fff; --ok: #12855b; --warn: #b06a00; --bad: #c22a2a;
|
|
12
|
+
--chip: #eef1f6; --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
|
|
13
|
+
}
|
|
14
|
+
@media (prefers-color-scheme: dark) {
|
|
15
|
+
:root {
|
|
16
|
+
--bg: #111318; --panel: #171a21; --ink: #e7eaf0; --muted: #9aa3b2; --line: #262b35;
|
|
17
|
+
--accent: #6f9dff; --accent-ink: #0b1220; --ok: #4cc79a; --warn: #e0a44a; --bad: #f27a7a;
|
|
18
|
+
--chip: #1e222b; --shadow: none;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
* { box-sizing: border-box; }
|
|
22
|
+
body {
|
|
23
|
+
margin: 0; background: var(--bg); color: var(--ink);
|
|
24
|
+
font: 14px/1.55 ui-sans-serif, system-ui, -apple-system, "Segoe UI", "PingFang SC", "Noto Sans CJK SC", sans-serif;
|
|
25
|
+
}
|
|
26
|
+
code, .mono { font-family: ui-monospace, SFMono-Regular, "JetBrains Mono", Menlo, monospace; font-size: .92em; }
|
|
27
|
+
.bar {
|
|
28
|
+
position: sticky; top: 0; z-index: 20; display: flex; flex-wrap: wrap; gap: .75rem 1rem;
|
|
29
|
+
align-items: center; justify-content: space-between; padding: .75rem 1.25rem;
|
|
30
|
+
background: color-mix(in srgb, var(--panel) 92%, transparent); backdrop-filter: blur(8px);
|
|
31
|
+
border-bottom: 1px solid var(--line);
|
|
32
|
+
}
|
|
33
|
+
.bar .ident { display: flex; align-items: center; gap: .5rem; font-size: 1rem; }
|
|
34
|
+
.bar .path { color: var(--muted); font-size: .82rem; flex: 1 1 18rem; }
|
|
35
|
+
.bar .actions { display: flex; gap: .5rem; align-items: center; }
|
|
36
|
+
.chip { background: var(--chip); border-radius: 999px; padding: .1rem .55rem; font-size: .78rem; color: var(--muted); }
|
|
37
|
+
.chip.warn { color: var(--warn); border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent); background: transparent; }
|
|
38
|
+
.dot { width: .55rem; height: .55rem; border-radius: 50%; background: transparent; border: 1px solid var(--line); }
|
|
39
|
+
.dot.on { background: var(--warn); border-color: var(--warn); }
|
|
40
|
+
button {
|
|
41
|
+
font: inherit; border-radius: .5rem; border: 1px solid var(--line); background: var(--panel);
|
|
42
|
+
color: var(--ink); padding: .38rem .8rem; cursor: pointer;
|
|
43
|
+
}
|
|
44
|
+
button:hover { border-color: var(--muted); }
|
|
45
|
+
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
|
|
46
|
+
button.primary:disabled { opacity: .55; cursor: progress; }
|
|
47
|
+
button.ghost { background: transparent; }
|
|
48
|
+
button.small { padding: .18rem .5rem; font-size: .8rem; }
|
|
49
|
+
main { max-width: 1180px; margin: 0 auto; padding: 1.25rem 1.25rem 4rem; }
|
|
50
|
+
section { margin-bottom: 2rem; }
|
|
51
|
+
h2 { font-size: 1rem; margin: 0 0 .75rem; letter-spacing: .01em; }
|
|
52
|
+
h3 { font-size: .95rem; margin: 0; }
|
|
53
|
+
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: .75rem; box-shadow: var(--shadow); }
|
|
54
|
+
.grid { display: grid; gap: .75rem 1rem; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); }
|
|
55
|
+
.field { display: flex; flex-direction: column; gap: .25rem; min-width: 0; }
|
|
56
|
+
.field > span { font-size: .8rem; color: var(--muted); }
|
|
57
|
+
.field input[type=text], .field input[type=number], .field input[type=password], .field select {
|
|
58
|
+
font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line);
|
|
59
|
+
border-radius: .45rem; padding: .35rem .5rem; min-width: 0;
|
|
60
|
+
}
|
|
61
|
+
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
|
|
62
|
+
.field.invalid input, .field.invalid select { border-color: var(--bad); }
|
|
63
|
+
.check { display: flex; align-items: center; gap: .45rem; font-size: .86rem; }
|
|
64
|
+
.pad { padding: 1rem; }
|
|
65
|
+
details.panel { padding: 0; }
|
|
66
|
+
details.panel > summary {
|
|
67
|
+
cursor: pointer; padding: .7rem 1rem; font-weight: 600; font-size: .9rem;
|
|
68
|
+
display: flex; align-items: center; gap: .5rem;
|
|
69
|
+
}
|
|
70
|
+
details.panel > div { padding: 0 1rem 1rem; }
|
|
71
|
+
.toolbar { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; margin-bottom: .75rem; }
|
|
72
|
+
.toolbar input[type=search] {
|
|
73
|
+
font: inherit; padding: .35rem .6rem; border-radius: .45rem; border: 1px solid var(--line);
|
|
74
|
+
background: var(--panel); color: var(--ink); min-width: 14rem;
|
|
75
|
+
}
|
|
76
|
+
.cards { display: flex; flex-direction: column; gap: .6rem; }
|
|
77
|
+
.card { background: var(--panel); border: 1px solid var(--line); border-radius: .7rem; box-shadow: var(--shadow); overflow: hidden; }
|
|
78
|
+
.card.on { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
|
|
79
|
+
.card-head { display: flex; gap: .75rem; align-items: flex-start; padding: .7rem .9rem; }
|
|
80
|
+
.card-head .titles { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; gap: .3rem; }
|
|
81
|
+
.card-head .titles code { color: var(--muted); font-size: .78rem; }
|
|
82
|
+
.badges { display: flex; flex-wrap: wrap; gap: .3rem; }
|
|
83
|
+
.badge { font-size: .74rem; padding: .05rem .5rem; border-radius: 999px; background: var(--chip); color: var(--muted); white-space: nowrap; }
|
|
84
|
+
.badge.ok { color: var(--ok); }
|
|
85
|
+
.badge.bad { color: var(--bad); }
|
|
86
|
+
.badge.warn { color: var(--warn); }
|
|
87
|
+
.switch { position: relative; display: inline-flex; width: 2.1rem; height: 1.2rem; flex: 0 0 auto; margin-top: .1rem; }
|
|
88
|
+
.switch input { position: absolute; opacity: 0; }
|
|
89
|
+
.switch span { position: absolute; inset: 0; border-radius: 999px; background: var(--line); transition: background .15s; }
|
|
90
|
+
.switch span::after {
|
|
91
|
+
content: ""; position: absolute; top: .15rem; left: .15rem; width: .9rem; height: .9rem;
|
|
92
|
+
border-radius: 50%; background: var(--panel); transition: transform .15s;
|
|
93
|
+
}
|
|
94
|
+
.switch input:checked + span { background: var(--accent); }
|
|
95
|
+
.switch input:checked + span::after { transform: translateX(.9rem); }
|
|
96
|
+
.card-body { border-top: 1px solid var(--line); padding: .9rem; display: grid; gap: .9rem; }
|
|
97
|
+
.row { display: grid; gap: .75rem 1rem; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
|
|
98
|
+
.cred { display: flex; flex-direction: column; gap: .4rem; }
|
|
99
|
+
.cred-head { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }
|
|
100
|
+
.cred-input { display: flex; flex-wrap: wrap; gap: .4rem; }
|
|
101
|
+
.cred-input input { flex: 1 1 18rem; font: inherit; color: var(--ink); background: var(--bg); border: 1px solid var(--line); border-radius: .45rem; padding: .35rem .5rem; }
|
|
102
|
+
.note { font-size: .78rem; color: var(--muted); }
|
|
103
|
+
.note a { color: var(--accent); }
|
|
104
|
+
pre.out {
|
|
105
|
+
margin: 0; padding: .6rem .7rem; background: var(--bg); border: 1px solid var(--line);
|
|
106
|
+
border-radius: .5rem; max-height: 16rem; overflow: auto; white-space: pre-wrap; word-break: break-word;
|
|
107
|
+
font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .8rem;
|
|
108
|
+
}
|
|
109
|
+
#issues { margin-bottom: 1.25rem; }
|
|
110
|
+
#issues .panel { border-color: var(--bad); }
|
|
111
|
+
#issues ul { margin: .4rem 0 0; padding-left: 1.1rem; }
|
|
112
|
+
#issues li { font-size: .85rem; }
|
|
113
|
+
#banner .panel { border-color: var(--warn); }
|
|
114
|
+
.toast {
|
|
115
|
+
position: fixed; left: 50%; bottom: 1.5rem; transform: translate(-50%, 1rem); opacity: 0;
|
|
116
|
+
background: var(--ink); color: var(--bg); padding: .5rem 1rem; border-radius: .5rem;
|
|
117
|
+
font-size: .85rem; pointer-events: none; transition: opacity .18s, transform .18s; z-index: 40;
|
|
118
|
+
max-width: min(40rem, 90vw);
|
|
119
|
+
}
|
|
120
|
+
.toast.show { opacity: 1; transform: translate(-50%, 0); }
|
|
121
|
+
footer { max-width: 1180px; margin: 0 auto; padding: 0 1.25rem 2.5rem; color: var(--muted); font-size: .8rem; }
|
|
122
|
+
.hidden { display: none !important; }
|
|
123
|
+
</style>
|
|
124
|
+
</head>
|
|
125
|
+
<body>
|
|
126
|
+
<header class="bar">
|
|
127
|
+
<div class="ident">
|
|
128
|
+
<strong>websearch-mcp</strong>
|
|
129
|
+
<span class="chip" id="version">—</span>
|
|
130
|
+
<span class="chip mono" id="revision">—</span>
|
|
131
|
+
<span class="chip warn" id="auth" hidden></span>
|
|
132
|
+
<span class="dot" id="dirty" title="有未保存的更改"></span>
|
|
133
|
+
</div>
|
|
134
|
+
<div class="path mono" id="configPath">—</div>
|
|
135
|
+
<div class="actions">
|
|
136
|
+
<button class="ghost" id="lang">EN</button>
|
|
137
|
+
<button class="ghost" id="reload">重新加载</button>
|
|
138
|
+
<button class="primary" id="save">保存</button>
|
|
139
|
+
</div>
|
|
140
|
+
</header>
|
|
141
|
+
<div id="issues"></div>
|
|
142
|
+
<div id="banner"></div>
|
|
143
|
+
<main id="app"></main>
|
|
144
|
+
<footer id="foot"></footer>
|
|
145
|
+
<div class="toast" id="toast"></div>
|
|
146
|
+
<script type="module">
|
|
147
|
+
const I18N = {
|
|
148
|
+
zh: {
|
|
149
|
+
save: '保存', reload: '重新加载', dirty: '有未保存的更改', clean: '已保存',
|
|
150
|
+
saved: '已保存并热重载', savedNoChange: '没有需要保存的更改', reloaded: '已从磁盘重新加载',
|
|
151
|
+
loadError: '磁盘上的配置无法解析,页面暂时显示默认值;保存后即覆盖。',
|
|
152
|
+
sessionExpired: '会话已过期,请用 --print-url 重新打开链接。',
|
|
153
|
+
strategy: '检索策略', mode: '模式', parallel: '并行(全部启用后端)', single: '单后端',
|
|
154
|
+
singleBackend: '单后端选择', singleBackendHint: 'single 模式下唯一使用的后端',
|
|
155
|
+
maxResults: '结果上限', concurrency: '并发', overallTimeout: '总超时 (ms)', backendTimeout: '超时 (ms)',
|
|
156
|
+
query: '查询默认值', language: '语言', region: '地区', recency: '时效', dedup: '去重', fusion: '融合',
|
|
157
|
+
safeSearch: '安全搜索', includeDomains: '包含域名', excludeDomains: '排除域名',
|
|
158
|
+
none: '不限制', any: '不限', day: '一天', week: '一周', month: '一月', year: '一年',
|
|
159
|
+
url: '按 URL', urlTitle: '按 URL + 标题', rrf: 'RRF', ordered: '按顺序拼接',
|
|
160
|
+
advanced: '高级', retry: '重试', cache: '缓存', breaker: '熔断', diagnostics: '诊断记录',
|
|
161
|
+
browser: '共享浏览器', tools: '输出', compactDefault: '默认精简输出(只返回 URL / 标题 / 日期)',
|
|
162
|
+
webSearchPlus: '启用 web_search_plus 兼容字段',
|
|
163
|
+
enabled: '启用', retryEnabled: '启用重试', statuses: '重试状态码', maxRetries: '最多重试次数',
|
|
164
|
+
initialDelay: '初始延迟 (ms)', maxDelay: '最大延迟 (ms)', networkErrors: '网络错误也重试', timeouts: '超时也重试',
|
|
165
|
+
cacheEnabled: '启用缓存', ttl: 'TTL (秒)', maxEntries: '条目上限',
|
|
166
|
+
breakerEnabled: '启用熔断', threshold: '失败阈值', cooldown: '冷却 (ms)',
|
|
167
|
+
diagEnabled: '记录诊断', diagEntries: '记录条数',
|
|
168
|
+
browserEndpoint: '浏览器端点', statePath: '登录态文件', browserTimeout: '浏览器超时 (ms)', maxReads: '并发读取上限',
|
|
169
|
+
browserToken: '浏览器令牌', browserTokenNote: '保护浏览器端点,不是站点账号凭证',
|
|
170
|
+
backends: '后端', filter: '按名称或 id 过滤', onlyEnabled: '只看已启用',
|
|
171
|
+
summary: (on, total, missing) => `已启用 ${on} / ${total},缺凭证 ${missing}`,
|
|
172
|
+
credential: '凭证', credentialSave: '保存凭证', credentialClear: '清除',
|
|
173
|
+
credNone: '未配置', credFile: '已存于文件', credEnv: '由环境变量提供', credRequired: '无需凭证',
|
|
174
|
+
credEnvWins: (ref) => `环境变量 ${ref} 优先,页面写入的值不会生效。`,
|
|
175
|
+
credPlaceholder: '粘贴凭证值',
|
|
176
|
+
test: '测试', testing: '测试中…', probe: '测试查询', result: '结果',
|
|
177
|
+
tier: { free: '免费', allowance: '赠送额度', paid: '付费', trial: '试用', account: '账号会话', 'self-hosted': '自建' },
|
|
178
|
+
docs: '文档', pricing: '定价', expand: '展开', collapse: '收起',
|
|
179
|
+
issues: '配置未通过校验', noChange: '—',
|
|
180
|
+
foot: (host, port) => `监听 ${host}:${port} · 保存后正在运行的 MCP 进程会在约 150ms 内重读配置`,
|
|
181
|
+
companionOn: '容器副本同步至',
|
|
182
|
+
noAuth: '未鉴权',
|
|
183
|
+
cleared: '凭证已清除', credSaved: '凭证已保存', missingCred: '缺凭证',
|
|
184
|
+
credBrowserNote: '读取走共享浏览器:端点与令牌在「共享浏览器」一节填写。',
|
|
185
|
+
credAnonymousNote: '当前模式不需要凭证;切换到 key / oauth / session 模式后这里会出现凭证输入框。',
|
|
186
|
+
},
|
|
187
|
+
en: {
|
|
188
|
+
save: 'Save', reload: 'Reload', dirty: 'Unsaved changes', clean: 'Saved',
|
|
189
|
+
saved: 'Saved and hot-reloaded', savedNoChange: 'Nothing to save', reloaded: 'Reloaded from disk',
|
|
190
|
+
loadError: 'The file on disk does not parse, so defaults are shown; saving replaces it.',
|
|
191
|
+
sessionExpired: 'Session expired. Open the link from --print-url again.',
|
|
192
|
+
strategy: 'Strategy', mode: 'Mode', parallel: 'Parallel (every enabled backend)', single: 'Single backend',
|
|
193
|
+
singleBackend: 'Single backend', singleBackendHint: 'The only backend used in single mode',
|
|
194
|
+
maxResults: 'Max results', concurrency: 'Concurrency', overallTimeout: 'Overall timeout (ms)', backendTimeout: 'Timeout (ms)',
|
|
195
|
+
query: 'Query defaults', language: 'Language', region: 'Region', recency: 'Recency', dedup: 'Dedup', fusion: 'Fusion',
|
|
196
|
+
safeSearch: 'Safe search', includeDomains: 'Include domains', excludeDomains: 'Exclude domains',
|
|
197
|
+
none: 'Any', any: 'Any', day: 'Day', week: 'Week', month: 'Month', year: 'Year',
|
|
198
|
+
url: 'By URL', urlTitle: 'By URL + title', rrf: 'RRF', ordered: 'Ordered concat',
|
|
199
|
+
advanced: 'Advanced', retry: 'Retry', cache: 'Cache', breaker: 'Breaker', diagnostics: 'Diagnostics',
|
|
200
|
+
browser: 'Shared browser', tools: 'Output', compactDefault: 'Compact output by default (URL / title / date only)',
|
|
201
|
+
webSearchPlus: 'Expose the web_search_plus compatibility fields',
|
|
202
|
+
enabled: 'Enabled', retryEnabled: 'Retry enabled', statuses: 'Retry statuses', maxRetries: 'Max retries',
|
|
203
|
+
initialDelay: 'Initial delay (ms)', maxDelay: 'Max delay (ms)', networkErrors: 'Retry network errors', timeouts: 'Retry timeouts',
|
|
204
|
+
cacheEnabled: 'Cache enabled', ttl: 'TTL (seconds)', maxEntries: 'Max entries',
|
|
205
|
+
breakerEnabled: 'Breaker enabled', threshold: 'Failure threshold', cooldown: 'Cooldown (ms)',
|
|
206
|
+
diagEnabled: 'Record diagnostics', diagEntries: 'Entries kept',
|
|
207
|
+
browserEndpoint: 'Browser endpoint', statePath: 'Session state file', browserTimeout: 'Browser timeout (ms)', maxReads: 'Concurrent reads',
|
|
208
|
+
browserToken: 'Browser token', browserTokenNote: 'Protects the browser endpoint, not a site account',
|
|
209
|
+
backends: 'Backends', filter: 'Filter by name or id', onlyEnabled: 'Enabled only',
|
|
210
|
+
summary: (on, total, missing) => `${on} of ${total} enabled, ${missing} missing a credential`,
|
|
211
|
+
credential: 'Credential', credentialSave: 'Save credential', credentialClear: 'Clear',
|
|
212
|
+
credNone: 'Not set', credFile: 'Stored in file', credEnv: 'Provided by environment', credRequired: 'Not required',
|
|
213
|
+
credEnvWins: (ref) => `${ref} wins over the file, so a value saved here is ignored.`,
|
|
214
|
+
credPlaceholder: 'Paste the credential value',
|
|
215
|
+
test: 'Test', testing: 'Testing…', probe: 'Probe query', result: 'Result',
|
|
216
|
+
tier: { free: 'free', allowance: 'allowance', paid: 'paid', trial: 'trial', account: 'account session', 'self-hosted': 'self-hosted' },
|
|
217
|
+
docs: 'Docs', pricing: 'Pricing', expand: 'Expand', collapse: 'Collapse',
|
|
218
|
+
issues: 'The configuration did not validate', noChange: '—',
|
|
219
|
+
foot: (host, port) => `Listening on ${host}:${port} · a running MCP process re-reads the file within about 150ms`,
|
|
220
|
+
companionOn: 'container copy kept in sync at',
|
|
221
|
+
noAuth: 'no auth',
|
|
222
|
+
cleared: 'Credential cleared', credSaved: 'Credential saved', missingCred: 'missing credential',
|
|
223
|
+
credBrowserNote: 'Reads go through the shared browser: set its endpoint and token under Shared browser.',
|
|
224
|
+
credAnonymousNote: 'This mode needs no credential; a key, oauth or session mode adds the field here.',
|
|
225
|
+
},
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
const state = {
|
|
229
|
+
snap: null, draft: null, dirty: false,
|
|
230
|
+
lang: localStorage.getItem('wsm.lang') || 'zh',
|
|
231
|
+
filter: '', onlyEnabled: false, open: new Set(), closed: new Set(), results: {}, issues: [], probeQuery: '', compactByDefault: false,
|
|
232
|
+
};
|
|
233
|
+
const t = (key, ...args) => {
|
|
234
|
+
const table = I18N[state.lang] || I18N.zh;
|
|
235
|
+
const value = table[key] ?? I18N.zh[key] ?? key;
|
|
236
|
+
return typeof value === 'function' ? value(...args) : value;
|
|
237
|
+
};
|
|
238
|
+
const local = (field) => (field && typeof field === 'object' ? (field[state.lang] ?? field.en ?? '') : String(field ?? ''));
|
|
239
|
+
const clone = (value) => structuredClone(value);
|
|
240
|
+
|
|
241
|
+
function h(tag, props = {}, ...kids) {
|
|
242
|
+
const node = document.createElement(tag);
|
|
243
|
+
for (const [key, value] of Object.entries(props)) {
|
|
244
|
+
if (value === undefined || value === null || value === false) continue;
|
|
245
|
+
if (key === 'class') node.className = value;
|
|
246
|
+
else if (key === 'dataset') Object.assign(node.dataset, value);
|
|
247
|
+
else if (key === 'html') node.innerHTML = value;
|
|
248
|
+
else if (key.startsWith('on') && typeof value === 'function') node.addEventListener(key.slice(2).toLowerCase(), value);
|
|
249
|
+
else if (value === true) node.setAttribute(key, '');
|
|
250
|
+
else node.setAttribute(key, String(value));
|
|
251
|
+
}
|
|
252
|
+
for (const kid of kids.flat(4)) {
|
|
253
|
+
if (kid === undefined || kid === null || kid === false) continue;
|
|
254
|
+
node.append(kid && kid.nodeType ? kid : document.createTextNode(String(kid)));
|
|
255
|
+
}
|
|
256
|
+
return node;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const getPath = (object, path) => path.split('.').reduce((acc, key) => (acc === undefined || acc === null ? undefined : acc[key]), object);
|
|
260
|
+
function setPath(object, path, value) {
|
|
261
|
+
const keys = path.split('.');
|
|
262
|
+
const last = keys.pop();
|
|
263
|
+
let cursor = object;
|
|
264
|
+
for (const key of keys) {
|
|
265
|
+
if (cursor[key] === undefined || cursor[key] === null || typeof cursor[key] !== 'object') cursor[key] = {};
|
|
266
|
+
cursor = cursor[key];
|
|
267
|
+
}
|
|
268
|
+
cursor[last] = value;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function toast(message, tone = '') {
|
|
272
|
+
const node = document.getElementById('toast');
|
|
273
|
+
node.textContent = message;
|
|
274
|
+
node.className = `toast show ${tone}`;
|
|
275
|
+
clearTimeout(toast.timer);
|
|
276
|
+
toast.timer = setTimeout(() => { node.className = 'toast'; }, 3200);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function markDirty() {
|
|
280
|
+
state.dirty = true;
|
|
281
|
+
document.getElementById('dirty').classList.add('on');
|
|
282
|
+
document.getElementById('dirty').title = t('dirty');
|
|
283
|
+
}
|
|
284
|
+
function markClean() {
|
|
285
|
+
state.dirty = false;
|
|
286
|
+
document.getElementById('dirty').classList.remove('on');
|
|
287
|
+
document.getElementById('dirty').title = t('clean');
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function field(spec) {
|
|
291
|
+
const wrap = h('label', { class: 'field', dataset: { path: spec.path || '' } });
|
|
292
|
+
wrap.append(h('span', {}, spec.label));
|
|
293
|
+
const value = spec.path ? getPath(state.draft, spec.path) : spec.value;
|
|
294
|
+
let input;
|
|
295
|
+
if (spec.type === 'select') {
|
|
296
|
+
input = h('select', { dataset: { kind: 'select' } },
|
|
297
|
+
spec.options.map(([optionValue, optionLabel]) => h('option', { value: optionValue, selected: String(value) === String(optionValue) }, optionLabel)));
|
|
298
|
+
} else if (spec.type === 'check') {
|
|
299
|
+
wrap.className = 'check';
|
|
300
|
+
input = h('input', { type: 'checkbox', checked: value === true, dataset: { kind: 'check' } });
|
|
301
|
+
wrap.replaceChildren(input, h('span', {}, spec.label));
|
|
302
|
+
return wrap;
|
|
303
|
+
} else if (spec.type === 'list') {
|
|
304
|
+
input = h('input', { type: 'text', value: Array.isArray(value) ? value.join(', ') : '', placeholder: spec.placeholder || '', dataset: { kind: 'list' } });
|
|
305
|
+
} else if (spec.type === 'number') {
|
|
306
|
+
input = h('input', { type: 'number', value: value ?? '', min: spec.min, max: spec.max, step: spec.step || 1, dataset: { kind: 'number' } });
|
|
307
|
+
} else {
|
|
308
|
+
input = h('input', { type: spec.secret ? 'password' : 'text', value: value ?? '', placeholder: spec.placeholder || '', autocomplete: 'off', dataset: { kind: 'text' } });
|
|
309
|
+
}
|
|
310
|
+
wrap.append(input);
|
|
311
|
+
if (spec.hint) wrap.append(h('span', { class: 'note' }, spec.hint));
|
|
312
|
+
return wrap;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function bindFields(root) {
|
|
316
|
+
for (const host of root.querySelectorAll('[data-path]')) {
|
|
317
|
+
const path = host.dataset.path;
|
|
318
|
+
if (!path) continue;
|
|
319
|
+
const input = host.matches('input, select') ? host : host.querySelector('input, select');
|
|
320
|
+
if (!input) continue;
|
|
321
|
+
const kind = input.dataset.kind || 'text';
|
|
322
|
+
const apply = () => {
|
|
323
|
+
let value;
|
|
324
|
+
if (kind === 'check') value = input.checked;
|
|
325
|
+
else if (kind === 'number') { value = Number(input.value); if (!Number.isFinite(value)) return; }
|
|
326
|
+
else if (kind === 'list') value = input.value.split(',').map(item => item.trim()).filter(Boolean);
|
|
327
|
+
else value = input.value;
|
|
328
|
+
setPath(state.draft, path, value);
|
|
329
|
+
markDirty();
|
|
330
|
+
if (path.endsWith('.mode')) onModeChange(path.slice(0, -5), input.value);
|
|
331
|
+
if (path.endsWith('.enabled')) render();
|
|
332
|
+
};
|
|
333
|
+
input.addEventListener(kind === 'select' || kind === 'check' ? 'change' : 'input', apply);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function onModeChange(backendPath, mode) {
|
|
338
|
+
const backendId = backendPath.split('.').pop();
|
|
339
|
+
const definition = state.snap.catalog.find(item => item.id === backendId);
|
|
340
|
+
const previous = definition?.modes.find(item => item.endpoint === getPath(state.draft, `${backendPath}.endpoint`));
|
|
341
|
+
const next = definition?.modes.find(item => item.mode === mode);
|
|
342
|
+
if (next && (!previous || getPath(state.draft, `${backendPath}.endpoint`) === previous.endpoint)) {
|
|
343
|
+
setPath(state.draft, `${backendPath}.endpoint`, next.endpoint);
|
|
344
|
+
}
|
|
345
|
+
render();
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
function overview() {
|
|
349
|
+
const strategy = h('div', { class: 'panel pad' },
|
|
350
|
+
h('div', { class: 'grid' },
|
|
351
|
+
field({ path: 'mode', type: 'select', label: t('mode'), options: [['parallel', t('parallel')], ['single', t('single')]] }),
|
|
352
|
+
field({ path: 'singleBackend', type: 'select', label: t('singleBackend'), hint: t('singleBackendHint'), options: state.snap.catalog.map(item => [item.id, item.title]) }),
|
|
353
|
+
field({ path: 'maxResults', type: 'number', label: t('maxResults'), min: 1, max: 50 }),
|
|
354
|
+
field({ path: 'concurrency', type: 'number', label: t('concurrency'), min: 1, max: 20 }),
|
|
355
|
+
field({ path: 'overallTimeoutMs', type: 'number', label: t('overallTimeout'), min: 1000, max: 180000, step: 1000 }),
|
|
356
|
+
));
|
|
357
|
+
|
|
358
|
+
const query = h('div', { class: 'panel pad' },
|
|
359
|
+
h('div', { class: 'grid' },
|
|
360
|
+
field({ path: 'language', type: 'select', label: t('language'), options: [['', t('none')], ...(state.snap.languages || []).map(code => [code, code])] }),
|
|
361
|
+
field({ path: 'region', type: 'select', label: t('region'), options: [['', t('none')], ...(state.snap.regions || []).map(code => [code, code])] }),
|
|
362
|
+
field({ path: 'recency', type: 'select', label: t('recency'), options: [['any', t('any')], ['day', t('day')], ['week', t('week')], ['month', t('month')], ['year', t('year')]] }),
|
|
363
|
+
field({ path: 'dedup', type: 'select', label: t('dedup'), options: [['url', t('url')], ['url-title', t('urlTitle')]] }),
|
|
364
|
+
field({ path: 'fusion', type: 'select', label: t('fusion'), options: [['rrf', t('rrf')], ['ordered', t('ordered')]] }),
|
|
365
|
+
field({ path: 'safeSearch', type: 'check', label: t('safeSearch') }),
|
|
366
|
+
field({ path: 'includeDomains', type: 'list', label: t('includeDomains'), placeholder: 'example.com, docs.example.org' }),
|
|
367
|
+
field({ path: 'excludeDomains', type: 'list', label: t('excludeDomains'), placeholder: 'spam.example' }),
|
|
368
|
+
));
|
|
369
|
+
|
|
370
|
+
const retry = details('retry', t('retry'),
|
|
371
|
+
field({ path: 'retry.enabled', type: 'check', label: t('retryEnabled') }),
|
|
372
|
+
field({ path: 'retry.maxRetries', type: 'number', label: t('maxRetries'), min: 0, max: 5 }),
|
|
373
|
+
field({ path: 'retry.initialDelayMs', type: 'number', label: t('initialDelay'), min: 0, max: 60000 }),
|
|
374
|
+
field({ path: 'retry.maxDelayMs', type: 'number', label: t('maxDelay'), min: 0, max: 120000 }),
|
|
375
|
+
field({ path: 'retry.statuses', type: 'list', label: t('statuses'), placeholder: '408, 429, 500' }),
|
|
376
|
+
field({ path: 'retry.networkErrors', type: 'check', label: t('networkErrors') }),
|
|
377
|
+
field({ path: 'retry.timeouts', type: 'check', label: t('timeouts') }));
|
|
378
|
+
|
|
379
|
+
const cache = details('cache', t('cache'),
|
|
380
|
+
field({ path: 'cache.enabled', type: 'check', label: t('cacheEnabled') }),
|
|
381
|
+
field({ path: 'cache.ttlSeconds', type: 'number', label: t('ttl'), min: 1, max: 86400 }),
|
|
382
|
+
field({ path: 'cache.maxEntries', type: 'number', label: t('maxEntries'), min: 1, max: 1000 }));
|
|
383
|
+
|
|
384
|
+
const breaker = details('breaker', t('breaker'),
|
|
385
|
+
field({ path: 'breaker.enabled', type: 'check', label: t('breakerEnabled') }),
|
|
386
|
+
field({ path: 'breaker.threshold', type: 'number', label: t('threshold'), min: 1, max: 100 }),
|
|
387
|
+
field({ path: 'breaker.cooldownMs', type: 'number', label: t('cooldown'), min: 1000, max: 3600000 }));
|
|
388
|
+
|
|
389
|
+
const diagnostics = details('diagnostics', t('diagnostics'),
|
|
390
|
+
field({ path: 'diagnostics.enabled', type: 'check', label: t('diagEnabled') }),
|
|
391
|
+
field({ path: 'diagnostics.maxEntries', type: 'number', label: t('diagEntries'), min: 1, max: 500 }));
|
|
392
|
+
|
|
393
|
+
const browser = details('browser', t('browser'),
|
|
394
|
+
field({ path: 'browser.endpoint', type: 'text', label: t('browserEndpoint'), placeholder: 'ws://127.0.0.1:3030/ws' }),
|
|
395
|
+
field({ path: 'browser.statePath', type: 'text', label: t('statePath'), placeholder: '~/.config/dsh-search/browser/state.json' }),
|
|
396
|
+
field({ path: 'browser.timeoutMs', type: 'number', label: t('browserTimeout'), min: 1000, max: 120000 }),
|
|
397
|
+
field({ path: 'browser.maxConcurrentReads', type: 'number', label: t('maxReads'), min: 1, max: 8 }),
|
|
398
|
+
credentialRow('browser', state.snap.browser, t('browserToken'), t('browserTokenNote')));
|
|
399
|
+
|
|
400
|
+
const tools = details('tools', t('tools'),
|
|
401
|
+
topLevelCheck(t('compactDefault')),
|
|
402
|
+
field({ path: 'tools.webSearchPlus', type: 'check', label: t('webSearchPlus') }));
|
|
403
|
+
|
|
404
|
+
return h('section', {},
|
|
405
|
+
h('h2', {}, t('strategy')),
|
|
406
|
+
h('div', { class: 'cards' }, strategy),
|
|
407
|
+
h('h2', { style: 'margin-top:1.25rem' }, t('query')),
|
|
408
|
+
h('div', { class: 'cards' }, query),
|
|
409
|
+
h('h2', { style: 'margin-top:1.25rem' }, t('advanced')),
|
|
410
|
+
h('div', { class: 'cards' }, retry, cache, breaker, diagnostics, browser, tools));
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
function details(key, title, ...children) {
|
|
414
|
+
const node = h('details', { class: 'panel', open: state.open.has(key) },
|
|
415
|
+
h('summary', {}, title), h('div', { class: 'grid' }, children));
|
|
416
|
+
node.addEventListener('toggle', () => { if (node.open) state.open.add(key); else state.open.delete(key); });
|
|
417
|
+
return node;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
function topLevelCheck(label) {
|
|
421
|
+
const input = h('input', { type: 'checkbox', checked: state.compactByDefault === true });
|
|
422
|
+
input.addEventListener('change', () => { state.compactByDefault = input.checked; markDirty(); });
|
|
423
|
+
return h('label', { class: 'check', style: 'align-self:end' }, input, h('span', {}, label));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function credentialRow(id, credential, title, note) {
|
|
427
|
+
const input = h('input', { type: 'password', placeholder: t('credPlaceholder'), autocomplete: 'new-password' });
|
|
428
|
+
const save = h('button', { class: 'small', type: 'button' }, t('credentialSave'));
|
|
429
|
+
const clear = h('button', { class: 'small ghost', type: 'button' }, t('credentialClear'));
|
|
430
|
+
const scope = id === 'browser' ? { scope: 'browser' } : { scope: 'backend', id };
|
|
431
|
+
|
|
432
|
+
save.addEventListener('click', async () => {
|
|
433
|
+
const value = input.value.trim();
|
|
434
|
+
if (!value) { toast(t('credPlaceholder')); return; }
|
|
435
|
+
await postCredential({ ...scope, value });
|
|
436
|
+
input.value = '';
|
|
437
|
+
toast(t('credSaved'));
|
|
438
|
+
});
|
|
439
|
+
clear.addEventListener('click', async () => {
|
|
440
|
+
await postCredential({ ...scope, remove: true });
|
|
441
|
+
toast(t('cleared'));
|
|
442
|
+
});
|
|
443
|
+
|
|
444
|
+
return h('div', { class: 'cred', style: 'grid-column:1/-1' },
|
|
445
|
+
h('div', { class: 'cred-head' },
|
|
446
|
+
h('span', { class: 'note' }, `${title} · ${credential.ref}`),
|
|
447
|
+
credentialBadge(credential)),
|
|
448
|
+
note ? h('div', { class: 'note' }, note) : null,
|
|
449
|
+
h('div', { class: 'cred-input' }, input, save, clear),
|
|
450
|
+
credential.source === 'env' ? h('div', { class: 'note' }, t('credEnvWins', credential.ref)) : null);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
function credentialBadge(credential) {
|
|
454
|
+
const map = {
|
|
455
|
+
env: [t('credEnv'), 'warn'],
|
|
456
|
+
file: [t('credFile'), 'ok'],
|
|
457
|
+
none: [t('credNone'), 'bad'],
|
|
458
|
+
};
|
|
459
|
+
const [label, tone] = credential.mode === 'anonymous' || credential.mode === 'browser' ? [t('credRequired'), ''] : (map[credential.source] || map.none);
|
|
460
|
+
return h('span', { class: `badge ${tone}` }, `${label}${credential.length ? ` · ${credential.length}` : ''}`);
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
async function postCredential(body) {
|
|
464
|
+
const response = await fetch('/api/credentials', {
|
|
465
|
+
method: 'POST',
|
|
466
|
+
headers: { 'Content-Type': 'application/json' },
|
|
467
|
+
body: JSON.stringify(body),
|
|
468
|
+
});
|
|
469
|
+
const data = await response.json().catch(() => ({}));
|
|
470
|
+
if (!response.ok) { toast(data.error || t('sessionExpired')); return; }
|
|
471
|
+
await refresh({ keepDraft: true });
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
function backendCard(definition) {
|
|
475
|
+
const backend = state.draft.backends[definition.id];
|
|
476
|
+
const credential = state.snap.credentials.find(item => item.id === definition.id) || { source: 'none', length: 0, ref: '' };
|
|
477
|
+
const mode = definition.modes.find(item => item.mode === backend.mode) || definition.modes[0];
|
|
478
|
+
const cardKey = `card:${definition.id}`;
|
|
479
|
+
const open = state.open.has(cardKey) || (backend.enabled === true && !state.closed.has(cardKey));
|
|
480
|
+
|
|
481
|
+
const switchInput = h('input', { type: 'checkbox', checked: backend.enabled === true, dataset: { path: `backends.${definition.id}.enabled`, kind: 'check' } });
|
|
482
|
+
const badgeOfMode = h('span', { class: 'badge' }, `${backend.mode} · ${local(t('tier')[mode?.tier] || mode?.tier)}`);
|
|
483
|
+
const head = h('div', { class: 'card-head' },
|
|
484
|
+
h('label', { class: 'switch' }, switchInput, h('span')),
|
|
485
|
+
h('div', { class: 'titles' },
|
|
486
|
+
h('h3', {}, definition.title, ' ', h('code', {}, definition.id)),
|
|
487
|
+
h('div', { class: 'badges' }, badgeOfMode, credentialBadge({ ...credential, mode: backend.mode }))),
|
|
488
|
+
h('div', { style: 'display:flex;gap:.4rem' },
|
|
489
|
+
h('button', { class: 'ghost small', type: 'button', onclick: () => testBackend(definition.id) }, t('test')),
|
|
490
|
+
h('button', { class: 'ghost small', type: 'button', onclick: () => {
|
|
491
|
+
if (open) { state.closed.add(cardKey); state.open.delete(cardKey); } else { state.closed.delete(cardKey); state.open.add(cardKey); }
|
|
492
|
+
render();
|
|
493
|
+
} }, open ? t('collapse') : t('expand'))));
|
|
494
|
+
|
|
495
|
+
const body = h('div', { class: 'card-body' },
|
|
496
|
+
h('div', { class: 'row' },
|
|
497
|
+
field({ path: `backends.${definition.id}.mode`, type: 'select', label: t('mode'), options: definition.modes.map(item => [item.mode, `${item.mode} · ${local(t('tier')[item.tier] || item.tier)}`]) }),
|
|
498
|
+
field({ path: `backends.${definition.id}.endpoint`, type: 'text', label: 'endpoint' }),
|
|
499
|
+
field({ path: `backends.${definition.id}.timeoutMs`, type: 'number', label: t('backendTimeout'), min: 1000, max: 120000 }),
|
|
500
|
+
field({ path: `backends.${definition.id}.rateLimitPerMinute`, type: 'number', label: 'rate/min', min: 0, max: 100000 }),
|
|
501
|
+
field({ path: `backends.${definition.id}.dailyAttemptLimit`, type: 'number', label: 'daily', min: 0, max: 1000000 })),
|
|
502
|
+
definition.options.length
|
|
503
|
+
? h('div', { class: 'row' }, definition.options.map(option => optionField(definition.id, option)))
|
|
504
|
+
: null,
|
|
505
|
+
h('div', { class: 'note' },
|
|
506
|
+
mode ? local(mode.limits) : '',
|
|
507
|
+
mode?.pricingUrl ? h('a', { href: mode.pricingUrl, target: '_blank', rel: 'noreferrer' }, ` · ${t('pricing')}`) : null,
|
|
508
|
+
definition.docs ? h('a', { href: definition.docs, target: '_blank', rel: 'noreferrer' }, ` · ${t('docs')}`) : null),
|
|
509
|
+
backend.mode === 'key' || backend.mode === 'oauth' || backend.mode === 'session'
|
|
510
|
+
? credentialRow(definition.id, { ...credential, mode: backend.mode, ref: credential.ref || `DSH_SEARCH_${definition.id.replaceAll('-', '_').toUpperCase()}_KEY` }, t('credential'), mode ? local(mode.credentialShape || { en: '', zh: '' }) : '')
|
|
511
|
+
: h('div', { class: 'note', style: 'grid-column:1/-1' }, backend.mode === 'browser' ? t('credBrowserNote') : t('credAnonymousNote')),
|
|
512
|
+
state.results[definition.id] ? resultBox(definition.id) : null);
|
|
513
|
+
|
|
514
|
+
const card = h('article', { class: `card${backend.enabled ? ' on' : ''}` }, head);
|
|
515
|
+
if (open) card.append(body);
|
|
516
|
+
return card;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function optionField(backendId, option) {
|
|
520
|
+
const spec = {
|
|
521
|
+
path: `backends.${backendId}.options.${option.key}`,
|
|
522
|
+
label: `${local(option.label)} (${option.key})`,
|
|
523
|
+
hint: local(option.help),
|
|
524
|
+
};
|
|
525
|
+
if (option.choices) return field({ ...spec, type: 'select', options: option.choices.map(choice => [choice, choice]) });
|
|
526
|
+
if (option.type === 'boolean') return field({ ...spec, type: 'check' });
|
|
527
|
+
if (option.type === 'number') return field({ ...spec, type: 'number', min: option.min ?? 0, max: option.max ?? 100000 });
|
|
528
|
+
return field({ ...spec, type: 'text' });
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
function resultBox(backendId) {
|
|
532
|
+
const result = state.results[backendId];
|
|
533
|
+
return h('div', {},
|
|
534
|
+
h('div', { class: 'note' }, `${t('result')}${result.pending ? ` · ${t('testing')}` : ''}`),
|
|
535
|
+
h('pre', { class: 'out' }, result.text || result.error || ''));
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
async function testBackend(backendId) {
|
|
539
|
+
const query = state.probeQuery || 'websearch-mcp connectivity test';
|
|
540
|
+
state.results[backendId] = { pending: true, text: t('testing') };
|
|
541
|
+
render();
|
|
542
|
+
try {
|
|
543
|
+
const response = await fetch('/api/test', {
|
|
544
|
+
method: 'POST',
|
|
545
|
+
headers: { 'Content-Type': 'application/json' },
|
|
546
|
+
body: JSON.stringify({ backendId, query }),
|
|
547
|
+
});
|
|
548
|
+
const data = await response.json();
|
|
549
|
+
state.results[backendId] = data.ok
|
|
550
|
+
? { text: `${data.text}\n\n${data.sources.map(source => `- ${source.title || source.url} <${source.url}>`).join('\n')}` }
|
|
551
|
+
: { error: data.error || data.issues?.map(issue => `${issue.path}: ${issue.message}`).join('\n') };
|
|
552
|
+
} catch (error) {
|
|
553
|
+
state.results[backendId] = { error: String(error) };
|
|
554
|
+
}
|
|
555
|
+
render();
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function backendsSection() {
|
|
559
|
+
const enabled = state.snap.catalog.filter(item => state.draft.backends[item.id]?.enabled).length;
|
|
560
|
+
const missing = state.snap.catalog.filter(item => {
|
|
561
|
+
const backend = state.draft.backends[item.id];
|
|
562
|
+
const credential = state.snap.credentials.find(entry => entry.id === item.id);
|
|
563
|
+
return backend?.enabled && backend.mode !== 'anonymous' && backend.mode !== 'browser' && credential?.source === 'none';
|
|
564
|
+
}).length;
|
|
565
|
+
|
|
566
|
+
const probe = h('input', { type: 'search', value: state.probeQuery || '', placeholder: t('probe') });
|
|
567
|
+
probe.addEventListener('input', () => { state.probeQuery = probe.value; });
|
|
568
|
+
const search = h('input', { type: 'search', value: state.filter, placeholder: t('filter') });
|
|
569
|
+
search.addEventListener('input', () => { state.filter = search.value.trim().toLowerCase(); renderBackends(); });
|
|
570
|
+
const only = h('input', { type: 'checkbox', checked: state.onlyEnabled });
|
|
571
|
+
only.addEventListener('change', () => { state.onlyEnabled = only.checked; renderBackends(); });
|
|
572
|
+
|
|
573
|
+
const list = h('div', { class: 'cards', id: 'cards' });
|
|
574
|
+
const summary = h('span', { class: 'chip' }, t('summary', enabled, state.snap.catalog.length, missing));
|
|
575
|
+
const section = h('section', {},
|
|
576
|
+
h('h2', {}, t('backends'), ' ', summary),
|
|
577
|
+
h('div', { class: 'toolbar' }, search, h('label', { class: 'check' }, only, t('onlyEnabled')), probe),
|
|
578
|
+
list);
|
|
579
|
+
fillCards(list);
|
|
580
|
+
return section;
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
function fillCards(list) {
|
|
584
|
+
const catalog = state.snap.catalog.filter(definition => {
|
|
585
|
+
if (state.onlyEnabled && !state.draft.backends[definition.id]?.enabled) return false;
|
|
586
|
+
if (!state.filter) return true;
|
|
587
|
+
return `${definition.id} ${definition.title}`.toLowerCase().includes(state.filter);
|
|
588
|
+
});
|
|
589
|
+
list.replaceChildren(...catalog.map(backendCard));
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function renderBackends() {
|
|
593
|
+
const list = document.getElementById('cards');
|
|
594
|
+
if (list) fillCards(list);
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function renderBanner() {
|
|
598
|
+
const target = document.getElementById('banner');
|
|
599
|
+
const pieces = [];
|
|
600
|
+
if (state.snap.loadError) pieces.push(h('div', { class: 'panel pad' }, h('strong', {}, t('loadError')), h('pre', { class: 'out', style: 'margin-top:.5rem' }, state.snap.loadError)));
|
|
601
|
+
if (state.issues.length) {
|
|
602
|
+
pieces.push(h('div', { class: 'panel pad' }, h('strong', {}, t('issues')),
|
|
603
|
+
h('ul', {}, state.issues.map(issue => h('li', {}, h('code', {}, issue.path || '(config)'), ' — ', issue.message)))));
|
|
604
|
+
}
|
|
605
|
+
target.replaceChildren(...pieces);
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
function render() {
|
|
609
|
+
document.getElementById('version').textContent = state.snap.version || 'dev';
|
|
610
|
+
document.getElementById('revision').textContent = state.snap.revision;
|
|
611
|
+
const authChip = document.getElementById('auth');
|
|
612
|
+
const noAuth = state.snap.auth === 'none';
|
|
613
|
+
authChip.hidden = !noAuth;
|
|
614
|
+
if (noAuth) authChip.textContent = t('noAuth');
|
|
615
|
+
document.getElementById('configPath').textContent = state.snap.configPath;
|
|
616
|
+
document.getElementById('lang').textContent = state.lang === 'zh' ? 'EN' : '中文';
|
|
617
|
+
document.getElementById('reload').textContent = t('reload');
|
|
618
|
+
document.getElementById('save').textContent = t('save');
|
|
619
|
+
const companion = state.snap.companion?.active
|
|
620
|
+
? ` · ${t('companionOn')} ${state.snap.companion.path}`
|
|
621
|
+
: '';
|
|
622
|
+
document.getElementById('foot').textContent = `${t('foot', state.snap.bound?.host ?? '', state.snap.bound?.port ?? '')}${companion}`;
|
|
623
|
+
document.documentElement.lang = state.lang === 'zh' ? 'zh-CN' : 'en';
|
|
624
|
+
renderBanner();
|
|
625
|
+
const app = document.getElementById('app');
|
|
626
|
+
app.replaceChildren(overview(), backendsSection());
|
|
627
|
+
bindFields(app);
|
|
628
|
+
for (const issue of state.issues) {
|
|
629
|
+
const input = app.querySelector(`[data-path="${issue.path}"]`);
|
|
630
|
+
if (input) input.classList.add('invalid');
|
|
631
|
+
}
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
async function refresh({ keepDraft = false } = {}) {
|
|
635
|
+
const response = await fetch('/api/settings');
|
|
636
|
+
if (response.status === 401) { toast(t('sessionExpired')); return; }
|
|
637
|
+
state.snap = await response.json();
|
|
638
|
+
if (!keepDraft) {
|
|
639
|
+
state.draft = clone(state.snap.config);
|
|
640
|
+
state.compactByDefault = state.snap.compactByDefault === true;
|
|
641
|
+
state.issues = [];
|
|
642
|
+
}
|
|
643
|
+
if (state.probeQuery === undefined) state.probeQuery = '';
|
|
644
|
+
render();
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
async function save() {
|
|
648
|
+
const saveButton = document.getElementById('save');
|
|
649
|
+
saveButton.disabled = true;
|
|
650
|
+
try {
|
|
651
|
+
const response = await fetch('/api/settings', {
|
|
652
|
+
method: 'PUT',
|
|
653
|
+
headers: { 'Content-Type': 'application/json' },
|
|
654
|
+
body: JSON.stringify({
|
|
655
|
+
config: state.draft,
|
|
656
|
+
compactByDefault: state.compactByDefault === true,
|
|
657
|
+
expectedRevision: state.snap.revision,
|
|
658
|
+
}),
|
|
659
|
+
});
|
|
660
|
+
const data = await response.json().catch(() => ({}));
|
|
661
|
+
if (response.status === 401) { toast(t('sessionExpired')); return; }
|
|
662
|
+
if (response.status === 409) { toast(data.error || 'conflict'); await refresh(); return; }
|
|
663
|
+
if (!response.ok) {
|
|
664
|
+
state.issues = data.issues || [{ path: '', message: data.error || 'save failed' }];
|
|
665
|
+
render();
|
|
666
|
+
return;
|
|
667
|
+
}
|
|
668
|
+
state.issues = [];
|
|
669
|
+
markClean();
|
|
670
|
+
state.snap.revision = data.revision;
|
|
671
|
+
document.getElementById('revision').textContent = data.revision;
|
|
672
|
+
renderBanner();
|
|
673
|
+
toast(t('saved'));
|
|
674
|
+
await refresh({ keepDraft: true });
|
|
675
|
+
} finally {
|
|
676
|
+
saveButton.disabled = false;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function boot() {
|
|
681
|
+
document.getElementById('save').addEventListener('click', save);
|
|
682
|
+
document.getElementById('reload').addEventListener('click', async () => { state.issues = []; await refresh(); markClean(); toast(t('reloaded')); });
|
|
683
|
+
document.getElementById('lang').addEventListener('click', () => {
|
|
684
|
+
state.lang = state.lang === 'zh' ? 'en' : 'zh';
|
|
685
|
+
localStorage.setItem('wsm.lang', state.lang);
|
|
686
|
+
render();
|
|
687
|
+
});
|
|
688
|
+
refresh();
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
boot();
|
|
692
|
+
</script>
|
|
693
|
+
</body>
|
|
694
|
+
</html>
|