Skip to content

feat(chat): expose endAndContinue to custom agents - #4647

Open
gtremper wants to merge 3 commits into
triggerdotdev:mainfrom
gtremper:graham/chat-custom-agent-continue
Open

feat(chat): expose endAndContinue to custom agents#4647
gtremper wants to merge 3 commits into
triggerdotdev:mainfrom
gtremper:graham/chat-custom-agent-continue

Conversation

@gtremper

Copy link
Copy Markdown

Summary

Raw chat.customAgent() loops can now call chat.endAndContinue() to move the Session to a fresh run. The managed loop already used the same server operation through chat.requestUpgrade(), but raw loops could not call it directly.

Call the method between turns after detaching input listeners from the old run. Await it and return immediately. Unconsumed .in records stay on the Session for the continuation run.

I put this on the chat namespace next to the other raw chat primitives. Happy to move it if maintainers prefer a different API placement.

Testing

  • pnpm exec vitest run in packages/trigger-sdk (376 tests)
  • pnpm run build in packages/trigger-sdk
  • pnpm run format
  • pnpm run lint

Checklist

  • I followed the contributing guide
  • The PR title follows the convention
  • I tested the change

Changelog

Allow custom chat agents to rotate to a new task version without dropping unconsumed Session input.

@changeset-bot

changeset-bot Bot commented Aug 17, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 03b84d1

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

This PR includes changesets to release 29 packages
Name Type
@trigger.dev/sdk Patch
@trigger.dev/python Patch
@internal/dashboard-agent Patch
@internal/sdk-compat-tests Patch
@trigger.dev/build Patch
@trigger.dev/core 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/sso Patch
trigger.dev Patch
@internal/cache Patch
@internal/clickhouse Patch
@internal/llm-model-catalog Patch
@internal/metrics-pipeline Patch
@internal/redis Patch
@internal/replication Patch
@internal/run-engine Patch
@internal/run-store Patch
@internal/schedule-engine Patch
@internal/tracing Patch
@internal/tsql Patch
@internal/webhook-engine Patch
@internal/webhook-sources Patch
@internal/testcontainers 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 Aug 17, 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 Plus

Run ID: b5e59df1-7a38-4dc9-9851-180d2b16973e

📥 Commits

Reviewing files that changed from the base of the PR and between da59647 and 03b84d1.

📒 Files selected for processing (4)
  • .changeset/chat-custom-agent-end-and-continue.md
  • docs/ai-chat/custom-agents.mdx
  • docs/ai-chat/patterns/version-upgrades.mdx
  • packages/trigger-sdk/src/v3/ai.ts
🚧 Files skipped from review as they are similar to previous changes (3)
  • .changeset/chat-custom-agent-end-and-continue.md
  • docs/ai-chat/custom-agents.mdx
  • packages/trigger-sdk/src/v3/ai.ts

Included review availability: Your plan includes up to 10 reviews per rolling hour; 9 remain after this review.

📜 Recent review details
🧰 Additional context used
📓 Path-based instructions (1)
docs/**/*.mdx

📄 CodeRabbit inference engine (docs/CLAUDE.md)

docs/**/*.mdx: MDX documentation pages must include frontmatter with title (required), description (required), and sidebarTitle (optional) in YAML format
Use Mintlify components for structured content: , , , , , , /, /
Always import from @trigger.dev/sdk in code examples (never from @trigger.dev/sdk/v3)
Code examples must be complete and runnable where possible
Use language tags in code fences: typescript, bash, json

Files:

  • docs/ai-chat/patterns/version-upgrades.mdx
🧠 Learnings (4)
📚 Learning: 2026-03-10T12:44:14.176Z
Learnt from: nicktrn
Repo: triggerdotdev/trigger.dev PR: 3200
File: docs/config/config-file.mdx:353-368
Timestamp: 2026-03-10T12:44:14.176Z
Learning: In the trigger.dev repo, docs PRs are often companions to implementation PRs. When reviewing docs PRs (MDX files under docs/), check the PR description for any companion/related PR references and verify that the documented features exist in those companion PRs before flagging missing implementations. This ensures docs stay in sync with code changes across related PRs.

Applied to files:

  • docs/ai-chat/patterns/version-upgrades.mdx
📚 Learning: 2026-04-30T20:30:29.458Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3226
File: docs/ai-chat/quick-start.mdx:13-13
Timestamp: 2026-04-30T20:30:29.458Z
Learning: In this repo’s documentation MDX files (`docs/**/*.mdx`), use `ts` and `tsx` (not `typescript`) as the code-fence language tags for TypeScript/TSX snippets. Do not flag `ts`/`tsx` code-fence language tags as incorrect in any docs MDX file, since this is the site-wide Mintlify-compatible convention.

Applied to files:

  • docs/ai-chat/patterns/version-upgrades.mdx
📚 Learning: 2026-06-16T13:14:09.440Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3964
File: docs/ai-chat/reference.mdx:482-482
Timestamp: 2026-06-16T13:14:09.440Z
Learning: When documenting or reviewing usage of `ChatTurn.complete(source?)` (in `packages/trigger-sdk/src/v3/ai.ts`), note that `source` is optional (`source?: UIMessageStreamable`). Calling `complete()` with no `source` is valid specifically for a final head-start handover (`handover.isFinal`), because the warm partial already contains the response. If examples or guidance omit `source`, ensure they are in this final-hand-over context so they remain correct.

Applied to files:

  • docs/ai-chat/patterns/version-upgrades.mdx
📚 Learning: 2026-06-16T13:14:14.382Z
Learnt from: ericallam
Repo: triggerdotdev/trigger.dev PR: 3964
File: docs/ai-chat/reference.mdx:478-478
Timestamp: 2026-06-16T13:14:14.382Z
Learning: When reviewing RC-gated `ai-chat` docs under `docs/ai-chat/`, don’t immediately flag missing SDK type fields or implementation details just because the field isn’t present on the docs branch yet. Instead, find and cross-check the companion implementation PR that’s intended to land alongside the docs PR, and only report missing/incorrect fields if they are also absent in the companion SDK/type changes.

Applied to files:

  • docs/ai-chat/patterns/version-upgrades.mdx
🔇 Additional comments (1)
docs/ai-chat/patterns/version-upgrades.mdx (1)

159-164: LGTM!

Also applies to: 169-179


Walkthrough

Added chat.endAndContinue() for custom-agent runs. The SDK tracks custom-agent context, starts continuation runs through the Sessions API, and preserves session input. Upgrade handling now uses shared handoff logic. Tests cover successful handoffs, buffered input, server failures, and invalid call contexts. Documentation describes managed and custom-agent upgrade flows, listener cleanup, turn boundaries, version selection, recovery behavior, and immediate return requirements. Added a patch changeset for @trigger.dev/sdk.

Merge Risk: 🟡 Moderate · up to 03b84

The new handoff API can race continuation runs in some managed chat loops, while certain fallback paths may proceed without clear diagnostics. This creates bounded session-continuation correctness and troubleshooting risk that should be addressed or explicitly accepted before merging.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description includes a summary, testing steps, checklist, and changelog; only the issue reference and screenshots section are missing.
Title check ✅ Passed The title clearly and concisely describes exposing endAndContinue for custom chat agents.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@github-actions

Copy link
Copy Markdown
Contributor

Hi @gtremper, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details.

@github-actions github-actions Bot closed this Aug 17, 2026
@matt-aitken matt-aitken reopened this Aug 17, 2026
@matt-aitken
matt-aitken marked this pull request as ready for review August 17, 2026 07:27

@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

Comment on lines 10952 to +10954
async function writeUpgradeRequiredChunk(): Promise<StreamWriteResult> {
const ctx = taskContext.ctx;
const chatId = ctx?.run.id ? getChatIdFromContext() : undefined;
const callingRunId = ctx?.run.id;
const chatId = locals.get(chatExternalIdKey);
const callingRunId = locals.get(chatAgentRunContextKey)?.run.id;

@devin-ai-integration devin-ai-integration Bot Aug 17, 2026

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.

📝 Info: chatId/callingRunId resolution swap is behaviour-preserving

writeUpgradeRequiredChunk now resolves chatId from chatExternalIdKey instead of the session handle's id (the deleted getChatIdFromContext). Both are equivalent at every call site: chatSessionHandleKey is set as sessions.open(payload.chatId) in packages/trigger-sdk/src/v3/ai.ts:5364 and :5480, and SessionHandle.id is exactly the constructor argument (packages/trigger-sdk/src/v3/sessions.ts:248-254), i.e. payload.chatId — the same value stored in chatExternalIdKey. Similarly, chatAgentRunContextKey.run.id is runOptions.ctx.run.id, matching the previous taskContext.ctx?.run.id. The one contextual difference (the subtask/tool fallback in getChatSession() sets chatSessionHandleKey but not chatExternalIdKey) is not reachable from either writeUpgradeRequiredChunk call site (:7088, :9825), both of which live inside the agent loops that seed both keys.

Open in Devin Review

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

coderabbitai[bot]

This comment was marked as resolved.

@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 new potential issue.

Open in Devin Review

Comment on lines +73 to +76
vi.spyOn(apiClientManager, "clientOrThrow").mockReturnValue({
readSessionStreamRecords,
endAndContinueSession,
} as never);

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.

🟡 New test suite relies on mocks even though the repo forbids mocking in tests

The new test file replaces the real API client and stream plumbing with fake implementations (vi.spyOn(apiClientManager, "clientOrThrow").mockReturnValue(...) at packages/trigger-sdk/test/chat-end-and-continue.test.ts:73-76), which the repository's testing rules explicitly forbid.
Impact: The handoff is only verified against a stand-in, so a real change in server behaviour would not be caught.

Rule source and affected spots

AGENTS.md (Testing section) states: "We use vitest exclusively. Never mock anything - use testcontainers instead." The new suite mocks the API client in three places: packages/trigger-sdk/test/chat-end-and-continue.test.ts:73-76, :153-156, :178-180, and additionally stubs the session stream manager (DurableTestSessionStreamManager, :27-37).

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.

2 participants