Skip to content

Commit 09f8e7a

Browse files
committed
fix(sdk): thread the boot payload chatId into subtask metadata explicitly
Custom-loop subtask tool metadata read the chatId off the Session handle id. That value already equals the external chatId (the handle is opened on payload.chatId), but reading it from a dedicated locals slot set at run boot matches the documented ToolCallExecutionOptions contract directly and decouples from the handle id semantics.
1 parent 174ba12 commit 09f8e7a

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

  • packages/trigger-sdk/src/v3

packages/trigger-sdk/src/v3/ai.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ const chatTurnContextKey = locals.create<ChatTurnContext>("chat.turnContext");
160160
* @internal
161161
*/
162162
const chatSessionHandleKey = locals.create<SessionHandle>("chat.sessionHandle");
163+
// The external `chatId` from the boot payload — the value `ToolCallExecutionOptions.chatId`
164+
// is documented to carry. Custom-agent loops never set per-turn context, so subtask tool
165+
// metadata reads this directly rather than the Session handle id.
166+
const chatExternalIdKey = locals.create<string>("chat.externalId");
163167

164168
/**
165169
* S2 seq_num of the most recent `turn-complete` control record written by
@@ -964,12 +968,12 @@ function createTaskToolExecuteHandler<
964968
toolMeta.clientData = chatCtx.clientData;
965969
} else {
966970
// Hand-rolled chat.customAgent loops never set per-turn context, but
967-
// the wrapper binds the session handle at run boot — thread the
968-
// chatId from it so subtask chat helpers (`chat.stream.writer`
969-
// with target "root") can open the parent's session.
970-
const sessionHandle = locals.get(chatSessionHandleKey);
971-
if (sessionHandle) {
972-
toolMeta.chatId = sessionHandle.id;
971+
// the wrapper records the boot payload's external chatId at run boot
972+
// — thread it so subtask chat helpers (`chat.stream.writer` with
973+
// target "root") can open the parent's session.
974+
const chatExternalId = locals.get(chatExternalIdKey);
975+
if (chatExternalId) {
976+
toolMeta.chatId = chatExternalId;
973977
}
974978
}
975979

@@ -5154,6 +5158,7 @@ function chatCustomAgent<
51545158
// `chat.createStartSessionAction`) before this run is triggered.
51555159
// No client-side upsert needed.
51565160
locals.set(chatSessionHandleKey, sessions.open(payload.chatId));
5161+
locals.set(chatExternalIdKey, payload.chatId);
51575162
locals.set(chatAgentRunContextKey, runOptions.ctx);
51585163
// Initialize the turn-complete trim slot so `chat.writeTurnComplete`
51595164
// trims `session.out` back to the previous turn boundary. Without
@@ -5267,6 +5272,7 @@ function chatAgent<
52675272
// `chat.createStartSessionAction` or browser-direct) before this
52685273
// run is triggered — no client-side upsert needed here.
52695274
locals.set(chatSessionHandleKey, sessions.open(payload.chatId));
5275+
locals.set(chatExternalIdKey, payload.chatId);
52705276
// Mutable holder; advances in `writeTurnCompleteChunk` after each turn
52715277
// and is the trim target for the NEXT turn's trim record.
52725278
locals.set(lastTurnCompleteSeqNumKey, { value: undefined });

0 commit comments

Comments
 (0)