feat(chat): expose endAndContinue to custom agents - #4647
Conversation
🦋 Changeset detectedLatest commit: 31810b7 The changes in this PR will be included in the next version bump. This PR includes changesets to release 29 packages
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 |
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
🚧 Files skipped from review as they are similar to previous changes (4)
Included review availability: Your plan includes up to 10 reviews per rolling hour; 6 remain after this review. 📜 Recent review details🧰 Additional context used📓 Path-based instructions (11)packages/trigger-sdk/**/*.{ts,tsx}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
**/*.{ts,tsx}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
**/*.{ts,tsx,js,jsx}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
**/*.{test,spec}.{ts,tsx}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
**/*.ts📄 CodeRabbit inference engine (.cursor/rules/otel-metrics.mdc)
Files:
packages/trigger-sdk/**/*.{js,ts,jsx,tsx}📄 CodeRabbit inference engine (packages/trigger-sdk/CLAUDE.md)
Files:
packages/**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
**/*.test.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
{packages/core,apps/webapp}/**/*.{ts,tsx}📄 CodeRabbit inference engine (.github/copilot-instructions.md)
Files:
apps/webapp/**/*.{ts,tsx}📄 CodeRabbit inference engine (.cursor/rules/webapp.mdc)
Files:
apps/**/*.{ts,tsx}📄 CodeRabbit inference engine (AGENTS.md)
Files:
🧠 Learnings (22)📓 Common learnings📚 Learning: 2026-03-22T13:26:12.060ZApplied to files:
📚 Learning: 2026-03-22T19:24:14.403ZApplied to files:
📚 Learning: 2026-05-18T08:21:27.694ZApplied to files:
📚 Learning: 2026-05-18T08:21:27.694ZApplied to files:
📚 Learning: 2026-06-13T19:53:13.759ZApplied to files:
📚 Learning: 2026-06-17T17:13:49.929ZApplied to files:
📚 Learning: 2026-06-23T13:04:21.413ZApplied to files:
📚 Learning: 2026-05-18T14:40:02.173ZApplied to files:
📚 Learning: 2026-05-18T14:40:02.173ZApplied to files:
📚 Learning: 2026-05-19T22:37:47.286ZApplied to files:
📚 Learning: 2026-06-04T18:16:35.386ZApplied to files:
📚 Learning: 2026-06-09T17:58:04.699ZApplied to files:
📚 Learning: 2026-06-16T09:19:47.637ZApplied to files:
📚 Learning: 2026-08-16T18:36:58.179ZApplied to files:
📚 Learning: 2026-05-07T12:25:18.271ZApplied to files:
📚 Learning: 2026-05-28T20:02:10.647ZApplied to files:
📚 Learning: 2026-07-30T18:43:56.874ZApplied to files:
📚 Learning: 2026-08-08T12:49:17.489ZApplied to files:
📚 Learning: 2026-05-12T21:04:05.815ZApplied to files:
📚 Learning: 2026-06-25T18:21:51.905ZApplied to files:
📚 Learning: 2026-07-03T17:10:21.498ZApplied to files:
🔇 Additional comments (2)
WalkthroughAdded 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
|
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. |
| 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; |
There was a problem hiding this comment.
📝 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.
Was this helpful? React with 👍 or 👎 to provide feedback.
Summary
Raw
chat.customAgent()loops can now callchat.endAndContinue()to move the Session to a fresh run. The managed loop already used the same server operation throughchat.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
.inrecords stay on the Session for the continuation run.I put this on the
chatnamespace next to the other raw chat primitives. Happy to move it if maintainers prefer a different API placement.Testing
pnpm exec vitest runinpackages/trigger-sdk(374 tests)pnpm run buildinpackages/trigger-sdkpnpm run formatpnpm run lintChecklist
Changelog
Allow custom chat agents to rotate to a new task version without dropping unconsumed Session input.