UNPKG

@code-yeongyu/senpi

Version:

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

64 lines • 2.69 kB
import { Buffer } from "node:buffer"; import { transitionToTurnClaimed, transitionToTurnStreaming } from "./session-registry-state.js"; export function deliver(entry, turn, message) { if (entry.state === "TURN_CLAIMED") transitionToTurnStreaming(entry); turn.messages.push(message); turn.onMessage?.(message); } /** * Claude Code echoes a submitted prompt before that turn's first API call, on the same ordered stream, * so events ahead of our replay belong to a turn it is already running (an autonomous turn or a * background subagent). Only main-thread events are held, as a bounded courtesy for an early event; a * segment that outgrows the caps is another turn's and is dropped until that turn ends (senpi#2192). */ export function bufferBeforeReplay(registry, entry, turn, message) { if (message.parent_tool_use_id !== null || turn.preReplayOverflowed) return; turn.preReplay.push(message); turn.preReplayBytes += Buffer.byteLength(JSON.stringify(message)); if (turn.preReplay.length > turn.limits.maxMessages || turn.preReplayBytes > turn.limits.maxBytes) { dropPreReplay(turn); turn.preReplayOverflowed = true; return; } if (!registry.isCurrentGeneration(entry.senpiSessionId, turn.generation)) turn.preReplay.length = 0; } export function endForeignTurnBeforeReplay(turn) { dropPreReplay(turn); turn.preReplayOverflowed = false; } function dropPreReplay(turn) { turn.preReplay.length = 0; turn.preReplayBytes = 0; } export function claimTurn(entry, turn) { turn.claimed = true; transitionToTurnClaimed(entry); const buffered = turn.preReplay.splice(0); turn.preReplayBytes = 0; return buffered; } export function isReplayFor(message, uuid) { return message.type === "user" && "isReplay" in message && message.isReplay === true && message.uuid === uuid; } export function isAutonomousResult(message) { if (message.origin && message.origin.kind !== "human") return true; const wire = message; return wire.parent_tool_use_id != null || wire.subagent_type != null || wire.isSynthetic === true; } export function isForeignResult(message, turn) { if ("user_message_uuid" in message && message.user_message_uuid !== undefined) { return message.user_message_uuid !== turn.uuid; } return isAutonomousResult(message); } export function resultMatchesTurn(message, turn) { if ("user_message_uuid" in message && message.user_message_uuid !== undefined) { return message.user_message_uuid === turn.uuid; } return turn.claimed && !isAutonomousResult(message); } //# sourceMappingURL=session-turn-claim.js.map