cortico-world-web-search-multi 0.3.0 → 0.5.0
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/CHANGELOG.md +23 -0
- package/README.md +33 -2
- package/dist/webui.js +127 -0
- package/package.json +9 -1
- package/src/config.ts +3 -0
- package/src/definition.ts +2 -2
- package/src/status.ts +181 -0
- package/src/webui.ts +185 -0
- package/src/world.ts +105 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,28 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
- **新增「引擎状态」明细表面板**:`webUi()` 声明 `panels: [{ id: 'engines', getMethods: ['state'] }]`,`invoke('state')` 回 `PanelState`(14 行 + 汇总计数 + `updatedAt` + 当前模式)。每行含状态、说明文字、开关、密钥变量名与上次调用时间戳。
|
|
6
|
+
- **新增浏览器端产物** `src/webui.ts` → `dist/webui.js`(`cortico.webUiClient`)。它零依赖、零 import,宿主用 `import()` 直接加载。面板按 `ctx.interval` 每 5 秒刷新,卸载即停;文案随 `ctx.language` 取 zh / en 两份。
|
|
7
|
+
- **新增构建步骤**:`tsconfig.webui.json`(`types: []`,`include: src/webui.ts`)与 `pnpm build:webui`。`files` 加 `dist`,`.gitignore` 加 `dist/`(`files` 优先,npm 包仍带产物)。
|
|
8
|
+
- `Checkpoint` 增列 `switchOn` / `keyed` / `secretName` / `lastTs`,供明细表直接印出密钥变量名。
|
|
9
|
+
- 面板 id 两侧对齐由测试锁定:`webUi().panels[0].id` 与 `src/webui.ts` 里 `panels` 的键必须同为 `engines`。
|
|
10
|
+
- World id、配置键、工具名、事件与 `describe.json` 不变(新增的 lamps / badges / panels / invoke 都不进自描述三段)。测试 64 条通过。官方 `exportDescribe()` 与 `checkDescribe()` 本机实测:warnings 0,failures 0。
|
|
11
|
+
|
|
12
|
+
## 0.4.0
|
|
13
|
+
|
|
14
|
+
- **新增每家引擎的状态检查点**:`src/status.ts` 落 `ProviderLedger`,14 家逐条给出 `可用 / 匿名可用 / 缺密钥 / 已关闭 / 上次失败` 五态之一。静态部分只读面板开关与密钥存在性,不发请求、不花配额,判定口径与 `usableProviders()` 逐字一致。
|
|
15
|
+
- **被动记录上一次真实调用**:`searchHandler` 每轮把各家成败、错误文本(折行拉平、裁到 64 字符)与命中数写进台账,只留每家最近一次,存内存,重启即清空。
|
|
16
|
+
- **WebUI 露出**:`webUi()` 新增 14 枚徽标(每家一枚,顺序取 `SINGLE_MODE_PRIORITY`)与 3 盏总览灯(`引擎` 可用数与被挡名单、`上次调用` 本轮失败家、`聚合模式` 当前模式)。灯数在宿主 `MODULE_LAMP_MAX`(7) 之内。取配置或读密钥出错时降级成一盏红字提示灯,页面不炸。
|
|
17
|
+
- 未引入 `webUiClient` 构建步骤,因此没有可展开的明细表格。宿主要求声明的 `panels` 必须由产物包提供 `mount` 实现,纯数据面板不支持,故本版不声明 `panels`。
|
|
18
|
+
- World id、配置键、工具名 `web_search_multi`、事件与 `describe.json` 全部不变(tools / events / prompts 三段无改动,无需重生成)。搜索行为不变,新增测试 17 条。
|
|
19
|
+
|
|
20
|
+
## 0.3.1
|
|
21
|
+
|
|
22
|
+
- **显示名改为「聚合搜索」**:新增 `cortico.displayName` 与 `cortico.locales`(zh 聚合搜索 / en Aggregated Search)。`WorldDefinition.label` 与 `webUi().label` 改读新常量 `WEB_SEARCH_LABEL`,此前两处都用 id `web-search-multi`,市场与宿主 World 列表显示的是包名派生的英文 id。
|
|
23
|
+
- World id、配置键 `worlds.web-search-multi.*`、工具名 `web_search_multi` 与事件 `web-search-multi.started` 全部不变,已有配置与提示词无需迁移。
|
|
24
|
+
- 搜索行为无改动。
|
|
25
|
+
|
|
3
26
|
## 0.3.0
|
|
4
27
|
|
|
5
28
|
- **环境提示词三项变量化**:`src/ENV_PROMPT.md` 里「可用引擎范围」「已启用引擎的搜索方案(调用顺序/组合方式/结果汇总/失败回退)」「搜索模式」改为模板变量 `{{web_search_multi.availableProviders}}` / `{{web_search_multi.searchPlan}}` / `{{web_search_multi.searchMode}}`,由 `envPromptVars()` 按当前配置现算。可用范围按面板开关 + 实际密钥判定,不再是与配置脱钩的静态列表。
|
package/README.md
CHANGED
|
@@ -119,6 +119,34 @@ bocha → tavily → brave → exa → ali → zhipu → serper
|
|
|
119
119
|
|
|
120
120
|
`provider` 参数在两种模式下都优先于模式:显式指定就用那一家,未知 id / 开关关闭 / 缺密钥照旧明确报错。
|
|
121
121
|
|
|
122
|
+
## 引擎状态检查点
|
|
123
|
+
|
|
124
|
+
World 页面上每家常驻引擎都有一枚徽标,共 14 枚。徽标取五个状态之一,判定顺序固定:
|
|
125
|
+
|
|
126
|
+
| 状态 | 判据 | 徽标色 |
|
|
127
|
+
| --- | --- | --- |
|
|
128
|
+
| `已关闭` | 配置面板里该家开关为 false | 灰 |
|
|
129
|
+
| `缺密钥` | 开关打开,但该家需要密钥而密钥未配 | 灰 |
|
|
130
|
+
| `上次失败` | 本进程最近一次真实调用返回错误 | 灰 |
|
|
131
|
+
| `匿名可用` | 仅 anysearch,未配密钥 | 中性 |
|
|
132
|
+
| `可用` | 开关打开且密钥在手,上一次调用没报错 | 亮 |
|
|
133
|
+
|
|
134
|
+
前三条按顺序判定,所以「已关闭」压过「上次失败」,「缺密钥」压过「上次失败」。判定不发请求,不花配额。
|
|
135
|
+
|
|
136
|
+
徽标之外另有三盏总览灯。`引擎` 报可用家数与被挡名单,`上次调用` 在本轮有失败时转红并列出失败家,`聚合模式` 显示当前 `multi` 或 `single`。灯数上限是宿主的 `MODULE_LAMP_MAX`(7 盏),本 World 固定用 3 盏。
|
|
137
|
+
|
|
138
|
+
「上次失败」与命中数只记本进程最近一次,存在内存里,重启即清空。跨重启的历史属于运行日志,不在这个面板。
|
|
139
|
+
|
|
140
|
+
要看得更细就开「引擎状态」页签。它是一张 14 行的明细表,逐行报状态、说明文字、开关、密钥变量名与上一次调用的时间戳。表每 5 秒自动刷新,也可以手点刷新。数据走 `webUi().panels[id='engines']` 的 `invoke('state')`,面板实现打包在 `dist/webui.js`。
|
|
141
|
+
|
|
142
|
+
发布前要先生成产物:
|
|
143
|
+
|
|
144
|
+
```
|
|
145
|
+
pnpm build:webui # tsc -p tsconfig.webui.json,产出 dist/webui.js
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
`dist/` 已进 `.gitignore`,但列在 `package.json` 的 `files` 里,`files` 优先,所以 npm 包里带着它。改过 `src/webui.ts` 记得重跑 `build:webui`,否则 npm 上的产物与源码不一致。
|
|
149
|
+
|
|
122
150
|
## 各提供商价格与约束(官方量级参考,随官方变动)
|
|
123
151
|
|
|
124
152
|
| 提供商 | 价格 | 定位 | 硬约束 |
|
|
@@ -153,10 +181,13 @@ SDK 来自 `cortico` devDependency(`^0.2.17`),不需要 vendor submodule
|
|
|
153
181
|
|
|
154
182
|
```
|
|
155
183
|
pnpm install
|
|
156
|
-
pnpm typecheck
|
|
157
|
-
pnpm
|
|
184
|
+
pnpm typecheck # tsc --noEmit
|
|
185
|
+
pnpm build:webui # tsc -p tsconfig.webui.json,产出 dist/webui.js
|
|
186
|
+
pnpm test # vitest run:干装载 + fetch stub,不联网、不启宿主
|
|
158
187
|
```
|
|
159
188
|
|
|
189
|
+
`src/webui.ts` 走独立的 `tsconfig.webui.json`。那份配置 `types: []`,禁掉 node 类型,产物又必须零依赖,所以这个文件不许 import 任何东西。宿主用 `import()` 直接加载 `dist/webui.js`,产物里出现裸导入会在浏览器侧解析失败。
|
|
190
|
+
|
|
160
191
|
在 Cortico 检出根验证装载:
|
|
161
192
|
|
|
162
193
|
```
|
package/dist/webui.js
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器端面板产物:14 家引擎的状态明细表。
|
|
3
|
+
*
|
|
4
|
+
* 本文件由 `dist/webui.js` 单独编译(`tsconfig.webui.json`)。它不能 import 任何东西:
|
|
5
|
+
* 宿主用 `import()` 直接加载这一个文件,产物里出现裸导入会在浏览器侧解析失败。
|
|
6
|
+
* 下面两个 interface 是按 `cortico/src/web/shared/client-panel.ts:593-603`(读于 2026-11-23)
|
|
7
|
+
* 手写的结构副本,只声明本文件用到的成员。宿主运行时不校验来源,只按 `panels[panelId].mount` 取。
|
|
8
|
+
*
|
|
9
|
+
* 数据来自服务端 `webUi().panels[id='engines']` 的 `invoke('state')`,见 `src/world.ts`。
|
|
10
|
+
* 本文件不碰 `document.body`,只写 `ctx.root`;定时器与 fetch 都挂在 `ctx` 上,卸载即停。
|
|
11
|
+
*/
|
|
12
|
+
const STRINGS = {
|
|
13
|
+
zh: {
|
|
14
|
+
title: '引擎状态',
|
|
15
|
+
refresh: '刷新',
|
|
16
|
+
autoOn: '每 5 秒自动刷新',
|
|
17
|
+
headEngine: '引擎',
|
|
18
|
+
headState: '状态',
|
|
19
|
+
headDetail: '说明',
|
|
20
|
+
headLast: '上次调用',
|
|
21
|
+
summary: (ready, total, blocked, failed) => `可调用 ${ready}/${total} 家。被挡 ${blocked} 家。本轮失败 ${failed} 家。`,
|
|
22
|
+
loading: '读取中…',
|
|
23
|
+
error: '读取失败:',
|
|
24
|
+
never: '—',
|
|
25
|
+
mode: '聚合模式',
|
|
26
|
+
},
|
|
27
|
+
en: {
|
|
28
|
+
title: 'Engine status',
|
|
29
|
+
refresh: 'Refresh',
|
|
30
|
+
autoOn: 'Auto refresh every 5 s',
|
|
31
|
+
headEngine: 'Engine',
|
|
32
|
+
headState: 'State',
|
|
33
|
+
headDetail: 'Detail',
|
|
34
|
+
headLast: 'Last call',
|
|
35
|
+
summary: (ready, total, blocked, failed) => `${ready}/${total} callable. ${blocked} blocked. ${failed} failed this round.`,
|
|
36
|
+
loading: 'Loading…',
|
|
37
|
+
error: 'Load failed: ',
|
|
38
|
+
never: '—',
|
|
39
|
+
mode: 'Aggregation mode',
|
|
40
|
+
},
|
|
41
|
+
};
|
|
42
|
+
const pick = (language) => STRINGS[language === 'zh' ? 'zh' : 'en'];
|
|
43
|
+
/** 状态色。与后端五个 state 一一对应,未知值按中性处理。 */
|
|
44
|
+
const COLOR = {
|
|
45
|
+
ready: '#1a7f37',
|
|
46
|
+
anonymous: '#6b7280',
|
|
47
|
+
'no-key': '#9a6700',
|
|
48
|
+
off: '#6b7280',
|
|
49
|
+
'last-error': '#b42318',
|
|
50
|
+
};
|
|
51
|
+
const TEXT = {
|
|
52
|
+
zh: {
|
|
53
|
+
ready: '可用', anonymous: '匿名可用', 'no-key': '缺密钥', off: '已关闭', 'last-error': '上次失败',
|
|
54
|
+
},
|
|
55
|
+
en: {
|
|
56
|
+
ready: 'ready', anonymous: 'anonymous', 'no-key': 'missing key', off: 'switched off',
|
|
57
|
+
'last-error': 'last call failed',
|
|
58
|
+
},
|
|
59
|
+
};
|
|
60
|
+
export default {
|
|
61
|
+
panels: {
|
|
62
|
+
engines: {
|
|
63
|
+
mount(ctx) {
|
|
64
|
+
const s = pick(ctx.language);
|
|
65
|
+
const lang = ctx.language === 'zh' ? 'zh' : 'en';
|
|
66
|
+
const root = ctx.root;
|
|
67
|
+
root.style.cssText = 'font:13px/1.5 system-ui,sans-serif;color:currentColor';
|
|
68
|
+
const head = document.createElement('div');
|
|
69
|
+
head.style.cssText = 'display:flex;gap:10px;align-items:center;margin-bottom:8px;flex-wrap:wrap';
|
|
70
|
+
const summary = document.createElement('span');
|
|
71
|
+
const mode = document.createElement('span');
|
|
72
|
+
mode.style.cssText = 'opacity:.7';
|
|
73
|
+
const refresh = document.createElement('button');
|
|
74
|
+
refresh.type = 'button';
|
|
75
|
+
refresh.textContent = s.refresh;
|
|
76
|
+
const note = document.createElement('span');
|
|
77
|
+
note.style.cssText = 'opacity:.6';
|
|
78
|
+
note.textContent = s.autoOn;
|
|
79
|
+
head.append(summary, mode, refresh, note);
|
|
80
|
+
const table = document.createElement('table');
|
|
81
|
+
table.style.cssText = 'width:100%;border-collapse:collapse';
|
|
82
|
+
table.innerHTML = `<thead><tr>`
|
|
83
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headEngine}</th>`
|
|
84
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headState}</th>`
|
|
85
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headDetail}</th>`
|
|
86
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headLast}</th>`
|
|
87
|
+
+ `</tr></thead><tbody></tbody>`;
|
|
88
|
+
const body = table.querySelector('tbody');
|
|
89
|
+
root.append(head, table);
|
|
90
|
+
const cell = (text, color) => {
|
|
91
|
+
const td = document.createElement('td');
|
|
92
|
+
td.style.cssText = `padding:4px 8px;border-top:1px solid rgba(127,127,127,.25);${color ? `color:${color}` : ''}`;
|
|
93
|
+
td.textContent = text;
|
|
94
|
+
return td;
|
|
95
|
+
};
|
|
96
|
+
let busy = false;
|
|
97
|
+
const load = async () => {
|
|
98
|
+
if (busy)
|
|
99
|
+
return;
|
|
100
|
+
busy = true;
|
|
101
|
+
try {
|
|
102
|
+
const state = await ctx.invoke('state');
|
|
103
|
+
body.replaceChildren(...state.rows.map((row) => {
|
|
104
|
+
const tr = document.createElement('tr');
|
|
105
|
+
const label = TEXT[lang][row.state] ?? row.state;
|
|
106
|
+
tr.append(cell(row.id), cell(label, COLOR[row.state] ?? ''), cell(row.detail), cell(row.lastTs || s.never));
|
|
107
|
+
return tr;
|
|
108
|
+
}));
|
|
109
|
+
summary.textContent = s.summary(state.ready, state.total, state.blocked, state.failed);
|
|
110
|
+
mode.textContent = `${s.mode}: ${state.mode}`;
|
|
111
|
+
refresh.disabled = false;
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
summary.textContent = `${s.error}${error instanceof Error ? error.message : String(error)}`;
|
|
115
|
+
}
|
|
116
|
+
finally {
|
|
117
|
+
busy = false;
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
refresh.addEventListener('click', () => { void load(); }, { signal: ctx.signal });
|
|
121
|
+
ctx.interval(() => { void load(); }, 5_000);
|
|
122
|
+
summary.textContent = s.loading;
|
|
123
|
+
void load();
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
},
|
|
127
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cortico-world-web-search-multi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Cortico World: 多 provider 聚合搜索(Anysearch / Tavily / Exa / Bocha / 阿里 IQS / 智谱 / Perplexity / Serper / Brave),跨源去重,暴露 web_search_multi 工具",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,9 +17,15 @@
|
|
|
17
17
|
"packageManager": "pnpm@11.28.6",
|
|
18
18
|
"main": "./src/index.ts",
|
|
19
19
|
"cortico": {
|
|
20
|
+
"displayName": "聚合搜索",
|
|
21
|
+
"locales": {
|
|
22
|
+
"zh": { "displayName": "聚合搜索" },
|
|
23
|
+
"en": { "displayName": "Aggregated Search" }
|
|
24
|
+
},
|
|
20
25
|
"kind": "world",
|
|
21
26
|
"api": 6,
|
|
22
27
|
"describe": "describe.json",
|
|
28
|
+
"webUiClient": "dist/webui.js",
|
|
23
29
|
"tags": [
|
|
24
30
|
"internet",
|
|
25
31
|
"requires-key"
|
|
@@ -27,12 +33,14 @@
|
|
|
27
33
|
},
|
|
28
34
|
"files": [
|
|
29
35
|
"src",
|
|
36
|
+
"dist",
|
|
30
37
|
"LICENSE",
|
|
31
38
|
"README.md",
|
|
32
39
|
"CHANGELOG.md",
|
|
33
40
|
"describe.json"
|
|
34
41
|
],
|
|
35
42
|
"scripts": {
|
|
43
|
+
"build:webui": "tsc -p tsconfig.webui.json",
|
|
36
44
|
"test": "vitest run",
|
|
37
45
|
"typecheck": "tsc --noEmit"
|
|
38
46
|
},
|
package/src/config.ts
CHANGED
|
@@ -7,6 +7,9 @@ import type { WorldSection } from 'cortico/world.ts';
|
|
|
7
7
|
*/
|
|
8
8
|
export const WEB_SEARCH_ID = 'web-search-multi';
|
|
9
9
|
|
|
10
|
+
/** World 显示名。用于 `WorldDefinition.label` 与 `webUi().label`,与 package.json 的 `cortico.displayName` 一致。 */
|
|
11
|
+
export const WEB_SEARCH_LABEL = '聚合搜索';
|
|
12
|
+
|
|
10
13
|
/** 聚合搜索支持的提供商,按价格从低到高(官方价随变动,仅作量级参考)。 */
|
|
11
14
|
export type SearchProvider =
|
|
12
15
|
| 'anysearch'
|
package/src/definition.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { WorldDefinition } from 'cortico/world.ts';
|
|
2
2
|
import { describeTools } from 'cortico/world.ts';
|
|
3
3
|
import type { WebSearchConfigSection } from './config.ts';
|
|
4
|
-
import { WEB_SEARCH_DEFAULTS, WEB_SEARCH_ID } from './config.ts';
|
|
4
|
+
import { WEB_SEARCH_DEFAULTS, WEB_SEARCH_ID, WEB_SEARCH_LABEL } from './config.ts';
|
|
5
5
|
import { WEB_SEARCH_TOOL_DECLS, WebSearchWorld } from './world.ts';
|
|
6
6
|
|
|
7
7
|
/**
|
|
@@ -12,7 +12,7 @@ import { WEB_SEARCH_TOOL_DECLS, WebSearchWorld } from './world.ts';
|
|
|
12
12
|
*/
|
|
13
13
|
export const WebSearch: WorldDefinition<WebSearchConfigSection> = {
|
|
14
14
|
id: WEB_SEARCH_ID,
|
|
15
|
-
label:
|
|
15
|
+
label: WEB_SEARCH_LABEL,
|
|
16
16
|
defaults: () => structuredClone(WEB_SEARCH_DEFAULTS),
|
|
17
17
|
create: (ctx) => new WebSearchWorld({
|
|
18
18
|
cfg: ctx.cfg,
|
package/src/status.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 每家搜索引擎的状态检查点:静态判定 + 上一次真实调用的结果。
|
|
3
|
+
*
|
|
4
|
+
* 静态部分只读配置开关与密钥是否存在,不发请求。被动部分由 `WebSearchWorld`
|
|
5
|
+
* 在每次 `web_search_multi` 之后写入 `ProviderLedger`,只在本进程存续。
|
|
6
|
+
*
|
|
7
|
+
* 本文件不 import `world.ts`:adapter 表在那里,反向依赖会成环。判定口径与
|
|
8
|
+
* `prompt-vars.ts` 的 `usableProviders()` 逐字一致(开关没关 且(无需密钥 或 已配密钥))。
|
|
9
|
+
*/
|
|
10
|
+
import { ANONYMOUS_PROVIDERS, PROVIDER_SECRET, WEB_SEARCH_DEFAULTS } from './config.ts';
|
|
11
|
+
import type { ProviderSwitches, SearchProvider, WebSearchConfigSection } from './config.ts';
|
|
12
|
+
import { SINGLE_MODE_PRIORITY } from './prompt-vars.ts';
|
|
13
|
+
|
|
14
|
+
/** 一家引擎的检查点状态。 */
|
|
15
|
+
export type CheckState = 'ready' | 'anonymous' | 'no-key' | 'off' | 'last-error';
|
|
16
|
+
|
|
17
|
+
/** 上一次真实调用的记录。`error` 空串表示成功。 */
|
|
18
|
+
export interface LastRun {
|
|
19
|
+
ok: boolean;
|
|
20
|
+
error: string;
|
|
21
|
+
hits: number;
|
|
22
|
+
/** `nowIso(timezone)` 格式的时间戳。 */
|
|
23
|
+
ts: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** 一家引擎的检查点。`detail` 面向 WebUI,已经裁短。 */
|
|
27
|
+
export interface Checkpoint {
|
|
28
|
+
id: SearchProvider;
|
|
29
|
+
state: CheckState;
|
|
30
|
+
detail: string;
|
|
31
|
+
/** 配置面板里这家的开关。缺键视为打开。 */
|
|
32
|
+
switchOn: boolean;
|
|
33
|
+
/** 这家的密钥是否已配置。 */
|
|
34
|
+
keyed: boolean;
|
|
35
|
+
/** 这家的密钥变量名,明细表直接印出来,省得用户去翻 README。 */
|
|
36
|
+
secretName: string;
|
|
37
|
+
/** 本进程内这家最近一次调用的时间戳。没调过是空串。 */
|
|
38
|
+
lastTs: string;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `webUi().panels[id].invoke('state')` 的回执,也是 `src/webui.ts` 读的表结构。 */
|
|
42
|
+
export interface PanelState extends CheckSummary {
|
|
43
|
+
/** 本次读取的时刻,`nowIso(timezone)` 格式。 */
|
|
44
|
+
updatedAt: string;
|
|
45
|
+
/** 当前聚合模式的界面文案。 */
|
|
46
|
+
mode: string;
|
|
47
|
+
rows: Checkpoint[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 摘要计数,给状态灯与徽标用。 */
|
|
51
|
+
export interface CheckSummary {
|
|
52
|
+
total: number;
|
|
53
|
+
ready: number;
|
|
54
|
+
blocked: number;
|
|
55
|
+
failed: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** `detail` 里错误文本的最大长度;WebUI 徽标是单行控件。 */
|
|
59
|
+
const DETAIL_MAX = 64;
|
|
60
|
+
|
|
61
|
+
const clip = (value: string): string => {
|
|
62
|
+
const flat = value.replace(/\s+/g, ' ').trim();
|
|
63
|
+
return flat.length > DETAIL_MAX ? `${flat.slice(0, DETAIL_MAX - 1)}…` : flat;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/** 状态在 WebUI 上的一句话文案。 */
|
|
67
|
+
export const CHECK_LABEL: Record<CheckState, string> = {
|
|
68
|
+
ready: '可用',
|
|
69
|
+
anonymous: '匿名可用',
|
|
70
|
+
'no-key': '缺密钥',
|
|
71
|
+
off: '已关闭',
|
|
72
|
+
'last-error': '上次失败',
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 本进程的调用台账。只保留每家最近一次,内存上限等于提供商数量。
|
|
77
|
+
* 不持久化:跨重启的历史属于运行日志,不属于 WebUI 状态灯。
|
|
78
|
+
*/
|
|
79
|
+
export class ProviderLedger {
|
|
80
|
+
private readonly runs = new Map<SearchProvider, LastRun>();
|
|
81
|
+
|
|
82
|
+
/** 记下一轮并发调用的结果;同一家的旧记录被覆盖。 */
|
|
83
|
+
record(
|
|
84
|
+
entries: ReadonlyArray<{ id: SearchProvider; ok: boolean; error?: string; hits: number }>,
|
|
85
|
+
ts: string,
|
|
86
|
+
): void {
|
|
87
|
+
for (const entry of entries) {
|
|
88
|
+
this.runs.set(entry.id, {
|
|
89
|
+
ok: entry.ok,
|
|
90
|
+
error: clip(entry.error ?? ''),
|
|
91
|
+
hits: entry.hits,
|
|
92
|
+
ts,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
last(id: SearchProvider): LastRun | undefined {
|
|
98
|
+
return this.runs.get(id);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** 已记录过调用的家数,用于区分「没试过」与「试了都成功」。 */
|
|
102
|
+
get size(): number {
|
|
103
|
+
return this.runs.size;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
clear(): void {
|
|
107
|
+
this.runs.clear();
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* 逐家算检查点,顺序取 `SINGLE_MODE_PRIORITY`,与提示词里的「可用引擎范围」一致。
|
|
112
|
+
* 判定按四步走。第一步看开关,第二步看密钥,第三步看上一次调用,第四步区分匿名与可用。
|
|
113
|
+
*/
|
|
114
|
+
checkpoints(
|
|
115
|
+
cfg: Partial<WebSearchConfigSection> | null | undefined,
|
|
116
|
+
keyOf: (secretName: string) => string | undefined,
|
|
117
|
+
): Checkpoint[] {
|
|
118
|
+
const switches: ProviderSwitches = cfg?.providers ?? WEB_SEARCH_DEFAULTS.providers;
|
|
119
|
+
const rows: Checkpoint[] = [];
|
|
120
|
+
const push = (
|
|
121
|
+
id: SearchProvider,
|
|
122
|
+
state: CheckState,
|
|
123
|
+
detail: string,
|
|
124
|
+
switchOn: boolean,
|
|
125
|
+
keyed: boolean,
|
|
126
|
+
lastTs: string,
|
|
127
|
+
): void => {
|
|
128
|
+
rows.push({ id, state, detail, switchOn, keyed, secretName: PROVIDER_SECRET[id], lastTs });
|
|
129
|
+
};
|
|
130
|
+
for (const id of SINGLE_MODE_PRIORITY) {
|
|
131
|
+
const secretName = PROVIDER_SECRET[id];
|
|
132
|
+
const anonymous = ANONYMOUS_PROVIDERS.has(id);
|
|
133
|
+
const switchOn = switches[id] !== false;
|
|
134
|
+
const key = keyOf(secretName);
|
|
135
|
+
const keyed = Boolean(key);
|
|
136
|
+
const run = this.runs.get(id);
|
|
137
|
+
const lastTs = run?.ts ?? '';
|
|
138
|
+
if (!switchOn) {
|
|
139
|
+
push(id, 'off', run && !run.ok ? `关闭前上次失败:${run.error}` : '配置面板已关闭', false, keyed, lastTs);
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (!key && !anonymous) {
|
|
143
|
+
push(id, 'no-key', `缺少密钥 ${secretName}`, true, false, lastTs);
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
if (run && !run.ok) {
|
|
147
|
+
push(id, 'last-error', run.error || '上次调用失败', true, keyed, lastTs);
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
if (!key && anonymous) {
|
|
151
|
+
push(id, 'anonymous', run ? `匿名调用 ${run.hits} 条` : '匿名可用,尚未调用', true, false, lastTs);
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
push(id, 'ready', run ? `上次成功 ${run.hits} 条` : '已配密钥,尚未调用', true, true, lastTs);
|
|
155
|
+
}
|
|
156
|
+
return rows;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** 按检查点算摘要。`ready` 含匿名可用,与 `usableProviders()` 同口径。 */
|
|
161
|
+
export function summarize(rows: readonly Checkpoint[]): CheckSummary {
|
|
162
|
+
let ready = 0;
|
|
163
|
+
let blocked = 0;
|
|
164
|
+
let failed = 0;
|
|
165
|
+
for (const row of rows) {
|
|
166
|
+
if (row.state === 'ready' || row.state === 'anonymous') ready += 1;
|
|
167
|
+
if (row.state === 'no-key' || row.state === 'off') blocked += 1;
|
|
168
|
+
if (row.state === 'last-error') failed += 1;
|
|
169
|
+
}
|
|
170
|
+
return { total: rows.length, ready, blocked, failed };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** 失败的家的 id 列表,给状态灯 hint。 */
|
|
174
|
+
export function failedIds(rows: readonly Checkpoint[]): string[] {
|
|
175
|
+
return rows.filter((row) => row.state === 'last-error').map((row) => row.id);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/** 被挡在门外的家的 id 列表(关闭或缺密钥),给状态灯 hint。 */
|
|
179
|
+
export function blockedIds(rows: readonly Checkpoint[]): string[] {
|
|
180
|
+
return rows.filter((row) => row.state === 'no-key' || row.state === 'off').map((row) => row.id);
|
|
181
|
+
}
|
package/src/webui.ts
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 浏览器端面板产物:14 家引擎的状态明细表。
|
|
3
|
+
*
|
|
4
|
+
* 本文件由 `dist/webui.js` 单独编译(`tsconfig.webui.json`)。它不能 import 任何东西:
|
|
5
|
+
* 宿主用 `import()` 直接加载这一个文件,产物里出现裸导入会在浏览器侧解析失败。
|
|
6
|
+
* 下面两个 interface 是按 `cortico/src/web/shared/client-panel.ts:593-603`(读于 2026-11-23)
|
|
7
|
+
* 手写的结构副本,只声明本文件用到的成员。宿主运行时不校验来源,只按 `panels[panelId].mount` 取。
|
|
8
|
+
*
|
|
9
|
+
* 数据来自服务端 `webUi().panels[id='engines']` 的 `invoke('state')`,见 `src/world.ts`。
|
|
10
|
+
* 本文件不碰 `document.body`,只写 `ctx.root`;定时器与 fetch 都挂在 `ctx` 上,卸载即停。
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** 宿主给面板的运行时上下文的最小面。 */
|
|
14
|
+
interface PanelContext {
|
|
15
|
+
readonly language: string;
|
|
16
|
+
readonly root: HTMLElement;
|
|
17
|
+
readonly signal: AbortSignal;
|
|
18
|
+
invoke<T = unknown>(method: string, args?: unknown[]): Promise<T>;
|
|
19
|
+
interval(fn: () => void, ms: number): { dispose(): void };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** 服务端 `invoke('state')` 的回执。与 `src/status.ts` 的 `PanelState` 逐字对齐。 */
|
|
23
|
+
interface PanelState {
|
|
24
|
+
updatedAt: string;
|
|
25
|
+
mode: string;
|
|
26
|
+
total: number;
|
|
27
|
+
ready: number;
|
|
28
|
+
blocked: number;
|
|
29
|
+
failed: number;
|
|
30
|
+
rows: Array<{
|
|
31
|
+
id: string;
|
|
32
|
+
state: string;
|
|
33
|
+
detail: string;
|
|
34
|
+
switchOn: boolean;
|
|
35
|
+
keyed: boolean;
|
|
36
|
+
secretName: string;
|
|
37
|
+
lastTs: string;
|
|
38
|
+
}>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** 面板文案表。`summary` 是唯一带参数的条目。 */
|
|
42
|
+
interface PanelStrings {
|
|
43
|
+
title: string;
|
|
44
|
+
refresh: string;
|
|
45
|
+
autoOn: string;
|
|
46
|
+
headEngine: string;
|
|
47
|
+
headState: string;
|
|
48
|
+
headDetail: string;
|
|
49
|
+
headLast: string;
|
|
50
|
+
loading: string;
|
|
51
|
+
error: string;
|
|
52
|
+
never: string;
|
|
53
|
+
mode: string;
|
|
54
|
+
summary(ready: number, total: number, blocked: number, failed: number): string;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
const STRINGS: Record<'zh' | 'en', PanelStrings> = {
|
|
58
|
+
zh: {
|
|
59
|
+
title: '引擎状态',
|
|
60
|
+
refresh: '刷新',
|
|
61
|
+
autoOn: '每 5 秒自动刷新',
|
|
62
|
+
headEngine: '引擎',
|
|
63
|
+
headState: '状态',
|
|
64
|
+
headDetail: '说明',
|
|
65
|
+
headLast: '上次调用',
|
|
66
|
+
summary: (ready: number, total: number, blocked: number, failed: number) =>
|
|
67
|
+
`可调用 ${ready}/${total} 家。被挡 ${blocked} 家。本轮失败 ${failed} 家。`,
|
|
68
|
+
loading: '读取中…',
|
|
69
|
+
error: '读取失败:',
|
|
70
|
+
never: '—',
|
|
71
|
+
mode: '聚合模式',
|
|
72
|
+
},
|
|
73
|
+
en: {
|
|
74
|
+
title: 'Engine status',
|
|
75
|
+
refresh: 'Refresh',
|
|
76
|
+
autoOn: 'Auto refresh every 5 s',
|
|
77
|
+
headEngine: 'Engine',
|
|
78
|
+
headState: 'State',
|
|
79
|
+
headDetail: 'Detail',
|
|
80
|
+
headLast: 'Last call',
|
|
81
|
+
summary: (ready: number, total: number, blocked: number, failed: number) =>
|
|
82
|
+
`${ready}/${total} callable. ${blocked} blocked. ${failed} failed this round.`,
|
|
83
|
+
loading: 'Loading…',
|
|
84
|
+
error: 'Load failed: ',
|
|
85
|
+
never: '—',
|
|
86
|
+
mode: 'Aggregation mode',
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const pick = (language: string): PanelStrings => STRINGS[language === 'zh' ? 'zh' : 'en'];
|
|
91
|
+
|
|
92
|
+
/** 状态色。与后端五个 state 一一对应,未知值按中性处理。 */
|
|
93
|
+
const COLOR: Record<string, string> = {
|
|
94
|
+
ready: '#1a7f37',
|
|
95
|
+
anonymous: '#6b7280',
|
|
96
|
+
'no-key': '#9a6700',
|
|
97
|
+
off: '#6b7280',
|
|
98
|
+
'last-error': '#b42318',
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const TEXT: Record<'zh' | 'en', Record<string, string>> = {
|
|
102
|
+
zh: {
|
|
103
|
+
ready: '可用', anonymous: '匿名可用', 'no-key': '缺密钥', off: '已关闭', 'last-error': '上次失败',
|
|
104
|
+
},
|
|
105
|
+
en: {
|
|
106
|
+
ready: 'ready', anonymous: 'anonymous', 'no-key': 'missing key', off: 'switched off',
|
|
107
|
+
'last-error': 'last call failed',
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
export default {
|
|
112
|
+
panels: {
|
|
113
|
+
engines: {
|
|
114
|
+
mount(ctx: PanelContext): void {
|
|
115
|
+
const s = pick(ctx.language);
|
|
116
|
+
const lang = ctx.language === 'zh' ? 'zh' : 'en';
|
|
117
|
+
const root = ctx.root;
|
|
118
|
+
root.style.cssText = 'font:13px/1.5 system-ui,sans-serif;color:currentColor';
|
|
119
|
+
|
|
120
|
+
const head = document.createElement('div');
|
|
121
|
+
head.style.cssText = 'display:flex;gap:10px;align-items:center;margin-bottom:8px;flex-wrap:wrap';
|
|
122
|
+
const summary = document.createElement('span');
|
|
123
|
+
const mode = document.createElement('span');
|
|
124
|
+
mode.style.cssText = 'opacity:.7';
|
|
125
|
+
const refresh = document.createElement('button');
|
|
126
|
+
refresh.type = 'button';
|
|
127
|
+
refresh.textContent = s.refresh;
|
|
128
|
+
const note = document.createElement('span');
|
|
129
|
+
note.style.cssText = 'opacity:.6';
|
|
130
|
+
note.textContent = s.autoOn;
|
|
131
|
+
head.append(summary, mode, refresh, note);
|
|
132
|
+
|
|
133
|
+
const table = document.createElement('table');
|
|
134
|
+
table.style.cssText = 'width:100%;border-collapse:collapse';
|
|
135
|
+
table.innerHTML = `<thead><tr>`
|
|
136
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headEngine}</th>`
|
|
137
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headState}</th>`
|
|
138
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headDetail}</th>`
|
|
139
|
+
+ `<th style="text-align:left;padding:4px 8px">${s.headLast}</th>`
|
|
140
|
+
+ `</tr></thead><tbody></tbody>`;
|
|
141
|
+
const body = table.querySelector('tbody') as HTMLTableSectionElement;
|
|
142
|
+
root.append(head, table);
|
|
143
|
+
|
|
144
|
+
const cell = (text: string, color?: string): HTMLTableCellElement => {
|
|
145
|
+
const td = document.createElement('td');
|
|
146
|
+
td.style.cssText = `padding:4px 8px;border-top:1px solid rgba(127,127,127,.25);${color ? `color:${color}` : ''}`;
|
|
147
|
+
td.textContent = text;
|
|
148
|
+
return td;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
let busy = false;
|
|
152
|
+
const load = async (): Promise<void> => {
|
|
153
|
+
if (busy) return;
|
|
154
|
+
busy = true;
|
|
155
|
+
try {
|
|
156
|
+
const state = await ctx.invoke<PanelState>('state');
|
|
157
|
+
body.replaceChildren(...state.rows.map((row) => {
|
|
158
|
+
const tr = document.createElement('tr');
|
|
159
|
+
const label = (TEXT[lang] as Record<string, string>)[row.state] ?? row.state;
|
|
160
|
+
tr.append(
|
|
161
|
+
cell(row.id),
|
|
162
|
+
cell(label, COLOR[row.state] ?? ''),
|
|
163
|
+
cell(row.detail),
|
|
164
|
+
cell(row.lastTs || s.never),
|
|
165
|
+
);
|
|
166
|
+
return tr;
|
|
167
|
+
}));
|
|
168
|
+
summary.textContent = s.summary(state.ready, state.total, state.blocked, state.failed);
|
|
169
|
+
mode.textContent = `${s.mode}: ${state.mode}`;
|
|
170
|
+
refresh.disabled = false;
|
|
171
|
+
} catch (error) {
|
|
172
|
+
summary.textContent = `${s.error}${error instanceof Error ? error.message : String(error)}`;
|
|
173
|
+
} finally {
|
|
174
|
+
busy = false;
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
refresh.addEventListener('click', () => { void load(); }, { signal: ctx.signal });
|
|
179
|
+
ctx.interval(() => { void load(); }, 5_000);
|
|
180
|
+
summary.textContent = s.loading;
|
|
181
|
+
void load();
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
};
|
package/src/world.ts
CHANGED
|
@@ -13,8 +13,20 @@ import {
|
|
|
13
13
|
WEB_SEARCH_CONFIG_GROUP,
|
|
14
14
|
WEB_SEARCH_DEFAULTS,
|
|
15
15
|
WEB_SEARCH_ID,
|
|
16
|
+
WEB_SEARCH_LABEL,
|
|
16
17
|
} from './config.ts';
|
|
17
18
|
import type { ProviderSwitches, SearchProvider, WebSearchConfigSection } from './config.ts';
|
|
19
|
+
import {
|
|
20
|
+
ProviderLedger,
|
|
21
|
+
CHECK_LABEL,
|
|
22
|
+
blockedIds,
|
|
23
|
+
failedIds,
|
|
24
|
+
summarize,
|
|
25
|
+
} from './status.ts';
|
|
26
|
+
import type { Checkpoint, PanelState } from './status.ts';
|
|
27
|
+
|
|
28
|
+
/** WebUI 明细表的面板 id。`src/webui.ts` 的 `panels` 键必须与它逐字相同。 */
|
|
29
|
+
const WEB_SEARCH_PANEL_ID = 'engines';
|
|
18
30
|
import {
|
|
19
31
|
AVAILABLE_VAR,
|
|
20
32
|
ENV_FILE_VAR,
|
|
@@ -23,6 +35,7 @@ import {
|
|
|
23
35
|
PROMPT_VARS_FALLBACK,
|
|
24
36
|
SINGLE_MODE_PRIORITY,
|
|
25
37
|
buildPromptVars,
|
|
38
|
+
describeSearchMode,
|
|
26
39
|
resolveSearchMode,
|
|
27
40
|
usableProviders,
|
|
28
41
|
} from './prompt-vars.ts';
|
|
@@ -410,6 +423,9 @@ export class WebSearchWorld implements World {
|
|
|
410
423
|
|
|
411
424
|
private host: WorldHost | null = null;
|
|
412
425
|
|
|
426
|
+
/** 每家引擎本进程最近一次真实调用的台账,喂 WebUI 的检查点。 */
|
|
427
|
+
private readonly ledger = new ProviderLedger();
|
|
428
|
+
|
|
413
429
|
constructor(private readonly opts: WebSearchWorldOptions) {}
|
|
414
430
|
|
|
415
431
|
/** 按环境变量名取密钥;空白视为未配置。 */
|
|
@@ -445,9 +461,92 @@ export class WebSearchWorld implements World {
|
|
|
445
461
|
return [{ ...decl, handler: this.searchHandler.bind(this) }];
|
|
446
462
|
}
|
|
447
463
|
|
|
464
|
+
/**
|
|
465
|
+
* 逐家检查点。读配置或取密钥出错时给空表。
|
|
466
|
+
* 状态面只是观察窗口,不能让 WebUI 整页因为它报错。
|
|
467
|
+
*/
|
|
468
|
+
private checkpoints(): Checkpoint[] {
|
|
469
|
+
try {
|
|
470
|
+
return this.ledger.checkpoints(this.opts.cfg, (name) => this.keyOf(name));
|
|
471
|
+
} catch {
|
|
472
|
+
return [];
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
/**
|
|
477
|
+
* 三盏总览灯,固定少于 `MODULE_LAMP_MAX` 的 7 盏上限。
|
|
478
|
+
* 第一盏看覆盖面,第二盏看上一次真实调用,第三盏报当前聚合模式。
|
|
479
|
+
*/
|
|
480
|
+
private statusLamps(): NonNullable<WorldWebUiDecl['lamps']> {
|
|
481
|
+
const rows = this.checkpoints();
|
|
482
|
+
if (!rows.length) {
|
|
483
|
+
return [{ label: '引擎', state: 'error' as const, hint: '状态读取失败,检查配置面板。' }];
|
|
484
|
+
}
|
|
485
|
+
const sum = summarize(rows);
|
|
486
|
+
const blocked = blockedIds(rows);
|
|
487
|
+
const failed = failedIds(rows);
|
|
488
|
+
return [
|
|
489
|
+
{
|
|
490
|
+
label: '引擎',
|
|
491
|
+
state: sum.ready ? 'online' as const : 'offline' as const,
|
|
492
|
+
hint: sum.blocked
|
|
493
|
+
? `${sum.ready}/${sum.total} 家可调用,${blocked.join('、')} 因开关或密钥缺席。`
|
|
494
|
+
: `${sum.ready}/${sum.total} 家可调用,全部打开且已配密钥。`,
|
|
495
|
+
},
|
|
496
|
+
{
|
|
497
|
+
label: '上次调用',
|
|
498
|
+
state: failed.length ? 'error' as const : (this.ledger.size ? 'online' as const : 'offline' as const),
|
|
499
|
+
hint: failed.length
|
|
500
|
+
? `本轮失败 ${failed.length} 家(${failed.join('、')}),详情看对应徽标。`
|
|
501
|
+
: (this.ledger.size ? `已记录 ${this.ledger.size} 家的调用,无失败。` : '本进程还没有调用过 web_search_multi。'),
|
|
502
|
+
},
|
|
503
|
+
{
|
|
504
|
+
label: '聚合模式',
|
|
505
|
+
state: 'online' as const,
|
|
506
|
+
hint: describeSearchMode(resolveSearchMode(this.opts.cfg)),
|
|
507
|
+
},
|
|
508
|
+
];
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
/** 每家一枚徽标,顺序取 `SINGLE_MODE_PRIORITY`。超宽部分由宿主折进「更多」弹层。 */
|
|
512
|
+
private statusBadges(): NonNullable<WorldWebUiDecl['badges']> {
|
|
513
|
+
return this.checkpoints().map((row) => ({
|
|
514
|
+
label: row.id,
|
|
515
|
+
value: CHECK_LABEL[row.state],
|
|
516
|
+
tone: row.state === 'ready' ? 'on' as const : (row.state === 'anonymous' ? 'plain' as const : 'off' as const),
|
|
517
|
+
}));
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/** 明细表的回执。读配置出错时直接抛,宿主把面板转成错误卡。 */
|
|
521
|
+
private panelState(): PanelState {
|
|
522
|
+
const rows = this.checkpoints();
|
|
523
|
+
return {
|
|
524
|
+
updatedAt: nowIso(this.opts.timezone),
|
|
525
|
+
mode: describeSearchMode(resolveSearchMode(this.opts.cfg)),
|
|
526
|
+
...summarize(rows),
|
|
527
|
+
rows,
|
|
528
|
+
};
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** 面板数据面。只认 `state` 一个方法,其余抛错,让 WebUI 报出具体名字。 */
|
|
532
|
+
private async invokePanel(panel: string, method: string): Promise<unknown> {
|
|
533
|
+
if (panel !== WEB_SEARCH_PANEL_ID) throw new Error(`未知面板 ${panel}`);
|
|
534
|
+
if (method !== 'state') throw new Error(`未知方法 ${method}`);
|
|
535
|
+
return this.panelState();
|
|
536
|
+
}
|
|
537
|
+
|
|
448
538
|
webUi(): WorldWebUiDecl {
|
|
449
539
|
return {
|
|
450
|
-
label:
|
|
540
|
+
label: WEB_SEARCH_LABEL,
|
|
541
|
+
lamps: this.statusLamps(),
|
|
542
|
+
badges: this.statusBadges(),
|
|
543
|
+
panels: [{
|
|
544
|
+
id: WEB_SEARCH_PANEL_ID,
|
|
545
|
+
title: '引擎状态',
|
|
546
|
+
description: '14 家引擎逐条报开关、密钥与上一次真实调用的结果。每 5 秒自动刷新。',
|
|
547
|
+
getMethods: ['state'],
|
|
548
|
+
}],
|
|
549
|
+
invoke: (panel, method) => this.invokePanel(panel, method),
|
|
451
550
|
config: [WEB_SEARCH_CONFIG_GROUP],
|
|
452
551
|
promptDocs: [
|
|
453
552
|
{
|
|
@@ -565,6 +664,11 @@ export class WebSearchWorld implements World {
|
|
|
565
664
|
runProvider(adapter, query, maxResults, key, ctx?.signal)),
|
|
566
665
|
);
|
|
567
666
|
const failed = runs.filter((run) => run.error !== undefined);
|
|
667
|
+
// 先写台账,WebUI 的检查点在下一帧就能看见本轮结果。
|
|
668
|
+
this.ledger.record(
|
|
669
|
+
runs.map((run) => ({ id: run.id, ok: run.error === undefined, error: run.error, hits: run.hits.length })),
|
|
670
|
+
nowIso(this.opts.timezone),
|
|
671
|
+
);
|
|
568
672
|
const logged = failed.length
|
|
569
673
|
? logWarn(ctx, `${WEB_SEARCH_ID}: ${failed.length} 家提供商调用失败:${failed.map((run) => `${run.id}(${run.error})`).join(';')}`)
|
|
570
674
|
: false;
|