dac06753ce
Memind CI / Test, build, and release guards (push) Failing after 2s
Schedule LLM now binds an explicit provider key and fails closed on network errors so dead relay endpoints cannot block ordinary chat; relay bootstrap is opt-in only. Co-authored-by: Cursor <cursoragent@cursor.com>
125 lines
3.8 KiB
JavaScript
125 lines
3.8 KiB
JavaScript
const CONFIG_TABLE = 'h5_wechat_admin_config';
|
|
const CONFIG_KEY = 'schedule_llm';
|
|
|
|
function normalizeBoolean(value, fallback = false) {
|
|
if (value == null || value === '') return fallback;
|
|
if (typeof value === 'boolean') return value;
|
|
const normalized = String(value).trim().toLowerCase();
|
|
if (['1', 'true', 'yes', 'on'].includes(normalized)) return true;
|
|
if (['0', 'false', 'no', 'off'].includes(normalized)) return false;
|
|
return fallback;
|
|
}
|
|
|
|
function parseConfigJson(value) {
|
|
if (!value) return {};
|
|
if (typeof value === 'object') return value;
|
|
try {
|
|
return JSON.parse(String(value));
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
async function ensureConfigTable(pool) {
|
|
await pool.query(`
|
|
CREATE TABLE IF NOT EXISTS ${CONFIG_TABLE} (
|
|
config_key VARCHAR(64) PRIMARY KEY,
|
|
config_json JSON NOT NULL,
|
|
updated_by CHAR(36) NULL,
|
|
updated_at BIGINT NOT NULL
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
|
`);
|
|
}
|
|
|
|
function defaultsFromEnv(env = process.env) {
|
|
return {
|
|
scheduleLlmEnabled: normalizeBoolean(env.H5_WECHAT_SCHEDULE_LLM_ENABLED, false),
|
|
modelProviderKeyId: String(env.MEMIND_WECHAT_SCHEDULE_LLM_MODEL_PROVIDER_KEY_ID ?? '').trim() || null,
|
|
model: String(env.MEMIND_WECHAT_SCHEDULE_LLM_MODEL ?? '').trim() || null,
|
|
};
|
|
}
|
|
|
|
export function createWechatScheduleLlmConfigService(pool, { env = process.env } = {}) {
|
|
let ensurePromise = null;
|
|
|
|
async function ensureReady() {
|
|
if (!ensurePromise) ensurePromise = ensureConfigTable(pool);
|
|
await ensurePromise;
|
|
}
|
|
|
|
async function readRow() {
|
|
await ensureReady();
|
|
const [rows] = await pool.query(
|
|
`SELECT config_json, updated_by, updated_at
|
|
FROM ${CONFIG_TABLE}
|
|
WHERE config_key = ?
|
|
LIMIT 1`,
|
|
[CONFIG_KEY],
|
|
);
|
|
return rows[0] ?? null;
|
|
}
|
|
|
|
function mergeConfig(row) {
|
|
const defaults = defaultsFromEnv(env);
|
|
const stored = parseConfigJson(row?.config_json);
|
|
return {
|
|
scheduleLlmEnabled: normalizeBoolean(
|
|
stored.scheduleLlmEnabled,
|
|
defaults.scheduleLlmEnabled,
|
|
),
|
|
modelProviderKeyId: String(stored.modelProviderKeyId ?? defaults.modelProviderKeyId ?? '').trim() || null,
|
|
model: String(stored.model ?? defaults.model ?? '').trim() || null,
|
|
};
|
|
}
|
|
|
|
return {
|
|
async getConfig() {
|
|
const row = await readRow();
|
|
return {
|
|
...mergeConfig(row),
|
|
updatedAt: row?.updated_at ? Number(row.updated_at) : null,
|
|
updatedBy: row?.updated_by ?? null,
|
|
};
|
|
},
|
|
|
|
async isScheduleLlmEnabled() {
|
|
const config = await this.getConfig();
|
|
return config.scheduleLlmEnabled;
|
|
},
|
|
|
|
async updateConfig(payload = {}, { updatedBy = null } = {}) {
|
|
const current = await this.getConfig();
|
|
const next = {
|
|
scheduleLlmEnabled:
|
|
payload.scheduleLlmEnabled === undefined
|
|
? current.scheduleLlmEnabled
|
|
: normalizeBoolean(payload.scheduleLlmEnabled, current.scheduleLlmEnabled),
|
|
modelProviderKeyId:
|
|
payload.modelProviderKeyId === undefined
|
|
? (current.modelProviderKeyId ?? '')
|
|
: String(payload.modelProviderKeyId ?? '').trim(),
|
|
model:
|
|
payload.model === undefined
|
|
? (current.model ?? '')
|
|
: String(payload.model ?? '').trim(),
|
|
};
|
|
const now = Date.now();
|
|
await pool.query(
|
|
`INSERT INTO ${CONFIG_TABLE} (config_key, config_json, updated_by, updated_at)
|
|
VALUES (?, ?, ?, ?)
|
|
ON DUPLICATE KEY UPDATE
|
|
config_json = VALUES(config_json),
|
|
updated_by = VALUES(updated_by),
|
|
updated_at = VALUES(updated_at)`,
|
|
[CONFIG_KEY, JSON.stringify(next), updatedBy, now],
|
|
);
|
|
return this.getConfig();
|
|
},
|
|
};
|
|
}
|
|
|
|
export const wechatScheduleLlmConfigInternals = {
|
|
normalizeBoolean,
|
|
defaultsFromEnv,
|
|
};
|