@@ -3,6 +3,7 @@ import { AddressInfo } from "node:net";
33import { afterEach , describe , expect , it } from "vitest" ;
44import {
55 InMemoryTriggerChatRunStore ,
6+ createTriggerChatTransport ,
67 TriggerChatTransport ,
78} from "./chatTransport.js" ;
89import type { UIMessage , UIMessageChunk } from "ai" ;
@@ -279,6 +280,64 @@ describe("TriggerChatTransport", function () {
279280 expect ( ( options . idempotencyKey as string ) . length ) . toBe ( 64 ) ;
280281 } ) ;
281282
283+ it ( "supports creating transport with factory function" , async function ( ) {
284+ let observedRunId : string | undefined ;
285+
286+ const server = await startServer ( function ( req , res ) {
287+ if ( req . method === "POST" && req . url === "/api/v1/tasks/chat-task/trigger" ) {
288+ res . writeHead ( 200 , {
289+ "content-type" : "application/json" ,
290+ "x-trigger-jwt" : "pk_run_factory" ,
291+ } ) ;
292+ res . end ( JSON . stringify ( { id : "run_factory" } ) ) ;
293+ return ;
294+ }
295+
296+ if ( req . method === "GET" && req . url === "/realtime/v1/streams/run_factory/chat-stream" ) {
297+ res . writeHead ( 200 , {
298+ "content-type" : "text/event-stream" ,
299+ } ) ;
300+ writeSSE (
301+ res ,
302+ "1-0" ,
303+ JSON . stringify ( { type : "text-start" , id : "factory_1" } )
304+ ) ;
305+ writeSSE (
306+ res ,
307+ "2-0" ,
308+ JSON . stringify ( { type : "text-end" , id : "factory_1" } )
309+ ) ;
310+ res . end ( ) ;
311+ return ;
312+ }
313+
314+ res . writeHead ( 404 ) ;
315+ res . end ( ) ;
316+ } ) ;
317+
318+ const transport = createTriggerChatTransport ( {
319+ task : "chat-task" ,
320+ stream : "chat-stream" ,
321+ accessToken : "pk_trigger" ,
322+ baseURL : server . url ,
323+ onTriggeredRun : function onTriggeredRun ( state ) {
324+ observedRunId = state . runId ;
325+ } ,
326+ } ) ;
327+
328+ const stream = await transport . sendMessages ( {
329+ trigger : "submit-message" ,
330+ chatId : "chat-factory" ,
331+ messageId : undefined ,
332+ messages : [ ] ,
333+ abortSignal : undefined ,
334+ } ) ;
335+
336+ const chunks = await readChunks ( stream ) ;
337+ expect ( chunks ) . toHaveLength ( 2 ) ;
338+ expect ( observedRunId ) . toBe ( "run_factory" ) ;
339+ } ) ;
340+
282341 it ( "reconnects active streams using tracked lastEventId" , async function ( ) {
283342 let reconnectLastEventId : string | undefined ;
284343 let firstStreamResponse : ServerResponse < IncomingMessage > | undefined ;
0 commit comments