Skip to content

feat(sdk): chat.headStart handover for customAgent and createSession#3963

Open
ericallam wants to merge 2 commits into
mainfrom
feature/tri-11007-chatheadstart-handover-for-customagent-and-createsession
Open

feat(sdk): chat.headStart handover for customAgent and createSession#3963
ericallam wants to merge 2 commits into
mainfrom
feature/tri-11007-chatheadstart-handover-for-customagent-and-createsession

Conversation

@ericallam

Copy link
Copy Markdown
Member

Summary

chat.headStart (the warm step-1 fast path) previously handed its response over only to chat.agent. This extends handover to the other two backends: chat.customAgent consumes it with conversation.consumeHandover({ payload }) on turn 0, and chat.createSession surfaces it as turn.handover (call turn.complete() with no source to finalize a pure-text handover). The low-level chat.waitForHandover() and accumulator.applyHandover() are exported for hand-rolled loops.

It also adds triggerConfig to chat.headStart() and chat.openSession(), so the auto-triggered handover-prepare run inherits tags, queue, machine, and the other session run options the same way chat.createStartSessionAction() does. The chat:{chatId} tag is prepended automatically. Because the session is created once on the first head-start turn (idempotent on the chat id), this is the only place those options can be set for a head-start chat's lifetime.

Fix: tool-call resume

When the warm step-1 hands over a pending tool call (rather than pure text), the agent loop resumes that tool round. For it to merge cleanly the pipe threads the spliced partial as originalMessages, so the resumed tool-output chunk attaches to the handed-over tool-call instead of throwing No tool invocation found. MessageAccumulator.addResponse now also dedups by id (replace-in-place), so the persisted history doesn't carry a duplicate assistant message when the resumed response reuses the partial's id.

Incorporates the triggerConfig work from #3933 by @saasjesus, with createStartSessionAction extended to also forward maxDuration, region, and lockToVersion so the two session entry points stay consistent.

Verified end-to-end against a local environment: handover (pure-text and tool-call) on both new backends, a chat.agent regression pass, and triggerConfig tags and queue landing on the run.

chat.headStart previously handed the warm step-1 response over only to
chat.agent. Now chat.customAgent (via conversation.consumeHandover) and
chat.createSession (via turn.handover) consume it too, and a resumed tool
round merges into the handed-over assistant instead of throwing or
duplicating it. Also adds triggerConfig support to chat.headStart() and
chat.openSession() so the handover-prepare run inherits tags, queue, and
other session run options.

Co-authored-by: saasjesus <armin@chatarmin.com>
@changeset-bot

changeset-bot Bot commented Jun 16, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 32f0536

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 25 packages
Name Type
@trigger.dev/sdk Patch
@trigger.dev/python Patch
@internal/sdk-compat-tests Patch
@trigger.dev/build Patch
@trigger.dev/core Patch
@trigger.dev/plugins Patch
@trigger.dev/react-hooks Patch
@trigger.dev/redis-worker Patch
@trigger.dev/rsc Patch
@trigger.dev/schema-to-json Patch
@trigger.dev/database Patch
@trigger.dev/otlp-importer Patch
@trigger.dev/rbac Patch
trigger.dev Patch
@internal/cache Patch
@internal/clickhouse Patch
@internal/llm-model-catalog Patch
@internal/redis Patch
@internal/replication Patch
@internal/run-engine Patch
@internal/schedule-engine Patch
@internal/testcontainers Patch
@internal/tracing Patch
@internal/tsql Patch
@internal/zod-worker Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 93ec6fd5-6832-4510-a1ec-6e8fbd827bd0

📥 Commits

Reviewing files that changed from the base of the PR and between c4bc008 and 32f0536.

📒 Files selected for processing (1)
  • packages/trigger-sdk/src/v3/ai.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/trigger-sdk/src/v3/ai.ts
📜 Recent review details
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (32)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (3, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (5, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (4, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (1, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (11, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (2, 10)
  • GitHub Check: sdk-compat / Bun Runtime
  • GitHub Check: internal / 🧪 Unit Tests: Internal (10, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (9, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (8, 10)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (2, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (7, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (6, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (12, 12)
  • GitHub Check: internal / 🧪 Unit Tests: Internal (8, 12)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (4, 10)
  • GitHub Check: sdk-compat / Node.js 20.20 (ubuntu-latest)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (1, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (5, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (9, 10)
  • GitHub Check: sdk-compat / Deno Runtime
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (10, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (6, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (7, 10)
  • GitHub Check: webapp / 🧪 Unit Tests: Webapp (3, 10)
  • GitHub Check: sdk-compat / Cloudflare Workers
  • GitHub Check: sdk-compat / Node.js 22.12 (ubuntu-latest)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (1, 3)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (2, 3)
  • GitHub Check: packages / 🧪 Unit Tests: Packages (3, 3)
  • GitHub Check: typecheck / typecheck
  • GitHub Check: Build and publish previews

Walkthrough

This PR extends chat.headStart to work with chat.customAgent and chat.createSession backends, not only chat.agent. A new exported HandoverSignal type and internal spliceHandoverPartial() helper provide shared deduplication logic. chat.waitForHandover() is added as a public helper, and ChatMessageAccumulator gains applyHandover() and consumeHandover() methods; addResponse() now deduplicates by message id in-place. ChatTurn gains a handover: { isFinal: boolean } | null field and complete() becomes optional-argument. createChatSession detects turn-0 handover-prepare payloads and applies the spliced partial. Both chat.headStart/chat.openSession and chat.createStartSessionAction now accept and propagate triggerConfig fields including tags (capped at 5), queue, machine, maxDuration, region, and lockToVersion.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 63.64% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Description check ❓ Inconclusive The PR description provides comprehensive context covering feature objectives, implementation details, and testing validation, but does not follow the repository's required checklist template structure. Update the description to include the required checklist items from the template (contributing guide, PR title convention, testing confirmation) and organize sections as specified.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately captures the main feature addition: extending chat.headStart handover support to customAgent and createSession backends.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feature/tri-11007-chatheadstart-handover-for-customagent-and-createsession

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot]

This comment was marked as resolved.

@pkg-pr-new

pkg-pr-new Bot commented Jun 16, 2026

Copy link
Copy Markdown

Open in StackBlitz

@trigger.dev/build

npm i https://pkg.pr.new/@trigger.dev/build@32f0536

trigger.dev

npm i https://pkg.pr.new/trigger.dev@32f0536

@trigger.dev/core

npm i https://pkg.pr.new/@trigger.dev/core@32f0536

@trigger.dev/python

npm i https://pkg.pr.new/@trigger.dev/python@32f0536

@trigger.dev/react-hooks

npm i https://pkg.pr.new/@trigger.dev/react-hooks@32f0536

@trigger.dev/redis-worker

npm i https://pkg.pr.new/@trigger.dev/redis-worker@32f0536

@trigger.dev/rsc

npm i https://pkg.pr.new/@trigger.dev/rsc@32f0536

@trigger.dev/schema-to-json

npm i https://pkg.pr.new/@trigger.dev/schema-to-json@32f0536

@trigger.dev/sdk

npm i https://pkg.pr.new/@trigger.dev/sdk@32f0536

commit: 32f0536

@ericallam ericallam marked this pull request as ready for review June 16, 2026 12:41

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

Open in Devin Review

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚩 createChatSession iterator lacks a return() method — stop signal subscription can leak

The async iterator returned by createChatSession defines only next() with no return() method. When the user breaks out of the for await loop (e.g. via return after turn.complete() on a final handover), the stop.cleanup() call is never reached — the stop-input subscription stays active for the remainder of the run. The no-source complete() path at line 9494 calls sessionMsgSub.off() but not stop.cleanup(). This is pre-existing behavior (not introduced by this PR) and likely benign since the subscription is GC'd when the run ends, but worth noting for future hardening.

(Refers to line 9263)

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant