Add emitLastMessage flag to agent.continue()

When calling continue() with emitLastMessage=true, the agent loop
emits message_start/message_end events for the last message in context.
This allows messages added outside the loop (e.g., hook messages via
sendHookMessage) to trigger proper TUI rendering.

Changes across packages:
- packages/ai: agentLoopContinue() accepts emitLastMessage parameter
- packages/agent: Agent.continue(), transports updated to pass flag
- packages/coding-agent: sendHookMessage passes true when triggerTurn
This commit is contained in:
Mario Zechner 2025-12-27 01:31:31 +01:00
parent 30cd723411
commit 357bd946c2
6 changed files with 34 additions and 12 deletions

View file

@ -40,11 +40,18 @@ export function agentLoop(
* Used for retry after overflow - context already has user message or tool results.
* Throws if the last message is not a user message or tool result.
*/
/**
* Continue an agent loop from the current context without adding a new message.
* Used for retry after overflow - context already has user message or tool results.
* Throws if the last message is not a user message or tool result.
* @param emitLastMessage If true, emit message_start/message_end for the last message in context
*/
export function agentLoopContinue(
context: AgentContext,
config: AgentLoopConfig,
signal?: AbortSignal,
streamFn?: typeof streamSimple,
emitLastMessage?: boolean,
): EventStream<AgentEvent, AgentContext["messages"]> {
// Validate that we can continue from this context
const lastMessage = context.messages[context.messages.length - 1];
@ -63,7 +70,12 @@ export function agentLoopContinue(
stream.push({ type: "agent_start" });
stream.push({ type: "turn_start" });
// No user message events - we're continuing from existing context
// Optionally emit events for the last message (used when message was added outside the loop)
if (emitLastMessage) {
stream.push({ type: "message_start", message: lastMessage });
stream.push({ type: "message_end", message: lastMessage });
}
await runLoop(currentContext, newMessages, config, signal, stream, streamFn);
})();