Files
woniu336 141ed9f4a8 feat(llm): DeepSeek Flash + 思考模式;本地密钥加载器
- llm_profiles 增加 thinking / reasoning_effort(migrations/0002),
  ensureD1Schema 容忍 ALTER 重复列,已有库平滑升级
- /api/read 按 profile 组装 thinking / reasoning_effort;只转发 delta.content,
  思考链 reasoning_content 不下发;max_tokens 4096
- 后台 LLM 表单新增「思考模式」「思考强度」,列表显示当前设置;测试连通沿用同一配置
- scripts/load-secrets.mjs:启动前读取 .llm.local(gitignore),
  dev / dev:bg / dev:vinext / build:vinext / start:vinext 共用
- 解卦面板显示「研墨中 · Ns」,思考模式首字慢时有提示
- 新增 llm-request 纯函数与 4 项测试;总 68 项测试通过

本地实测:DeepSeek Flash + thinking=high,/api/read 约 23s、840 字、四段齐全;
UI 4 标题/5 段、0 错误。密钥仅存于 .llm.local(不进仓库/D1)。
2026-11-05 14:36:38 +08:00

124 lines
3.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env node
/**
* 后台启动开发栈(Windows 友好)。
*
* npm run dev:bg
*
* 按端口杀掉旧的 next dev(默认 3000,可用 PORT 覆盖),再用 detached 方式重拉,
* 日志写 logs/dev.out.log,最后等端口就绪。前台 `npm run dev` 仍然可用。
*/
import { execSync, spawn } from 'node:child_process';
import { loadLocalSecrets } from './load-secrets.mjs';
import fs from 'node:fs';
import net from 'node:net';
import path from 'node:path';
import process from 'node:process';
import { fileURLToPath } from 'node:url';
const projectRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
loadLocalSecrets(projectRoot);
const LOG_DIR = path.join(projectRoot, 'logs');
const LOG_FILE = path.join(LOG_DIR, 'dev.out.log');
const PORT = Number(process.env.PORT || 3000);
function pidsOnPort(port) {
if (process.platform !== 'win32') {
try {
const out = execSync(`lsof -ti tcp:${port}`, { encoding: 'utf-8' }).trim();
return out ? out.split(/\s+/).map(Number) : [];
} catch {
return [];
}
}
try {
const out = execSync(`netstat -ano -p tcp | findstr LISTENING | findstr :${port}`, {
encoding: 'utf-8',
stdio: ['ignore', 'pipe', 'ignore'],
});
return [...new Set(out.split(/\r?\n/).map((l) => l.trim().split(/\s+/).pop()).filter((x) => x && x !== '0').map(Number))];
} catch {
return [];
}
}
function killTree(pid) {
try {
if (process.platform === 'win32') execSync(`taskkill /PID ${pid} /T /F`, { stdio: 'ignore' });
else execSync(`kill -TERM -${pid} || kill -TERM ${pid}`, { stdio: 'ignore' });
return true;
} catch {
return false;
}
}
function canConnect(port, host = '127.0.0.1') {
return new Promise((resolve) => {
const socket = net.createConnection({ port, host });
socket.setTimeout(300);
socket.once('connect', () => {
socket.destroy();
resolve(true);
});
const no = () => {
socket.destroy();
resolve(false);
};
socket.once('error', no);
socket.once('timeout', no);
});
}
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
async function main() {
fs.mkdirSync(LOG_DIR, { recursive: true });
for (const pid of pidsOnPort(PORT)) {
console.log(`[dev:bg] ${killTree(pid) ? '已停止' : '停止失败'} PID ${pid}(端口 ${PORT})`);
}
for (let i = 0; i < 20; i += 1) {
if (!(await canConnect(PORT))) break;
await sleep(250);
}
// 清掉宿主泄漏的 Next 私有变量(详见 scripts/next.mjs 顶部说明)
const env = { ...process.env };
for (const key of Object.keys(env)) {
if (key.startsWith('__NEXT_PRIVATE_')) delete env[key];
}
const nextBin = path.join(projectRoot, 'node_modules', 'next', 'dist', 'bin', 'next');
const logFd = fs.openSync(LOG_FILE, 'w');
const child = spawn(process.execPath, [nextBin, 'dev', '--port', String(PORT)], {
cwd: projectRoot,
detached: true,
stdio: ['ignore', logFd, logFd],
windowsHide: true,
env,
});
child.unref();
fs.closeSync(logFd);
console.log(`[dev:bg] 已在后台启动 next dev(PID ${child.pid},端口 ${PORT},日志 ${path.relative(projectRoot, LOG_FILE)})`);
const deadline = Date.now() + 90_000;
while (Date.now() < deadline) {
if (await canConnect(PORT)) {
console.log(`[dev:bg] 就绪:http://localhost:${PORT}`);
return;
}
await sleep(400);
}
console.error('[dev:bg] 端口未就绪,日志尾部:');
try {
console.error(fs.readFileSync(LOG_FILE, 'utf-8').split(/\r?\n/).slice(-25).join('\n'));
} catch {
/* ignore */
}
process.exitCode = 1;
}
main().catch((e) => {
console.error(`[dev:bg] 失败:${e instanceof Error ? e.message : String(e)}`);
process.exitCode = 1;
});