// Copyright (c) 未来飞马 // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at https://mozilla.org/MPL/2.0/. // // Trademark Notice: // The MPL-2.0 license grants copyright permissions for source code only. // It does NOT grant any rights to use trademarks including "未来飞马", // "Harness Loop", "RSI", and associated slogan "让AI进化提前发生,让AI落地快人一步". // Any use of these trademarks requires separate written permission. import { readFileSync, existsSync } from 'node:fs'; import { hostname, homedir } from 'node:os'; // 读取 JSON 文件,自动剥 BOM,失败返回 null function readJson(p) { if (!existsSync(p)) return null; try { const raw = readFileSync(p, 'utf-8').replace(/^/, ''); return JSON.parse(raw); } catch { return null; } } /** * 身份解析高兼容链: * userid: FMODE_USERID → FMODE_AGENT_USERID → /opt/data/fmode-identity.json → $HOME/.fmode/config.json → ./.fmode/config.json * sessionToken: FMODE_SESSION_TOKEN → $HOME/.fmode/config/user.json → $HOME/.fmode/config.json → ./.fmode/config.json * agentId: FMODE_AGENT_ID → AGENT_ID → $HOME/.fmode/config.json → hostname() * * $HOME 容器 hermes 用户 = /opt/data;root 用户 = /root;两处都能命中。 */ export function resolveIdentity() { const home = process.env.HOME || homedir(); const userid = process.env.FMODE_USERID || process.env.FMODE_AGENT_USERID || readJson('/opt/data/fmode-identity.json')?.userid || readJson(`${home}/.fmode/config.json`)?.userid || readJson('./.fmode/config.json')?.userid || null; const sessionToken = process.env.FMODE_SESSION_TOKEN || readJson(`${home}/.fmode/config/user.json`)?.sessionToken || readJson(`${home}/.fmode/config.json`)?.sessionToken || readJson('./.fmode/config.json')?.sessionToken || null; const agentId = process.env.FMODE_AGENT_ID || process.env.AGENT_ID || readJson(`${home}/.fmode/config.json`)?.agentId || hostname(); return { userid, sessionToken, agentId }; } /** 仅打印身份摘要(不暴露密钥本体)*/ export function identitySummary(identity) { const { userid, sessionToken, agentId } = identity; return { userid: userid || '(未找到)', sessionToken: sessionToken ? `${sessionToken.slice(0, 8)}... (${sessionToken.length} 字符)` : '(未找到)', agentId, }; }