export function resolveNewsEngineBaseUrl(env = process.env) { return String(env.MEMIND_NEWS_ENGINE_URL ?? 'http://127.0.0.1:8092').trim().replace(/\/+$/, ''); } export async function fetchNewsEngine(path, { env = process.env, method = 'GET', body = null, fetchImpl = fetch, } = {}) { const baseUrl = resolveNewsEngineBaseUrl(env); if (!baseUrl) { return { ok: false, status: 503, data: { message: '未配置 MEMIND_NEWS_ENGINE_URL' } }; } const token = String(env.MEMIND_NEWS_ENGINE_API_TOKEN ?? '').trim(); const response = await fetchImpl(`${baseUrl}${path}`, { method, headers: { accept: 'application/json', ...(body ? { 'content-type': 'application/json' } : {}), ...(token ? { authorization: `Bearer ${token}` } : {}), }, body: body ? JSON.stringify(body) : undefined, signal: AbortSignal.timeout(Number(env.MEMIND_NEWS_ENGINE_TIMEOUT_MS ?? 120000) || 120000), }); const text = await response.text().catch(() => ''); let data = null; try { data = text ? JSON.parse(text) : null; } catch { data = { message: text.slice(0, 500) || `HTTP ${response.status}` }; } return { ok: response.ok, status: response.status, data }; }