@tnnevol/dsh-failover-search 7.0.1 → 7.0.2

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 CHANGED
@@ -9,7 +9,7 @@ DSH 会话内网页搜索的多来源插件:按 **TinyFish → Tavily → 官
9
9
  插件随 `fn-deepseek-harness` 自动安装。其他 DSH 环境手动安装,要求 DSH `0.2.0-rc.2`:
10
10
 
11
11
  ```sh
12
- dsh plugin --profile web add @tnnevol/dsh-failover-search@7.0.1
12
+ dsh plugin --profile web add @tnnevol/dsh-failover-search@7.0.2
13
13
  ```
14
14
 
15
15
  ## 这个包做什么
package/lib/client.js CHANGED
@@ -38961,8 +38961,22 @@ window.__ModuleLoader__.load({
38961
38961
  const OFFICIAL_SOURCE_ID = "deepseek-official";
38962
38962
  //#endregion
38963
38963
  //#region src/contracts/usage-rpc.ts
38964
- /** 客户端查询用量快照所用的 RPC 频道。 */
38965
- const FAILOVER_USAGE_CHANNEL = "/dsh-failover-search";
38964
+ /**
38965
+ * 插件自有 HTTP 端点的路径前缀。
38966
+ *
38967
+ * 挂在 `/plugins/dsh-failover-search` 下,与官方插件注册自有路由的方式保持一致
38968
+ * (`open-in-app` 用 `/open-in-app/*`,fnos 插件用 `/plugins/dsh-fnos/*`)。
38969
+ *
38970
+ * 不能自建其它顶层前缀:DSH 的 web 路由表里没有该前缀时,请求会落到
38971
+ * frontend-static 的兜底——非 GET/HEAD 一律 405、GET 一律 404。实测
38972
+ * `connection.rpc.handle()` 注册的频道在 fnOS 部署下没有进表,表现为详情页三个
38973
+ * 端点全部 405,用量、开关与账号掩码都拿不到数据。
38974
+ */
38975
+ const FAILOVER_ROUTES_PREFIX = "/plugins/dsh-failover-search";
38976
+ /** 端点路径:前缀 + 端点段。 */
38977
+ function failoverRoutePath(endpoint) {
38978
+ return `${FAILOVER_ROUTES_PREFIX}/${endpoint}`;
38979
+ }
38966
38980
  /** 用量区块调用的端点:读取当前快照(只读,不触发刷新)。 */
38967
38981
  const FAILOVER_USAGE_ENDPOINT = "usage";
38968
38982
  /**
@@ -40109,7 +40123,31 @@ window.__ModuleLoader__.load({
40109
40123
  en
40110
40124
  }), "dsh-failover-search: locale");
40111
40125
  const t = ctx.locale.bind(NS);
40112
- const rpc = ctx.get("connection").rpc;
40126
+ /** 传输层第一参数(频道)已不参与寻址;保留具名常量让调用点保持可读。 */
40127
+ const FAILOVER_USAGE_CHANNEL = FAILOVER_ROUTES_PREFIX;
40128
+ const rpc = { async call(_channel, endpoint, payload, signal) {
40129
+ const failure = (code, message) => ({
40130
+ ok: false,
40131
+ error: {
40132
+ code,
40133
+ message
40134
+ }
40135
+ });
40136
+ try {
40137
+ const response = await fetch(failoverRoutePath(endpoint), {
40138
+ method: "POST",
40139
+ headers: { "content-type": "application/json" },
40140
+ body: JSON.stringify(payload ?? {}),
40141
+ credentials: "same-origin",
40142
+ ...signal === void 0 ? {} : { signal }
40143
+ });
40144
+ const parsed = await response.json().catch(() => void 0);
40145
+ if (typeof parsed === "object" && parsed !== null && "ok" in parsed) return parsed;
40146
+ return failure(`http-${String(response.status)}`, `unexpected response from ${endpoint}`);
40147
+ } catch (error) {
40148
+ return failure("transport-failed", error instanceof Error ? error.message : String(error));
40149
+ }
40150
+ } };
40113
40151
  /**
40114
40152
  * 官方共享配置表单:按插件入口 id 取到,读写都在同一份配置文档上。
40115
40153
  *
package/lib/index.d.ts CHANGED
@@ -149,8 +149,20 @@ interface UsageAdapter {
149
149
  }
150
150
  //#endregion
151
151
  //#region src/contracts/usage-rpc.d.ts
152
- /** 客户端查询用量快照所用的 RPC 频道。 */
153
- declare const FAILOVER_USAGE_CHANNEL = "/dsh-failover-search";
152
+ /**
153
+ * 插件自有 HTTP 端点的路径前缀。
154
+ *
155
+ * 挂在 `/plugins/dsh-failover-search` 下,与官方插件注册自有路由的方式保持一致
156
+ * (`open-in-app` 用 `/open-in-app/*`,fnos 插件用 `/plugins/dsh-fnos/*`)。
157
+ *
158
+ * 不能自建其它顶层前缀:DSH 的 web 路由表里没有该前缀时,请求会落到
159
+ * frontend-static 的兜底——非 GET/HEAD 一律 405、GET 一律 404。实测
160
+ * `connection.rpc.handle()` 注册的频道在 fnOS 部署下没有进表,表现为详情页三个
161
+ * 端点全部 405,用量、开关与账号掩码都拿不到数据。
162
+ */
163
+ declare const FAILOVER_ROUTES_PREFIX = "/plugins/dsh-failover-search";
164
+ /** 端点路径:前缀 + 端点段。 */
165
+ declare function failoverRoutePath(endpoint: string): string;
154
166
  /** 用量区块调用的端点:读取当前快照(只读,不触发刷新)。 */
155
167
  declare const FAILOVER_USAGE_ENDPOINT = "usage";
156
168
  /**
@@ -575,4 +587,4 @@ declare const inject: string[];
575
587
  */
576
588
  declare function apply(ctx: Context, config?: FailoverSearchConfig): void;
577
589
  //#endregion
578
- export { type AccountConfig, Config, FAILOVER_ACCOUNTS_ENDPOINT, FAILOVER_PROVIDER_ID, FAILOVER_REFRESH_ENDPOINT, FAILOVER_REVEAL_ENDPOINT, FAILOVER_ROW_ID, FAILOVER_SETTINGS_NAMESPACE, FAILOVER_TOGGLE_ENDPOINT, FAILOVER_TOGGLE_SET_ENDPOINT, FAILOVER_USAGE_CHANNEL, FAILOVER_USAGE_ENDPOINT, type FailoverSearchConfig, FailoverSearchProvider, type FailoverSearchSettings, SessionToggleStore, type UsageSnapshot, UsageStore, apply, buildPool, ensureWebSelection, inject, maskApiKey, name, parsePublishedAt, usageFingerprint };
590
+ export { type AccountConfig, Config, FAILOVER_ACCOUNTS_ENDPOINT, FAILOVER_PROVIDER_ID, FAILOVER_REFRESH_ENDPOINT, FAILOVER_REVEAL_ENDPOINT, FAILOVER_ROUTES_PREFIX, FAILOVER_ROW_ID, FAILOVER_SETTINGS_NAMESPACE, FAILOVER_TOGGLE_ENDPOINT, FAILOVER_TOGGLE_SET_ENDPOINT, FAILOVER_USAGE_ENDPOINT, type FailoverSearchConfig, FailoverSearchProvider, type FailoverSearchSettings, SessionToggleStore, type UsageSnapshot, UsageStore, apply, buildPool, ensureWebSelection, failoverRoutePath, inject, maskApiKey, name, parsePublishedAt, usageFingerprint };
package/lib/index.js CHANGED
@@ -81,8 +81,22 @@ function readSettings(config) {
81
81
  }
82
82
  //#endregion
83
83
  //#region src/contracts/usage-rpc.ts
84
- /** 客户端查询用量快照所用的 RPC 频道。 */
85
- const FAILOVER_USAGE_CHANNEL = "/dsh-failover-search";
84
+ /**
85
+ * 插件自有 HTTP 端点的路径前缀。
86
+ *
87
+ * 挂在 `/plugins/dsh-failover-search` 下,与官方插件注册自有路由的方式保持一致
88
+ * (`open-in-app` 用 `/open-in-app/*`,fnos 插件用 `/plugins/dsh-fnos/*`)。
89
+ *
90
+ * 不能自建其它顶层前缀:DSH 的 web 路由表里没有该前缀时,请求会落到
91
+ * frontend-static 的兜底——非 GET/HEAD 一律 405、GET 一律 404。实测
92
+ * `connection.rpc.handle()` 注册的频道在 fnOS 部署下没有进表,表现为详情页三个
93
+ * 端点全部 405,用量、开关与账号掩码都拿不到数据。
94
+ */
95
+ const FAILOVER_ROUTES_PREFIX = "/plugins/dsh-failover-search";
96
+ /** 端点路径:前缀 + 端点段。 */
97
+ function failoverRoutePath(endpoint) {
98
+ return `${FAILOVER_ROUTES_PREFIX}/${endpoint}`;
99
+ }
86
100
  /** 用量区块调用的端点:读取当前快照(只读,不触发刷新)。 */
87
101
  const FAILOVER_USAGE_ENDPOINT = "usage";
88
102
  /**
@@ -1736,67 +1750,108 @@ function apply(ctx, config = Config({})) {
1736
1750
  }, "dsh-failover-search: usage refresh");
1737
1751
  ctx.inject(["connection", "webServer"], (connectionCtx) => {
1738
1752
  const injected = connectionCtx;
1739
- if (injected.connection === void 0) return;
1740
- injected.connection.rpc.handle(FAILOVER_USAGE_CHANNEL, async (endpoint, payload) => {
1741
- if (endpoint === "usage") return {
1742
- ok: true,
1743
- value: {
1744
- accounts: usage.snapshot(),
1745
- fresh: usage.isFresh(),
1746
- ...refreshedAt === void 0 ? {} : { fetchedAt: refreshedAt }
1753
+ if (injected.connection === void 0 || injected.webServer === void 0) return;
1754
+ /** 统一出口:信任判定 → 解析请求体 → 业务处理 → `{ ok, value }` 信封。 */
1755
+ const register = (endpoint, handle) => {
1756
+ connectionCtx.effect(() => injected.webServer.register({
1757
+ kind: "exact",
1758
+ path: failoverRoutePath(endpoint),
1759
+ handler: async (req, res) => {
1760
+ const rejection = injected.connection.requestRejection(req);
1761
+ if (rejection !== void 0) {
1762
+ res.statusCode = rejection;
1763
+ res.end();
1764
+ return;
1765
+ }
1766
+ if (req.method !== "POST") {
1767
+ res.statusCode = 405;
1768
+ res.setHeader("allow", "POST");
1769
+ res.end();
1770
+ return;
1771
+ }
1772
+ let payload = {};
1773
+ try {
1774
+ const chunks = [];
1775
+ for await (const chunk of req) chunks.push(chunk);
1776
+ const text = Buffer.concat(chunks).toString("utf8");
1777
+ if (text.length > 0) payload = JSON.parse(text);
1778
+ } catch {
1779
+ res.statusCode = 400;
1780
+ res.setHeader("content-type", "application/json; charset=utf-8");
1781
+ res.end(JSON.stringify({
1782
+ ok: false,
1783
+ error: {
1784
+ code: "invalid-json",
1785
+ message: "invalid JSON body"
1786
+ }
1787
+ }));
1788
+ return;
1789
+ }
1790
+ try {
1791
+ const value = await handle(payload);
1792
+ res.statusCode = 200;
1793
+ res.setHeader("content-type", "application/json; charset=utf-8");
1794
+ res.setHeader("cache-control", "no-store");
1795
+ res.end(JSON.stringify({
1796
+ ok: true,
1797
+ value
1798
+ }));
1799
+ } catch (error) {
1800
+ ctx.logger.warn("[failover-search] %s 处理失败:%s", endpoint, error instanceof Error ? error.message : String(error));
1801
+ res.statusCode = 500;
1802
+ res.setHeader("content-type", "application/json; charset=utf-8");
1803
+ res.end(JSON.stringify({
1804
+ ok: false,
1805
+ error: {
1806
+ code: "failover-route-failed",
1807
+ message: error instanceof Error ? error.message : String(error)
1808
+ }
1809
+ }));
1810
+ }
1747
1811
  }
1812
+ }), `dsh-failover-search: POST ${failoverRoutePath(endpoint)}`);
1813
+ };
1814
+ register(FAILOVER_USAGE_ENDPOINT, () => {
1815
+ return {
1816
+ accounts: usage.snapshot(),
1817
+ fresh: usage.isFresh(),
1818
+ ...refreshedAt === void 0 ? {} : { fetchedAt: refreshedAt }
1748
1819
  };
1749
- if (endpoint === "toggle") {
1750
- const request = payload ?? {};
1751
- return {
1752
- ok: true,
1753
- value: { enabled: toggle.isEnabled(request.sessionId) }
1754
- };
1755
- }
1756
- if (endpoint === "toggle-set") {
1757
- const request = payload ?? {};
1758
- if (request.sessionId === void 0) throw new Error("toggle-set requires a sessionId");
1759
- toggle.setEnabled(request.sessionId, request.enabled === true);
1760
- if (request.enabled === true) try {
1761
- if (await ensureWebSelection(ctx.get("settings")) === "updated") ctx.logger.info("[failover-search] 已把 web 接缝的搜索/抓取选择切回本插件");
1762
- } catch (error) {
1763
- ctx.logger.warn("[failover-search] 改写 web 接缝选择失败:%s", error instanceof Error ? error.message : String(error));
1764
- }
1765
- return {
1766
- ok: true,
1767
- value: { enabled: toggle.isEnabled(request.sessionId) }
1768
- };
1769
- }
1770
- if (endpoint === "refresh") {
1771
- await refreshUsage();
1772
- return {
1773
- ok: true,
1774
- value: {
1775
- accounts: usage.snapshot(),
1776
- fresh: usage.isFresh(),
1777
- ...refreshedAt === void 0 ? {} : { fetchedAt: refreshedAt }
1778
- }
1779
- };
1780
- }
1781
- if (endpoint === "reveal-key") {
1782
- const request = payload ?? {};
1783
- const current = readSettings(config);
1784
- const accounts = request.platform === "tavily" ? current.tavilyAccounts : request.platform === "tinyfish" ? current.tinyfishAccounts : [];
1785
- const match = buildPool(request.platform === "tavily" ? "tavily" : "tinyfish", accounts).find((account) => account.label === request.label);
1786
- if (match === void 0) throw new Error(`unknown account: ${String(request.platform)}/${String(request.label)}`);
1787
- return {
1788
- ok: true,
1789
- value: { key: match.key }
1790
- };
1791
- }
1792
- if (endpoint === "accounts") {
1793
- const current = readSettings(config);
1794
- return {
1795
- ok: true,
1796
- value: { accounts: [...collectAccounts(TINYFISH_PLATFORM_ID, current.tinyfishAccounts), ...collectAccounts(TAVILY_PLATFORM_ID, current.tavilyAccounts)] }
1797
- };
1820
+ });
1821
+ register(FAILOVER_TOGGLE_ENDPOINT, (payload) => {
1822
+ const request = payload;
1823
+ return { enabled: toggle.isEnabled(request.sessionId) };
1824
+ });
1825
+ register(FAILOVER_TOGGLE_SET_ENDPOINT, async (payload) => {
1826
+ const request = payload;
1827
+ if (request.sessionId === void 0) throw new Error("toggle-set requires a sessionId");
1828
+ toggle.setEnabled(request.sessionId, request.enabled === true);
1829
+ if (request.enabled === true) try {
1830
+ if (await ensureWebSelection(ctx.get("settings")) === "updated") ctx.logger.info("[failover-search] 已把 web 接缝的搜索/抓取选择切回本插件");
1831
+ } catch (error) {
1832
+ ctx.logger.warn("[failover-search] 改写 web 接缝选择失败:%s", error instanceof Error ? error.message : String(error));
1798
1833
  }
1799
- throw new Error(`unknown endpoint: ${endpoint}`);
1834
+ return { enabled: toggle.isEnabled(request.sessionId) };
1835
+ });
1836
+ register(FAILOVER_REFRESH_ENDPOINT, async () => {
1837
+ await refreshUsage();
1838
+ return {
1839
+ accounts: usage.snapshot(),
1840
+ fresh: usage.isFresh(),
1841
+ ...refreshedAt === void 0 ? {} : { fetchedAt: refreshedAt }
1842
+ };
1843
+ });
1844
+ register(FAILOVER_REVEAL_ENDPOINT, (payload) => {
1845
+ const request = payload;
1846
+ const current = readSettings(config);
1847
+ const accounts = request.platform === "tavily" ? current.tavilyAccounts : request.platform === "tinyfish" ? current.tinyfishAccounts : [];
1848
+ const match = buildPool(request.platform === "tavily" ? "tavily" : "tinyfish", accounts).find((account) => account.label === request.label);
1849
+ if (match === void 0) throw new Error(`unknown account: ${String(request.platform)}/${String(request.label)}`);
1850
+ return { key: match.key };
1851
+ });
1852
+ register(FAILOVER_ACCOUNTS_ENDPOINT, () => {
1853
+ const current = readSettings(config);
1854
+ return { accounts: [...collectAccounts(TINYFISH_PLATFORM_ID, current.tinyfishAccounts), ...collectAccounts(TAVILY_PLATFORM_ID, current.tavilyAccounts)] };
1800
1855
  });
1801
1856
  });
1802
1857
  }
@@ -1809,4 +1864,4 @@ function collectAccounts(platform, accounts) {
1809
1864
  }));
1810
1865
  }
1811
1866
  //#endregion
1812
- export { Config, FAILOVER_ACCOUNTS_ENDPOINT, FAILOVER_PROVIDER_ID, FAILOVER_REFRESH_ENDPOINT, FAILOVER_REVEAL_ENDPOINT, FAILOVER_ROW_ID, FAILOVER_SETTINGS_NAMESPACE, FAILOVER_TOGGLE_ENDPOINT, FAILOVER_TOGGLE_SET_ENDPOINT, FAILOVER_USAGE_CHANNEL, FAILOVER_USAGE_ENDPOINT, FailoverSearchProvider, SessionToggleStore, UsageStore, apply, buildPool, ensureWebSelection, inject, maskApiKey, name, parsePublishedAt, usageFingerprint };
1867
+ export { Config, FAILOVER_ACCOUNTS_ENDPOINT, FAILOVER_PROVIDER_ID, FAILOVER_REFRESH_ENDPOINT, FAILOVER_REVEAL_ENDPOINT, FAILOVER_ROUTES_PREFIX, FAILOVER_ROW_ID, FAILOVER_SETTINGS_NAMESPACE, FAILOVER_TOGGLE_ENDPOINT, FAILOVER_TOGGLE_SET_ENDPOINT, FAILOVER_USAGE_ENDPOINT, FailoverSearchProvider, SessionToggleStore, UsageStore, apply, buildPool, ensureWebSelection, failoverRoutePath, inject, maskApiKey, name, parsePublishedAt, usageFingerprint };
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tnnevol/dsh-failover-search",
3
3
  "displayName": "Failover Search",
4
4
  "type": "module",
5
- "version": "7.0.1",
5
+ "version": "7.0.2",
6
6
  "description": "Composite DSH web-search provider that fails over TinyFish → Tavily → official DeepSeek, spreads requests across multiple accounts per platform, and shows platform usage on the plugin page.",
7
7
  "author": "tnnevol",
8
8
  "license": "Apache-2.0",
@@ -118,8 +118,8 @@
118
118
  "tsdown": "^0.22.2",
119
119
  "typescript": "^5.9.3",
120
120
  "vitest": "^4.1.8",
121
- "@tnnevol/dsh-semi-ui": "7.0.1",
122
- "@tnnevol/dsh-e2e-kit": "7.0.1"
121
+ "@tnnevol/dsh-e2e-kit": "7.0.2",
122
+ "@tnnevol/dsh-semi-ui": "7.0.2"
123
123
  },
124
124
  "scripts": {
125
125
  "build": "tsdown",