Commit Graph

1848 Commits

Author SHA1 Message Date
Eric Allam 254bf42fe9 Add support for triggering from the backend 2026-05-10 22:10:16 +01:00
Eric Allam 817367aa73 feat(sdk): add onChatSuspend/onChatResume hooks, exitAfterPreloadIdle, chat.withClientData, and ChatBuilder 2026-05-10 22:10:11 +01:00
Eric Allam dde3934670 feat(sdk): ctx on chat.task hooks; ai-chat E2B sandbox; docs patterns
- Pass TaskRunContext through chat lifecycle events, CompactedEvent, and
  ChatTaskRunPayload; use ctx.run.id for chat access tokens
- Export TaskRunContext from @trigger.dev/sdk
- ai-chat reference: executeCode via E2B, code-sandbox module, warm on
  onTurnStart, dispose on token onWait and onComplete; chat.local run id
- Docs: database persistence + code sandbox pattern pages; reference and
  backend updates for ctx; chat.defer anchor; navigation
2026-05-10 22:10:11 +01:00
Eric Allam 5a8ba63726 Add run-scoped PAT renewal for chat transport 2026-05-10 22:09:48 +01:00
Eric Allam be820d084a feat(sdk): ToolSet typing for toolFromTask, ai.toolExecute, deprecate ai.tool
- Assert toolFromTask return as AI SDK ToolSet-compatible; import ToolSet from ai
- Add changesets for @trigger.dev/sdk
- ai-chat reference: chat-tools module, registry language model helper, streamText cleanup
- Prisma migration removing user tool demo; demo docs and next config tweaks
2026-05-10 22:09:48 +01:00
Eric Allam 71abce7f93 feat(chat): add chat.inject() for background context injection and chat.defer improvements
- chat.inject(): queue model messages from background work for injection
  at the next prepareStep boundary or before the next turn's run()
- Deferred work from onTurnComplete no longer blocks waiting for next message
- Background queue persists across turns (not reset) so deferred work from
  onTurnComplete can inject into the next turn
- Reference app: self-review pattern using generateObject + chat.inject()
- Hide transient data-turn-status and data-background-context-injected parts in UI
2026-05-10 22:09:48 +01:00
Eric Allam 9d334e8ace feat(ai): add triggerAndSubscribe method and use it in ai.tool
Replace triggerAndWait with triggerAndSubscribe in ai.tool to fix:
- Parallel tool calls (no more preventMultipleWaits errors)
- Stop signal while suspended (parent stays alive, child gets cancelled)

New task.triggerAndSubscribe() method: trigger + subscribeToRun in a
single span, with abort signal support and cancelOnAbort option.

Convert deepResearch to a schemaTask + ai.tool in the reference app.

refs TRI-7986
2026-05-10 22:09:48 +01:00
Eric Allam 49e858ef56 Add a writer to easily write chunks in callbacks 2026-05-10 22:09:48 +01:00
Eric Allam f05d638717 feat(chat): add compaction option, pendingMessages steering, and usePendingMessages hook 2026-05-10 22:09:48 +01:00
Eric Allam f7442f1312 better compaction support in our other chat variants 2026-05-10 22:09:48 +01:00
Eric Allam c21e920a3b feat: support message compaction 2026-05-10 22:09:48 +01:00
Eric Allam aeb0fbb6a2 refactor: rename warmTimeout to idleTimeout across chat APIs 2026-05-10 22:09:48 +01:00
Eric Allam 856d2527d0 feat(chat): add chat.prompt API with provider registry support 2026-05-10 22:09:48 +01:00
Eric Allam 78900720af Add support for toUIMessageStream() options 2026-05-10 22:08:38 +01:00
Eric Allam 1f6ea4b2d9 Introduce the chat session API and better docs organization 2026-05-10 22:08:38 +01:00
Eric Allam 25f7dedd14 feat(chat): add composable primitives, raw task example, and task mode switcher 2026-05-10 22:08:38 +01:00
Eric Allam 2908b3598d feat(streams): add inputStream.waitWithWarmup(), warm timeout config in sidebar, preload payload option 2026-05-10 22:08:38 +01:00
Eric Allam dc84597ff5 feat(chat): add chat.defer(), preload toggle, TTFB measurement, and fix ChatTaskWirePayload export 2026-05-10 22:08:35 +01:00
Eric Allam 3d0b02752e feat(chat): auto-hydrate chat.local values in ai.tool subtasks 2026-05-10 22:08:35 +01:00
Eric Allam b2dc691951 feat(chat): add preload support, dynamic tools, and preload-specific timeouts
- Add onPreload hook and preloaded field to all lifecycle events
- Add transport.preload(chatId) for eagerly starting runs before first message
- Add preloadWarmTimeoutInSeconds and preloadTimeout task options
- Add preload:true run tag and chat.preloaded span attributes
- Add UserTool model for per-user dynamic tools loaded from DB
- Load dynamic tools in onPreload/onChatStart via chat.local
- Build dynamicTool() instances in run and spread into streamText tools
- Reference project: preload on new chat, dynamic company-info and user-preferences tools
2026-05-10 22:08:35 +01:00
Eric Allam 1cc19f8ca8 feat(ai): pass chat context and toolCallId to subtasks, add typed ai.chatContext helpers
- Store chat turn context (chatId, turn, continuation, clientData) in locals for auto-detection
- toolFromTask now auto-detects chat context and passes it to subtask metadata
- Skip serializing messages array (can be large, rarely needed by subtasks)
- Tag subtask runs with toolCallId for dashboard visibility
- Add ai.toolCallId() convenience helper
- Add ai.chatContext<typeof myChat>() with typed clientData inference
- Add ai.chatContextOrThrow<typeof myChat>() that throws if not in a chat context
- Update deepResearch example to use ai.chatContextOrThrow
- Document all helpers in ai-chat guide
2026-05-10 22:08:35 +01:00
Eric Allam acb1f9820e feat(chat): expose typed chat.stream, add deepResearch subtask example, per-chat model persistence, debug panel
- Export chat.stream (typed RealtimeDefinedStream<UIMessageChunk>) for writing custom data to the chat stream
- Add deepResearch subtask using data-* chunks to stream progress back to parent chat via target: root
- Use AI SDK data-research-progress chunk protocol with id-based updates for live progress
- Add ResearchProgress component and generic data-* fallback renderer in frontend
- Persist model per chat in DB (schema + onChatStart), model selector only on new chats
- Add collapsible debug panel showing run ID (with dashboard link), chat ID, model, status, session info
- Document chat.stream API, data-* chunks, and subtask streaming pattern in docs
2026-05-10 22:08:35 +01:00
Eric Allam 5c1aa24a04 feat(chat): add stop handling, abort cleanup, continuation support, and reference project enhancements
- Fix onFinish race condition: await onFinishPromise so capturedResponseMessage is set before accumulation
- Add chat.isStopped() helper accessible from anywhere during a turn
- Add chat.cleanupAbortedParts() to remove incomplete tool/reasoning/text parts on stop
- Auto-cleanup aborted parts before passing to onTurnComplete
- Clean incoming messages from frontend to prevent tool_use without tool_result API errors
- Add stopped and rawResponseMessage fields to TurnCompleteEvent
- Add continuation and previousRunId fields to all lifecycle hooks and run payload
- Add span attributes (chat.id, chat.turn, chat.stopped, chat.continuation, chat.previous_run_id, etc.)
- Add webFetch tool and reasoning model support to ai-chat reference project
- Render reasoning parts in frontend chat component
- Document all new fields in ai-chat guide
2026-05-10 22:08:35 +01:00
Eric Allam fc9768a66f feat: add chat.local for per-run typed data with Proxy access and dirty tracking 2026-05-10 22:08:35 +01:00
Eric Allam 28afa9829e Make clientData typesafe and pass to all chat.task hooks 2026-05-10 22:08:35 +01:00
Eric Allam 800725fb46 Add triggerOptions to the transport, auto-tag with the chat ID 2026-05-10 22:08:35 +01:00
Eric Allam 2b00606c31 use locals for the chat pipe counter instead of a module global 2026-05-10 22:08:35 +01:00
Eric Allam 0df407edc4 fixed chat tests 2026-05-10 22:08:35 +01:00
Eric Allam 1151b20530 Minor fixes around reconnecting streams 2026-05-10 22:08:35 +01:00
Eric Allam 21050cc7e3 feat: add onTurnStart hook, lastEventId support, and stream resume deduplication 2026-05-10 22:08:35 +01:00
Eric Allam bfa3341d6b build full example with persisting messages, adding necessary hooks, and documenting it all 2026-05-10 22:08:35 +01:00
Eric Allam 8640cf0d15 Accumulate messages in the task, allowing us to only have to send user mesages from the transport 2026-05-10 22:08:30 +01:00
Eric Allam 58e7ef4447 Fix double looping when resuming from an input stream waitpoint 2026-05-10 22:08:30 +01:00
Eric Allam 6c3c1937cc Added better telemetry support to view turns 2026-05-10 22:08:30 +01:00
Eric Allam 09b7f4d085 provide already converted UIMessages to the run function for better dx 2026-05-10 22:08:30 +01:00
Eric Allam 82ef8e2979 Add clientData support 2026-05-10 22:08:30 +01:00
Eric Allam 03a2434922 Add warmTimeoutInSeconds option 2026-05-10 22:08:13 +01:00
Eric Allam e6bf1e9c10 add stopping support and fix issue with the OpenAI responses API and stopped streams 2026-05-10 22:08:13 +01:00
Eric Allam 89e311302a use input streams and rename chatTask and chatState to chat.task and chat.state 2026-05-10 22:08:13 +01:00
Eric Allam 9f22201092 expose a useTriggerChatTransport hook 2026-05-10 22:08:13 +01:00
Eric Allam 19539e7b32 Added tool example 2026-05-10 22:08:13 +01:00
Eric Allam 15f48ff05a Use a single run with iterative waitpoint token completions 2026-05-10 22:07:39 +01:00
Cursor Agent 0b9bb989bf fix: address CodeRabbit review feedback
1. Add null/object guard before enqueuing UIMessageChunk from SSE stream
   to handle heartbeat or malformed events safely
2. Use incrementing counter instead of Date.now() in test message
   factories to avoid duplicate IDs
3. Add test covering publicAccessToken from trigger response being used
   for stream subscription auth

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00
Cursor Agent 03b26fb8d9 refactor: delete packages/ai/ — moved to @trigger.dev/sdk subpaths
All functionality now lives in:
- @trigger.dev/sdk/chat (frontend transport)
- @trigger.dev/sdk/ai (backend chatTask, pipeChat)

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00
Cursor Agent ab30c080f5 test: move chat transport tests to @trigger.dev/sdk
Move and adapt tests from packages/ai to packages/trigger-sdk.
- Import from ./chat.js instead of ./transport.js
- Use 'task' option instead of 'taskId'
- All 17 tests passing

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00
Cursor Agent 0a2e7f134a feat: add chat transport and AI chat helpers to @trigger.dev/sdk
Two new subpath exports:

@trigger.dev/sdk/chat (frontend, browser-safe):
- TriggerChatTransport — ChatTransport implementation for useChat
- createChatTransport() — factory function
- TriggerChatTransportOptions type

@trigger.dev/sdk/ai (backend, adds to existing ai.tool/ai.currentToolOptions):
- chatTask() — pre-typed task wrapper with auto-pipe
- pipeChat() — pipe StreamTextResult to realtime stream
- CHAT_STREAM_KEY constant
- ChatTaskPayload type
- ChatTaskOptions type
- PipeChatOptions type

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00
Cursor Agent c92b28c3cd refactor: avoid double-resolving accessToken in sendMessages
Use the already-resolved token when creating ApiClient instead of
calling resolveAccessToken() again through getApiClient().

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00
Cursor Agent 8891110950 feat: support dynamic accessToken function for token refresh
The accessToken option now accepts either a string or a function
returning a string. This enables dynamic token refresh patterns:

  new TriggerChatTransport({
    taskId: 'my-task',
    accessToken: () => getLatestToken(),
  })

The function is called on each sendMessages() call, allowing fresh
tokens to be used for each task trigger.

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00
Cursor Agent c6e4c7d90c refactor: remove internal ChatSessionState from public exports
ChatSessionState is an implementation detail of the transport's
session tracking. Users don't need to access it since the sessions
map is private.

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00
Cursor Agent 7d097ce769 test: add abort signal, multiple sessions, and body merging tests
Adds 3 additional test cases:
- Abort signal gracefully closes the stream
- Multiple independent chat sessions tracked correctly
- ChatRequestOptions.body is merged into task payload

Co-authored-by: Eric Allam <eric@trigger.dev>
2026-05-10 21:56:47 +01:00