@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
82 lines • 3.12 kB
JavaScript
import { canonicalizePath } from "../../../utils/paths.js";
/**
* {@link canonicalizePath} memoized per path. Create one per sessions array: a rebuilt list gets a
* fresh resolver, so its answers are as current as the listing they belong to, while tree
* rebuilds and per-row current-session checks on the same list stop hitting the filesystem.
*/
export function createCanonicalPathResolver() {
const cache = new Map();
return (path) => {
if (!path)
return path;
let canonical = cache.get(path);
if (canonical === undefined) {
canonical = canonicalizePath(path);
cache.set(path, canonical);
}
return canonical;
};
}
/**
* Build a tree structure from sessions based on parentSessionPath.
* Returns root nodes sorted by modified date (descending).
*/
export function buildSessionTree(sessions, canonicalize) {
const byPath = new Map();
for (const session of sessions) {
const sessionPath = canonicalize(session.path) ?? session.path;
byPath.set(sessionPath, { session, children: [], latestActivity: session.modified.getTime() });
}
const roots = [];
for (const session of sessions) {
const sessionPath = canonicalize(session.path) ?? session.path;
const node = byPath.get(sessionPath);
const parentPath = canonicalize(session.parentSessionPath);
if (parentPath && byPath.has(parentPath)) {
byPath.get(parentPath).children.push(node);
}
else {
roots.push(node);
}
}
const updateLatestActivity = (node) => {
let latestActivity = node.session.modified.getTime();
for (const child of node.children) {
latestActivity = Math.max(latestActivity, updateLatestActivity(child));
}
node.latestActivity = latestActivity;
return latestActivity;
};
for (const root of roots) {
updateLatestActivity(root);
}
// Sort children and roots by latest activity in each subtree (descending)
const sortNodes = (nodes) => {
nodes.sort((a, b) => b.latestActivity - a.latestActivity);
for (const node of nodes) {
sortNodes(node.children);
}
};
sortNodes(roots);
return roots;
}
/**
* Flatten tree into display list with tree structure metadata.
*/
export function flattenSessionTree(roots) {
const result = [];
const walk = (node, depth, ancestorContinues, isLast) => {
result.push({ session: node.session, depth, isLast, ancestorContinues });
for (let i = 0; i < node.children.length; i++) {
const childIsLast = i === node.children.length - 1;
// Only show continuation line for non-root ancestors
const continues = depth > 0 ? !isLast : false;
walk(node.children[i], depth + 1, [...ancestorContinues, continues], childIsLast);
}
};
for (let i = 0; i < roots.length; i++) {
walk(roots[i], 0, [], i === roots.length - 1);
}
return result;
}
//# sourceMappingURL=session-selector-tree.js.map