@@ -4,15 +4,25 @@ import {
44 embeddedAgentLog ,
55 setActiveEmbeddedRun ,
66} from "openclaw/plugin-sdk/agent-harness-runtime" ;
7+ import type { queueAgentHarnessMessage } from "openclaw/plugin-sdk/agent-harness-runtime" ;
8+ import type { AttemptTranscriptJournal } from "./attempt-transcript-journal.js" ;
79import type { AttemptParamsLike } from "./attempt-types.js" ;
810import type { attachEventBridge } from "./event-bridge.js" ;
11+ import type { SessionLike } from "./event-bridge.js" ;
912import type { CopilotUserInputBridge } from "./user-input-bridge.js" ;
13+
14+ const DEFAULT_STEERING_DELIVERY_TIMEOUT_MS = 120_000 ;
15+ type CopilotQueueMessageOptions = Parameters < typeof queueAgentHarnessMessage > [ 2 ] ;
16+
1017export function registerCopilotActiveRun ( params : {
1118 abortActiveSession : ( ) => void ;
1219 bridge : ReturnType < typeof attachEventBridge > | undefined ;
20+ canAcceptSteering : ( ) => boolean ;
1321 input : AttemptParamsLike ;
1422 isAborted : ( ) => boolean ;
1523 isSettled : ( ) => boolean ;
24+ session : SessionLike ;
25+ transcriptJournal : AttemptTranscriptJournal ;
1626 userInputBridge : CopilotUserInputBridge ;
1727} ) {
1828 const cancelGatewayQuestionBestEffort = ( resolvedBy : string ) => {
@@ -25,21 +35,52 @@ export function registerCopilotActiveRun(params: {
2535 } ;
2636 const activeRunHandle = {
2737 kind : "embedded" as const ,
28- queueMessage : async ( text : string , options ?: { isInboundUserMessage ?: boolean } ) => {
38+ queueMessage : async ( text : string , options ?: CopilotQueueMessageOptions ) => {
2939 if (
3040 options ?. isInboundUserMessage === true &&
3141 ( await claimPendingAgentQuestionAnswer ( {
3242 sessionKey : params . input . sessionKey ?? params . input . sessionId ,
3343 text,
44+ persist : options . userTurnTranscriptRecorder
45+ ? async ( ) => {
46+ await options . userTurnTranscriptRecorder ?. persistApproved ( ) ;
47+ }
48+ : undefined ,
3449 } ) )
3550 ) {
36- return ;
51+ return undefined ;
3752 }
38- throw new Error ( "Copilot runtime is not waiting for user input." ) ;
53+ if ( params . isSettled ( ) || params . isAborted ( ) ) {
54+ throw new Error ( "Copilot steering is unavailable after the active run ended" ) ;
55+ }
56+ if ( ! params . canAcceptSteering ( ) ) {
57+ throw new Error ( "Copilot steering is unavailable before initial user validation" ) ;
58+ }
59+ const messageId = await params . session . send ( { prompt : text } ) ;
60+ if ( options ?. waitForTranscriptCommit === true ) {
61+ try {
62+ await waitForPersistenceReceipt (
63+ params . transcriptJournal . waitForSdkUserPersisted ( messageId ) ,
64+ options . deliveryTimeoutMs ,
65+ ) ;
66+ } catch ( error ) {
67+ return {
68+ transcriptCommit : "unconfirmed" as const ,
69+ errorMessage :
70+ error instanceof Error
71+ ? error . message
72+ : "Copilot accepted steering but its transcript receipt was not confirmed" ,
73+ } ;
74+ }
75+ }
76+ return undefined ;
3977 } ,
40- isStreaming : ( ) => ! params . isSettled ( ) && ! params . isAborted ( ) ,
78+ isStreaming : ( ) => params . canAcceptSteering ( ) && ! params . isSettled ( ) && ! params . isAborted ( ) ,
4179 isAborted : params . isAborted ,
4280 isCompacting : ( ) => params . bridge ?. isCompacting ( ) ?? false ,
81+ // session.send resolves with the injected user-message id; the journal
82+ // receipt resolves only after that exact SDK event reaches canonical history.
83+ supportsTranscriptCommitWait : true ,
4384 sourceReplyDeliveryMode : params . input . sourceReplyDeliveryMode ,
4485 cancel : ( ) => {
4586 cancelGatewayQuestionBestEffort ( "run-cancel" ) ;
@@ -60,3 +101,32 @@ export function registerCopilotActiveRun(params: {
60101 ) ;
61102 return activeRunHandle ;
62103}
104+
105+ async function waitForPersistenceReceipt (
106+ receipt : Promise < void > ,
107+ requestedTimeoutMs : number | undefined ,
108+ ) : Promise < void > {
109+ const timeoutMs =
110+ typeof requestedTimeoutMs === "number" &&
111+ Number . isFinite ( requestedTimeoutMs ) &&
112+ requestedTimeoutMs > 0
113+ ? requestedTimeoutMs
114+ : DEFAULT_STEERING_DELIVERY_TIMEOUT_MS ;
115+ let timer : ReturnType < typeof setTimeout > | undefined ;
116+ try {
117+ await Promise . race ( [
118+ receipt ,
119+ new Promise < never > ( ( _ , reject ) => {
120+ timer = setTimeout (
121+ ( ) => reject ( new Error ( "Copilot steering transcript receipt timed out" ) ) ,
122+ timeoutMs ,
123+ ) ;
124+ timer . unref ?.( ) ;
125+ } ) ,
126+ ] ) ;
127+ } finally {
128+ if ( timer ) {
129+ clearTimeout ( timer ) ;
130+ }
131+ }
132+ }
0 commit comments