@wkgeo/cli 0.0.0-stage → 1.0.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 +208 -0
- package/LICENSE +21 -0
- package/README.md +1042 -3
- package/SKILL.md +927 -0
- package/lib/ai/client.mjs +161 -0
- package/lib/ai/config.mjs +221 -0
- package/lib/ai/context.mjs +468 -0
- package/lib/ai/log.mjs +53 -0
- package/lib/api.mjs +339 -0
- package/lib/args.mjs +235 -0
- package/lib/cli.mjs +168 -0
- package/lib/cms.mjs +330 -0
- package/lib/commands/ai.mjs +392 -0
- package/lib/commands/apps.mjs +116 -0
- package/lib/commands/auth.mjs +1288 -0
- package/lib/commands/billing.mjs +666 -0
- package/lib/commands/cms.mjs +2533 -0
- package/lib/commands/config-auth.mjs +621 -0
- package/lib/commands/config.mjs +434 -0
- package/lib/commands/dnscred.mjs +428 -0
- package/lib/commands/domainops.mjs +546 -0
- package/lib/commands/domainorder.mjs +439 -0
- package/lib/commands/import.mjs +164 -0
- package/lib/commands/index.mjs +528 -0
- package/lib/commands/logs.mjs +141 -0
- package/lib/commands/orders.mjs +444 -0
- package/lib/commands/overview.mjs +394 -0
- package/lib/commands/plugins.mjs +678 -0
- package/lib/commands/site.mjs +435 -0
- package/lib/commands/siteops.mjs +1012 -0
- package/lib/commands/sites.mjs +905 -0
- package/lib/commands/skill.mjs +119 -0
- package/lib/config.mjs +351 -0
- package/lib/credentials.mjs +160 -0
- package/lib/device-store.mjs +132 -0
- package/lib/device.mjs +464 -0
- package/lib/errors.mjs +299 -0
- package/lib/format.mjs +339 -0
- package/lib/http.mjs +71 -0
- package/lib/index.mjs +44 -0
- package/lib/local-import.mjs +185 -0
- package/lib/login.mjs +1638 -0
- package/lib/oauth-urls.mjs +163 -0
- package/lib/page.mjs +393 -0
- package/lib/pkce.mjs +48 -0
- package/lib/proxy.mjs +454 -0
- package/lib/qr.mjs +31 -0
- package/lib/runtime.mjs +650 -0
- package/lib/site-state.mjs +137 -0
- package/package.json +53 -4
- package/wkgeo.mjs +17 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地大模型的 HTTP 客户端(OpenAI 兼容协议:`GET /models`、`POST /chat/completions`)。
|
|
3
|
+
*
|
|
4
|
+
* 三条硬规矩(都是"数据不出本机"的一部分):
|
|
5
|
+
* 1. **只连本机**:非 localhost/127.0.0.1/::1 的地址在发请求**之前**就被 `assertLocalEndpoint` 拒掉;
|
|
6
|
+
* 2. **串行**:本文件不提供并发接口(一次调用一条请求),别为了快改成 `Promise.all`;
|
|
7
|
+
* 3. **只重试一次**,且只有网络错误与 5xx 才重试 —— 4xx 重试没意义(还会把限流打得更死)。
|
|
8
|
+
*
|
|
9
|
+
* ⚠️ 任何错误文案里都不许出现 `apiKey`;`Authorization` 头只在内部拼一次。
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { request } from '../http.mjs';
|
|
13
|
+
import { CliError } from '../errors.mjs';
|
|
14
|
+
import { assertLocalEndpoint, hostOf } from './config.mjs';
|
|
15
|
+
|
|
16
|
+
/** 粗略估 token(中文 1 字≈1 token,英文 4 字符≈1 token;只用于日志与预算提示) */
|
|
17
|
+
export function estimateTokens(text) {
|
|
18
|
+
const s = String(text || '');
|
|
19
|
+
if (!s) return 0;
|
|
20
|
+
return Math.ceil(s.length / 1.7);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** 把"调不通"翻译成人话(每种错误给一条下一步,别只抛堆栈) */
|
|
24
|
+
function aiError(message, { kind = 'ai_unreachable', hint = '', cause } = {}) {
|
|
25
|
+
return new CliError(message, { kind, hint, cause });
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function reachHint(baseUrl) {
|
|
29
|
+
return (
|
|
30
|
+
`连不上本地模型(${hostOf(baseUrl) || baseUrl})。先确认服务在跑:` +
|
|
31
|
+
'Ollama 用 `ollama serve`(默认 11434);LM Studio 在「Local Server」页点 Start(默认 1234)。'
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @param {object} opts
|
|
37
|
+
* @param {object} opts.config `resolveAiConfig()` 的返回值
|
|
38
|
+
* @param {Function} [opts.fetchImpl] 单测注入
|
|
39
|
+
*/
|
|
40
|
+
export function createAiClient({ config, fetchImpl = globalThis.fetch } = {}) {
|
|
41
|
+
const base = String(config.baseUrl || '').replace(/\/+$/, '');
|
|
42
|
+
const timeoutMs = config.timeoutMs || 60000;
|
|
43
|
+
const model = String(config.model || '');
|
|
44
|
+
|
|
45
|
+
/** 发一次请求(含 1 次重试);返回 `{status, body, text}` */
|
|
46
|
+
async function call(method, pathname, body, { timeoutMs: t } = {}) {
|
|
47
|
+
const url = `${base}${pathname}`;
|
|
48
|
+
const headers = { Accept: 'application/json' };
|
|
49
|
+
if (config.apiKey) headers.Authorization = `Bearer ${config.apiKey}`;
|
|
50
|
+
if (body !== undefined) headers['Content-Type'] = 'application/json';
|
|
51
|
+
|
|
52
|
+
const once = () =>
|
|
53
|
+
request(url, {
|
|
54
|
+
method,
|
|
55
|
+
headers,
|
|
56
|
+
body: body === undefined ? undefined : JSON.stringify(body),
|
|
57
|
+
timeoutMs: t || timeoutMs,
|
|
58
|
+
fetchImpl,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
let res = await once();
|
|
62
|
+
const retryable = Boolean(res.networkError) || (typeof res.status === 'number' && res.status >= 500);
|
|
63
|
+
if (retryable) res = await once();
|
|
64
|
+
|
|
65
|
+
if (res.networkError) {
|
|
66
|
+
const timedOut = /超时/.test(String(res.networkError));
|
|
67
|
+
throw aiError(
|
|
68
|
+
timedOut ? `本地模型 ${Math.round((t || timeoutMs) / 1000)}s 内没有响应` : `请求本地模型失败:${res.networkError}`,
|
|
69
|
+
{
|
|
70
|
+
kind: timedOut ? 'ai_timeout' : 'ai_unreachable',
|
|
71
|
+
hint: timedOut
|
|
72
|
+
? '7B 模型首次加载会慢;可以 `--model` 换更小的模型,或在 ~/.wkongai/config.json 里调大 ai.timeoutMs'
|
|
73
|
+
: reachHint(base),
|
|
74
|
+
cause: res.cause,
|
|
75
|
+
},
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
if (res.status === 404) {
|
|
80
|
+
throw aiError(`本地模型端点返回 404(${method} ${pathname})`, {
|
|
81
|
+
kind: 'ai_not_found',
|
|
82
|
+
hint:
|
|
83
|
+
'这个地址上大概率不是 OpenAI 兼容接口:Ollama 要带 /v1(默认 http://127.0.0.1:11434/v1),' +
|
|
84
|
+
'LM Studio 是 http://127.0.0.1:1234/v1。用 `wkgeo ai ping` 验证。',
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
if (res.status === 401 || res.status === 403) {
|
|
88
|
+
throw aiError(`本地模型要求鉴权(HTTP ${res.status})`, {
|
|
89
|
+
kind: 'ai_auth',
|
|
90
|
+
hint: `如果这个端点真的要 Key:设置环境变量 ${config.apiKeyEnv || 'WKONGAI_AI_KEY'}(不会打印出来)`,
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
if (res.status === 429) {
|
|
94
|
+
throw aiError('本地模型限流(HTTP 429)', { kind: 'ai_rate_limited', hint: '等几秒再试;一次只跑一条请求。' });
|
|
95
|
+
}
|
|
96
|
+
if (res.status >= 400) {
|
|
97
|
+
const text = String(res.text || '');
|
|
98
|
+
const tooLong = /context|length|token/i.test(text);
|
|
99
|
+
throw aiError(`本地模型返回 HTTP ${res.status}${text ? `:${text.slice(0, 300)}` : ''}`, {
|
|
100
|
+
kind: tooLong ? 'ai_context_too_long' : 'ai_bad_request',
|
|
101
|
+
hint: tooLong
|
|
102
|
+
? '上下文超限:减少 `--context` 里的数据域(例如只留 site,overview),或调小 ai.maxContextChars'
|
|
103
|
+
: '模型名不对时也会走这里:`wkgeo ai ping` 看可用模型,再用 `--model` 指定。',
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (res.parseError) {
|
|
107
|
+
throw aiError('本地模型返回的不是 JSON(端点地址可能不对)', { kind: 'ai_bad_response', hint: reachHint(base) });
|
|
108
|
+
}
|
|
109
|
+
return res;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
config,
|
|
114
|
+
baseUrl: base,
|
|
115
|
+
model,
|
|
116
|
+
|
|
117
|
+
/** `GET /models` → `['qwen2.5:7b', ...]`(**不打印 Key**) */
|
|
118
|
+
async listModels() {
|
|
119
|
+
const res = await call('GET', '/models');
|
|
120
|
+
const body = res.body || {};
|
|
121
|
+
const rows = Array.isArray(body.data) ? body.data : Array.isArray(body.models) ? body.models : [];
|
|
122
|
+
const ids = rows
|
|
123
|
+
.map((one) => (typeof one === 'string' ? one : one && (one.id || one.name || one.model)))
|
|
124
|
+
.map((one) => String(one || '').trim())
|
|
125
|
+
.filter(Boolean);
|
|
126
|
+
return { models: ids, raw: body };
|
|
127
|
+
},
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* `POST /chat/completions`(非流式)。
|
|
131
|
+
* @param {{messages:Array<{role:string,content:string}>, model?:string, temperature?:number, timeoutMs?:number}} args
|
|
132
|
+
* @returns {Promise<{text:string, model:string, usage:object|null}>}
|
|
133
|
+
*/
|
|
134
|
+
async chat({ messages, model: m, temperature = 0.2, timeoutMs: t } = {}) {
|
|
135
|
+
const useModel = String(m || model || '').trim();
|
|
136
|
+
if (!useModel) throw aiError('没有指定模型(配置 ai.model 或 --model)', { kind: 'ai_no_model', hint: 'wkgeo ai ping 看可用模型' });
|
|
137
|
+
const res = await call(
|
|
138
|
+
'POST',
|
|
139
|
+
'/chat/completions',
|
|
140
|
+
{ model: useModel, messages, temperature, stream: false },
|
|
141
|
+
{ timeoutMs: t },
|
|
142
|
+
);
|
|
143
|
+
const body = res.body || {};
|
|
144
|
+
const choice = Array.isArray(body.choices) ? body.choices[0] : null;
|
|
145
|
+
const content =
|
|
146
|
+
(choice && choice.message && choice.message.content) ||
|
|
147
|
+
(choice && choice.text) ||
|
|
148
|
+
(body.message && body.message.content) ||
|
|
149
|
+
'';
|
|
150
|
+
if (!String(content).trim()) {
|
|
151
|
+
throw aiError('本地模型回了空内容', {
|
|
152
|
+
kind: 'ai_empty',
|
|
153
|
+
hint: '换个模型试试(`--model`);有些模型不支持非流式,Ollama 拉官方模型最稳。',
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return { text: String(content), model: String(body.model || useModel), usage: body.usage || null };
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export { assertLocalEndpoint };
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地大模型的配置解析(`wkgeo ai` / `wkgeo context export` 共用)。
|
|
3
|
+
*
|
|
4
|
+
* 配置优先级(从高到低):
|
|
5
|
+
* 1. 命令行 `--model` / `--base-url`(只覆盖单次调用)
|
|
6
|
+
* 2. 环境变量 `WKONGAI_AI_*`
|
|
7
|
+
* 3. `~/.wkongai/config.json` 里的 `ai` 段(与 `wkgeo site use` 的 `cms_site` 同一个文件,
|
|
8
|
+
* `writeSiteState()` 写站点时会**保留**其它键,所以两者不会互相踩)
|
|
9
|
+
* 4. 内置默认值(`AI_DEFAULTS`)—— 对 Ollama / LM Studio 的 OpenAI 兼容端点都开箱可用
|
|
10
|
+
*
|
|
11
|
+
* ⚠️ 这里**绝不**打印 apiKey:只允许出现"用了哪个环境变量 / 长度"。
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import fs from 'node:fs';
|
|
15
|
+
import { CliError } from '../errors.mjs';
|
|
16
|
+
|
|
17
|
+
/** 默认值:本机 Ollama(OpenAI 兼容层)+ 7B 量化模型 */
|
|
18
|
+
export const AI_DEFAULTS = Object.freeze({
|
|
19
|
+
provider: 'ollama',
|
|
20
|
+
/** Ollama 的 OpenAI 兼容根:`/v1` 不能少(`/v1/chat/completions`) */
|
|
21
|
+
baseUrl: 'http://127.0.0.1:11434/v1',
|
|
22
|
+
model: 'qwen2.5:7b',
|
|
23
|
+
/** 从哪个环境变量取 Key(本地模型通常不需要;LM Studio 也不校验) */
|
|
24
|
+
apiKeyEnv: 'WKONGAI_AI_KEY',
|
|
25
|
+
/** 单次请求超时(毫秒);本地 7B 首次加载会慢,给足 60s */
|
|
26
|
+
timeoutMs: 60000,
|
|
27
|
+
/** 拼进 prompt 的上下文上限(字符数,不是 token) */
|
|
28
|
+
maxContextChars: 120000,
|
|
29
|
+
/** 允许连非本机地址(**默认关**;开了会在启动时打印警告) */
|
|
30
|
+
allowRemote: false,
|
|
31
|
+
/** 允许把订单**聚合**(条数/总额)给模型(默认关;明细永远不给) */
|
|
32
|
+
allowOrders: false,
|
|
33
|
+
/** 额外放行的数据域(白名单之外一律不放) */
|
|
34
|
+
allowData: [],
|
|
35
|
+
/** 硬禁用:无论怎么配都不给(`orders` 不在这里:它由 `allowOrders` 单独把关) */
|
|
36
|
+
denyData: ['wallet', 'users'],
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
/** 本机回环地址白名单(安全边界就是这一行) */
|
|
40
|
+
const LOOPBACK_HOSTS = new Set(['localhost', '127.0.0.1', '::1', '[::1]', '0:0:0:0:0:0:0:1', '::ffff:127.0.0.1']);
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* 这个地址是不是本机?
|
|
44
|
+
* @param {string} baseUrl
|
|
45
|
+
* @returns {boolean}
|
|
46
|
+
*/
|
|
47
|
+
export function isLoopbackUrl(baseUrl) {
|
|
48
|
+
try {
|
|
49
|
+
const u = new URL(String(baseUrl || ''));
|
|
50
|
+
const host = String(u.hostname || '').toLowerCase();
|
|
51
|
+
return LOOPBACK_HOSTS.has(host) || host.endsWith('.localhost');
|
|
52
|
+
} catch {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** 取 `url` 的 host(只用于报错文案,**不带** path/query,避免把 Key 拼在 URL 里时泄漏) */
|
|
58
|
+
export function hostOf(baseUrl) {
|
|
59
|
+
try {
|
|
60
|
+
return new URL(String(baseUrl || '')).host || '';
|
|
61
|
+
} catch {
|
|
62
|
+
return '';
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* 闸门:非本机地址默认拒绝(这是"数据不出本机"的唯一执行点)。
|
|
68
|
+
*
|
|
69
|
+
* @param {string} baseUrl
|
|
70
|
+
* @param {{allowRemote?:boolean}} [opts]
|
|
71
|
+
* @returns {{local:boolean, remote:boolean}}
|
|
72
|
+
*/
|
|
73
|
+
export function assertLocalEndpoint(baseUrl, { allowRemote = false } = {}) {
|
|
74
|
+
if (isLoopbackUrl(baseUrl)) return { local: true, remote: false };
|
|
75
|
+
if (allowRemote === true) return { local: false, remote: true };
|
|
76
|
+
throw new CliError(`拒绝连接非本机地址「${hostOf(baseUrl) || baseUrl}」`, {
|
|
77
|
+
kind: 'ai_remote_blocked',
|
|
78
|
+
hint:
|
|
79
|
+
'本地模型默认只允许 localhost / 127.0.0.1 / ::1。确实要连局域网或云端的 OpenAI 兼容端点,' +
|
|
80
|
+
'请在 ~/.wkongai/config.json 里把 ai.allowRemote 设为 true(站点数据会离开本机,风险自负)。',
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** 布尔环境变量:`1/true/yes/on` 为真,`0/false/no/off/''` 为假 */
|
|
85
|
+
function envFlag(value) {
|
|
86
|
+
if (value === undefined || value === null || value === '') return undefined;
|
|
87
|
+
const s = String(value).trim().toLowerCase();
|
|
88
|
+
if (['1', 'true', 'yes', 'on'].includes(s)) return true;
|
|
89
|
+
if (['0', 'false', 'no', 'off'].includes(s)) return false;
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function toInt(value, fallback) {
|
|
94
|
+
const n = Number(value);
|
|
95
|
+
return Number.isFinite(n) && n > 0 ? Math.floor(n) : fallback;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 读 `~/.wkongai/config.json` 里的 `ai` 段(读不到就当空;坏 JSON 直接报错,别静默用默认值) */
|
|
99
|
+
export function readAiConfig(configFile) {
|
|
100
|
+
if (!configFile) return {};
|
|
101
|
+
let text = '';
|
|
102
|
+
try {
|
|
103
|
+
text = fs.readFileSync(configFile, 'utf8');
|
|
104
|
+
} catch {
|
|
105
|
+
return {};
|
|
106
|
+
}
|
|
107
|
+
if (!text.trim()) return {};
|
|
108
|
+
let parsed;
|
|
109
|
+
try {
|
|
110
|
+
parsed = JSON.parse(text);
|
|
111
|
+
} catch (e) {
|
|
112
|
+
throw new CliError(`配置文件不是合法 JSON:${configFile}`, {
|
|
113
|
+
kind: 'config',
|
|
114
|
+
hint: '修好 JSON 再跑(这个文件里还有 `wkgeo site use` 记住的站点,别直接删)',
|
|
115
|
+
cause: e,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const ai = parsed && typeof parsed === 'object' ? parsed.ai : null;
|
|
119
|
+
return ai && typeof ai === 'object' && !Array.isArray(ai) ? ai : {};
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/** 把 `--base-url` 之类的值规整成可直接拼 `/chat/completions` 的根(去掉尾斜杠,补 `/v1`) */
|
|
123
|
+
export function normalizeBaseUrl(raw) {
|
|
124
|
+
let text = String(raw || '').trim().replace(/\/+$/, '');
|
|
125
|
+
if (!text) return AI_DEFAULTS.baseUrl;
|
|
126
|
+
if (!/^https?:\/\//i.test(text)) text = `http://${text}`;
|
|
127
|
+
try {
|
|
128
|
+
const u = new URL(text);
|
|
129
|
+
if (!u.pathname || u.pathname === '/') u.pathname = '/v1';
|
|
130
|
+
return `${u.origin}${u.pathname.replace(/\/+$/, '')}`;
|
|
131
|
+
} catch {
|
|
132
|
+
return text;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 算出本次调用真正生效的 AI 配置。
|
|
138
|
+
*
|
|
139
|
+
* @param {object} [opts]
|
|
140
|
+
* @param {object} [opts.flags] 命令行标志(`--model` / `--base-url`)
|
|
141
|
+
* @param {object} [opts.env] 环境变量(默认 `process.env`)
|
|
142
|
+
* @param {string} [opts.siteStateFile] `~/.wkongai/config.json` 的路径(CLI 的 siteStateFile 就是它)
|
|
143
|
+
* @returns {object} 含 `{provider,baseUrl,model,apiKeyEnv,apiKey,timeoutMs,maxContextChars,allowRemote,allowOrders,allowData,denyData,sources}`
|
|
144
|
+
*/
|
|
145
|
+
export function resolveAiConfig({ flags = {}, env = process.env, siteStateFile = '' } = {}) {
|
|
146
|
+
const fromFile = readAiConfig(siteStateFile);
|
|
147
|
+
const sources = [];
|
|
148
|
+
|
|
149
|
+
const choose = (name, fileKey, envKeys, fallback, transform = (v) => v) => {
|
|
150
|
+
const flagVal = flags[name];
|
|
151
|
+
if (flagVal !== undefined && flagVal !== null && flagVal !== '') {
|
|
152
|
+
sources.push(`${fileKey || name}=命令行`);
|
|
153
|
+
return transform(flagVal);
|
|
154
|
+
}
|
|
155
|
+
for (const key of envKeys) {
|
|
156
|
+
const v = env[key];
|
|
157
|
+
if (v !== undefined && v !== null && v !== '') {
|
|
158
|
+
sources.push(`${fileKey || name}=环境变量 ${key}`);
|
|
159
|
+
return transform(v);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (fromFile[fileKey || name] !== undefined && fromFile[fileKey || name] !== null) {
|
|
163
|
+
return transform(fromFile[fileKey || name]);
|
|
164
|
+
}
|
|
165
|
+
return fallback;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
const baseUrl = normalizeBaseUrl(
|
|
169
|
+
choose('baseUrl', 'baseUrl', ['WKONGAI_AI_BASE_URL'], fromFile.baseUrl || AI_DEFAULTS.baseUrl),
|
|
170
|
+
);
|
|
171
|
+
const model = String(choose('model', 'model', ['WKONGAI_AI_MODEL'], fromFile.model || AI_DEFAULTS.model) || AI_DEFAULTS.model);
|
|
172
|
+
const provider = String(fromFile.provider || env.WKONGAI_AI_PROVIDER || AI_DEFAULTS.provider);
|
|
173
|
+
const apiKeyEnv = String(fromFile.apiKeyEnv || env.WKONGAI_AI_KEY_ENV || AI_DEFAULTS.apiKeyEnv);
|
|
174
|
+
const timeoutMs = toInt(
|
|
175
|
+
choose(undefined, 'timeoutMs', ['WKONGAI_AI_TIMEOUT_MS'], fromFile.timeoutMs || AI_DEFAULTS.timeoutMs),
|
|
176
|
+
AI_DEFAULTS.timeoutMs,
|
|
177
|
+
);
|
|
178
|
+
const maxContextChars = toInt(
|
|
179
|
+
choose(undefined, 'maxContextChars', ['WKONGAI_AI_MAX_CONTEXT_CHARS'], fromFile.maxContextChars || AI_DEFAULTS.maxContextChars),
|
|
180
|
+
AI_DEFAULTS.maxContextChars,
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
const allowRemote =
|
|
184
|
+
envFlag(env.WKONGAI_AI_ALLOW_REMOTE) ??
|
|
185
|
+
(typeof fromFile.allowRemote === 'boolean' ? fromFile.allowRemote : AI_DEFAULTS.allowRemote);
|
|
186
|
+
const allowOrders =
|
|
187
|
+
envFlag(env.WKONGAI_AI_ALLOW_ORDERS) ??
|
|
188
|
+
(typeof fromFile.allowOrders === 'boolean' ? fromFile.allowOrders : AI_DEFAULTS.allowOrders);
|
|
189
|
+
|
|
190
|
+
/** 额外放行的数据域(逗号分隔) */
|
|
191
|
+
const allowDataEnv = String(env.WKONGAI_AI_ALLOW_DATA || '').trim();
|
|
192
|
+
const allowData = allowDataEnv
|
|
193
|
+
? allowDataEnv.split(',').map((s) => s.trim()).filter(Boolean)
|
|
194
|
+
: Array.isArray(fromFile.allowData)
|
|
195
|
+
? fromFile.allowData.map(String)
|
|
196
|
+
: [];
|
|
197
|
+
|
|
198
|
+
const denyData = Array.isArray(fromFile.denyData) && fromFile.denyData.length
|
|
199
|
+
? fromFile.denyData.map(String)
|
|
200
|
+
: [...AI_DEFAULTS.denyData];
|
|
201
|
+
|
|
202
|
+
/** Key 只从环境变量取(**不许写进配置文件**,配置文件容易被同步/备份) */
|
|
203
|
+
const apiKey = String(env[apiKeyEnv] || '').trim();
|
|
204
|
+
|
|
205
|
+
return {
|
|
206
|
+
provider,
|
|
207
|
+
baseUrl,
|
|
208
|
+
model,
|
|
209
|
+
apiKeyEnv,
|
|
210
|
+
/** ⚠️ 只用于请求头;任何输出/日志都不许出现 */
|
|
211
|
+
apiKey,
|
|
212
|
+
timeoutMs,
|
|
213
|
+
maxContextChars,
|
|
214
|
+
allowRemote,
|
|
215
|
+
allowOrders,
|
|
216
|
+
allowData,
|
|
217
|
+
denyData,
|
|
218
|
+
/** 人话版的"这些值从哪来的"(`wkgeo ai ping` 会打出来) */
|
|
219
|
+
sources: sources.length ? sources : ['全部内置默认'],
|
|
220
|
+
};
|
|
221
|
+
}
|