@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
150 lines • 5.84 kB
JavaScript
import { getLatestPhasesFromBranchEntries, getLatestTodoStateFromBranchEntries, isTodoAsk, TODO_RESTORE_REQUEST_TYPE, } from "../todotools/state.js";
const TODO_SNAPSHOT_CUSTOM_TYPE = "compaction.todo-snapshot";
const TODO_SNAPSHOT_SCHEMA = "senpi.compaction.todo-snapshot.v1";
const TODO_STATE_ENTRY_TYPE = "senpi.todo-state";
function isRecord(value) {
return typeof value === "object" && value !== null;
}
function isTodoStatus(value) {
// Legacy todowrite entries carried arbitrary status strings (e.g. "blocked").
// The bridge is a pass-through snapshot carrier, not a validator: preserve
// every string-status entry so restore never silently drops legacy todos.
return value === undefined || typeof value === "string";
}
function isTodoEntry(value) {
if (!isRecord(value))
return false;
const hasId = typeof value.id === "string";
const hasContent = typeof value.content === "string" || typeof value.text === "string";
return (hasId || hasContent) && isTodoStatus(value.status);
}
function isTodoPhase(value) {
if (!isRecord(value) || typeof value.name !== "string" || !Array.isArray(value.tasks))
return false;
return value.tasks.every(isTodoEntry);
}
function isCustomTodoEntry(entry) {
return (entry.type === "custom" &&
(entry.customType.startsWith("todowrite") || entry.customType === TODO_STATE_ENTRY_TYPE));
}
function isLegacyTodoListEntry(entry) {
return entry.type === "custom" && entry.customType === "todo-list";
}
function readTodosFromEntry(entry) {
const data = entry.data;
if (!isRecord(data))
return [];
if (Array.isArray(data.todos))
return data.todos.filter(isTodoEntry);
if (Array.isArray(data.phases)) {
return data.phases.filter(isTodoPhase).flatMap((phase) => phase.tasks);
}
return [];
}
function isCustomEntryEnvelope(value) {
return (isRecord(value) &&
value.type === "custom" &&
typeof value.id === "string" &&
(value.parentId === null || typeof value.parentId === "string") &&
typeof value.timestamp === "string" &&
typeof value.customType === "string");
}
function normalizeLegacySnapshotEntries(entries) {
for (let index = entries.length - 1; index >= 0; index -= 1) {
const entry = entries[index];
if (!isLegacyTodoListEntry(entry) && !isCustomTodoEntry(entry))
continue;
if (entry.customType === TODO_STATE_ENTRY_TYPE) {
return getLatestPhasesFromBranchEntries(entries.slice(0, index + 1));
}
return readTodosFromEntry(entry);
}
return [];
}
function normalizeSnapshotItems(value) {
if (!Array.isArray(value))
return null;
if (value.every(isTodoPhase))
return value;
if (value.every(isCustomEntryEnvelope))
return normalizeLegacySnapshotEntries(value);
if (value.every(isTodoEntry))
return value;
return null;
}
function getCurrentTodoPhases(ctx) {
return getLatestPhasesFromBranchEntries(ctx.sessionManager.getBranch());
}
function findLatestTodoSnapshot(ctx) {
const entries = ctx.sessionManager.getBranch();
for (let index = entries.length - 1; index >= 0; index -= 1) {
const entry = entries[index];
if (entry.type !== "custom" || entry.customType !== TODO_SNAPSHOT_CUSTOM_TYPE)
continue;
const data = entry.data;
if (!isRecord(data) || data.schema !== TODO_SNAPSHOT_SCHEMA)
continue;
const todos = normalizeSnapshotItems(data.todos);
if (!todos)
continue;
return {
schema: TODO_SNAPSHOT_SCHEMA,
todos,
capturedAt: typeof data.capturedAt === "number" ? data.capturedAt : 0,
...(isTodoAsk(data.ask) ? { ask: data.ask } : {}),
};
}
return null;
}
export function findTodoEntries(entries, options) {
return entries
.filter((entry) => isLegacyTodoListEntry(entry) || isCustomTodoEntry(entry))
.filter((entry) => options?.branchId === undefined || entry.parentId === options.branchId)
.flatMap(readTodosFromEntry);
}
export function createTodoSnapshot(ctx) {
const { phases, ask } = getLatestTodoStateFromBranchEntries(ctx.sessionManager.getBranch());
return {
schema: TODO_SNAPSHOT_SCHEMA,
todos: phases,
capturedAt: Date.now(),
...(ask ? { ask } : {}),
};
}
export function persistTodoSnapshot(pi, snapshot) {
pi.appendEntry(TODO_SNAPSHOT_CUSTOM_TYPE, snapshot);
}
export function captureTodoSnapshot(piOrTodos, ctxOrPi, _branchId) {
if (Array.isArray(piOrTodos)) {
const pi = ctxOrPi;
persistTodoSnapshot(pi, {
schema: TODO_SNAPSHOT_SCHEMA,
todos: piOrTodos,
capturedAt: Date.now(),
});
return;
}
persistTodoSnapshot(piOrTodos, createTodoSnapshot(ctxOrPi));
}
export function restoreTodosIfMissing(piOrSnapshot, ctxOrCurrentTodos, _pi) {
if (Array.isArray(piOrSnapshot) && Array.isArray(ctxOrCurrentTodos)) {
if (ctxOrCurrentTodos.length > 0) {
return { applied: false, restoredTodos: ctxOrCurrentTodos };
}
return { applied: piOrSnapshot.length > 0, restoredTodos: piOrSnapshot };
}
const pi = piOrSnapshot;
const ctx = ctxOrCurrentTodos;
if (getCurrentTodoPhases(ctx).length > 0)
return;
const snapshot = findLatestTodoSnapshot(ctx);
if (!snapshot || snapshot.todos.length === 0)
return;
pi.sendMessage({
customType: TODO_RESTORE_REQUEST_TYPE,
content: `Restore missing todo tasks from snapshot: ${JSON.stringify(snapshot.todos)}`,
display: false,
details: snapshot,
}, { triggerTurn: true, deliverAs: "nextTurn" });
}
//# sourceMappingURL=todo-bridge.js.map