Skip to content

Commit bff0494

Browse files
authored
refactor(gateway): typed internal agent turn path for recovery (#121223)
* refactor(gateway): add typed internal agent turn path * chore(plugin-sdk): refresh API baseline
1 parent 2904bbf commit bff0494

12 files changed

Lines changed: 447 additions & 211 deletions

‎docs/.generated/plugin-sdk-api-baseline.sha256‎

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ a592581a61518734a8a410ec9f71069529ee5fbb54848daf650199eb1cb557ca module/account
33
71522995185b956a0cc4927a472cc8d1153e5e998874bfd9a750513175174713 module/account-id
44
64fc61ddb3ed2367ad193c8174e5ddf4f563d7456acd44ab3529219a8d0776fb module/account-resolution
55
4fbb1c87e99399f842a20d75d5e35a4b7064a1b7f02115c23f9a2a7cdcfb57ee module/agent-config-primitives
6-
1fbd58fb125a1ab69bacc7a700cac67a8a1d6c0d3b229212561c8cd1c8d72381 module/agent-harness
7-
789148fb788284586e15d65e8c5115bbfe7df91d201aff952dd63b70c37dda8f module/agent-harness-runtime
6+
dc5f4ba23a5831a5f9619049c6f87bdc8d2b9cd272025653750ee84ec0f8b222 module/agent-harness
7+
d0ee7a77ac65b3670dcd5526678e13bc966b76def43137445e377282085ed95e module/agent-harness-runtime
88
762a2c3df44621e9464fdd3ef437be2a878bfa9a7e8ca7670f88ae549e601614 module/agent-media-payload
9-
8913627dfb1dded389ccfba6609af2b1f45b9e434098b29500988c618ce3fc9c module/agent-runtime
9+
eca72eae4704a6828e07dbcd5e8618c409187324067578e519e7569a03fa6122 module/agent-runtime
1010
66cad8b985017a2487e47d6794dede2d16348f717e7f445cd5df4af2eb7f95f4 module/agent-scope-runtime
1111
8fecb210e22bce4532b6ab649b09465f0bd2c857a44abf40db7d683d6491e6da module/allow-from
1212
f0cd21c1854e209363fc2cbc684ba9dee9668ba7b5d0b57a96d4c8da825d7337 module/allowlist-config-edit
@@ -26,19 +26,19 @@ d1341161e2f5d44eaf7eca3a777ab3f106c7db1db32778bf98c75c7fa7d57ca4 module/channel
2626
c2cc71d5070b6071c51248b0648d1ad1a9468d3737df890adc77ec02025e8853 module/channel-config-primitives
2727
9fa7e5f9b3515f0bbc252210b034d39c25de9eb431def30dddce3198c309efac module/channel-config-schema
2828
a6af4971aa36345f1a32d97981bd36cc29794f62f30bd57859df2816c65f6fde module/channel-contract
29-
02a07336e2ccb44ffe617f75dd95590b46e4b7780860c9f73f6c3ee0d376cb3b module/channel-core
29+
f72c233f06b911825496edb619dfd76fe33e0a52172f2dbf8d5b79969edb983f module/channel-core
3030
40f8e726de245e17cf7bf908bb7d6fb084f4927f5ccc20b0c089f47af1bd7a0e module/channel-dm-policy
31-
58918972c9b991058b1b82c2e36f37e15b7d01d1a9bd8dec29e4080b9ab2b532 module/channel-entry-contract
31+
b76423e8c53a0fa835dd3d633edcb147b8cdc3a3e6a9ad7a3b5e54bbbdf2649b module/channel-entry-contract
3232
56b185eb99eb4981056befd38cd9511b655923934acb1c22aeec7d9c504bdf96 module/channel-feedback
33-
deffb7f24c1d0dc4494e31e5f2041b91d5f052b90e7ff9083c6c8f9003bced79 module/channel-inbound
33+
de6f1079345b0e161eaa306b9f59aa33cbf5b7858bccc6c61e946800d6c00472 module/channel-inbound
3434
780abefd1600c55da219da43c769c873d071ab616b5e02d22f365677ed5a7be4 module/channel-inbound-debounce
3535
b72485da4fe5d7f73352f4474d00e30b749b64c8a2066755e104958cd96036c2 module/channel-ingress-runtime
3636
f83762680c0d0aa6fa1bad9e53c70bfdf3694126dbdd9491db764a69ed3f6d67 module/channel-lifecycle
3737
0e47457e38d1df0bd572e1408cde2ca6a788b65205f43c585316b5ad3a8f2f16 module/channel-logging
38-
67f3db77df2c786a6dc3429b4f94dbad89e152591dd3727ce3ad33bbb0b9c5e0 module/channel-message
39-
401b0346239cadfb4db1c48e07f4e76f8f793839a926e828c47566667c645655 module/channel-outbound
40-
1108ed0e70c34994add0fcb5f6b0681568ef81a714b2bf7441edf9a5775283fc module/channel-pairing
41-
a1157e965dfa77ce935f73bc54bf393fd6bfb1495d95b5154c43c5e13f22d4a6 module/channel-plugin-common
38+
3f0c67dd5b9d53bdec6c7c255228e3579f1746c014b1212b361fbc10c97af7b3 module/channel-message
39+
afc036e15843f795bb295c6d9d4e378cca6d816c29c613ecfb49d47353fc8f3a module/channel-outbound
40+
5e0a4e78fa4877753e944a24afcb52bc18b84a99b56f26550ee83e311eae54bd module/channel-pairing
41+
024aff38b066e93618c36a0ecf825cb4e5ab6de5c4b9b3a4030c27f8252a1ada module/channel-plugin-common
4242
c2c7900e47c0595bbc8a166aa60cfbc5a0e02f9a4c0985d5fd630c7dec4c3dca module/channel-policy
4343
fde35c74c5db0f89b235210a1eca1b4e0712181c456f24793958fa73ee0ce042 module/channel-reply-pipeline
4444
482370e60135db9bfaf07f24bab549e5fde09ab265a6061a1f587c5d93929e91 module/channel-runtime-context
@@ -51,31 +51,31 @@ fde35c74c5db0f89b235210a1eca1b4e0712181c456f24793958fa73ee0ce042 module/channel
5151
67df67da5ae72e9eaeb19d41b6bd2432ec4fd8b7b63b2b616fb98f3b4e0ec41d module/channel-streaming-config
5252
fdeffe356c7c4edeec9f8fd03edcadc375eabc7a9412e582b10c3180e3ef40fc module/cli-argv
5353
ad12670dbfe538f8d0ebf4fb2b68080e93a760278278e6b1ce9bb129d4b2d533 module/collection-runtime
54-
8134198b58398da63ddc6e04040d1150d9f462bf8ae56f944acbc84db2a5362e module/command-auth
55-
38214365f960d43d71625f58d5fa5cc7a28d4b01fa0f9c8a621bacd9c3bac41e module/command-auth-native
54+
4f410c7170f48dff594d3ba06636289cc85295e294f76dd92c14a41f3a699d64 module/command-auth
55+
0bc3a0265e7cc89e47470386571fd7bb1ff4baf7754edf2155556de6df824f2f module/command-auth-native
5656
396f60be14d8fe1144076cffeafc2e2ddc0379b5e664f9fd45d13a0edc4a9f2f module/command-detection
5757
5155909167a810ca258ec9baf4814e7d495cf25bd287fbb22be55eb1a8d88dfe module/command-primitives-runtime
5858
0ab02df5c3904386bfb062e7dce0a24e2ed835b54dcc0112e367346624cb0efe module/command-status
5959
02011f70a5de5f1861609206038b101d3e9bbf39bf45a3cfaaae56596299225b module/config-contracts
6060
7ee5bc35d30165be33ab229a96dd6a6e3e04c620fa4c41753d568798c514feb3 module/config-mutation
6161
08beaa7b5b8797bf248068c73332847b196c5ce47a74c67e788b3281aea46a97 module/config-runtime
6262
069ca892c9d9c0d82f9a0d1e72970da2b1bc5d3c4fc15e127b18bbf78292cd2a module/conversation-runtime
63-
82e34c7a2af309677ce2f54477ad026369b2880b96684e38989e1896dd12cf83 module/core
64-
eceacf43fa56f443298bb5f489ae78677cb9f13df4d4abad7634b5d5537a0e01 module/dedupe-runtime
63+
ef1c0830cfd2fccec7388acdba7d88c3352657e617e5da5291f749233942b095 module/core
64+
943df87a9d7c71a60b588da8c069eadc29cd1dd9d8aafd6bdf558225719f8fa8 module/dedupe-runtime
6565
ebef0e650ab45e44c9335e2b3e15588c968cea6dadd125364a076f9c50ad1e8c module/device-bootstrap
6666
9ecd22bc3acae3f9b0556b305c4232abe54a7993b910caa789cba6cf917dd1e2 module/diagnostic-runtime
6767
6d90412b97efcc675d16acc3aeb2752520a613ce4bb25b87e47f84fd6b36736d module/directory-runtime
68-
841d0b1fcb267198e4422be8b3ebdc6625bf24ec5ab513606ff72ac2353993ab module/discord
68+
2c859542e09d467fd6ac2d46d5c3e79a2d707bc517500901c4f8c65f244dd565 module/discord
6969
f2d69888d0c799e12d97b5f92393906aa843be25f61cb6a6c55d32f1db800a44 module/error-runtime
7070
1caf8cc5552ca5e392599457e3f9616e033de199d9f47c12320cdee617a6b0e9 module/extension-shared
7171
dd9f6e0fd33cc88b22543c1ee30cc09cf4de4d8f30dff7b7f9cebef885c21543 module/gateway-method-runtime
72-
721c77e48f30f92bd41f9eddab0a573e1791a01f3f98d6f1ef1e8de4fc09916d module/gateway-runtime
72+
7919b19db8bf80043ac2fd10d7cdadcc3ec5367752cc6cbe7d0f5b61886276bb module/gateway-runtime
7373
575656e5e0195c8d1813a4e2e3a271e800bb97d44f2dd78c242e3b6714ffa097 module/group-access
7474
77726dc396a269ca62fe958c4c797de54d94aba47b7b166e0b66052b604c4b93 module/health
7575
74252b820ea1d21457f01a4e3b868233737e83b134f3d8b8cecfb2392a9647e7 module/hook-runtime
7676
4d9f7d3c3e59113b493f3cac812b3c117968035af10a751a92f2aca8e2ec8806 module/inbound-envelope
7777
4928af5d2509f696b896f53ac790303a0742202dbcdae3e44fe6d1b434a9c1ba module/inbound-event-delivery
78-
a551c9ef5540138c0e0b40c341860150688e35505d5a06f5047a0b5be5e80375 module/inbound-reply-dispatch
78+
785de60e1b38da50cb409722b93b6fbe6ecb9ddf0e6fdecf1024b93c58fcf930 module/inbound-reply-dispatch
7979
7f75e1166081708656b561095753ab05480265a55bc40a8e5ca34e221a4798e2 module/infra-runtime
8080
ce73721421f1b903dd04ead4df173582e59ea3e9990248102c448b419cc6d272 module/ingress-effect-once
8181
2fdc37835ccc4651c580847250adf8e42c165483eb239907aea981a6552e24b4 module/interactive-runtime
@@ -89,49 +89,49 @@ d8e039f3ebb4c4bf7e30217105e2aae84934f73df9e05be27db4f746ab45f1a9 module/media-r
8989
6a52f93107335f88751704352cc01e62add06f854a5b7d765e2a5ee87c0313b6 module/media-store
9090
6e5b09a983a69041873ece5bc3b719c0dc913e5a69e2afdf6c7958889e711097 module/media-understanding
9191
7f74e947cca0cf894869bede29815863d5eacca016d04d6262e9d41b3999ae94 module/media-understanding-runtime
92-
cf4f49e9e1c45913acd6a9332f8bb35a8b7de102edcc6eeebacf3fa666101552 module/meeting-runtime
92+
4284e7bab8b48c14c4950f703f6b4fc3e0a23164086e5773b41cb35e1d23db99 module/meeting-runtime
9393
e5d2b540090c17602a1c36b42559f7b516e20730e06bbdd8606597c66ef2ef95 module/memory-core-host-engine-foundation
94-
451082d20bde79f9e50570a3e38dd4f7f880337055840774ca1df02381cad98b module/memory-host-core
94+
58b06d2d025e63e852270e871dbd8bc2c308fbfa815d2b31cb7330a27302a94c module/memory-host-core
9595
1efa0aadc4261d1c6073058cbf3dcc9fa681424819bdd14333e19b249bbc4b18 module/messaging-targets
9696
3b35256be44a7f2f69a1c2ebb33b1028d6bdc8c714a426f3b466a61056c5c5d7 module/model-session-runtime
97-
0b554d2ebaddc7d4cddc3fb31585371b036381170ce1c5640741c7858c86f960 module/models-provider-runtime
97+
27d34ee308fa91484d6fa40190b205b1e139a9aaf310850c1e8a5b9912ae7b2e module/models-provider-runtime
9898
b518b4caa7f138d448fe08f5fc9e5f67da5d4b26aafaee2370feb2300e1baa21 module/native-command-config-runtime
9999
1e8b5e7bd8234919d339e750368cbe6425e7463d7e63eb5200d728cc17d6e959 module/native-command-registry
100100
15d1d490e4f7909d2d563b83988258a9cc5fa1344cffb0cd3dcd7592c0467632 module/param-readers
101101
ca7a56bb1a6169b4cf9befbf5aa21da280a8086fdc49fca4eec520a7a7c98549 module/persistent-dedupe
102102
f8b250da3eab2f85b2bf358872f910b29e541ea04358498a6deab353fa0520fc module/plugin-config-runtime
103-
405b226c64bcdcd6c4d630cf4f223b00028cd9cddb111e82b24d406a3fc7e202 module/plugin-entry
104-
ee2c2ccde86934c67a0d39ec2203a30fddd16b3a3ef7aec338b45694f1f7a375 module/plugin-runtime
105-
9a84a66ddde2759e863b6ecf34e97cb99825e0911f36a0af14fc7e80869f03be module/provider-auth
106-
474c4a92d7b4050f023bb5586617cc978c2c31578d5723e64c03ea127ac70e0d module/provider-catalog-runtime
103+
63aa620ad90d217e31bb55f448cbdfacf823dd17c1b1005baf5f8122b350d555 module/plugin-entry
104+
a8e736985eb018d9f7545b94875cbb4c1dc4c351ecd3142faf72f5ebfe4f66e7 module/plugin-runtime
105+
e70beb5db3ada1eb416ffc97240c81b3a8b5b8e09ea974b18bc6a7c00904267f module/provider-auth
106+
04183b53b3e6ad09347ac7b63f700cf81edaecfe29e3782cf467f53349fa7930 module/provider-catalog-runtime
107107
8131147d699394bd06503e2ea2f5f1a50b1594a87dded6d118b74a8d0328c8f6 module/proxy-capture
108108
913c32adcd94ea9c8b7efac0f7c65231500c49129e638c0682eb632da3d5a0c8 module/question-gateway-runtime
109109
f319c52a0f1655de752efa55a983893e56e4afa0727f6cc4896c3238c984e91c module/reply-chunking
110-
1fde835f8398ce0f627c794fedfa735db750685909dbba1552b005834b59f9e7 module/reply-dispatch-runtime
110+
2c2e5df7b8ece32b837f152a4aff60a8c674a6b65b62c8c8f5af3afd128a43fe module/reply-dispatch-runtime
111111
73f861fa3179d5af1159853c5acab0eec7a6c8f9398dcb75ea770e784fca6727 module/reply-history
112112
ecc6e3190892ff416ce69d26c8ce0aa08a468c8689ca3606c18b0271e2d87597 module/reply-payload
113-
33479b0cc629940332a7724ee380912be19614894b70a369d1d85ceda47fa6b6 module/reply-runtime
113+
cf0283a960539456c02488b19e77eb789598e0f3a0abd59b215d6b402dad4079 module/reply-runtime
114114
aa07d85d99fdd2b1e0cbe9975fb6dcae66b8bdce2607c6bd5402ae68bb15118c module/root-walk
115115
292405cda407e2569f731212f6f981b12bfe682836d5f6e58c6ae194ba27f144 module/routing
116116
7877a7e58fa32a64107154e5b714c6d165e96989d4aa5f43e0afac085a187af0 module/run-command
117117
14c9f7ce9f8b8a47799f6a870110d1d8469223a01a2dba01fc8bc1f11a35cfe0 module/runtime
118118
f7900c5ff5827e1b53a926367d807bd4c81b313ab74fd0409d571ae881226a60 module/runtime-config-snapshot
119119
8e33ccbbe610c6c3ad04a140f7511a16cf406733c8459216b1fb4b1e7f0208d9 module/runtime-env
120120
7e871b7319745678bb83fcfc1b54c8751b0ab1af92ff06c01d0659ac92863c11 module/runtime-group-policy
121-
0d38198bd13bd5b0ed2f04ccefd805a6dbe9213b3b1a1c75bde1ce42c87b08c7 module/runtime-store
121+
b7d824cbec83bf90000c941ed96a4f2ebc298cd21b395fea08004d9f171ac1d4 module/runtime-store
122122
d17862c40825af1ddf0257b44f1e1cbb9c375e8e5ed668fae75d530d1a465cf9 module/secret-file
123123
8e2ac4d3973d8d8ce4478e3440d66ee5c0d9213b0fe9e927c421d14fd31e5e86 module/secret-input
124124
5958846179128ea6b29135fb90ce4e40b8df7de63d1c65d1696e52294cbc0f52 module/secret-input-runtime
125125
72ca4634e0038ad6dc80c4ce12eb34014e1fa990c7eabe19bb4501b2722ee685 module/secret-ref-runtime
126126
6db2af355b0d1d46f493a5d34a63a6ea5231dafbd8b896d976a52da681868f67 module/security-runtime
127-
f700943c355142a66fe0a5033ff1c04c60acdc963529662f60ca5d1370a1e557 module/session-catalog
128-
48b55e67e967e5ff8aa5b258b8dff308729945b2d024ddec7a5f920a81a4382a module/session-discussion
127+
1198f77ee999aecc74f6b033131922dadcab05be774d7893635a24d456b3681b module/session-catalog
128+
faad33f1fb9314e36ec58c2c675cd277d04e1c41554e4cb80a38a79776976f68 module/session-discussion
129129
cd31b3430b196d78604de2682802f95453ce8e32c2222ae5f94241e422a2e1e6 module/session-store-runtime
130130
e54f5c86a55683028a8c4a7c821cf9b9f11b48f91ae9b08e79f8e94b91eb5fde module/setup
131131
41c4790efda179c600fcc8cc4f2cd9c0505efd86df15a1a840aeb99c839bc627 module/setup-runtime
132132
44d37e0d9131ad2859f41068f2604090c784e65f1bd6ebda8e051b6f2e5e1660 module/setup-tools
133133
dc901275501e473a69a8e9435e1c95096c47f1bb569cf3240c1e82c769fffaa0 module/skill-commands-runtime
134-
68a81d3ec617556543ecc74189a4b01ca4d5431461e82029c0fa6e1d07b32648 module/speech-settings
134+
623e70e5fa77936c6a15ca3a0ae5cf47713e80ef47525a00fa18c3629ff71fde module/speech-settings
135135
1e4df0f48b50a8eb546459af1e947ba143c287e3d9c13706c45b67d411c46ad5 module/ssrf-policy
136136
e4940099376e18e34c234d4d90d836cbe234c3e891e40b7b4879168b386b1121 module/ssrf-runtime
137137
3c3c812d2d0c997ec81d117346347b802a3445e8e0d76aa6d0e3259f3cf55f98 module/state-paths
@@ -141,11 +141,11 @@ f097d0096b21c8a052f0f649b7512ecf2aba4744ae6956f001950e053828b309 module/string-
141141
aef35bee2502cd6ed8765409b758e452aff8ac9469fd773e6a2a44c9a1bc3f66 module/temp-path
142142
87fa81b9e58d8fc04a4b4202d2d37fca339615f5225687d9db905151439e0f4d module/text-chunking
143143
50d2fa56b2ad57edd12d74201d18ab43045b5c9da0ab98cc158cebe9bd8b768b module/text-runtime
144-
9bdbe8da529a24ee4413a50bfe92b239684e2499236a2e37c0b9961f52fb96ee module/tool-plugin
144+
6a10eaa7c058895f47b47a578f7a055deb065b8215e794203a099fe5d8ce1cf1 module/tool-plugin
145145
dc1a073c59ab61e2789533b777b3f0cb9af689d64a97796b10e8aa82552510db module/tool-results
146146
5d4f1dbde1238799e5244b9c0b799aa2fb7d607d0c072d9a70659978825ef4ee module/tool-send
147147
cda105b721d498df23a554c6b68be150b8fe66b8b9172185c31a0b3b0646b1dc module/web-media
148-
18f0f10817717fba6d3a4c660c62e58d83c3b75a15be848b24da30ee31399075 module/webhook-ingress
148+
13e687addfc0a57760ab390928bbd5c38bd185f0ddf7b511033b76a41c7e3b16 module/webhook-ingress
149149
ddd05cb74246c393fef4d3fd8331eddd4db72a000fe45e9e515357b757636854 module/webhook-request-guards
150150
de59e86e126b75d13251cba7ebbe27b44d9b5588785d98df5ff4d6722374c81f module/widget-html
151151
9161b36ec0ab062ea41b363c894fcd672a7727f21cb726739f99f9c184fce69d module/zod

‎src/agents/main-session-restart-dispatch.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { applySessionEntryReplacements } from "../config/sessions/session-access
1414
import type { OpenClawConfig } from "../config/types.openclaw.js";
1515
import { isTrustedMessageActionTurnIngress } from "../gateway/message-action-turn-capability.js";
1616
import type { GatewayRecoveryRuntime } from "../gateway/server-instance-runtime.types.js";
17+
import type { AgentRunRequest } from "../gateway/server-methods/agent-request-types.js";
1718
import { getAgentEventLifecycleGeneration } from "../infra/agent-events.js";
1819
import { createSubsystemLogger } from "../logging/subsystem.js";
1920
import { findRestartRecoveryUnsafeReplyHook } from "../plugins/restart-recovery-hook-safety.js";
@@ -513,7 +514,7 @@ export async function resumeMainSession(params: {
513514
? "failed"
514515
: "skipped";
515516
}
516-
const agentParams: Record<string, unknown> = {
517+
const agentParams: AgentRunRequest = {
517518
message: buildResumeMessage(sanitizedPendingText),
518519
sessionKey: dispatchSessionKey,
519520
expectedExistingSessionId: params.entry.sessionId,

‎src/agents/subagent-registry.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/** Coordinates subagent registration, lifecycle, delivery, steering, recovery, and persistence. */
2+
import type { AgentWaitParams } from "../../packages/gateway-protocol/src/index.js";
23
import type { OpenClawConfig } from "../config/types.openclaw.js";
34
import { callGateway } from "../gateway/call.js";
45
import { getGatewayRecoveryRuntime } from "../gateway/server-recovery-runtime-context.js";
@@ -440,7 +441,7 @@ const subagentRunManager = createSubagentRunManager({
440441
// Registry waits are Gateway-owned lifecycle work. Keep them on the
441442
// owning instance when one exists; standalone processes authenticate normally.
442443
return await gatewayRuntime.waitForAgent<T>(
443-
(request.params ?? {}) as Record<string, unknown>,
444+
(request.params ?? {}) as AgentWaitParams,
444445
request.timeoutMs ?? undefined,
445446
);
446447
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
import type { AgentWaitParams, ErrorShape } from "../../../packages/gateway-protocol/src/index.js";
2+
import type { GatewayMethodRegistry } from "../methods/registry.js";
3+
import {
4+
waitForGatewayDispatch,
5+
unwrapGatewayMethodDispatchResponse,
6+
} from "../server-in-process-dispatch.js";
7+
import { runWithGatewayRequestEnvelope } from "../server-methods.js";
8+
import { prepareAgentRequestPreflight } from "../server-methods/agent-request-preflight.js";
9+
import type { AgentRunRequest } from "../server-methods/agent-request-types.js";
10+
import type { GatewayRequestOptions } from "../server-methods/types.js";
11+
import { createAgentTurnService } from "./agent-turn-service.js";
12+
import { captureAgentTurnPrincipal } from "./principal.js";
13+
import type { AgentTurnFrame, AgentTurnIo } from "./types.js";
14+
15+
type InternalAgentTurnFacadeOptions = {
16+
client: NonNullable<GatewayRequestOptions["client"]>;
17+
getContext: () => GatewayRequestOptions["context"];
18+
getMethodRegistry: () => GatewayMethodRegistry;
19+
isWebchatConnect?: GatewayRequestOptions["isWebchatConnect"];
20+
};
21+
22+
function unwrapAgentTurnFrame(method: string, frame: AgentTurnFrame): unknown {
23+
const [ok, payload, error] = frame;
24+
return unwrapGatewayMethodDispatchResponse(method, { ok, payload, error });
25+
}
26+
27+
function throwEnvelopeRejection(method: string, error: ErrorShape): never {
28+
return unwrapGatewayMethodDispatchResponse(method, {
29+
ok: false,
30+
error,
31+
}) as never;
32+
}
33+
34+
/** Typed, frame-free access to agent turns owned by the running Gateway instance. */
35+
export function createInternalAgentTurnFacade(options: InternalAgentTurnFacadeOptions) {
36+
const isWebchatConnect = options.isWebchatConnect ?? (() => false);
37+
38+
const dispatch = async <T = unknown>(
39+
request: AgentRunRequest,
40+
timeoutMs?: number,
41+
): Promise<T> => {
42+
const method = "agent";
43+
const context = options.getContext();
44+
const methodRegistry = options.getMethodRegistry();
45+
let acceptance: AgentTurnFrame | undefined;
46+
const io: AgentTurnIo = {
47+
emitAcceptance: (frame) => {
48+
acceptance ??= frame;
49+
},
50+
// Recovery consumes only acceptance; terminal state remains available through wait.
51+
emitFinal: () => {},
52+
};
53+
const operation = runWithGatewayRequestEnvelope(
54+
method,
55+
options.client,
56+
async () => {
57+
const principal = captureAgentTurnPrincipal(options.client);
58+
const preflight = prepareAgentRequestPreflight({
59+
request,
60+
context,
61+
client: principal,
62+
io,
63+
});
64+
if (!preflight) {
65+
return;
66+
}
67+
await createAgentTurnService({ context, isWebchatConnect }).startTurn({
68+
preflight,
69+
principal,
70+
io,
71+
});
72+
},
73+
{
74+
context,
75+
isWebchatConnect,
76+
methodRegistry,
77+
reject: (error) => io.emitAcceptance([false, undefined, error]),
78+
},
79+
);
80+
const response = operation.then(
81+
() => {
82+
if (!acceptance) {
83+
throw new Error(`Gateway method "${method}" completed without a response.`);
84+
}
85+
return acceptance;
86+
},
87+
(error: unknown) => {
88+
if (acceptance) {
89+
return acceptance;
90+
}
91+
throw error;
92+
},
93+
);
94+
return unwrapAgentTurnFrame(
95+
method,
96+
await waitForGatewayDispatch(method, response, timeoutMs),
97+
) as T;
98+
};
99+
100+
const wait = async <T = unknown>(params: AgentWaitParams, timeoutMs?: number): Promise<T> => {
101+
const method = "agent.wait";
102+
const context = options.getContext();
103+
const result = runWithGatewayRequestEnvelope(
104+
method,
105+
options.client,
106+
() => createAgentTurnService({ context, isWebchatConnect }).waitForTurn(params),
107+
{
108+
context,
109+
isWebchatConnect,
110+
methodRegistry: options.getMethodRegistry(),
111+
reject: (error) => throwEnvelopeRejection(method, error),
112+
},
113+
);
114+
return (await waitForGatewayDispatch(method, result, timeoutMs)) as T;
115+
};
116+
117+
return { dispatch, wait };
118+
}

0 commit comments

Comments
 (0)