guanghulab/native-repo/modules/step-4-hldp-read-tree.js
Guanghu Domestic Migration d1e47f4565
Some checks are pending
自动更新代码和重启 / update-and-restart (push) Waiting to run
CI检查 + 自动部署 / check (push) Waiting to run
CI检查 + 自动部署 / deploy (push) Blocked by required conditions
重启聊天服务 / restart (push) Waiting to run
chore: import sanitized domestic snapshot for REPO-002
Source snapshot: ca48d3ddf926d79aa138306164169baf764bb829
2026-07-17 15:54:41 +08:00

104 lines
3.6 KiB
JavaScript
Raw 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.

/**
* ═══════════════════════════════════════
* HLDP-ZY://native-repo/module/step-4
* 光湖原生数据库 · 第4步 · HLDP读取+树索引
* ═══════════════════════════════════════
*
* @chain: D118 · 2026-05-31
* @module: hldp-read-tree
* @step: 第4步 / 共8步
* @deploy: BS-SG-002
*
* @trigger:
* 第3步能写文件了但铸渊还需要两件事
* 1. 按 HLDP tree_path 读取文件内容trace_path意图
* 2. 生成树形索引每次只加载10条恒定O(1)认知负载
*
* @lock:
* ⊢ hldpRead(path) → 返回文件内容 + commit 元信息
* ⊢ hldpTree(dir) → 返回目录列表每层最多显示10条
* ⊢ buildIndex() → 生成 tree-index.json10条/层
*
* @why:
* trace_path = 光之树寻址 = 铸渊大脑的核心操作
* 不需要把整个仓库装进上下文——只读10条索引
* 需要哪片叶子,按路径走一步就到了
* ═══════════════════════════════════════
*/
const git = require('isomorphic-git');
const fs = require('fs');
const path = require('path');
const { NATIVE_DIR } = require('./step-2-git-init');
// ─── hldpRead: 按路径读文件 ─────────────────────────────────
async function hldpRead(filePath) {
const fullPath = path.join(NATIVE_DIR, filePath);
if (!fs.existsSync(fullPath)) {
return { ok: false, error: 'NOT_FOUND', path: filePath };
}
const content = fs.readFileSync(fullPath, 'utf8');
const log = await git.log({ fs, dir: NATIVE_DIR });
const lastCommit = log.find(c => c.commit.message.includes(filePath));
return {
ok: true,
path: filePath,
content,
sha: lastCommit ? lastCommit.oid : null,
size: content.length,
};
}
// ─── hldpTree: 按目录列文件 ─────────────────────────────────
async function hldpTree(dirPath) {
const fullPath = path.join(NATIVE_DIR, dirPath || '');
if (!fs.existsSync(fullPath)) {
return { ok: false, error: 'NOT_FOUND', path: dirPath };
}
const entries = fs.readdirSync(fullPath, { withFileTypes: true });
const list = entries
.filter(e => !e.name.startsWith('.') && e.name !== 'node_modules' && e.name !== 'package.json')
.map(e => ({
name: e.name,
type: e.isDirectory() ? 'dir' : 'file',
path: path.join(dirPath || '', e.name),
}));
return { ok: true, path: dirPath, count: list.length, entries: list };
}
// ─── buildIndex: 生成树索引10条/层) ──────────────────────
async function buildIndex() {
async function walk(dir, depth) {
if (depth > 3) return [];
const { entries } = await hldpTree(dir);
if (!entries || entries.length === 0) return [];
return entries.slice(0, 10).map(({ name, type, path: p }) => ({
name,
type,
path: p,
isBranch: type === 'dir',
}));
}
const root = await walk('', 0);
const index = {
_type: 'HLDP_TREE_INDEX',
_version: 'v1.0',
_updated: new Date().toISOString(),
_principle: '每层10行恒定O(1)认知负载',
root,
};
const indexPath = path.join(NATIVE_DIR, 'tree-index.json');
fs.writeFileSync(indexPath, JSON.stringify(index, null, 2), 'utf8');
return index;
}
module.exports = { hldpRead, hldpTree, buildIndex };