@qzhike/agent-search 1.0.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.
Files changed (40) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +123 -0
  3. package/dist/adapters/brave-config.js +46 -0
  4. package/dist/adapters/brave.js +83 -0
  5. package/dist/adapters/deeproute-mirror.js +31 -0
  6. package/dist/adapters/exa-config.js +35 -0
  7. package/dist/adapters/exa.js +91 -0
  8. package/dist/adapters/jina-config.js +102 -0
  9. package/dist/adapters/jina-reader.js +83 -0
  10. package/dist/adapters/jina.js +90 -0
  11. package/dist/adapters/kimi.js +196 -0
  12. package/dist/adapters/metaso-config.js +35 -0
  13. package/dist/adapters/metaso.js +102 -0
  14. package/dist/adapters/minimax-config.js +54 -0
  15. package/dist/adapters/minimax.js +85 -0
  16. package/dist/adapters/provider-auth.js +18 -0
  17. package/dist/adapters/ragflow-config.js +63 -0
  18. package/dist/adapters/ragflow.js +170 -0
  19. package/dist/adapters/searxng-config.js +40 -0
  20. package/dist/adapters/searxng.js +86 -0
  21. package/dist/adapters/serper-config.js +35 -0
  22. package/dist/adapters/serper.js +80 -0
  23. package/dist/adapters/tavily-config.js +46 -0
  24. package/dist/adapters/tavily.js +98 -0
  25. package/dist/adapters/zai-config.js +39 -0
  26. package/dist/adapters/zai.js +91 -0
  27. package/dist/config-schema.js +221 -0
  28. package/dist/http.js +49 -0
  29. package/dist/index.js +84 -0
  30. package/dist/plugin-meta.js +11 -0
  31. package/dist/read-url.js +38 -0
  32. package/dist/result.js +326 -0
  33. package/dist/run-provider.js +42 -0
  34. package/dist/search-concurrent.js +23 -0
  35. package/dist/search-fallback.js +14 -0
  36. package/dist/search.js +43 -0
  37. package/dist/types.js +2 -0
  38. package/openclaw.plugin.json +547 -0
  39. package/package.json +25 -0
  40. package/skills/qzhike-agent-search/SKILL.md +90 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Qzhike
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,123 @@
1
+ # @qzhike/agent-search
2
+
3
+ OpenClaw 插件:为 Agent 提供 **原生工具** `qzhike_search`(多后端检索 + RAGFlow 知识库)与 `qzhike_read_url`(Jina Reader 读页)。安装后随包加载 Skill **`qzhike-agent-search`**,指导工具路由与编排。
4
+
5
+ ## 工具
6
+
7
+ | 工具 | 用途 |
8
+ |---|---|
9
+ | **`qzhike_search`** | 知识库(RAGFlow)+ 公网搜索;支持 fallback / concurrent |
10
+ | **`qzhike_read_url`** | 给定 URL 拉取可读 Markdown 正文 |
11
+
12
+ 配置路径:`plugins.entries.qzhike-agent-search.config`(OpenClaw 解析 `${ENV}`;**插件不读** `process.env`)。
13
+
14
+ ## 安装
15
+
16
+ ```bash
17
+ openclaw plugins install @qzhike/agent-search
18
+ openclaw gateway restart
19
+ openclaw plugins inspect qzhike-agent-search --runtime --json
20
+ ```
21
+
22
+ 本地路径(开发):
23
+
24
+ ```bash
25
+ openclaw plugins install --link /absolute/path/to/qzhike-agent-search
26
+ ```
27
+
28
+ ## OpenClaw 配置
29
+
30
+ ### 启用插件与工具
31
+
32
+ ```json5
33
+ {
34
+ plugins: {
35
+ allow: ["qzhike-agent-search"],
36
+ entries: {
37
+ "qzhike-agent-search": {
38
+ enabled: true,
39
+ config: {
40
+ searchMode: "fallback",
41
+ fallback: ["ragflow", "searxng", "brave", "tavily"],
42
+ providers: {
43
+ ragflow: {
44
+ auth: "deeproute",
45
+ baseUrl: "${DEEPROUTE_CONSOLE_URL}",
46
+ apiKey: "${DEEPROUTE_API_KEY}",
47
+ },
48
+ brave: {
49
+ auth: "deeproute",
50
+ baseUrl: "${DEEPROUTE_CONSOLE_URL}",
51
+ apiKey: "${DEEPROUTE_API_KEY}",
52
+ },
53
+ },
54
+ },
55
+ },
56
+ },
57
+ },
58
+ tools: {
59
+ allow: ["qzhike_search", "qzhike_read_url"],
60
+ deny: ["web_search"],
61
+ },
62
+ }
63
+ ```
64
+
65
+ 在 `~/.openclaw/.env` 中设置 `DEEPROUTE_CONSOLE_URL`、`DEEPROUTE_API_KEY` 等占位符。更完整的 DeepRoute 全 provider 模板见开源仓库 **`examples/openclaw.config.json5`**(npm 包内不含该文件)。
66
+
67
+ ### DeepRoute 扩展服务(凭据托管)
68
+
69
+ 插件 `auth: "deeproute"` 时,只需 Console **origin + user `sk`**;厂商 Key、RAGFlow `dataset_ids`、SearXNG Base URL 等在 Console **扩展服务**入库。
70
+
71
+ | 插件 provider | Console 扩展服务键 |
72
+ |---|---|
73
+ | `ragflow` | `ragflow_base_url`、`ragflow`、`ragflow_dataset_ids` |
74
+ | `searxng` | `searxng` |
75
+ | `brave` / `tavily` / `serper` / `metaso` / `zai` / `exa` / `jina` / `minimax` | 同名键 |
76
+ | `kimi` | 不走 mirror;`baseUrl`: `{Console}/api/v1` + 同一 user `sk` |
77
+
78
+ ## Bundled Skill
79
+
80
+ 本包含 **`skills/qzhike-agent-search/SKILL.md`**,在 `openclaw.plugin.json` 中声明;**启用插件并 restart Gateway 后自动加载**,一般无需再 symlink 到 `~/.openclaw/skills/`。
81
+
82
+ Skill 约定:
83
+
84
+ - 检索类问题优先 **`qzhike_search`**(组织内文档 → 将 `ragflow` 放在 `fallback` 前列)
85
+ - 已有 URL 需全文 → **`qzhike_read_url`**
86
+ - 已安装本插件时 **`deny: ["web_search"]`**,避免与内置搜索冲突
87
+
88
+ 可在 `~/.openclaw/workspace/TOOLS.md` 中强化路由规则(见 Skill 正文)。
89
+
90
+ ## 工具 API(摘要)
91
+
92
+ ### `qzhike_search`
93
+
94
+ | 参数 | 说明 |
95
+ |---|---|
96
+ | `query` | 必填,检索词 |
97
+ | `count` | 可选,1–10 |
98
+ | `searchMode` | 可选,`fallback` / `concurrent` |
99
+ | `provider` | 可选,强制单一 backend |
100
+
101
+ ### `qzhike_read_url`
102
+
103
+ | 参数 | 说明 |
104
+ |---|---|
105
+ | `url` | 必填,HTTP(S) URL |
106
+
107
+ ## 排错
108
+
109
+ | 现象 | 检查 |
110
+ |---|---|
111
+ | 工具未出现 | `plugins.allow` 含 `qzhike-agent-search`;`tools.allow` 含两个工具名 |
112
+ | provider 被跳过 | `plugins.entries.qzhike-agent-search.config.providers.*` 是否配齐 |
113
+ | RAGFlow 无结果 | Console 扩展服务 `ragflow_*`;deeproute 下勿在插件 config 写 `dataset_ids` |
114
+ | 改配置不生效 | `openclaw gateway restart` 并新开会话 |
115
+
116
+ ## 要求
117
+
118
+ - Node >= 22
119
+ - OpenClaw >= 2026.5.17
120
+
121
+ ## License
122
+
123
+ MIT
@@ -0,0 +1,46 @@
1
+ import { configString, trimTrailingSlashes } from "../http.js";
2
+ import { resolveDeeprouteMirrorRoot } from "./deeproute-mirror.js";
3
+ import { resolveProviderAuthMode } from "./provider-auth.js";
4
+ export const DEFAULT_BRAVE_DIRECT_BASE = "https://api.search.brave.com";
5
+ export const BRAVE_WEB_SEARCH_PATH = "/res/v1/web/search";
6
+ export function resolveBraveAuthMode(auth) {
7
+ return resolveProviderAuthMode(auth);
8
+ }
9
+ export function resolveBraveBaseUrl(cfg, auth) {
10
+ const configured = configString(cfg?.baseUrl);
11
+ if (configured) {
12
+ if (auth === "deeproute") {
13
+ return resolveDeeprouteMirrorRoot(configured, "brave");
14
+ }
15
+ return trimTrailingSlashes(configured);
16
+ }
17
+ if (auth === "direct") {
18
+ return DEFAULT_BRAVE_DIRECT_BASE;
19
+ }
20
+ return undefined;
21
+ }
22
+ export function buildBraveSearchUrl(baseUrl, query, count, extra) {
23
+ const url = new URL(`${baseUrl}${BRAVE_WEB_SEARCH_PATH}`);
24
+ url.searchParams.set("q", query);
25
+ url.searchParams.set("count", String(count));
26
+ const country = extra?.country?.trim();
27
+ if (country) {
28
+ url.searchParams.set("country", country);
29
+ }
30
+ const freshness = extra?.freshness?.trim();
31
+ if (freshness) {
32
+ url.searchParams.set("freshness", freshness);
33
+ }
34
+ return url.toString();
35
+ }
36
+ export function buildBraveRequestHeaders(auth, apiKey) {
37
+ const headers = { Accept: "application/json" };
38
+ if (auth === "deeproute") {
39
+ headers.Authorization = `Bearer ${apiKey}`;
40
+ }
41
+ else {
42
+ headers["X-Subscription-Token"] = apiKey;
43
+ }
44
+ return headers;
45
+ }
46
+ //# sourceMappingURL=brave-config.js.map
@@ -0,0 +1,83 @@
1
+ import { configString, fetchJson } from "../http.js";
2
+ import { formatResultsAsContent, resolveTimeoutSeconds } from "../result.js";
3
+ import { buildBraveRequestHeaders, buildBraveSearchUrl, resolveBraveAuthMode, resolveBraveBaseUrl, } from "./brave-config.js";
4
+ export async function searchBrave(config, query, count, signal, runOptions) {
5
+ const provider = "brave";
6
+ const cfg = config.providers?.brave;
7
+ const auth = resolveBraveAuthMode(cfg?.auth);
8
+ if (!auth) {
9
+ return {
10
+ ok: false,
11
+ provider,
12
+ error: "invalid_auth",
13
+ message: 'Invalid providers.brave.auth. Use "direct" (Brave API) or "deeproute" (Console mirror).',
14
+ };
15
+ }
16
+ const apiKey = configString(cfg?.apiKey);
17
+ if (!apiKey) {
18
+ return {
19
+ ok: false,
20
+ provider,
21
+ error: "missing_api_key",
22
+ message: auth === "deeproute"
23
+ ? "deeproute: set providers.brave.apiKey to your New API user sk (Bearer)."
24
+ : "direct: set providers.brave.apiKey to your Brave X-Subscription-Token.",
25
+ };
26
+ }
27
+ const baseUrl = resolveBraveBaseUrl(cfg, auth);
28
+ if (!baseUrl) {
29
+ return {
30
+ ok: false,
31
+ provider,
32
+ error: "missing_base_url",
33
+ message: "deeproute: set providers.brave.baseUrl (e.g. http://host:20316/api/mirror/brave).",
34
+ };
35
+ }
36
+ const url = buildBraveSearchUrl(baseUrl, query, count, {
37
+ country: runOptions?.brave?.country ?? cfg?.country,
38
+ freshness: runOptions?.brave?.freshness ?? cfg?.freshness,
39
+ });
40
+ const startedAt = Date.now();
41
+ try {
42
+ const { data } = await fetchJson(url, {
43
+ method: "GET",
44
+ headers: buildBraveRequestHeaders(auth, apiKey),
45
+ timeoutSeconds: resolveTimeoutSeconds(config),
46
+ signal,
47
+ });
48
+ const raw = Array.isArray(data.web?.results) ? data.web.results : [];
49
+ const results = raw.slice(0, count).map((entry) => ({
50
+ title: entry.title ?? "",
51
+ url: entry.url ?? "",
52
+ snippet: entry.description ?? "",
53
+ siteName: entry.url ? safeHostname(entry.url) : undefined,
54
+ }));
55
+ const citations = [...new Set(results.map((r) => r.url).filter(Boolean))];
56
+ return {
57
+ ok: true,
58
+ provider,
59
+ query,
60
+ tookMs: Date.now() - startedAt,
61
+ results,
62
+ citations,
63
+ content: formatResultsAsContent(results),
64
+ };
65
+ }
66
+ catch (err) {
67
+ return {
68
+ ok: false,
69
+ provider,
70
+ error: "brave_request_failed",
71
+ message: err instanceof Error ? err.message : String(err),
72
+ };
73
+ }
74
+ }
75
+ function safeHostname(url) {
76
+ try {
77
+ return new URL(url).hostname;
78
+ }
79
+ catch {
80
+ return undefined;
81
+ }
82
+ }
83
+ //# sourceMappingURL=brave.js.map
@@ -0,0 +1,31 @@
1
+ import { trimTrailingSlashes } from "../http.js";
2
+ /**
3
+ * Resolve mirror root for `auth: "deeproute"`.
4
+ * Accepts Console **origin only** (`http://host:20316`) or legacy full path
5
+ * (`…/api/mirror/{provider}`) for backward compatibility.
6
+ */
7
+ export function resolveDeeprouteMirrorRoot(baseUrl, provider) {
8
+ const trimmed = trimTrailingSlashes(baseUrl.trim());
9
+ const mirrorSuffix = `/api/mirror/${provider}`;
10
+ if (trimmed.endsWith(mirrorSuffix)) {
11
+ return trimmed;
12
+ }
13
+ try {
14
+ const href = trimmed.includes("://") ? trimmed : `http://${trimmed}`;
15
+ const u = new URL(href);
16
+ const path = u.pathname.replace(/\/$/, "") || "";
17
+ if (path === mirrorSuffix || path.endsWith(mirrorSuffix)) {
18
+ return `${u.origin}${path}`;
19
+ }
20
+ return `${u.origin}${mirrorSuffix}`;
21
+ }
22
+ catch {
23
+ return `${trimmed}${mirrorSuffix}`;
24
+ }
25
+ }
26
+ export function joinMirrorPath(mirrorRoot, upstreamPath) {
27
+ const root = trimTrailingSlashes(mirrorRoot);
28
+ const path = upstreamPath.startsWith("/") ? upstreamPath : `/${upstreamPath}`;
29
+ return `${root}${path}`;
30
+ }
31
+ //# sourceMappingURL=deeproute-mirror.js.map
@@ -0,0 +1,35 @@
1
+ import { configString, trimTrailingSlashes } from "../http.js";
2
+ import { joinMirrorPath, resolveDeeprouteMirrorRoot, } from "./deeproute-mirror.js";
3
+ import { buildDeeprouteBearerHeaders, resolveProviderAuthMode, } from "./provider-auth.js";
4
+ export const DEFAULT_EXA_DIRECT_BASE = "https://api.exa.ai";
5
+ export const EXA_SEARCH_PATH = "/search";
6
+ export function resolveExaAuthMode(auth) {
7
+ return resolveProviderAuthMode(auth);
8
+ }
9
+ export function resolveExaSearchUrl(cfg, auth) {
10
+ const configured = configString(cfg?.baseUrl);
11
+ if (auth === "deeproute") {
12
+ if (!configured)
13
+ return undefined;
14
+ const root = resolveDeeprouteMirrorRoot(configured, "exa");
15
+ return joinMirrorPath(root, EXA_SEARCH_PATH);
16
+ }
17
+ const base = configured
18
+ ? trimTrailingSlashes(configured)
19
+ : DEFAULT_EXA_DIRECT_BASE;
20
+ return joinMirrorPath(base, EXA_SEARCH_PATH);
21
+ }
22
+ export function buildExaRequestHeaders(auth, apiKey) {
23
+ const headers = {
24
+ Accept: "application/json",
25
+ "Content-Type": "application/json",
26
+ };
27
+ if (auth === "deeproute") {
28
+ Object.assign(headers, buildDeeprouteBearerHeaders(apiKey));
29
+ }
30
+ else {
31
+ headers["x-api-key"] = apiKey;
32
+ }
33
+ return headers;
34
+ }
35
+ //# sourceMappingURL=exa-config.js.map
@@ -0,0 +1,91 @@
1
+ import { configString, fetchJson } from "../http.js";
2
+ import { formatResultsAsContent, resolveTimeoutSeconds } from "../result.js";
3
+ import { buildExaRequestHeaders, resolveExaAuthMode, resolveExaSearchUrl, } from "./exa-config.js";
4
+ export async function searchExa(config, query, count, signal) {
5
+ const provider = "exa";
6
+ const cfg = config.providers?.exa;
7
+ const auth = resolveExaAuthMode(cfg?.auth);
8
+ if (!auth) {
9
+ return {
10
+ ok: false,
11
+ provider,
12
+ error: "invalid_auth",
13
+ message: 'Invalid providers.exa.auth. Use "direct" or "deeproute".',
14
+ };
15
+ }
16
+ const apiKey = configString(cfg?.apiKey);
17
+ if (!apiKey) {
18
+ return {
19
+ ok: false,
20
+ provider,
21
+ error: "missing_api_key",
22
+ message: auth === "deeproute"
23
+ ? "deeproute: set providers.exa.apiKey to your DeepRoute user sk."
24
+ : "direct: set providers.exa.apiKey to your Exa API key.",
25
+ };
26
+ }
27
+ const url = resolveExaSearchUrl(cfg, auth);
28
+ if (!url) {
29
+ return {
30
+ ok: false,
31
+ provider,
32
+ error: "missing_base_url",
33
+ message: "deeproute: set providers.exa.baseUrl to Console origin (e.g. http://host:20316).",
34
+ };
35
+ }
36
+ const startedAt = Date.now();
37
+ try {
38
+ const { data } = await fetchJson(url, {
39
+ method: "POST",
40
+ headers: buildExaRequestHeaders(auth, apiKey),
41
+ body: {
42
+ query,
43
+ numResults: count,
44
+ type: "auto",
45
+ useAutoprompt: true,
46
+ contents: { highlights: { maxCharacters: 500 } },
47
+ },
48
+ timeoutSeconds: resolveTimeoutSeconds(config),
49
+ signal,
50
+ });
51
+ const raw = Array.isArray(data.results) ? data.results : [];
52
+ const results = raw.slice(0, count).map((entry) => {
53
+ const snippet = Array.isArray(entry.highlights) && entry.highlights.length > 0
54
+ ? entry.highlights.join(" ")
55
+ : (entry.text ?? "");
56
+ return {
57
+ title: entry.title ?? "",
58
+ url: entry.url ?? "",
59
+ snippet,
60
+ siteName: entry.url ? safeHostname(entry.url) : undefined,
61
+ };
62
+ });
63
+ const citations = [...new Set(results.map((r) => r.url).filter(Boolean))];
64
+ return {
65
+ ok: true,
66
+ provider,
67
+ query,
68
+ tookMs: Date.now() - startedAt,
69
+ results,
70
+ citations,
71
+ content: formatResultsAsContent(results),
72
+ };
73
+ }
74
+ catch (err) {
75
+ return {
76
+ ok: false,
77
+ provider,
78
+ error: "exa_request_failed",
79
+ message: err instanceof Error ? err.message : String(err),
80
+ };
81
+ }
82
+ }
83
+ function safeHostname(url) {
84
+ try {
85
+ return new URL(url).hostname;
86
+ }
87
+ catch {
88
+ return undefined;
89
+ }
90
+ }
91
+ //# sourceMappingURL=exa.js.map
@@ -0,0 +1,102 @@
1
+ import { configString, trimTrailingSlashes } from "../http.js";
2
+ import { joinMirrorPath, resolveDeeprouteMirrorRoot, } from "./deeproute-mirror.js";
3
+ import { buildDeeprouteBearerHeaders, resolveProviderAuthMode, } from "./provider-auth.js";
4
+ export const DEFAULT_JINA_SEARCH_DIRECT_BASE = "https://s.jina.ai";
5
+ export const DEFAULT_JINA_READER_DIRECT_BASE = "https://r.jina.ai";
6
+ export const JINA_SEARCH_MIRROR_SUFFIX = "/s";
7
+ export const JINA_READER_MIRROR_PREFIX = "/r";
8
+ export function resolveJinaAuthMode(auth) {
9
+ return resolveProviderAuthMode(auth);
10
+ }
11
+ export function resolveJinaSearchBaseUrl(cfg, auth) {
12
+ const configured = configString(cfg?.baseUrl);
13
+ if (configured) {
14
+ if (auth === "deeproute") {
15
+ return resolveDeeprouteMirrorRoot(configured, "jina");
16
+ }
17
+ return trimTrailingSlashes(configured);
18
+ }
19
+ if (auth === "direct") {
20
+ return DEFAULT_JINA_SEARCH_DIRECT_BASE;
21
+ }
22
+ return undefined;
23
+ }
24
+ export function resolveJinaSearchUrl(baseUrl, auth) {
25
+ if (auth === "deeproute") {
26
+ return joinMirrorPath(baseUrl, JINA_SEARCH_MIRROR_SUFFIX);
27
+ }
28
+ const trimmed = trimTrailingSlashes(baseUrl || DEFAULT_JINA_SEARCH_DIRECT_BASE);
29
+ try {
30
+ const url = new URL(trimmed);
31
+ if (url.hostname.toLowerCase() === "s.jina.ai") {
32
+ url.pathname = url.pathname === "" ? "/" : url.pathname;
33
+ return url.toString();
34
+ }
35
+ url.pathname = `${url.pathname.replace(/\/$/, "")}/`;
36
+ return url.toString();
37
+ }
38
+ catch {
39
+ return `${DEFAULT_JINA_SEARCH_DIRECT_BASE}/`;
40
+ }
41
+ }
42
+ export function resolveJinaReaderBaseUrl(cfg, auth) {
43
+ const configured = configString(cfg?.baseUrl);
44
+ if (configured) {
45
+ if (auth === "deeproute") {
46
+ return resolveDeeprouteMirrorRoot(configured, "jina");
47
+ }
48
+ return trimTrailingSlashes(configured);
49
+ }
50
+ if (auth === "direct") {
51
+ return DEFAULT_JINA_READER_DIRECT_BASE;
52
+ }
53
+ return undefined;
54
+ }
55
+ /** Build GET URL for Jina Reader (direct r.jina.ai or DeepRoute /api/mirror/jina/r/…). */
56
+ export function resolveJinaReaderUrl(baseUrl, auth, targetPageUrl) {
57
+ const encoded = encodeURI(targetPageUrl.trim());
58
+ if (auth === "deeproute") {
59
+ return joinMirrorPath(baseUrl, `${JINA_READER_MIRROR_PREFIX}/${encoded}`);
60
+ }
61
+ const root = trimTrailingSlashes(baseUrl || DEFAULT_JINA_READER_DIRECT_BASE);
62
+ try {
63
+ const url = new URL(root);
64
+ if (url.hostname.toLowerCase() === "r.jina.ai") {
65
+ return `${url.origin}/${encoded}`;
66
+ }
67
+ return `${root}/${encoded}`;
68
+ }
69
+ catch {
70
+ return `${DEFAULT_JINA_READER_DIRECT_BASE}/${encoded}`;
71
+ }
72
+ }
73
+ export function buildJinaReaderRequestHeaders(auth, apiKey) {
74
+ const headers = { Accept: "application/json" };
75
+ if (auth === "deeproute") {
76
+ if (apiKey) {
77
+ Object.assign(headers, buildDeeprouteBearerHeaders(apiKey));
78
+ }
79
+ return headers;
80
+ }
81
+ if (apiKey) {
82
+ headers.Authorization = `Bearer ${apiKey}`;
83
+ }
84
+ return headers;
85
+ }
86
+ export function buildJinaSearchRequestHeaders(auth, apiKey) {
87
+ const headers = {
88
+ Accept: "application/json",
89
+ "Content-Type": "application/json",
90
+ };
91
+ if (auth === "deeproute") {
92
+ if (!apiKey) {
93
+ return headers;
94
+ }
95
+ Object.assign(headers, buildDeeprouteBearerHeaders(apiKey));
96
+ }
97
+ else if (apiKey) {
98
+ headers.Authorization = `Bearer ${apiKey}`;
99
+ }
100
+ return headers;
101
+ }
102
+ //# sourceMappingURL=jina-config.js.map
@@ -0,0 +1,83 @@
1
+ import { configString, fetchJson } from "../http.js";
2
+ import { resolveTimeoutSeconds } from "../result.js";
3
+ import { buildJinaReaderRequestHeaders, resolveJinaAuthMode, resolveJinaReaderBaseUrl, resolveJinaReaderUrl, } from "./jina-config.js";
4
+ function extractReaderFields(data, fallbackUrl) {
5
+ const payload = data.data ?? data;
6
+ return {
7
+ title: payload.title?.trim() ?? "",
8
+ content: payload.content?.trim() ?? "",
9
+ url: payload.url?.trim() || fallbackUrl,
10
+ };
11
+ }
12
+ export async function readUrlWithJina(config, targetUrl, signal) {
13
+ const provider = "jina";
14
+ const cfg = config.providers?.jina;
15
+ const auth = resolveJinaAuthMode(cfg?.auth) ?? "direct";
16
+ const apiKey = configString(cfg?.apiKey);
17
+ if (auth === "deeproute" && !apiKey) {
18
+ return {
19
+ ok: false,
20
+ provider,
21
+ error: "missing_api_key",
22
+ message: "deeproute: set providers.jina.apiKey to your DeepRoute user sk (Bearer). Reader does not require a Console Jina vendor key.",
23
+ };
24
+ }
25
+ const baseUrl = resolveJinaReaderBaseUrl(cfg, auth);
26
+ if (!baseUrl) {
27
+ return {
28
+ ok: false,
29
+ provider,
30
+ error: "missing_base_url",
31
+ message: "deeproute: set providers.jina.baseUrl to Console origin (e.g. http://host:20316).",
32
+ };
33
+ }
34
+ let normalizedTarget;
35
+ try {
36
+ normalizedTarget = new URL(targetUrl).toString();
37
+ }
38
+ catch {
39
+ return {
40
+ ok: false,
41
+ provider,
42
+ error: "invalid_url",
43
+ message: `Invalid URL: ${targetUrl}`,
44
+ };
45
+ }
46
+ const url = resolveJinaReaderUrl(baseUrl, auth, normalizedTarget);
47
+ const startedAt = Date.now();
48
+ try {
49
+ const { data, text } = await fetchJson(url, {
50
+ method: "GET",
51
+ headers: buildJinaReaderRequestHeaders(auth, apiKey),
52
+ timeoutSeconds: resolveTimeoutSeconds(config),
53
+ signal,
54
+ });
55
+ const { title, content, url: resolvedUrl, } = extractReaderFields(data, normalizedTarget);
56
+ const bodyText = content || (text && !content ? text.slice(0, 500_000) : "");
57
+ if (!bodyText.trim()) {
58
+ return {
59
+ ok: false,
60
+ provider,
61
+ error: "empty_content",
62
+ message: "Jina Reader returned no content for this URL.",
63
+ };
64
+ }
65
+ return {
66
+ ok: true,
67
+ provider,
68
+ url: resolvedUrl,
69
+ tookMs: Date.now() - startedAt,
70
+ title,
71
+ content: bodyText,
72
+ };
73
+ }
74
+ catch (err) {
75
+ return {
76
+ ok: false,
77
+ provider,
78
+ error: "jina_reader_failed",
79
+ message: err instanceof Error ? err.message : String(err),
80
+ };
81
+ }
82
+ }
83
+ //# sourceMappingURL=jina-reader.js.map