UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

164 lines • 6.8 kB
import { POST_COMPACT_RESTORATION_CUSTOM_TYPE } from "./restoration-tracker.js"; const CHECKPOINT_CUSTOM_TYPE = "compaction.agent-checkpoint"; const CHECKPOINT_SCHEMA = "senpi.compaction.agent-checkpoint.v1"; const RESTORATION_DIRECTIVE = "[restore checkpointed session agent configuration after compaction]"; function isExtensionAPI(value) { return "getActiveTools" in value && "getThinkingLevel" in value; } /** * The latest agent name any custom entry recorded, folded without a full-history view: compaction captures it * while a run is active, when a materialized view of a long compacted session would be held until idle (senpi#3110). */ const latestAgentName = { initial: () => null, add: (latest, entry) => { if (entry.type !== "custom" || !isRecord(entry.data)) return latest; if (typeof entry.data.agentName === "string") return entry.data.agentName; if (typeof entry.data.agent === "string") return entry.data.agent; return latest; }, }; function deriveAgentName(ctx) { return ctx.sessionManager.reduceHistory(latestAgentName); } function isRecord(value) { return typeof value === "object" && value !== null; } function parseCheckpoint(value) { if (!isRecord(value)) return null; if (value.schema === CHECKPOINT_SCHEMA && isRecord(value.data)) { return parseCheckpoint(value.data); } const activeTools = Array.isArray(value.activeTools) ? value.activeTools.filter((tool) => typeof tool === "string") : []; const model = isRecord(value.model) ? { provider: typeof value.model.provider === "string" ? value.model.provider : "", modelId: typeof value.model.modelId === "string" ? value.model.modelId : "", } : undefined; return { activeTools, thinkingLevel: typeof value.thinkingLevel === "string" ? value.thinkingLevel : null, modelId: typeof value.modelId === "string" ? value.modelId : model?.modelId, agentName: typeof value.agentName === "string" ? value.agentName : null, timestamp: typeof value.timestamp === "number" ? value.timestamp : undefined, model, }; } function serializeCheckpoint(checkpoint) { return { ...checkpoint, schema: CHECKPOINT_SCHEMA, data: checkpoint, }; } export function captureAgentCheckpoint(piOrInput, ctx) { if (isExtensionAPI(piOrInput) && ctx) { return { activeTools: piOrInput.getActiveTools(), thinkingLevel: piOrInput.getThinkingLevel(), modelId: ctx.model?.id, agentName: deriveAgentName(ctx), timestamp: Date.now(), model: ctx.model ? { provider: ctx.model.provider, modelId: ctx.model.id } : undefined, }; } const input = piOrInput; return { activeTools: input.activeTools ?? [], thinkingLevel: null, modelId: input.model?.modelId, agentName: input.agentName ?? null, timestamp: Date.now(), model: input.model, }; } export function persistCheckpoint(piOrCheckpoint, checkpointOrPi) { if ("appendEntry" in piOrCheckpoint) { const checkpoint = checkpointOrPi; piOrCheckpoint.appendEntry(CHECKPOINT_CUSTOM_TYPE, serializeCheckpoint(checkpoint)); return; } const pi = checkpointOrPi; pi.appendEntry(CHECKPOINT_CUSTOM_TYPE, serializeCheckpoint(piOrCheckpoint)); } /** The session's latest valid checkpoint, folded without a full-history view: this runs every turn (senpi#2537). */ const latestCheckpoint = { initial: () => null, add: (latest, entry) => entry.type === "custom" && entry.customType === CHECKPOINT_CUSTOM_TYPE ? (parseCheckpoint(entry.data) ?? latest) : latest, }; export function getLatestCheckpoint(source) { if ("sessionManager" in source) return source.sessionManager.reduceHistory(latestCheckpoint); const checkpoints = source.persistedCheckpoints ?? []; if (checkpoints.length > 0) { return [...checkpoints].sort((left, right) => (right.timestamp ?? 0) - (left.timestamp ?? 0))[0]; } const appended = source.appendCalls ?.filter((call) => call.customType === CHECKPOINT_CUSTOM_TYPE) .map((call) => parseCheckpoint(call.data)) .filter((checkpoint) => checkpoint !== null); return appended?.sort((left, right) => (right.timestamp ?? 0) - (left.timestamp ?? 0))[0]; } function buildRestorationHints(checkpoint) { const modelId = checkpoint.modelId ?? checkpoint.model?.modelId; const lines = [ RESTORATION_DIRECTIVE, "", "Restore checkpointed session configuration:", `- Agent: ${checkpoint.agentName ?? "unknown"}`, `- Tools: ${checkpoint.activeTools && checkpoint.activeTools.length > 0 ? checkpoint.activeTools.join(", ") : "none"}`, `- Model: ${modelId ?? "unknown"}`, ]; return lines.join("\n"); } export function createRestorationDirectiveState() { return { deliveredCheckpointTimestamp: null }; } function directiveMessage(checkpoint) { return { customType: POST_COMPACT_RESTORATION_CUSTOM_TYPE, content: buildRestorationHints(checkpoint), display: false, }; } function withDirective(message, checkpoint) { const hints = buildRestorationHints(checkpoint); const existing = typeof message.content === "string" ? message.content : undefined; if (existing === undefined) return message; return { ...message, content: `${hints}\n\n${existing}` }; } /** * Attach the restoration directive to the one-shot post-compact message, once * per checkpoint. Returns the pending message unchanged when the checkpoint is * stale or its directive was already delivered. */ export function attachRestorationDirective(state, checkpoint, message) { if (!checkpoint) return message; const timestamp = checkpoint.timestamp ?? null; if (timestamp !== null && state.deliveredCheckpointTimestamp === timestamp) return message; state.deliveredCheckpointTimestamp = timestamp; return message ? withDirective(message, checkpoint) : directiveMessage(checkpoint); } export function injectRestorationDirective(systemPromptOrCheckpoint, checkpointOrFallback) { if (typeof systemPromptOrCheckpoint === "string") { const checkpoint = checkpointOrFallback; return `${systemPromptOrCheckpoint}\n\n${buildRestorationHints(checkpoint)}`; } if (checkpointOrFallback && "model" in checkpointOrFallback && checkpointOrFallback.model) { return `${RESTORATION_DIRECTIVE}\nModel: ${checkpointOrFallback.model.modelId}`; } return RESTORATION_DIRECTIVE; } //# sourceMappingURL=checkpoint-state.js.map