1+ /**
2+ * Shared state and context contracts for embedded-agent subscription handlers.
3+ * Message, tool, compaction, and liveness handlers all mutate this single
4+ * state shape while keeping their implementation files decoupled.
5+ */
16import type { InlineCodeState } from "../../packages/markdown-core/src/code-spans.js" ;
27import type { FenceScanState } from "../../packages/markdown-core/src/fences.js" ;
38import type { HeartbeatToolResponse } from "../auto-reply/heartbeat-tool-response.js" ;
49import type { ReplyDirectiveParseResult } from "../auto-reply/reply/reply-directives.js" ;
510import type { ReasoningLevel } from "../auto-reply/thinking.js" ;
611import type { HookRunner } from "../plugins/hooks.js" ;
12+ import type { AssistantPhase } from "../shared/chat-message-content.js" ;
713import type { AcceptedSessionSpawn } from "./accepted-session-spawn.js" ;
814import type { EmbeddedBlockChunker } from "./embedded-agent-block-chunker.js" ;
915import type {
@@ -34,20 +40,43 @@ type EmbeddedSubscribeLogger = {
3440 warn : ( message : string , meta ?: Record < string , unknown > ) => void ;
3541} ;
3642
43+ /** Per-tool metadata tracked between tool start/update/end events. */
3744export type ToolCallSummary = {
3845 meta ?: string ;
3946 mutatingAction : boolean ;
4047 actionFingerprint ?: string ;
4148 fileTarget ?: import ( "./tool-mutation.js" ) . FileTarget ;
4249} ;
4350
51+ /** User-visible assistant stream payload emitted to subscribers. */
52+ export type AssistantStreamData = {
53+ text : string ;
54+ delta : string ;
55+ replace ?: true ;
56+ mediaUrls ?: string [ ] ;
57+ phase ?: AssistantPhase ;
58+ } ;
59+
60+ /** Deferred assistant stream event plus whether it should emit partial replies. */
61+ export type AssistantStreamDelivery = {
62+ data : AssistantStreamData ;
63+ emitPartialReply : boolean ;
64+ } ;
65+
66+ /** Mutable subscription state shared by embedded-agent event handlers. */
4467export type EmbeddedAgentSubscribeState = {
4568 /** Assistant messages that existed when the current compaction started.
4669 * Used to scope stale-usage clearing to pre-compaction messages only.
4770 * Messages added during/after compaction are excluded. (#50795) */
4871 preCompactionAssistantMessages ?: Set < object > ;
4972 assistantTexts : string [ ] ;
50- toolMetas : Array < { toolName ?: string ; meta ?: string ; asyncStarted ?: boolean } > ;
73+ toolMetas : Array < {
74+ toolName ?: string ;
75+ meta ?: string ;
76+ asyncStarted ?: boolean ;
77+ asyncTaskRunId ?: string ;
78+ asyncTaskId ?: string ;
79+ } > ;
5180 acceptedSessionSpawns : AcceptedSessionSpawn [ ] ;
5281 toolMetaById : Map < string , ToolCallSummary > ;
5382 toolSummaryById : Set < string > ;
@@ -97,6 +126,9 @@ export type EmbeddedAgentSubscribeState = {
97126 lastStreamedReasoning ?: string ;
98127 lastBlockReplyText ?: string ;
99128 lastDeliveredBlockReplyText ?: string ;
129+ deferBlockReplyDelivery : boolean ;
130+ deferredBlockReplies : BlockReplyPayload [ ] ;
131+ deferredAssistantEvents : AssistantStreamDelivery [ ] ;
100132 toolExecutionSinceLastBlockReply : boolean ;
101133 reasoningStreamOpen : boolean ;
102134 assistantMessageIndex : number ;
@@ -123,7 +155,9 @@ export type EmbeddedAgentSubscribeState = {
123155 yielded ?: boolean ;
124156 timeoutPhase ?: AgentRunTimeoutPhase ;
125157 providerStarted ?: boolean ;
158+ terminalAborted ?: boolean ;
126159 hadDeterministicSideEffect ?: boolean ;
160+ pendingEventChain : Promise < void > | null ;
127161
128162 messagingToolSentTexts : string [ ] ;
129163 messagingToolSentTextsNormalized : string [ ] ;
@@ -148,6 +182,7 @@ export type EmbeddedAgentSubscribeState = {
148182 lastAssistant ?: AgentMessage ;
149183} ;
150184
185+ /** Handler context bundling params, mutable state, emitters, and helper hooks. */
151186export type EmbeddedAgentSubscribeContext = {
152187 params : SubscribeEmbeddedAgentSessionParams ;
153188 state : EmbeddedAgentSubscribeState ;
@@ -216,7 +251,15 @@ export type EmbeddedAgentSubscribeContext = {
216251 getUsageTotals : ( ) => NormalizedUsage | undefined ;
217252 getCompactionCount : ( ) => number ;
218253 getLastCompactionTokensAfter : ( ) => number | undefined ;
254+ emitAssistantStreamData : (
255+ data : AssistantStreamData ,
256+ options ?: { emitPartialReply ?: boolean } ,
257+ ) => void ;
219258 emitBlockReply : ( payload : BlockReplyPayload ) => void ;
259+ flushDeferredAssistantEvents : ( ) => void ;
260+ flushDeferredBlockReplies : ( ) => void ;
261+ clearDeferredAssistantEvents : ( ) => void ;
262+ clearDeferredBlockReplies : ( ) => void ;
220263} ;
221264
222265/**
0 commit comments