Auto-reply: delay onAgentRunStart until real activity

This commit is contained in:
Shakker
2026-02-19 18:47:07 +00:00
committed by Shakker
parent 4b7d89100e
commit 7579e9511e
2 changed files with 123 additions and 1 deletions

View File

@@ -88,7 +88,14 @@ export async function runAgentTurnWithFallback(params: {
const directlySentBlockKeys = new Set<string>();
const runId = params.opts?.runId ?? crypto.randomUUID();
params.opts?.onAgentRunStart?.(runId);
let didNotifyAgentRunStart = false;
const notifyAgentRunStart = () => {
if (didNotifyAgentRunStart) {
return;
}
didNotifyAgentRunStart = true;
params.opts?.onAgentRunStart?.(runId);
};
if (params.sessionKey) {
registerAgentRunContext(runId, {
sessionKey: params.sessionKey,
@@ -160,6 +167,7 @@ export async function runAgentTurnWithFallback(params: {
if (isCliProvider(provider, params.followupRun.run.config)) {
const startedAt = Date.now();
notifyAgentRunStart();
emitAgentEvent({
runId,
stream: "lifecycle",
@@ -310,6 +318,12 @@ export async function runAgentTurnWithFallback(params: {
: undefined,
onReasoningEnd: params.opts?.onReasoningEnd,
onAgentEvent: async (evt) => {
// Signal run start only after the embedded agent emits real activity.
const hasLifecyclePhase =
evt.stream === "lifecycle" && typeof evt.data.phase === "string";
if (evt.stream !== "lifecycle" || hasLifecyclePhase) {
notifyAgentRunStart();
}
// Trigger typing when tools start executing.
// Must await to ensure typing indicator starts before tool summaries are emitted.
if (evt.stream === "tool") {