UNPKG

@code-yeongyu/senpi

Version:

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

101 lines • 3.59 kB
import { createRepetitiveTurnsState, isNearDuplicateOfPreviousTurn, normalizeTurnText, REPETITIVE_TURNS_MIN_NORMALIZED_CHARS, REPETITIVE_TURNS_RULE_NAME, recordTurnText, } from "./detectors/repetitive-turns.js"; export function collectAssistantText(message) { if (!Array.isArray(message.content)) return null; let combined = ""; for (const block of message.content) { if (typeof block !== "object" || block === null) continue; if (!("type" in block) || block.type !== "text") continue; const value = Reflect.get(block, "text"); if (typeof value === "string") combined += value; } return combined.length > 0 ? combined : null; } const RESTORED_TURN_LIMIT = 8; export function readPersistedAssistantTexts(ctx) { const texts = []; for (const entry of ctx.sessionManager.getEntries()) { if (typeof entry !== "object" || entry === null) continue; if (!("type" in entry) || entry.type !== "message") continue; const message = Reflect.get(entry, "message"); if (typeof message !== "object" || message === null) continue; if (Reflect.get(message, "role") !== "assistant") continue; const text = collectAssistantText({ content: Reflect.get(message, "content") }); if (text !== null) texts.push(text); } return texts.slice(-RESTORED_TURN_LIMIT); } export class RepetitiveTurnsLane { #state = createRepetitiveTurnsState(); #armed = false; #recovering = false; #currentTurnText = ""; #lastCompletedTurnText = null; #enabled = true; configure(disabledRules) { this.#enabled = !disabledRules.has(REPETITIVE_TURNS_RULE_NAME); } resetSession() { this.#state = createRepetitiveTurnsState(); this.#armed = false; this.#recovering = false; this.#currentTurnText = ""; this.#lastCompletedTurnText = null; } restoreFromHistory(assistantTexts) { for (const text of assistantTexts) { this.recordCompletedTurn(text); } this.#armed = false; } resetTurn() { this.#armed = false; this.#currentTurnText = ""; } disarm() { this.#armed = false; } get armed() { return this.#armed; } observeTextDelta(delta, canArm) { if (this.#enabled && !this.#armed && !this.#recovering && canArm && this.#lastCompletedTurnText !== null) { const candidate = normalizeTurnText(this.#currentTurnText + delta); if (candidate.length >= REPETITIVE_TURNS_MIN_NORMALIZED_CHARS && isNearDuplicateOfPreviousTurn(candidate, this.#lastCompletedTurnText)) { this.#armed = true; } } this.#currentTurnText += delta; return this.#armed; } commitArmedTurn(turnText) { this.#armed = false; this.#recovering = true; if (turnText !== null) { this.#lastCompletedTurnText = normalizeTurnText(turnText); } } recordCompletedTurn(turnText) { const normalized = normalizeTurnText(turnText); this.#lastCompletedTurnText = normalized; if (!this.#enabled) return; const match = recordTurnText(this.#state, turnText); if (match === null) { if (this.#recovering) this.#recovering = false; return; } this.#armed = true; } } //# sourceMappingURL=repetitive-turns-lane.js.map