@trigger.dev/sdk 4.8.6 → 4.8.8
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.d.ts +5 -3
- package/dist/commonjs/v3/ai.js +144 -54
- package/dist/commonjs/v3/ai.js.map +1 -1
- package/dist/commonjs/v3/steeringContext.js +53 -4
- package/dist/commonjs/v3/steeringContext.js.map +1 -1
- package/dist/commonjs/v3/toolOutputJson.d.ts +17 -0
- package/dist/commonjs/v3/toolOutputJson.js +241 -0
- package/dist/commonjs/v3/toolOutputJson.js.map +1 -0
- package/dist/commonjs/version.js +1 -1
- package/dist/esm/v3/ai.d.ts +5 -3
- package/dist/esm/v3/ai.js +144 -54
- package/dist/esm/v3/ai.js.map +1 -1
- package/dist/esm/v3/steeringContext.js +53 -4
- package/dist/esm/v3/steeringContext.js.map +1 -1
- package/dist/esm/v3/toolOutputJson.d.ts +17 -0
- package/dist/esm/v3/toolOutputJson.js +238 -0
- package/dist/esm/v3/toolOutputJson.js.map +1 -0
- package/dist/esm/version.js +1 -1
- package/docs/ai-chat/compaction.mdx +9 -3
- package/docs/apikeys.mdx +4 -3
- package/docs/github-integration.mdx +11 -1
- package/docs/mcp-remote.mdx +6 -4
- package/docs/mcp-tools.mdx +96 -2
- package/package.json +2 -2
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
import { BACKGROUND_INJECTION_TYPE, PENDING_MESSAGE_INJECTED_TYPE } from "./ai-shared.js";
|
|
2
|
-
|
|
2
|
+
/**
|
|
3
|
+
* A message as JSON text, or null when JSON cannot hold it. The input an override
|
|
4
|
+
* is retained from is the AI SDK's own, so a tool result in it can still hold a
|
|
5
|
+
* BigInt or a circular reference. Plain `JSON.stringify` is tried first, since a
|
|
6
|
+
* replacer function takes it off V8's fast path and tool results can be large;
|
|
7
|
+
* only a BigInt (a TypeError) retries with the BigInt encoded by value, as an
|
|
8
|
+
* object a string cannot equal. Anything still unserializable is null.
|
|
9
|
+
*/
|
|
10
|
+
function serializeMessage(message) {
|
|
11
|
+
try {
|
|
12
|
+
return JSON.stringify(message);
|
|
13
|
+
}
|
|
14
|
+
catch {
|
|
15
|
+
try {
|
|
16
|
+
return JSON.stringify(message, (_key, value) => typeof value === "bigint" ? { $bigint: value.toString() } : value);
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
3
23
|
/**
|
|
4
24
|
* Every response message of the generation so far, oldest first, taken from
|
|
5
25
|
* what the AI SDK hands prepareStep rather than inferred from the steps.
|
|
@@ -59,11 +79,40 @@ export function retainStepMessages(prepare) {
|
|
|
59
79
|
return retained ? { ...result, messages: retained } : result;
|
|
60
80
|
};
|
|
61
81
|
}
|
|
62
|
-
/**
|
|
82
|
+
/**
|
|
83
|
+
* How many of `next` already end `input`, as the longest overlap.
|
|
84
|
+
*
|
|
85
|
+
* A message is the same as another when it is the same object or both serialize
|
|
86
|
+
* to the same JSON. One that JSON cannot hold counts as different, so it is not
|
|
87
|
+
* de-duplicated: a head-start tool result with a circular reference is sent
|
|
88
|
+
* twice, which is rare and better than a failed step. Each message is
|
|
89
|
+
* serialized at most once, so the cost is linear in the messages' size rather
|
|
90
|
+
* than once per overlap length tried.
|
|
91
|
+
*/
|
|
63
92
|
function alreadyIncluded(input, next) {
|
|
93
|
+
const serialized = new Map();
|
|
94
|
+
const json = (message) => {
|
|
95
|
+
let text = serialized.get(message);
|
|
96
|
+
if (text === undefined) {
|
|
97
|
+
text = serializeMessage(message);
|
|
98
|
+
serialized.set(message, text);
|
|
99
|
+
}
|
|
100
|
+
return text;
|
|
101
|
+
};
|
|
102
|
+
const same = (a, b) => {
|
|
103
|
+
if (a === b)
|
|
104
|
+
return true;
|
|
105
|
+
if (a.role !== b.role)
|
|
106
|
+
return false;
|
|
107
|
+
const text = json(a);
|
|
108
|
+
return text !== null && text === json(b);
|
|
109
|
+
};
|
|
64
110
|
for (let count = Math.min(input.length, next.length); count > 0; count--) {
|
|
65
|
-
const
|
|
66
|
-
|
|
111
|
+
const start = input.length - count;
|
|
112
|
+
let matches = true;
|
|
113
|
+
for (let i = 0; i < count && matches; i++)
|
|
114
|
+
matches = same(input[start + i], next[i]);
|
|
115
|
+
if (matches)
|
|
67
116
|
return count;
|
|
68
117
|
}
|
|
69
118
|
return 0;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"steeringContext.js","sourceRoot":"","sources":["../../../src/v3/steeringContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,yBAAyB,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AA2B1F,MAAM,
|
|
1
|
+
{"version":3,"file":"steeringContext.js","sourceRoot":"","sources":["../../../src/v3/steeringContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,yBAAyB,EAAE,6BAA6B,EAAE,MAAM,gBAAgB,CAAC;AA2B1F;;;;;;;GAOG;AACH,SAAS,gBAAgB,CAAC,OAAqB;IAC7C,IAAI,CAAC;QACH,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IACjC,CAAC;IAAC,MAAM,CAAC;QACP,IAAI,CAAC;YACH,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,EAAE,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAC7C,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,EAAE,OAAO,EAAE,KAAK,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,CAClE,CAAC;QACJ,CAAC;QAAC,MAAM,CAAC;YACP,OAAO,IAAI,CAAC;QACd,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,0BAA0B,CAAC,KAAsB;IAC/D,OAAO,KAAK,CAAC,gBAAgB,IAAI,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,CAAC,QA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AE,EAAE,IAAI,EAAE,MAAe,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,MAAe,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;gBAChF,CAAC,CAAC,CAAC;gBACH,IAAI,KAAK,CAAC,MAAM;oBAAE,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,OAAO,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACxD,CAAC;QACH,CAAC;QACD,IAAI,KAAK,CAAC,MAAM;YAAE,KAAK,CAAC,IAAI,CAAC,EAAE,GAAG,OAAO,EAAE,KAAK,EAAE,CAAC,CAAC;IACtD,CAAC;IACD,MAAM,KAAK,EAAE,CAAC;IACd,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type ModelMessage } from "ai";
|
|
2
|
+
/**
|
|
3
|
+
* Makes every JSON tool result in `messages` plain JSON, the shape the AI SDK
|
|
4
|
+
* validates a prompt against. Applied to the stored history between calls and,
|
|
5
|
+
* through the managed `prepareStep`, to each step's messages.
|
|
6
|
+
*
|
|
7
|
+
* From 7.0.101 the AI SDK converts a tool's output itself; earlier versions keep
|
|
8
|
+
* what `execute` returned. A `Date` from an ORM row then fails the next model
|
|
9
|
+
* call (AI SDK 6), or the next step of the same call (AI SDK 7 before 7.0.101),
|
|
10
|
+
* with a schema error naming only a message index. A value JSON can carry is
|
|
11
|
+
* converted the way the newer SDK does it, including a `BigInt` when the app
|
|
12
|
+
* defines `BigInt.prototype.toJSON`. One it cannot (a `BigInt` otherwise, a
|
|
13
|
+
* circular reference) becomes an error result naming the tool and the field, logged once,
|
|
14
|
+
* so the rest of the history stays valid. On an AI SDK that converts tool outputs
|
|
15
|
+
* itself this does nothing. Returns the same array when nothing changed.
|
|
16
|
+
*/
|
|
17
|
+
export declare function withJsonToolResults(messages: ModelMessage[]): Promise<ModelMessage[]>;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { logger } from "@trigger.dev/core/v3";
|
|
2
|
+
import { convertToModelMessages } from "ai";
|
|
3
|
+
const IDENTIFIER = /^[A-Za-z_$][\w$]*$/;
|
|
4
|
+
const formatPath = (keys) => keys.reduce((path, key) => typeof key === "number"
|
|
5
|
+
? `${path}[${key}]`
|
|
6
|
+
: IDENTIFIER.test(key)
|
|
7
|
+
? path
|
|
8
|
+
? `${path}.${key}`
|
|
9
|
+
: key
|
|
10
|
+
: `${path}[${JSON.stringify(key)}]`, "") || "the top level";
|
|
11
|
+
/**
|
|
12
|
+
* Walks a value once, without copying it. Returns the first value JSON cannot
|
|
13
|
+
* hold with the keys leading to it, and sets `convert` when a JSON round trip
|
|
14
|
+
* would change the value (a `Date`, anything with `toJSON`, `undefined`, a
|
|
15
|
+
* non-finite number, a class instance, a sparse array).
|
|
16
|
+
*
|
|
17
|
+
* Kept lean: keys are pushed and popped and only joined into a path on failure,
|
|
18
|
+
* and only the chain of ancestors is kept, so an object referenced twice is not
|
|
19
|
+
* taken for a cycle.
|
|
20
|
+
*/
|
|
21
|
+
function inspect(root) {
|
|
22
|
+
const ancestors = [];
|
|
23
|
+
const keys = [];
|
|
24
|
+
let convert = false;
|
|
25
|
+
const visit = (value) => {
|
|
26
|
+
switch (typeof value) {
|
|
27
|
+
case "string":
|
|
28
|
+
case "boolean":
|
|
29
|
+
return undefined;
|
|
30
|
+
case "number":
|
|
31
|
+
if (!Number.isFinite(value))
|
|
32
|
+
convert = true;
|
|
33
|
+
return undefined;
|
|
34
|
+
case "bigint":
|
|
35
|
+
// An app can give BigInt a `toJSON` (an ORM's BigInt ids), which
|
|
36
|
+
// `JSON.stringify`, and so the AI SDK, then honours.
|
|
37
|
+
if (typeof BigInt.prototype.toJSON === "function") {
|
|
38
|
+
convert = true;
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
return "BigInt";
|
|
42
|
+
case "undefined":
|
|
43
|
+
case "function":
|
|
44
|
+
case "symbol":
|
|
45
|
+
convert = true;
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
if (value === null)
|
|
49
|
+
return undefined;
|
|
50
|
+
const object = value;
|
|
51
|
+
if (ancestors.includes(object))
|
|
52
|
+
return "circular reference";
|
|
53
|
+
if (typeof object.toJSON === "function") {
|
|
54
|
+
convert = true;
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
ancestors.push(object);
|
|
58
|
+
if (Array.isArray(object)) {
|
|
59
|
+
for (let index = 0; index < object.length; index++) {
|
|
60
|
+
if (!(index in object))
|
|
61
|
+
convert = true;
|
|
62
|
+
keys.push(index);
|
|
63
|
+
const kind = visit(object[index]);
|
|
64
|
+
if (kind)
|
|
65
|
+
return kind;
|
|
66
|
+
keys.pop();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
const prototype = Object.getPrototypeOf(object);
|
|
71
|
+
if (prototype !== Object.prototype && prototype !== null)
|
|
72
|
+
convert = true;
|
|
73
|
+
for (const key in object) {
|
|
74
|
+
if (!Object.hasOwn(object, key))
|
|
75
|
+
continue;
|
|
76
|
+
keys.push(key);
|
|
77
|
+
const kind = visit(object[key]);
|
|
78
|
+
if (kind)
|
|
79
|
+
return kind;
|
|
80
|
+
keys.pop();
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
ancestors.pop();
|
|
84
|
+
return undefined;
|
|
85
|
+
};
|
|
86
|
+
const kind = visit(root);
|
|
87
|
+
return kind ? { convert, issue: { kind, keys } } : { convert };
|
|
88
|
+
}
|
|
89
|
+
function check(value) {
|
|
90
|
+
const { issue, convert } = inspect(value);
|
|
91
|
+
let problem;
|
|
92
|
+
if (issue) {
|
|
93
|
+
problem = `${issue.kind} at ${formatPath(issue.keys)}`;
|
|
94
|
+
}
|
|
95
|
+
else if (convert) {
|
|
96
|
+
try {
|
|
97
|
+
// Same conversion as the AI SDK's own from 7.0.101: a Date becomes its ISO string.
|
|
98
|
+
const serialized = JSON.stringify(value);
|
|
99
|
+
return { value: serialized === undefined ? null : JSON.parse(serialized) };
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
problem = error instanceof Error ? error.message : String(error);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
return { value };
|
|
107
|
+
}
|
|
108
|
+
return { problem };
|
|
109
|
+
}
|
|
110
|
+
const unserializableMessage = (toolName, problem) => `Tool "${toolName}" returned a value that cannot be sent to the model as JSON: ${problem}. ` +
|
|
111
|
+
"Convert it in the tool's execute before returning it, for example to a string or a number.";
|
|
112
|
+
/**
|
|
113
|
+
* Tool outputs already checked, and what each became, per tool call. AI SDK
|
|
114
|
+
* versions that keep the raw output pass the same object on every later
|
|
115
|
+
* conversion and step, so each result is walked, and converted, once per run
|
|
116
|
+
* however often the history is rebuilt. The tool call is part of the key because
|
|
117
|
+
* a tool can return the same object from several calls, changed in between (a
|
|
118
|
+
* shared state object): each call keeps the value it returned, as the AI SDK's
|
|
119
|
+
* own conversion from 7.0.101 does. Outputs are held weakly: entries go with them.
|
|
120
|
+
*/
|
|
121
|
+
const checked = new WeakMap();
|
|
122
|
+
/**
|
|
123
|
+
* Calls remembered per output, oldest dropped first. Bounds what an object that
|
|
124
|
+
* outlives its run (a module-level value returned by every call, in a reused
|
|
125
|
+
* process) keeps; a dropped call is only checked again.
|
|
126
|
+
*/
|
|
127
|
+
const MAX_CALLS_PER_OUTPUT = 64;
|
|
128
|
+
/**
|
|
129
|
+
* The cached verdict for one call's output, logging a problem the first time it
|
|
130
|
+
* is found. A bare value such as a top-level `BigInt` cannot be cached, so it is
|
|
131
|
+
* checked, and logged, on every conversion.
|
|
132
|
+
*/
|
|
133
|
+
function checkedOutput(value, part) {
|
|
134
|
+
if (typeof value !== "object" || value === null)
|
|
135
|
+
return reported(check(value), part);
|
|
136
|
+
let calls = checked.get(value);
|
|
137
|
+
if (!calls)
|
|
138
|
+
checked.set(value, (calls = new Map()));
|
|
139
|
+
let result = calls.get(part.toolCallId);
|
|
140
|
+
if (!result) {
|
|
141
|
+
calls.set(part.toolCallId, (result = reported(check(value), part)));
|
|
142
|
+
if (calls.size > MAX_CALLS_PER_OUTPUT)
|
|
143
|
+
calls.delete(calls.keys().next().value);
|
|
144
|
+
}
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
function reported(result, part) {
|
|
148
|
+
if ("problem" in result)
|
|
149
|
+
logger.error(unserializableMessage(part.toolName, result.problem));
|
|
150
|
+
return result;
|
|
151
|
+
}
|
|
152
|
+
let sdkConvertsToolOutputs;
|
|
153
|
+
/**
|
|
154
|
+
* Whether the installed AI SDK turns tool outputs into plain JSON itself, as it
|
|
155
|
+
* does from 7.0.101. Asked once per process by converting one tool part holding a
|
|
156
|
+
* `Date`, so it follows what the SDK does rather than its version number. Where
|
|
157
|
+
* it does, a `toModelOutput` returning `{ type: "json" }` is left as the tool
|
|
158
|
+
* built it, as the SDK itself leaves it.
|
|
159
|
+
*/
|
|
160
|
+
function aiSdkConvertsToolOutputs() {
|
|
161
|
+
sdkConvertsToolOutputs ??= (async () => {
|
|
162
|
+
const probe = {
|
|
163
|
+
id: "probe",
|
|
164
|
+
role: "assistant",
|
|
165
|
+
parts: [
|
|
166
|
+
{
|
|
167
|
+
type: "dynamic-tool",
|
|
168
|
+
toolName: "probe",
|
|
169
|
+
toolCallId: "probe",
|
|
170
|
+
state: "output-available",
|
|
171
|
+
input: {},
|
|
172
|
+
output: new Date(0),
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
};
|
|
176
|
+
try {
|
|
177
|
+
const converted = await convertToModelMessages([probe]);
|
|
178
|
+
const result = converted
|
|
179
|
+
.flatMap((message) => (Array.isArray(message.content) ? message.content : []))
|
|
180
|
+
.find((part) => isJsonToolResult(part));
|
|
181
|
+
return typeof result?.output.value === "string";
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
})();
|
|
187
|
+
return sdkConvertsToolOutputs;
|
|
188
|
+
}
|
|
189
|
+
const isJsonToolResult = (part) => {
|
|
190
|
+
const candidate = part;
|
|
191
|
+
return (candidate?.type === "tool-result" &&
|
|
192
|
+
(candidate.output?.type === "json" || candidate.output?.type === "error-json"));
|
|
193
|
+
};
|
|
194
|
+
/**
|
|
195
|
+
* Makes every JSON tool result in `messages` plain JSON, the shape the AI SDK
|
|
196
|
+
* validates a prompt against. Applied to the stored history between calls and,
|
|
197
|
+
* through the managed `prepareStep`, to each step's messages.
|
|
198
|
+
*
|
|
199
|
+
* From 7.0.101 the AI SDK converts a tool's output itself; earlier versions keep
|
|
200
|
+
* what `execute` returned. A `Date` from an ORM row then fails the next model
|
|
201
|
+
* call (AI SDK 6), or the next step of the same call (AI SDK 7 before 7.0.101),
|
|
202
|
+
* with a schema error naming only a message index. A value JSON can carry is
|
|
203
|
+
* converted the way the newer SDK does it, including a `BigInt` when the app
|
|
204
|
+
* defines `BigInt.prototype.toJSON`. One it cannot (a `BigInt` otherwise, a
|
|
205
|
+
* circular reference) becomes an error result naming the tool and the field, logged once,
|
|
206
|
+
* so the rest of the history stays valid. On an AI SDK that converts tool outputs
|
|
207
|
+
* itself this does nothing. Returns the same array when nothing changed.
|
|
208
|
+
*/
|
|
209
|
+
export async function withJsonToolResults(messages) {
|
|
210
|
+
if (await aiSdkConvertsToolOutputs())
|
|
211
|
+
return messages;
|
|
212
|
+
let result;
|
|
213
|
+
messages.forEach((message, index) => {
|
|
214
|
+
if (!Array.isArray(message.content))
|
|
215
|
+
return;
|
|
216
|
+
let content;
|
|
217
|
+
message.content.forEach((part, partIndex) => {
|
|
218
|
+
if (!isJsonToolResult(part))
|
|
219
|
+
return;
|
|
220
|
+
const outcome = checkedOutput(part.output.value, part);
|
|
221
|
+
if ("value" in outcome && outcome.value === part.output.value)
|
|
222
|
+
return;
|
|
223
|
+
content ??= [...message.content];
|
|
224
|
+
content[partIndex] = {
|
|
225
|
+
...part,
|
|
226
|
+
output: "value" in outcome
|
|
227
|
+
? { ...part.output, value: outcome.value }
|
|
228
|
+
: { type: "error-text", value: unserializableMessage(part.toolName, outcome.problem) },
|
|
229
|
+
};
|
|
230
|
+
});
|
|
231
|
+
if (!content)
|
|
232
|
+
return;
|
|
233
|
+
result ??= [...messages];
|
|
234
|
+
result[index] = { ...message, content };
|
|
235
|
+
});
|
|
236
|
+
return result ?? messages;
|
|
237
|
+
}
|
|
238
|
+
//# sourceMappingURL=toolOutputJson.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"toolOutputJson.js","sourceRoot":"","sources":["../../../src/v3/toolOutputJson.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAC;AAC9C,OAAO,EAAE,sBAAsB,EAAqC,MAAM,IAAI,CAAC;AAM/E,MAAM,UAAU,GAAG,oBAAoB,CAAC;AACxC,MAAM,UAAU,GAAG,CAAC,IAAyB,EAAE,EAAE,CAC/C,IAAI,CAAC,MAAM,CACT,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE,CACZ,OAAO,GAAG,KAAK,QAAQ;IACrB,CAAC,CAAC,GAAG,IAAI,IAAI,GAAG,GAAG;IACnB,CAAC,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,CAAC;QACpB,CAAC,CAAC,IAAI;YACJ,CAAC,CAAC,GAAG,IAAI,IAAI,GAAG,EAAE;YAClB,CAAC,CAAC,GAAG;QACP,CAAC,CAAC,GAAG,IAAI,IAAI,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,EACzC,EAAE,CACH,IAAI,eAAe,CAAC;AAEvB;;;;;;;;;GASG;AACH,SAAS,OAAO,CAAC,IAAa;IAC5B,MAAM,SAAS,GAAa,EAAE,CAAC;IAC/B,MAAM,IAAI,GAAwB,EAAE,CAAC;IACrC,IAAI,OAAO,GAAG,KAAK,CAAC;IACpB,MAAM,KAAK,GAAG,CAAC,KAAc,EAAsB,EAAE;QACnD,QAAQ,OAAO,KAAK,EAAE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O,IAAI,OAAO,CAAC,KAAK,KAAK,IAAI,CAAC,MAAM,CAAC,KAAK;gBAAE,OAAO;YACtE,OAAO,KAAK,CAAC,GAAI,OAAO,CAAC,OAAqB,CAAC,CAAC;YAChD,OAAO,CAAC,SAAS,CAAC,GAAG;gBACnB,GAAG,IAAI;gBACP,MAAM,EACJ,OAAO,IAAI,OAAO;oBAChB,CAAC,CAAC,EAAE,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE;oBAC1C,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,EAAE,qBAAqB,CAAC,IAAI,CAAC,QAAQ,EAAE,OAAO,CAAC,OAAO,CAAC,EAAE;aAC3F,CAAC;QACJ,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,OAAO;YAAE,OAAO;QACrB,MAAM,KAAK,CAAC,GAAG,QAAQ,CAAC,CAAC;QACzB,MAAM,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,OAAO,EAAE,OAAO,EAAkB,CAAC;IAC1D,CAAC,CAAC,CAAC;IACH,OAAO,MAAM,IAAI,QAAQ,CAAC;AAC5B,CAAC"}
|
package/dist/esm/version.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const VERSION = "4.8.
|
|
1
|
+
export const VERSION = "4.8.8";
|
|
2
2
|
//# sourceMappingURL=version.js.map
|
|
@@ -388,9 +388,15 @@ for (let turn = 0; turn < 100; turn++) {
|
|
|
388
388
|
const { message } = await chat.pipeAndCapture(result);
|
|
389
389
|
if (message) await conversation.addResponse(message);
|
|
390
390
|
|
|
391
|
-
// Outer-loop compaction
|
|
392
|
-
|
|
393
|
-
|
|
391
|
+
// Outer-loop compaction. Pass the last step's usage: it's what the model's
|
|
392
|
+
// context window held on its final call. `result.usage` is the turn's sum on
|
|
393
|
+
// AI SDK 7, so read it from `steps`, and pass the turn's sum separately.
|
|
394
|
+
const lastStepUsage = (await result.steps).at(-1)?.usage;
|
|
395
|
+
await conversation.compactIfNeeded(lastStepUsage, {
|
|
396
|
+
chatId: payload.chatId,
|
|
397
|
+
turn,
|
|
398
|
+
turnUsage: await result.totalUsage,
|
|
399
|
+
});
|
|
394
400
|
|
|
395
401
|
await db.chat.update({ data: { messages: conversation.uiMessages } });
|
|
396
402
|
await chat.writeTurnComplete();
|
package/docs/apikeys.mdx
CHANGED
|
@@ -132,9 +132,10 @@ Give a key a list of allowed IP addresses to limit where it can be used. Request
|
|
|
132
132
|
- [Public Access Tokens](/realtime/auth) created with the key are not IP-restricted, since they are meant for browsers. Creating one still requires an allowed IP address.
|
|
133
133
|
|
|
134
134
|
<Tip>
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
Give each backend its own IP-restricted key and move your applications to those keys. The
|
|
136
|
+
dashboard shows only the last four characters of the root key, but masking doesn't stop it from
|
|
137
|
+
working. To invalidate a root key your apps no longer use, choose **Regenerate…** and revoke
|
|
138
|
+
the previous key immediately.
|
|
138
139
|
</Tip>
|
|
139
140
|
|
|
140
141
|
## Change a key's access
|
|
@@ -54,7 +54,7 @@ When you connect a repository, the default branch of your repository will be use
|
|
|
54
54
|
When you configure a production or staging branch, every push to that branch will trigger a deployment.
|
|
55
55
|
Our build server will install the project dependencies, build your project, and deploy it to the corresponding environment.
|
|
56
56
|
|
|
57
|
-
|
|
57
|
+
Builds for an environment run one at a time, in the order the pushes arrived, so a push made while another build is running waits in a queue. Each preview branch has its own queue. A deployment still queued 15 minutes after it was created times out.
|
|
58
58
|
|
|
59
59
|
<Note>
|
|
60
60
|
When you connect a repository, the default branch of your repository will be used as the production tracking branch by default.
|
|
@@ -72,6 +72,16 @@ The name of the preview branch matches the branch name of the pull request.
|
|
|
72
72
|
Preview branch deployments require the preview environment to be enabled on your project. Learn more about [preview branches](/deployment/preview-branches).
|
|
73
73
|
</Note>
|
|
74
74
|
|
|
75
|
+
### Skip superseded builds
|
|
76
|
+
|
|
77
|
+
When you push a newer commit while an older one is still queued, the older build is skipped. Its deployment shows as **Superseded** and its GitHub check is marked skipped, so only the newest queued commit builds. A build that has already started always finishes. Each preview branch is handled on its own.
|
|
78
|
+
|
|
79
|
+
Turn this on or off for each environment with the **Skip superseded** switches in your project's Git settings. It's on by default for staging and preview, and off for production.
|
|
80
|
+
|
|
81
|
+
<Warning>
|
|
82
|
+
With [version skew protection](/deployment/atomic-deployment), runs from a skipped commit's app wait for a build that never happens, then expire. On Vercel this affects staging and preview. Turn Skip superseded off for an environment whose app serves traffic from every commit.
|
|
83
|
+
</Warning>
|
|
84
|
+
|
|
75
85
|
## Ignored branches
|
|
76
86
|
|
|
77
87
|
Ignored branches stop pull requests from matching branches from getting a preview deployment, for example Dependabot updates.
|
package/docs/mcp-remote.mdx
CHANGED
|
@@ -57,10 +57,12 @@ An OAuth grant stops working after 30 days without use, and 90 days after it was
|
|
|
57
57
|
|
|
58
58
|
Each OAuth grant carries a list of scopes. A tool call is denied if the scopes or your role in the organization don't allow it.
|
|
59
59
|
|
|
60
|
-
| Kind | Scopes
|
|
61
|
-
| ----- |
|
|
62
|
-
| Read | `read:alerts`, `read:branches`, `read:deployments`, `read:organizations`, `read:projects`, `read:prompts`, `read:query`, `read:runs`, `read:sessions`, `read:webhooks` |
|
|
63
|
-
| Write | `trigger:tasks`, `update:prompts`, `write:alerts`, `write:projects`, `write:query`, `write:runs`, `write:sessions`, `write:webhooks`
|
|
60
|
+
| Kind | Scopes |
|
|
61
|
+
| ----- | ------ |
|
|
62
|
+
| Read | `read:alerts`, `read:batch`, `read:branches`, `read:deployments`, `read:environments`, `read:envvars`, `read:errors`, `read:organizations`, `read:projects`, `read:prompts`, `read:query`, `read:queues`, `read:runs`, `read:sessions`, `read:tasks`, `read:waitpoints`, `read:webhooks` |
|
|
63
|
+
| Write | `manage:project`, `trigger:tasks`, `update:prompts`, `write:alerts`, `write:batch`, `write:branches`, `write:deployments`, `write:errors`, `write:projects`, `write:query`, `write:runs`, `write:sessions`, `write:tasks`, `write:waitpoints`, `write:webhooks` |
|
|
64
|
+
|
|
65
|
+
If a client doesn't ask for specific scopes, it gets every read scope except `read:envvars`, which a client must ask for by name.
|
|
64
66
|
|
|
65
67
|
## Write confirmation
|
|
66
68
|
|
package/docs/mcp-tools.mdx
CHANGED
|
@@ -72,21 +72,38 @@ Trigger a task to run with a specific payload. You can add a delay, set tags, co
|
|
|
72
72
|
|
|
73
73
|
### get_run_details
|
|
74
74
|
|
|
75
|
-
Get detailed information about a specific task run, including logs and status. Enable debug mode to get the full trace with all logs and spans.
|
|
75
|
+
Get detailed information about a specific task run, including logs and status. Enable debug mode to get the full trace with all logs and spans. On the [remote MCP server](/mcp-remote), the details include the run's metadata values, cut off after 4,000 characters.
|
|
76
76
|
|
|
77
77
|
**Example usage:**
|
|
78
78
|
- `"Show me details for run run_abc123"`
|
|
79
79
|
- `"Why did this run fail?"`
|
|
80
|
+
- `"What progress did run run_abc123 report in its metadata?"`
|
|
80
81
|
|
|
81
82
|
### list_runs
|
|
82
83
|
|
|
83
|
-
List runs for a project. Filter by status, task, tags, version, machine size, or time period.
|
|
84
|
+
List runs for a project. Filter by status, task, tags, version, machine size, region, queue, batch, schedule, bulk action, or time period. To match several versions, separate them with commas. If you pass `period`, it takes precedence and `from` and `to` are ignored. On the [remote MCP server](/mcp-remote), a batch, schedule or bulk action ID that does not start with lowercase `batch_`, `sched_` or `bulk_` is refused.
|
|
84
85
|
|
|
85
86
|
**Example usage:**
|
|
86
87
|
- `"Show me recent runs"`
|
|
87
88
|
- `"List failed runs from the last 7 days"`
|
|
88
89
|
- `"What runs are currently executing?"`
|
|
89
90
|
|
|
91
|
+
### list_run_tags
|
|
92
|
+
|
|
93
|
+
List the tags used on runs in an environment, in alphabetical order. Search by part of a tag name, choose how far back to look (the last 7 days by default), and set how many tags to return (up to 100). Only available on the [remote MCP server](/mcp-remote).
|
|
94
|
+
|
|
95
|
+
**Example usage:**
|
|
96
|
+
- `"Which run tags did we use this week?"`
|
|
97
|
+
- `"List tags that contain user: from the last 30 days"`
|
|
98
|
+
|
|
99
|
+
### read_run_stream
|
|
100
|
+
|
|
101
|
+
Read what a run streamed (`output`), or what was sent to one of its input streams (`input`), like the streams on the run page in the dashboard. Without a stream key, lists the run's output stream keys. Returns up to 200 records per call (50 by default) with a cursor for the next ones, and can wait up to 50 seconds for a record. Each record is cut off after 4,000 characters, and one call returns at most 40,000 characters of records. Chat input streams can't be read through the MCP server (open the run in the dashboard to see them), and runs on the first-generation stream store aren't supported. Only available on the [remote MCP server](/mcp-remote).
|
|
102
|
+
|
|
103
|
+
**Example usage:**
|
|
104
|
+
- `"What did run run_abc123 stream?"`
|
|
105
|
+
- `"Show the progress stream of that run"`
|
|
106
|
+
|
|
90
107
|
### wait_for_run_to_complete
|
|
91
108
|
|
|
92
109
|
Wait for a specific run to finish and return the result.
|
|
@@ -116,6 +133,83 @@ Replay a run as a new run of the same task, in the same environment. The new run
|
|
|
116
133
|
- `"Replay that failed run with userId set to user_456"`
|
|
117
134
|
- `"Replay run run_abc123 on a large-1x machine in 10 minutes"`
|
|
118
135
|
|
|
136
|
+
### search_logs
|
|
137
|
+
|
|
138
|
+
Search the logs of an environment, newest first, like the Logs page in the dashboard. Filter by text, level (`TRACE`, `DEBUG`, `INFO`, `WARN`, `ERROR`), task (up to 50), run, and time period (the last day by default, never past your plan's log retention). Pass either `period` or `from` and `to`, not both. Returns up to 100 logs per page. One call keeps searching older logs until the page is full, starts no new read after 10 seconds, and says which part of the time range it searched; when more logs may exist it returns a cursor for the rest. A cursor that does not match the filters is not used, and the reply says the search started again from the newest logs. Messages longer than 500 characters are cut off in the list. Only available on the [remote MCP server](/mcp-remote), for organizations that have the Logs page.
|
|
139
|
+
|
|
140
|
+
**Example usage:**
|
|
141
|
+
- `"Show me error logs from the last hour"`
|
|
142
|
+
- `"Search the logs for payment declined"`
|
|
143
|
+
- `"What did run run_abc123 log?"`
|
|
144
|
+
|
|
145
|
+
### get_log
|
|
146
|
+
|
|
147
|
+
Get one log entry in full, including its attributes, using a log ID from `search_logs`. The message and the attributes are each cut off after 4,000 characters. Only available on the [remote MCP server](/mcp-remote), for organizations that have the Logs page.
|
|
148
|
+
|
|
149
|
+
**Example usage:**
|
|
150
|
+
- `"Show me the full details of that error log"`
|
|
151
|
+
|
|
152
|
+
### reset_run_idempotency_key
|
|
153
|
+
|
|
154
|
+
Clear the idempotency key of a run, so the next trigger with the same key starts a new run instead of returning this one. A run without an idempotency key is refused without asking for approval. Only available on the [remote MCP server](/mcp-remote), and you approve each call.
|
|
155
|
+
|
|
156
|
+
**Example usage:**
|
|
157
|
+
- `"Reset the idempotency key on run run_abc123 so I can trigger it again"`
|
|
158
|
+
|
|
159
|
+
### update_run_metadata
|
|
160
|
+
|
|
161
|
+
Update the metadata of a run that has not finished. Pass `metadata` to replace it all, or `operations` to change single keys (`set`, `delete`, `append`, `remove`, `increment`), applied in order. An `update` operation replaces all metadata with the object you pass. The approval page lists every operation in order with the new value it writes, so a change too large to show in full (about 64 KB) is refused; the API's own size limit applies too. A change that would leave the metadata as it is is refused without asking for approval. Only available on the [remote MCP server](/mcp-remote), and you approve each call.
|
|
162
|
+
|
|
163
|
+
**Example usage:**
|
|
164
|
+
- `"Set progress to 50 in the metadata of run run_abc123"`
|
|
165
|
+
|
|
166
|
+
### add_run_tags
|
|
167
|
+
|
|
168
|
+
Add tags to a run. A run can have up to 10 tags of up to 128 characters each. The reply names the tags added, the tags skipped because the run already had them, and any duplicate or blank entries ignored; if the run already has every tag, the call is refused without asking for approval. Only available on the [remote MCP server](/mcp-remote), and you approve each call.
|
|
169
|
+
|
|
170
|
+
**Example usage:**
|
|
171
|
+
- `"Tag run run_abc123 with needs-review"`
|
|
172
|
+
|
|
173
|
+
### reschedule_run
|
|
174
|
+
|
|
175
|
+
Change when a delayed run starts, as a duration like `1h30m` or an ISO 8601 date. A duration is counted from when the approved call runs, not from when it was requested. Runs that are not delayed can't be rescheduled, and are refused without asking for approval. Only available on the [remote MCP server](/mcp-remote), and you approve each call.
|
|
176
|
+
|
|
177
|
+
**Example usage:**
|
|
178
|
+
- `"Push run run_abc123 back by two hours"`
|
|
179
|
+
|
|
180
|
+
## Bulk Action Tools
|
|
181
|
+
|
|
182
|
+
A bulk action cancels or replays many runs at once, like the bulk actions in the dashboard. These tools are only available on the [remote MCP server](/mcp-remote).
|
|
183
|
+
|
|
184
|
+
### list_bulk_actions
|
|
185
|
+
|
|
186
|
+
List the bulk actions of an environment, newest first, up to 100 per page with a cursor for older ones.
|
|
187
|
+
|
|
188
|
+
**Example usage:**
|
|
189
|
+
- `"Show me recent bulk actions"`
|
|
190
|
+
|
|
191
|
+
### get_bulk_action
|
|
192
|
+
|
|
193
|
+
Get one bulk action: its type, status, how many runs it selected, succeeded and failed, the filter or run IDs it selected, and when it was created and completed.
|
|
194
|
+
|
|
195
|
+
**Example usage:**
|
|
196
|
+
- `"How far along is bulk action bulk_abc123?"`
|
|
197
|
+
|
|
198
|
+
### create_bulk_action
|
|
199
|
+
|
|
200
|
+
Cancel or replay runs, selected by up to 500 run IDs or by a filter (status, task, version, tag, queue, machine, region, batch, schedule, bulk action, error, root runs only, and a time range). Root runs only can't be combined with batch or schedule, which always include child runs. A filter field with several values matches runs that have any of them, and every field you set must match. Without a time range, only runs created in the last 7 days are selected; pass `period`, or `from` and `to`, to reach older runs. The filter is checked again as each batch of runs is processed. Run IDs that are not in the environment, or that are older than 7 days, are skipped. You approve every bulk action, whatever its size: the approval page shows the action, the environment, every filter value and the time window or every run ID, and how many runs match now. The number may differ by the time the action starts, because runs change status and new runs are indexed. Replaying creates a new run for every selected run, and the new runs consume compute.
|
|
201
|
+
|
|
202
|
+
**Example usage:**
|
|
203
|
+
- `"Cancel the queued runs of my-task in prod from the last 7 days"`
|
|
204
|
+
- `"Replay every run that failed in the last day"`
|
|
205
|
+
|
|
206
|
+
### abort_bulk_action
|
|
207
|
+
|
|
208
|
+
Stop a bulk action that is still in progress. No new batch of runs starts, but a batch already being processed may finish. Runs it already cancelled stay cancelled, and runs it already replayed keep running. A bulk action that has already finished, or that is not in the environment, is refused without asking for approval. You approve each call.
|
|
209
|
+
|
|
210
|
+
**Example usage:**
|
|
211
|
+
- `"Stop bulk action bulk_abc123"`
|
|
212
|
+
|
|
119
213
|
## Deployment Tools
|
|
120
214
|
|
|
121
215
|
### deploy
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trigger.dev/sdk",
|
|
3
|
-
"version": "4.8.
|
|
3
|
+
"version": "4.8.8",
|
|
4
4
|
"description": "trigger.dev Node.JS SDK",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -77,7 +77,7 @@
|
|
|
77
77
|
"dependencies": {
|
|
78
78
|
"@opentelemetry/api": "1.9.1",
|
|
79
79
|
"@opentelemetry/semantic-conventions": "1.41.1",
|
|
80
|
-
"@trigger.dev/core": "4.8.
|
|
80
|
+
"@trigger.dev/core": "4.8.8",
|
|
81
81
|
"uncrypto": "^0.1.3"
|
|
82
82
|
},
|
|
83
83
|
"devDependencies": {
|