@@ -94,6 +94,28 @@ private data class RealtimeToolCompletion(
9494 val messageEl : JsonElement ? ,
9595)
9696
97+ private data class RealtimeToolCompletionDispatch (
98+ val toolRun : RealtimeToolRun ,
99+ val state : String ,
100+ val messageEl : JsonElement ? ,
101+ )
102+
103+ private sealed interface RealtimeToolRegistration {
104+ data object SessionEnded : RealtimeToolRegistration
105+
106+ data object AwaitingCompletion : RealtimeToolRegistration
107+
108+ data class Completed (val dispatch : RealtimeToolCompletionDispatch ) : RealtimeToolRegistration
109+ }
110+
111+ private sealed interface RealtimeToolCompletionDecision {
112+ data object NotHandled : RealtimeToolCompletionDecision
113+
114+ data object Consumed : RealtimeToolCompletionDecision
115+
116+ data class Dispatch (val completion : RealtimeToolCompletionDispatch ) : RealtimeToolCompletionDecision
117+ }
118+
97119class TalkModeManager internal constructor(
98120 private val context : Context ,
99121 private val scope : CoroutineScope ,
@@ -170,6 +192,7 @@ class TalkModeManager internal constructor(
170192 private var realtimeAppendJob: Job ? = null
171193
172194 // Realtime tool calls can complete before their chat final arrives; cache by call/run id until both sides meet.
195+ private val realtimeToolLock = Any ()
173196 private val realtimeToolRuns = LinkedHashMap <String , RealtimeToolRun >()
174197 private val pendingRealtimeToolCalls = LinkedHashSet <String >()
175198 private val pendingRealtimeToolCompletions = LinkedHashMap <String , RealtimeToolCompletion >()
@@ -472,10 +495,7 @@ class TalkModeManager internal constructor(
472495 val activeSession = mainSessionKey.ifBlank { " main" }
473496 if (eventSession != null && eventSession != activeSession) return
474497
475- if (maybeCompleteRealtimeToolCall(runId = runId, state = state, messageEl = obj[" message" ])) {
476- return
477- }
478- if (holdPendingRealtimeToolCompletion(runId = runId, state = state, messageEl = obj[" message" ])) {
498+ if (handleRealtimeToolCompletion(runId = runId, state = state, messageEl = obj[" message" ])) {
479499 return
480500 }
481501
@@ -1034,9 +1054,11 @@ class TalkModeManager internal constructor(
10341054 }
10351055 realtimeCaptureJob = null
10361056 realtimeAppendJob = null
1037- realtimeToolRuns.clear()
1038- pendingRealtimeToolCalls.clear()
1039- pendingRealtimeToolCompletions.clear()
1057+ synchronized(realtimeToolLock) {
1058+ realtimeToolRuns.clear()
1059+ pendingRealtimeToolCalls.clear()
1060+ pendingRealtimeToolCompletions.clear()
1061+ }
10401062 realtimeUserEntryId = null
10411063 realtimeUserEntryAwaitingFinal = false
10421064 realtimeUserEntryAwaitingFinalStartedAtMs = null
@@ -1071,7 +1093,9 @@ class TalkModeManager internal constructor(
10711093 forced : Boolean = false,
10721094 ) {
10731095 val relaySessionId = realtimeSessionId ? : return
1074- pendingRealtimeToolCalls.add(callId)
1096+ synchronized(realtimeToolLock) {
1097+ pendingRealtimeToolCalls.add(callId)
1098+ }
10751099 scope.launch {
10761100 try {
10771101 if (name == REALTIME_AGENT_CONTROL_TOOL ) {
@@ -1093,18 +1117,11 @@ class TalkModeManager internal constructor(
10931117 session.request(" talk.client.toolCall" , params.toString(), timeoutMs = 15_000 )
10941118 val runId = parseRunId(response)
10951119 if (! runId.isNullOrBlank()) {
1096- if (realtimeSessionId != relaySessionId) return @launch
1097- realtimeToolRuns[runId] =
1098- RealtimeToolRun (callId = callId, relaySessionId = relaySessionId)
1099- val completion = pendingRealtimeToolCompletions.remove(runId)
1100- if (completion != null ) {
1101- maybeCompleteRealtimeToolCall(
1102- runId = runId,
1103- state = completion.state,
1104- messageEl = completion.messageEl,
1105- )
1106- } else {
1107- _statusText .value = " Thinking…"
1120+ when (val registration = registerRealtimeToolRun(runId, callId, relaySessionId)) {
1121+ RealtimeToolRegistration .SessionEnded -> return @launch
1122+ RealtimeToolRegistration .AwaitingCompletion -> _statusText .value = " Thinking…"
1123+ is RealtimeToolRegistration .Completed ->
1124+ dispatchRealtimeToolCompletion(registration.dispatch)
11081125 }
11091126 } else {
11101127 submitRealtimeToolError(callId, " tool call returned no run id" , relaySessionId)
@@ -1114,55 +1131,96 @@ class TalkModeManager internal constructor(
11141131 Log .w(tag, " realtime toolCall failed: ${err.message ? : err::class .simpleName} " )
11151132 submitRealtimeToolError(callId, err.message ? : " tool call failed" , relaySessionId)
11161133 } finally {
1117- pendingRealtimeToolCalls.remove(callId)
1134+ synchronized(realtimeToolLock) {
1135+ pendingRealtimeToolCalls.remove(callId)
1136+ }
11181137 }
11191138 }
11201139 }
11211140
1122- private fun holdPendingRealtimeToolCompletion (
1141+ private fun registerRealtimeToolRun (
11231142 runId : String ,
1124- state : String ,
1125- messageEl : JsonElement ? ,
1126- ): Boolean {
1127- if (realtimeSessionId == null || pendingRealtimeToolCalls.isEmpty()) return false
1128- if (state != " final" && state != " aborted" && state != " error" ) return false
1129- pendingRealtimeToolCompletions[runId] =
1130- RealtimeToolCompletion (state = state, messageEl = messageEl)
1131- return true
1132- }
1143+ callId : String ,
1144+ relaySessionId : String ,
1145+ ): RealtimeToolRegistration =
1146+ synchronized(realtimeToolLock) {
1147+ if (realtimeSessionId != relaySessionId) {
1148+ return @synchronized RealtimeToolRegistration .SessionEnded
1149+ }
1150+ val toolRun = RealtimeToolRun (callId = callId, relaySessionId = relaySessionId)
1151+ val completion = pendingRealtimeToolCompletions.remove(runId)
1152+ if (completion == null ) {
1153+ realtimeToolRuns[runId] = toolRun
1154+ RealtimeToolRegistration .AwaitingCompletion
1155+ } else {
1156+ RealtimeToolRegistration .Completed (
1157+ RealtimeToolCompletionDispatch (
1158+ toolRun = toolRun,
1159+ state = completion.state,
1160+ messageEl = completion.messageEl,
1161+ ),
1162+ )
1163+ }
1164+ }
11331165
1134- private fun maybeCompleteRealtimeToolCall (
1166+ private fun handleRealtimeToolCompletion (
11351167 runId : String ,
11361168 state : String ,
11371169 messageEl : JsonElement ? ,
11381170 ): Boolean {
1139- val toolRun = realtimeToolRuns[runId] ? : return false
1140- if (toolRun.relaySessionId != realtimeSessionId) {
1141- realtimeToolRuns.remove(runId)
1142- return true
1171+ if (state != " final" && state != " aborted" && state != " error" ) return false
1172+ val decision =
1173+ synchronized(realtimeToolLock) {
1174+ val toolRun = realtimeToolRuns.remove(runId)
1175+ if (toolRun != null ) {
1176+ if (toolRun.relaySessionId != realtimeSessionId) {
1177+ return @synchronized RealtimeToolCompletionDecision .Consumed
1178+ }
1179+ return @synchronized RealtimeToolCompletionDecision .Dispatch (
1180+ RealtimeToolCompletionDispatch (toolRun = toolRun, state = state, messageEl = messageEl),
1181+ )
1182+ }
1183+ if (realtimeSessionId == null || pendingRealtimeToolCalls.isEmpty()) {
1184+ return @synchronized RealtimeToolCompletionDecision .NotHandled
1185+ }
1186+ pendingRealtimeToolCompletions[runId] =
1187+ RealtimeToolCompletion (state = state, messageEl = messageEl)
1188+ while (pendingRealtimeToolCompletions.size > maxCachedRunCompletions) {
1189+ pendingRealtimeToolCompletions.remove(pendingRealtimeToolCompletions.keys.first())
1190+ }
1191+ RealtimeToolCompletionDecision .Consumed
1192+ }
1193+ return when (decision) {
1194+ RealtimeToolCompletionDecision .NotHandled -> false
1195+ RealtimeToolCompletionDecision .Consumed -> true
1196+ is RealtimeToolCompletionDecision .Dispatch -> {
1197+ dispatchRealtimeToolCompletion(decision.completion)
1198+ true
1199+ }
11431200 }
1144- when (state) {
1201+ }
1202+
1203+ private fun dispatchRealtimeToolCompletion (dispatch : RealtimeToolCompletionDispatch ) {
1204+ when (dispatch.state) {
11451205 " final" -> {
1146- realtimeToolRuns.remove(runId )
1147- val text = extractTextFromChatEventMessage(messageEl).orEmpty()
1206+ val text = extractTextFromChatEventMessage(dispatch.messageEl).orEmpty( )
1207+ val toolRun = dispatch.toolRun
11481208 scope.launch {
11491209 submitRealtimeToolResult(
11501210 callId = toolRun.callId,
11511211 result = buildJsonObject { put(" text" , JsonPrimitive (text)) },
11521212 sessionId = toolRun.relaySessionId,
11531213 )
11541214 }
1155- return true
11561215 }
11571216 " aborted" , " error" -> {
1158- realtimeToolRuns.remove(runId)
1217+ val toolRun = dispatch.toolRun
1218+ val state = dispatch.state
11591219 scope.launch {
11601220 submitRealtimeToolError(toolRun.callId, state, toolRun.relaySessionId)
11611221 }
1162- return true
11631222 }
11641223 }
1165- return false
11661224 }
11671225
11681226 private suspend fun submitRealtimeToolError (
0 commit comments