- 新增 instance.js:data_root/instance-id,首次启动生成 12 位 hex,跨 core 唯一 - engine 传 context_id = <core_id>:<user>:<session_id>,三层定位不撞车 - callModel 对 store: true 的模型只发新增消息 + X-Context-Id/Hash, 409 时改发全量重试一次;指针存内存,重启自动回落全量 - config.js 把 store 字段透传到模型对象(以前会被丢掉) - 未配 store 的模型行为完全不变
30 lines
942 B
JavaScript
30 lines
942 B
JavaScript
'use strict';
|
|
// 实例标识:跨 core 隔离用,作 relay 的 X-Context-Id 前缀。
|
|
// 首次启动生成并落盘到 <data_root>/instance-id,之后只读同一个值。
|
|
// 删了数据目录会换新 id —— 旧缓存成孤儿,由 relay 的 TTL 自行清理,不影响正确性。
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const crypto = require('node:crypto');
|
|
|
|
const cache = new Map(); // dataRoot -> id
|
|
|
|
function instanceId(dataRoot) {
|
|
const root = path.resolve(String(dataRoot || '.'));
|
|
if (cache.has(root)) return cache.get(root);
|
|
const file = path.join(root, 'instance-id');
|
|
let id = '';
|
|
try {
|
|
id = fs.readFileSync(file, 'utf8').trim();
|
|
} catch (_) {}
|
|
if (!/^[0-9a-f]{12}$/.test(id)) {
|
|
id = crypto.randomBytes(6).toString('hex');
|
|
try {
|
|
fs.writeFileSync(file, id + '\n');
|
|
} catch (_) {}
|
|
}
|
|
cache.set(root, id);
|
|
return id;
|
|
}
|
|
|
|
module.exports = { instanceId };
|