@trigger.dev/sdk 4.5.1 → 4.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/commonjs/v3/ai.js +66 -22
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/chat-react.d.ts +1 -0
- package/dist/commonjs/v3/chat-react.js +4 -1
- package/dist/commonjs/v3/chat-react.js.map +1 -1
- package/dist/commonjs/v3/chat.d.ts +100 -0
- package/dist/commonjs/v3/chat.js +203 -19
- package/dist/commonjs/v3/chat.js.map +1 -1
- package/dist/commonjs/v3/index.d.ts +1 -1
- package/dist/commonjs/v3/index.js.map +1 -1
- package/dist/commonjs/v3/runs-bulk.test.d.ts +1 -0
- package/dist/commonjs/v3/runs-bulk.test.js +141 -0
- package/dist/commonjs/v3/runs-bulk.test.js.map +1 -0
- package/dist/commonjs/v3/runs.d.ts +19 -2
- package/dist/commonjs/v3/runs.js +87 -0
- package/dist/commonjs/v3/runs.js.map +1 -1
- package/dist/commonjs/v3/sessions.js +5 -2
- package/dist/commonjs/v3/sessions.js.map +1 -1
- package/dist/commonjs/v3/shared.d.ts +2 -2
- package/dist/commonjs/v3/shared.js +79 -4
- package/dist/commonjs/v3/shared.js.map +1 -1
- package/dist/commonjs/v3/shared.test.js +37 -0
- package/dist/commonjs/v3/shared.test.js.map +1 -1
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.js +66 -22
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/chat-react.d.ts +1 -0
- package/dist/esm/v3/chat-react.js +4 -1
- package/dist/esm/v3/chat-react.js.map +1 -1
- package/dist/esm/v3/chat.d.ts +100 -0
- package/dist/esm/v3/chat.js +204 -20
- package/dist/esm/v3/chat.js.map +1 -1
- package/dist/esm/v3/index.d.ts +1 -1
- package/dist/esm/v3/index.js.map +1 -1
- package/dist/esm/v3/runs-bulk.test.d.ts +1 -0
- package/dist/esm/v3/runs-bulk.test.js +139 -0
- package/dist/esm/v3/runs-bulk.test.js.map +1 -0
- package/dist/esm/v3/runs.d.ts +18 -1
- package/dist/esm/v3/runs.js +87 -0
- package/dist/esm/v3/runs.js.map +1 -1
- package/dist/esm/v3/sessions.js +5 -2
- package/dist/esm/v3/sessions.js.map +1 -1
- package/dist/esm/v3/shared.d.ts +2 -2
- package/dist/esm/v3/shared.js +79 -5
- package/dist/esm/v3/shared.js.map +1 -1
- package/dist/esm/v3/shared.test.js +38 -1
- package/dist/esm/v3/shared.test.js.map +1 -1
- package/dist/esm/version.js +1 -1
- package/docs/ai-chat/frontend.mdx +46 -0
- package/docs/ai-chat/pending-messages.mdx +3 -1
- package/docs/ai-chat/reference.mdx +20 -0
- package/docs/billing-limits.mdx +73 -0
- package/docs/how-to-reduce-your-spend.mdx +1 -14
- package/docs/limits.mdx +5 -0
- package/docs/self-hosting/docker.mdx +24 -1
- package/docs/self-hosting/env/webapp.mdx +6 -1
- package/docs/self-hosting/kubernetes.mdx +26 -0
- package/docs/self-hosting/overview.mdx +4 -1
- package/docs/self-hosting/security.mdx +50 -0
- package/docs/troubleshooting.mdx +4 -0
- package/package.json +2 -2
package/dist/esm/v3/ai.js
CHANGED
|
@@ -2916,6 +2916,11 @@ function chatAgent(options) {
|
|
|
2916
2916
|
// default, `inFlightUsers`). The turn-loop checks this queue ahead of
|
|
2917
2917
|
// `messagesInput.waitWithIdleTimeout` so recovered turns fire first.
|
|
2918
2918
|
const bootInjectedQueue = [];
|
|
2919
|
+
// Messages consumed by a turn's `messagesInput.on` handler, dispatched
|
|
2920
|
+
// one per turn by the end-of-turn pickup. Loop-level on purpose:
|
|
2921
|
+
// consuming a record advances the committed `.in` cursor, so entries
|
|
2922
|
+
// dropped with a turn-local buffer are lost permanently.
|
|
2923
|
+
const pendingWireMessages = [];
|
|
2919
2924
|
const couldHavePriorState = payload.continuation === true || ctx.attempt.number > 1;
|
|
2920
2925
|
// `.in` resume cursor, computed at most once per boot. The boot
|
|
2921
2926
|
// block below resolves it (snapshot field or records scan) and the
|
|
@@ -3622,6 +3627,9 @@ function chatAgent(options) {
|
|
|
3622
3627
|
} // end else (no boot-injected first turn)
|
|
3623
3628
|
}
|
|
3624
3629
|
for (let turn = 0; turn < maxTurns; turn++) {
|
|
3630
|
+
// Declared here so the finally can detach it — a handler leaked past
|
|
3631
|
+
// its turn duplicates every mid-stream message into the shared buffer.
|
|
3632
|
+
let turnMsgSub;
|
|
3625
3633
|
try {
|
|
3626
3634
|
// Extract turn-level context before entering the span. Slim
|
|
3627
3635
|
// wire: at most one delta message per record. `headStartMessages`
|
|
@@ -3697,8 +3705,6 @@ function chatAgent(options) {
|
|
|
3697
3705
|
const stopSignal = stopController.signal;
|
|
3698
3706
|
const cancelSignal = runSignal;
|
|
3699
3707
|
const combinedSignal = AbortSignal.any([runSignal, stopController.signal]);
|
|
3700
|
-
// Buffer messages that arrive during streaming
|
|
3701
|
-
const pendingMessages = [];
|
|
3702
3708
|
const pmConfig = locals.get(chatPendingMessagesKey);
|
|
3703
3709
|
const msgSub = messagesInput.on(async (msg) => {
|
|
3704
3710
|
// If pendingMessages is configured, route to the steering queue
|
|
@@ -3745,8 +3751,9 @@ function chatAgent(options) {
|
|
|
3745
3751
|
return; // Don't add to wire buffer — frontend handles non-injected case
|
|
3746
3752
|
}
|
|
3747
3753
|
// No pendingMessages config — standard wire buffer for next turn
|
|
3748
|
-
|
|
3754
|
+
pendingWireMessages.push(msg);
|
|
3749
3755
|
});
|
|
3756
|
+
turnMsgSub = msgSub;
|
|
3750
3757
|
// Track new messages for this turn (user input + assistant response).
|
|
3751
3758
|
const turnNewModelMessages = [];
|
|
3752
3759
|
const turnNewUIMessages = [];
|
|
@@ -4801,9 +4808,10 @@ function chatAgent(options) {
|
|
|
4801
4808
|
return "continue";
|
|
4802
4809
|
}
|
|
4803
4810
|
// If messages arrived during streaming (without pendingMessages config),
|
|
4804
|
-
//
|
|
4805
|
-
|
|
4806
|
-
|
|
4811
|
+
// dispatch the oldest as the next turn. The rest stay queued
|
|
4812
|
+
// and drain one per turn.
|
|
4813
|
+
if (pendingWireMessages.length > 0) {
|
|
4814
|
+
currentWirePayload = pendingWireMessages.shift();
|
|
4807
4815
|
return "continue";
|
|
4808
4816
|
}
|
|
4809
4817
|
// chat.requestUpgrade() was called — exit the loop so the
|
|
@@ -4889,6 +4897,9 @@ function chatAgent(options) {
|
|
|
4889
4897
|
// Turn error handler: write an error chunk + turn-complete to the stream
|
|
4890
4898
|
// so the client sees the error, then wait for the next message instead
|
|
4891
4899
|
// of killing the entire run. This keeps the conversation alive.
|
|
4900
|
+
// Detach the turn's message handler first — left attached it would
|
|
4901
|
+
// eat the very message the wait below is waiting for.
|
|
4902
|
+
turnMsgSub?.off();
|
|
4892
4903
|
if (turnError instanceof Error &&
|
|
4893
4904
|
turnError.name === "AbortError" &&
|
|
4894
4905
|
runSignal.aborted) {
|
|
@@ -5008,6 +5019,11 @@ function chatAgent(options) {
|
|
|
5008
5019
|
currentWirePayload = bootInjectedQueue.shift();
|
|
5009
5020
|
continue;
|
|
5010
5021
|
}
|
|
5022
|
+
// Same for messages buffered during the errored turn — already consumed, idling strands them.
|
|
5023
|
+
if (pendingWireMessages.length > 0) {
|
|
5024
|
+
currentWirePayload = pendingWireMessages.shift();
|
|
5025
|
+
continue;
|
|
5026
|
+
}
|
|
5011
5027
|
// Wait for the next message — same as after a successful turn
|
|
5012
5028
|
const effectiveIdleTimeout = metadata.get(IDLE_TIMEOUT_METADATA_KEY) ??
|
|
5013
5029
|
idleTimeoutInSeconds;
|
|
@@ -5023,6 +5039,9 @@ function chatAgent(options) {
|
|
|
5023
5039
|
currentWirePayload = next.output;
|
|
5024
5040
|
// Continue to next iteration of the for loop
|
|
5025
5041
|
}
|
|
5042
|
+
finally {
|
|
5043
|
+
turnMsgSub?.off();
|
|
5044
|
+
}
|
|
5026
5045
|
}
|
|
5027
5046
|
}
|
|
5028
5047
|
finally {
|
|
@@ -5933,8 +5952,17 @@ function createChatSession(payload, options) {
|
|
|
5933
5952
|
const accumulator = new ChatMessageAccumulator();
|
|
5934
5953
|
let previousTurnUsage;
|
|
5935
5954
|
let cumulativeUsage = emptyUsage();
|
|
5955
|
+
// Messages consumed mid-turn, dispatched one per next(). Iterator-level
|
|
5956
|
+
// for the same reason as the agent loop's `pendingWireMessages`:
|
|
5957
|
+
// consumed records never replay, so a turn-local buffer loses them.
|
|
5958
|
+
const sessionPendingWire = [];
|
|
5959
|
+
// The current turn's message subscription — detached defensively at the
|
|
5960
|
+
// top of next() in case user code threw without complete()/done().
|
|
5961
|
+
let activeMsgSub;
|
|
5936
5962
|
return {
|
|
5937
5963
|
async next() {
|
|
5964
|
+
activeMsgSub?.off();
|
|
5965
|
+
activeMsgSub = undefined;
|
|
5938
5966
|
if (!booted) {
|
|
5939
5967
|
booted = true;
|
|
5940
5968
|
await seedSessionInResumeCursorForCustomLoop(currentPayload);
|
|
@@ -5994,23 +6022,29 @@ function createChatSession(payload, options) {
|
|
|
5994
6022
|
currentPayload = { ...currentPayload, continuation: true };
|
|
5995
6023
|
}
|
|
5996
6024
|
}
|
|
5997
|
-
// Subsequent turns:
|
|
6025
|
+
// Subsequent turns: drain buffered mid-turn messages first (they
|
|
6026
|
+
// were consumed and won't be re-delivered), then wait.
|
|
5998
6027
|
if (turn > 0) {
|
|
5999
|
-
|
|
6000
|
-
|
|
6001
|
-
stop.cleanup();
|
|
6002
|
-
return { done: true, value: undefined };
|
|
6028
|
+
if (sessionPendingWire.length > 0) {
|
|
6029
|
+
currentPayload = sessionPendingWire.shift();
|
|
6003
6030
|
}
|
|
6004
|
-
|
|
6005
|
-
|
|
6006
|
-
|
|
6007
|
-
|
|
6008
|
-
|
|
6009
|
-
|
|
6010
|
-
|
|
6011
|
-
|
|
6031
|
+
else {
|
|
6032
|
+
// chat.requestUpgrade() / chat.endRun() — exit before waiting
|
|
6033
|
+
if (locals.get(chatUpgradeRequestedKey) || locals.get(chatEndRunRequestedKey)) {
|
|
6034
|
+
stop.cleanup();
|
|
6035
|
+
return { done: true, value: undefined };
|
|
6036
|
+
}
|
|
6037
|
+
const next = await messagesInput.waitWithIdleTimeout({
|
|
6038
|
+
idleTimeoutInSeconds,
|
|
6039
|
+
timeout,
|
|
6040
|
+
spanName: "waiting for next message",
|
|
6041
|
+
});
|
|
6042
|
+
if (!next.ok || runSignal.aborted) {
|
|
6043
|
+
stop.cleanup();
|
|
6044
|
+
return { done: true, value: undefined };
|
|
6045
|
+
}
|
|
6046
|
+
currentPayload = next.output;
|
|
6012
6047
|
}
|
|
6013
|
-
currentPayload = next.output;
|
|
6014
6048
|
}
|
|
6015
6049
|
// Check limits
|
|
6016
6050
|
if (turn >= maxTurns || runSignal.aborted) {
|
|
@@ -6035,10 +6069,10 @@ function createChatSession(payload, options) {
|
|
|
6035
6069
|
clientData: currentPayload.metadata,
|
|
6036
6070
|
});
|
|
6037
6071
|
// Listen for messages during streaming (steering + next-turn buffer)
|
|
6038
|
-
const sessionPendingWire = [];
|
|
6039
6072
|
const sessionMsgSub = messagesInput.on(async (msg) => {
|
|
6040
|
-
sessionPendingWire.push(msg);
|
|
6041
6073
|
if (sessionPendingMessages) {
|
|
6074
|
+
// Steering route — the frontend re-sends non-injected
|
|
6075
|
+
// messages on turn complete, so don't also buffer the wire.
|
|
6042
6076
|
// Slim wire: at most one delta message per record. Read
|
|
6043
6077
|
// `msg.message` directly — no array slicing needed.
|
|
6044
6078
|
const lastUIMessage = msg.message;
|
|
@@ -6063,8 +6097,11 @@ function createChatSession(payload, options) {
|
|
|
6063
6097
|
/* non-fatal */
|
|
6064
6098
|
}
|
|
6065
6099
|
}
|
|
6100
|
+
return;
|
|
6066
6101
|
}
|
|
6102
|
+
sessionPendingWire.push(msg);
|
|
6067
6103
|
});
|
|
6104
|
+
activeMsgSub = sessionMsgSub;
|
|
6068
6105
|
// Accumulate messages. Slim wire: pass the single delta message as
|
|
6069
6106
|
// a 0-or-1-length array. The accumulator's behavior is unchanged —
|
|
6070
6107
|
// it still appends user messages and reconverts on regenerate.
|
|
@@ -6155,6 +6192,11 @@ function createChatSession(payload, options) {
|
|
|
6155
6192
|
throw error;
|
|
6156
6193
|
}
|
|
6157
6194
|
}
|
|
6195
|
+
finally {
|
|
6196
|
+
// Detach at stream end (like the agent loop): the steering queue
|
|
6197
|
+
// can't inject anymore, so later arrivals must buffer for the next turn.
|
|
6198
|
+
sessionMsgSub.off();
|
|
6199
|
+
}
|
|
6158
6200
|
if (response) {
|
|
6159
6201
|
const cleaned = stop.signal.aborted && !runSignal.aborted
|
|
6160
6202
|
? cleanupAbortedParts(response)
|
|
@@ -6298,6 +6340,8 @@ function createChatSession(payload, options) {
|
|
|
6298
6340
|
return { done: false, value: turnObj };
|
|
6299
6341
|
},
|
|
6300
6342
|
async return() {
|
|
6343
|
+
activeMsgSub?.off();
|
|
6344
|
+
activeMsgSub = undefined;
|
|
6301
6345
|
// `stop` only exists once next() has booted the iterator.
|
|
6302
6346
|
stop?.cleanup();
|
|
6303
6347
|
return { done: true, value: undefined };
|