@@ -59,6 +59,7 @@ import {
5959 SessionStreamInstance ,
6060 TRIGGER_CONTROL_SUBTYPE ,
6161 apiClientManager ,
62+ type SessionTriggerConfig ,
6263} from "@trigger.dev/core/v3" ;
6364// Runtime VALUES via the ESM/CJS shim so the CJS build can `require` ESM-only
6465// `ai@7` (see ../imports/ai-runtime.ts).
@@ -195,6 +196,12 @@ export type HeadStartHandlerOptions<TTools extends Record<string, Tool>> = {
195196 * exiting. Defaults to 60.
196197 */
197198 idleTimeoutInSeconds ?: number ;
199+ /**
200+ * Run options for the auto-triggered `handover-prepare` session run —
201+ * tags, queue, machine, etc. Mirrors `chat.createStartSessionAction`.
202+ * The `chat:{chatId}` tag is prepended automatically.
203+ */
204+ triggerConfig ?: Partial < SessionTriggerConfig > ;
198205} ;
199206
200207// ---------------------------------------------------------------------------
@@ -220,6 +227,7 @@ export const chat = {
220227 req,
221228 agentId : opts . agentId ,
222229 idleTimeoutInSeconds : opts . idleTimeoutInSeconds ,
230+ triggerConfig : opts . triggerConfig ,
223231 } ) ;
224232
225233 const helper : HeadStartChatHelper < TTools > = {
@@ -249,6 +257,7 @@ export const chat = {
249257 req : Request ;
250258 agentId : string ;
251259 idleTimeoutInSeconds ?: number ;
260+ triggerConfig ?: Partial < SessionTriggerConfig > ;
252261 } ) : Promise < HeadStartSession > {
253262 return openHandoverSession ( opts ) . then ( ( s ) => s . handle ) ;
254263 } ,
@@ -304,6 +313,7 @@ async function openHandoverSession(opts: {
304313 req : Request ;
305314 agentId : string ;
306315 idleTimeoutInSeconds ?: number ;
316+ triggerConfig ?: Partial < SessionTriggerConfig > ;
307317} ) : Promise < InternalSession > {
308318 const wirePayload = ( await opts . req . json ( ) ) as ChatTaskWirePayload ;
309319 const chatId = wirePayload . chatId ;
@@ -323,7 +333,35 @@ async function openHandoverSession(opts: {
323333 const modelMessages = await convertToModelMessages ( uiMessages ) ;
324334
325335 const apiClient = resolveApiClient ( ) ;
326- const idleTimeoutInSeconds = opts . idleTimeoutInSeconds ?? 60 ;
336+ const idleTimeoutInSeconds =
337+ opts . idleTimeoutInSeconds ?? opts . triggerConfig ?. idleTimeoutInSeconds ?? 60 ;
338+
339+ const userTags = opts . triggerConfig ?. tags ?? [ ] ;
340+ const tags = [ `chat:${ chatId } ` , ...userTags ] . slice ( 0 , 5 ) ;
341+
342+ const triggerConfig : SessionTriggerConfig = {
343+ basePayload : {
344+ ...( opts . triggerConfig ?. basePayload ?? { } ) ,
345+ ...wirePayload ,
346+ chatId,
347+ trigger : "handover-prepare" ,
348+ idleTimeoutInSeconds,
349+ } ,
350+ ...( opts . triggerConfig ?. machine ? { machine : opts . triggerConfig . machine } : { } ) ,
351+ ...( opts . triggerConfig ?. queue ? { queue : opts . triggerConfig . queue } : { } ) ,
352+ tags,
353+ ...( opts . triggerConfig ?. maxAttempts !== undefined
354+ ? { maxAttempts : opts . triggerConfig . maxAttempts }
355+ : { } ) ,
356+ ...( opts . triggerConfig ?. maxDuration !== undefined
357+ ? { maxDuration : opts . triggerConfig . maxDuration }
358+ : { } ) ,
359+ ...( opts . triggerConfig ?. region ? { region : opts . triggerConfig . region } : { } ) ,
360+ ...( opts . triggerConfig ?. lockToVersion
361+ ? { lockToVersion : opts . triggerConfig . lockToVersion }
362+ : { } ) ,
363+ idleTimeoutInSeconds,
364+ } ;
327365
328366 // Create the session and trigger the chat.agent's `handover-prepare`
329367 // run atomically. `createSession` is idempotent on `(env, externalId
@@ -342,15 +380,7 @@ async function openHandoverSession(opts: {
342380 type : "chat.agent" ,
343381 externalId : chatId ,
344382 taskIdentifier : opts . agentId ,
345- triggerConfig : {
346- basePayload : {
347- ...wirePayload ,
348- chatId,
349- trigger : "handover-prepare" ,
350- idleTimeoutInSeconds,
351- } ,
352- idleTimeoutInSeconds,
353- } ,
383+ triggerConfig,
354384 } ) ;
355385 const sessionPublicAccessToken = created . publicAccessToken ;
356386
0 commit comments