@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
90 lines • 3.46 kB
JavaScript
export class AgentAbortProvenance {
#source;
#agentEndEvent;
#settlingAgentEndEvent;
#agentEndBoundaryOpen = false;
#lateUserJoin = false;
#lateUserJoinDelivered = false;
get hasOpenAgentEndBoundary() {
return this.#agentEndBoundaryOpen || this.#agentEndEvent !== undefined;
}
get currentSource() {
return this.#source ?? this.#agentEndEvent?.abortSource ?? this.#settlingAgentEndEvent?.abortSource;
}
begin(source) {
this.#source = source;
this.#settlingAgentEndEvent = undefined;
this.#agentEndBoundaryOpen = false;
this.#lateUserJoin = false;
this.#lateUserJoinDelivered = false;
return source === "user";
}
join(source, isStreaming) {
if (source === "user" && (this.#agentEndEvent !== undefined || this.#agentEndBoundaryOpen)) {
this.#source = "user";
if (!this.#lateUserJoinDelivered)
this.#lateUserJoin = true;
const event = this.#agentEndEvent ?? this.#settlingAgentEndEvent;
if (event !== undefined) {
event.aborted = true;
event.abortSource = "user";
}
return { abortCurrentAgent: false, userOwned: true };
}
if (source === "user" && this.#source !== undefined) {
this.#source = "user";
return { abortCurrentAgent: false, userOwned: true };
}
if (this.#source === undefined) {
if (!isStreaming)
return { abortCurrentAgent: false, userOwned: false };
this.#source = source;
return { abortCurrentAgent: true, userOwned: source === "user" };
}
return { abortCurrentAgent: false, userOwned: false };
}
#findProviderAbortSource(messages) {
const message = messages.at(-1);
return message?.role === "assistant" && message.abortSource === "provider" ? { abortSource: "provider" } : {};
}
beginAgentEnd(messages, willRetry, abortedWithoutSource) {
const event = {
type: "agent_end",
messages,
willRetry,
...(this.#source !== undefined || abortedWithoutSource ? { aborted: true } : {}),
...(this.#source !== undefined ? { abortSource: this.#source } : this.#findProviderAbortSource(messages)),
};
this.#agentEndEvent = event;
this.#settlingAgentEndEvent = undefined;
this.#agentEndBoundaryOpen = false;
this.#lateUserJoin = false;
this.#lateUserJoinDelivered = false;
return event;
}
endAgentEnd(event) {
if (this.#agentEndEvent === event) {
this.#agentEndEvent = undefined;
this.#settlingAgentEndEvent = event;
this.#agentEndBoundaryOpen = true;
}
this.#source = undefined;
}
takeLateUserJoin() {
const lateUserJoin = this.#lateUserJoin;
this.#lateUserJoin = false;
if (lateUserJoin)
this.#lateUserJoinDelivered = true;
return lateUserJoin;
}
closeAgentEndBoundary() {
this.#agentEndBoundaryOpen = false;
this.#settlingAgentEndEvent = undefined;
}
joinOpenBoundary(source) {
if (!this.#agentEndBoundaryOpen && this.#agentEndEvent === undefined)
return undefined;
return this.join(source, false);
}
}
//# sourceMappingURL=agent-abort-provenance.js.map