33 ConversationRole ,
44 StopReason as BedrockStopReason ,
55} from "@aws-sdk/client-bedrock-runtime" ;
6+ import { withProviderAcceptanceObserver } from "openclaw/plugin-sdk/provider-transport-runtime" ;
67import { afterEach , describe , expect , it , vi } from "vitest" ;
78import { streamSimpleBedrock } from "./stream.runtime.js" ;
89
@@ -23,6 +24,26 @@ async function* events(items: unknown[]) {
2324 yield * items ;
2425}
2526
27+ function streamBedrockForTest ( options : Parameters < typeof streamSimpleBedrock > [ 2 ] = { } ) {
28+ return streamSimpleBedrock (
29+ model as never ,
30+ { messages : [ { role : "user" , content : "Hello" , timestamp : 0 } ] } as never ,
31+ options ,
32+ ) ;
33+ }
34+
35+ function expectDestroyedClient (
36+ send : ReturnType < typeof vi . spyOn > ,
37+ destroy : ReturnType < typeof vi . spyOn > ,
38+ ) {
39+ expect ( send ) . toHaveBeenCalledOnce ( ) ;
40+ expect ( destroy ) . toHaveBeenCalledOnce ( ) ;
41+ expect ( destroy . mock . contexts [ 0 ] ) . toBe ( send . mock . contexts [ 0 ] ) ;
42+ expect ( destroy . mock . invocationCallOrder [ 0 ] ) . toBeGreaterThan (
43+ send . mock . invocationCallOrder [ 0 ] ?? 0 ,
44+ ) ;
45+ }
46+
2647afterEach ( ( ) => {
2748 vi . restoreAllMocks ( ) ;
2849} ) ;
@@ -109,3 +130,124 @@ describe("Bedrock provider-owned stream lifecycle", () => {
109130 }
110131 } ) ;
111132} ) ;
133+
134+ describe ( "Bedrock stream client lifecycle" , ( ) => {
135+ it ( "destroys the client after a successful stream" , async ( ) => {
136+ let markStreamBlocked ! : ( ) => void ;
137+ const streamBlocked = new Promise < void > ( ( resolve ) => {
138+ markStreamBlocked = resolve ;
139+ } ) ;
140+ let releaseStream ! : ( ) => void ;
141+ const streamReleased = new Promise < void > ( ( resolve ) => {
142+ releaseStream = resolve ;
143+ } ) ;
144+ async function * successfulStream ( ) {
145+ yield { messageStart : { role : ConversationRole . ASSISTANT } } ;
146+ markStreamBlocked ( ) ;
147+ await streamReleased ;
148+ yield { messageStop : { stopReason : BedrockStopReason . END_TURN } } ;
149+ }
150+ const send = vi . spyOn ( BedrockRuntimeClient . prototype , "send" ) . mockResolvedValue ( {
151+ $metadata : { httpStatusCode : 200 , requestId : "bedrock-request-1" } ,
152+ stream : successfulStream ( ) ,
153+ } as never ) ;
154+ const destroy = vi . spyOn ( BedrockRuntimeClient . prototype , "destroy" ) ;
155+ const acceptanceObserver = vi . fn ( ) ;
156+ const onResponse = vi . fn ( ) ;
157+ const options = withProviderAcceptanceObserver ( { onResponse } , acceptanceObserver ) ;
158+
159+ const resultPromise = streamBedrockForTest ( options ) . result ( ) ;
160+ await streamBlocked ;
161+ expect ( destroy ) . not . toHaveBeenCalled ( ) ;
162+
163+ releaseStream ( ) ;
164+ const result = await resultPromise ;
165+
166+ expect ( result . stopReason ) . toBe ( "stop" ) ;
167+ expect ( acceptanceObserver ) . toHaveBeenCalledWith ( {
168+ kind : "http_response" ,
169+ status : 200 ,
170+ headers : { "x-amzn-requestid" : "bedrock-request-1" } ,
171+ } ) ;
172+ expect ( onResponse ) . toHaveBeenCalledWith (
173+ { status : 200 , headers : { "x-amzn-requestid" : "bedrock-request-1" } } ,
174+ expect . objectContaining ( { provider : "amazon-bedrock" } ) ,
175+ ) ;
176+ expectDestroyedClient ( send , destroy ) ;
177+ } ) ;
178+
179+ it ( "cancels an unread stream when provider acceptance fails" , async ( ) => {
180+ const close = vi . fn ( async ( ) => ( { done : true as const , value : undefined } ) ) ;
181+ const responseIterator = {
182+ next : vi . fn ( ( ) => new Promise < IteratorResult < never > > ( ( ) => { } ) ) ,
183+ return : close ,
184+ [ Symbol . asyncIterator ] ( ) {
185+ return this ;
186+ } ,
187+ } ;
188+ const send = vi . spyOn ( BedrockRuntimeClient . prototype , "send" ) . mockResolvedValue ( {
189+ $metadata : { httpStatusCode : 200 } ,
190+ stream : responseIterator ,
191+ } as never ) ;
192+ const destroy = vi . spyOn ( BedrockRuntimeClient . prototype , "destroy" ) ;
193+ const hookError = new Error ( "acceptance observer failed" ) ;
194+ const options = withProviderAcceptanceObserver ( { } , ( ) => {
195+ throw hookError ;
196+ } ) ;
197+
198+ const result = await streamBedrockForTest ( options ) . result ( ) ;
199+
200+ expect ( result ) . toMatchObject ( {
201+ stopReason : "error" ,
202+ errorMessage : "acceptance observer failed" ,
203+ } ) ;
204+ expect ( close ) . toHaveBeenCalledOnce ( ) ;
205+ expectDestroyedClient ( send , destroy ) ;
206+ } ) ;
207+
208+ it ( "destroys the client after a provider error" , async ( ) => {
209+ const send = vi
210+ . spyOn ( BedrockRuntimeClient . prototype , "send" )
211+ . mockRejectedValue ( new Error ( "synthetic provider failure" ) ) ;
212+ const destroy = vi . spyOn ( BedrockRuntimeClient . prototype , "destroy" ) ;
213+
214+ const result = await streamBedrockForTest ( ) . result ( ) ;
215+
216+ expect ( result . stopReason ) . toBe ( "error" ) ;
217+ expect ( result . errorMessage ) . toBe ( "synthetic provider failure" ) ;
218+ expectDestroyedClient ( send , destroy ) ;
219+ } ) ;
220+
221+ it ( "destroys the client when response stream iteration fails" , async ( ) => {
222+ async function * failingStream ( ) {
223+ yield { messageStart : { role : ConversationRole . ASSISTANT } } ;
224+ throw new Error ( "synthetic iterator failure" ) ;
225+ }
226+ const send = vi . spyOn ( BedrockRuntimeClient . prototype , "send" ) . mockResolvedValue ( {
227+ $metadata : { httpStatusCode : 200 } ,
228+ stream : failingStream ( ) ,
229+ } as never ) ;
230+ const destroy = vi . spyOn ( BedrockRuntimeClient . prototype , "destroy" ) ;
231+
232+ const result = await streamBedrockForTest ( ) . result ( ) ;
233+
234+ expect ( result . stopReason ) . toBe ( "error" ) ;
235+ expect ( result . errorMessage ) . toBe ( "synthetic iterator failure" ) ;
236+ expectDestroyedClient ( send , destroy ) ;
237+ } ) ;
238+
239+ it ( "destroys the client after an aborted request" , async ( ) => {
240+ const controller = new AbortController ( ) ;
241+ controller . abort ( ) ;
242+ const send = vi
243+ . spyOn ( BedrockRuntimeClient . prototype , "send" )
244+ . mockRejectedValue ( new Error ( "synthetic abort" ) ) ;
245+ const destroy = vi . spyOn ( BedrockRuntimeClient . prototype , "destroy" ) ;
246+
247+ const result = await streamBedrockForTest ( { signal : controller . signal } ) . result ( ) ;
248+
249+ expect ( result . stopReason ) . toBe ( "aborted" ) ;
250+ expect ( result . errorMessage ) . toBe ( "synthetic abort" ) ;
251+ expectDestroyedClient ( send , destroy ) ;
252+ } ) ;
253+ } ) ;
0 commit comments