ai 7.0.179 → 7.0.181
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/CHANGELOG.md +18 -0
- package/dist/{default-stop-condition-wtuJTiTo.js → default-stop-condition-D8PwkeWe.js} +52 -27
- package/dist/default-stop-condition-D8PwkeWe.js.map +1 -0
- package/dist/{index-CM_8u8gB.d.ts → index-CKEJqCzY.d.ts} +69 -4
- package/dist/index-CKEJqCzY.d.ts.map +1 -0
- package/dist/index.d.ts +2 -2
- package/dist/index.js +192 -119
- package/dist/index.js.map +1 -1
- package/dist/internal/index.d.ts +8 -4
- package/dist/internal/index.d.ts.map +1 -1
- package/dist/internal/index.js +1 -1
- package/docs/03-agents/04-loop-control.mdx +35 -1
- package/docs/03-agents/06-policy-tool-approvals.mdx +5 -2
- package/docs/03-agents/08-terminal-ui.mdx +10 -0
- package/docs/03-ai-sdk-core/16-mcp-tools.mdx +59 -0
- package/docs/03-ai-sdk-core/65-lifecycle-callbacks.mdx +6 -0
- package/docs/04-ai-sdk-ui/21-error-handling.mdx +21 -0
- package/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx +19 -0
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +25 -0
- package/docs/07-reference/01-ai-sdk-core/16-tool-loop-agent.mdx +7 -0
- package/docs/07-reference/01-ai-sdk-core/25-create-mcp-tools.mdx +65 -0
- package/docs/07-reference/06-ai-sdk-tui/01-run-agent-tui.mdx +3 -0
- package/package.json +4 -4
- package/src/agent/default-stop-condition.ts +12 -3
- package/src/agent/tool-loop-agent-settings.ts +11 -0
- package/src/agent/tool-loop-agent.ts +3 -1
- package/src/extract/restricted-telemetry-dispatcher.ts +9 -5
- package/src/generate-text/default-generate-text-result.ts +4 -0
- package/src/generate-text/generate-text-events.ts +7 -0
- package/src/generate-text/generate-text-result.ts +9 -0
- package/src/generate-text/generate-text.ts +37 -5
- package/src/generate-text/index.ts +1 -0
- package/src/generate-text/steering.ts +19 -5
- package/src/generate-text/stopped-by.ts +43 -0
- package/src/generate-text/stream-language-model-call.ts +21 -2
- package/src/generate-text/stream-text-result.ts +18 -0
- package/src/generate-text/stream-text.ts +59 -6
- package/src/logger/create-log-warnings.ts +17 -0
- package/src/model/as-language-model-v3.ts +3 -0
- package/src/model/as-language-model-v4.ts +5 -1
- package/src/model/resolve-model.ts +8 -3
- package/src/prompt/convert-to-language-model-prompt.ts +15 -6
- package/src/util/log-v2-compatibility-warning.ts +5 -2
- package/dist/default-stop-condition-wtuJTiTo.js.map +0 -1
- package/dist/index-CM_8u8gB.d.ts.map +0 -1
|
@@ -22,11 +22,15 @@ export function toTelemetryEvent<
|
|
|
22
22
|
|
|
23
23
|
function redactFileLocations<STATE>(state: STATE): STATE {
|
|
24
24
|
if (!Array.isArray(state)) return state;
|
|
25
|
-
return state.map(part =>
|
|
26
|
-
part?.type
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
return state.map(part => {
|
|
26
|
+
if (part?.type !== 'text' && part?.type !== 'file') return part;
|
|
27
|
+
const copy = { ...part };
|
|
28
|
+
delete copy.providerOptions;
|
|
29
|
+
if (copy.type === 'file' && isFileLocation(copy.data)) {
|
|
30
|
+
copy.data = '[redacted]';
|
|
31
|
+
}
|
|
32
|
+
return copy;
|
|
33
|
+
}) as STATE;
|
|
30
34
|
}
|
|
31
35
|
|
|
32
36
|
function isFileLocation(data: unknown): boolean {
|
|
@@ -6,6 +6,7 @@ import type { Output } from './output';
|
|
|
6
6
|
import type { InferCompleteOutput } from './output-utils';
|
|
7
7
|
import { convertToReasoningOutputs } from './reasoning-output';
|
|
8
8
|
import type { ResponseMessage } from './response-message';
|
|
9
|
+
import type { StoppedBy } from './stopped-by';
|
|
9
10
|
|
|
10
11
|
export class DefaultGenerateTextResult<
|
|
11
12
|
TOOLS extends ToolSet,
|
|
@@ -14,6 +15,7 @@ export class DefaultGenerateTextResult<
|
|
|
14
15
|
> implements GenerateTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT> {
|
|
15
16
|
readonly steps: GenerateTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT>['steps'];
|
|
16
17
|
readonly totalUsage: LanguageModelUsage;
|
|
18
|
+
readonly stoppedBy: StoppedBy | undefined;
|
|
17
19
|
private readonly _output: InferCompleteOutput<OUTPUT> | undefined;
|
|
18
20
|
|
|
19
21
|
constructor(options: {
|
|
@@ -21,11 +23,13 @@ export class DefaultGenerateTextResult<
|
|
|
21
23
|
steps: GenerateTextResult<TOOLS, RUNTIME_CONTEXT, OUTPUT>['steps'];
|
|
22
24
|
output: InferCompleteOutput<OUTPUT> | undefined;
|
|
23
25
|
totalUsage: LanguageModelUsage;
|
|
26
|
+
stoppedBy?: StoppedBy;
|
|
24
27
|
}) {
|
|
25
28
|
this.initialResponseMessages = options.initialResponseMessages;
|
|
26
29
|
this.steps = options.steps;
|
|
27
30
|
this._output = options.output;
|
|
28
31
|
this.totalUsage = options.totalUsage;
|
|
32
|
+
this.stoppedBy = options.stoppedBy;
|
|
29
33
|
}
|
|
30
34
|
|
|
31
35
|
private readonly initialResponseMessages: Array<ResponseMessage>;
|
|
@@ -15,6 +15,7 @@ import type { Output } from './output';
|
|
|
15
15
|
import type { ResponseMessage } from './response-message';
|
|
16
16
|
import type { StepResult } from './step-result';
|
|
17
17
|
import type { ToolOrder } from './tool-order';
|
|
18
|
+
import type { StoppedBy } from './stopped-by';
|
|
18
19
|
|
|
19
20
|
/**
|
|
20
21
|
* Event passed to the `onStart` callback.
|
|
@@ -246,6 +247,12 @@ export type GenerateTextEndEvent<
|
|
|
246
247
|
RUNTIME_CONTEXT
|
|
247
248
|
>['rawFinishReason'];
|
|
248
249
|
|
|
250
|
+
/**
|
|
251
|
+
* Why the generation stopped its tool loop, e.g. because of a stop
|
|
252
|
+
* condition. `generateText`, `streamText`, and `ToolLoopAgent` always set it.
|
|
253
|
+
*/
|
|
254
|
+
readonly stoppedBy?: StoppedBy;
|
|
255
|
+
|
|
249
256
|
/** Aggregated token usage across all steps. */
|
|
250
257
|
readonly usage: LanguageModelUsage;
|
|
251
258
|
|
|
@@ -24,6 +24,7 @@ import type {
|
|
|
24
24
|
StaticToolResult,
|
|
25
25
|
TypedToolResult,
|
|
26
26
|
} from './tool-result';
|
|
27
|
+
import type { StoppedBy } from './stopped-by';
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
30
|
* The result of a `generateText` call.
|
|
@@ -112,6 +113,14 @@ export interface GenerateTextResult<
|
|
|
112
113
|
*/
|
|
113
114
|
readonly rawFinishReason: string | undefined;
|
|
114
115
|
|
|
116
|
+
/**
|
|
117
|
+
* Why the generation stopped its tool loop. Unlike `finishReason`, which
|
|
118
|
+
* describes the last model call, it tells whether the model, a stop
|
|
119
|
+
* condition, a tool without an `execute` function, or a tool approval ended
|
|
120
|
+
* the loop. `generateText` and `ToolLoopAgent` always set it.
|
|
121
|
+
*/
|
|
122
|
+
readonly stoppedBy?: StoppedBy;
|
|
123
|
+
|
|
115
124
|
/**
|
|
116
125
|
* The total token usage of all steps.
|
|
117
126
|
* When there are multiple steps, the usage is the sum of all step usages.
|
|
@@ -21,7 +21,8 @@ import {
|
|
|
21
21
|
type UserModelMessage,
|
|
22
22
|
} from '@ai-sdk/provider-utils';
|
|
23
23
|
import { ToolChoiceViolationError } from '../error';
|
|
24
|
-
import {
|
|
24
|
+
import { createLogWarnings } from '../logger/create-log-warnings';
|
|
25
|
+
import type { LogWarningsFunction } from '../logger/log-warnings';
|
|
25
26
|
import { resolveLanguageModel } from '../model/resolve-model';
|
|
26
27
|
import type { ModelMessage } from '../prompt';
|
|
27
28
|
import { cloneModelMessages } from '../prompt/clone-model-message';
|
|
@@ -108,6 +109,11 @@ import {
|
|
|
108
109
|
type SteeringNotDeliveredReason,
|
|
109
110
|
type SteeringRun,
|
|
110
111
|
} from './steering';
|
|
112
|
+
import {
|
|
113
|
+
getModelStoppedBy,
|
|
114
|
+
getPendingToolCallStoppedBy,
|
|
115
|
+
type StoppedBy,
|
|
116
|
+
} from './stopped-by';
|
|
111
117
|
import {
|
|
112
118
|
isStepCount,
|
|
113
119
|
isStopConditionMet,
|
|
@@ -241,6 +247,7 @@ export type GenerateTextInclude = {
|
|
|
241
247
|
* @param experimental_onToolCallFinish - Deprecated alias for `onToolExecutionEnd`.
|
|
242
248
|
* @param onStepFinish - Callback that is called when each step (LLM call) is finished, including intermediate steps.
|
|
243
249
|
* @param onStepError - Callback that is called when a step that started fails before it produces a step result, i.e. when the provider call throws or rejects, or when the call is aborted or times out.
|
|
250
|
+
* @param onWarning - Callback that receives the warnings of the call instead of the global warning logger.
|
|
244
251
|
* @param onEnd - Callback that is called when all steps are finished and the response is complete.
|
|
245
252
|
* @param onFinish - Deprecated alias for `onEnd`.
|
|
246
253
|
*
|
|
@@ -333,6 +340,7 @@ async function generateTextInternal<
|
|
|
333
340
|
onStepEnd,
|
|
334
341
|
onStepError,
|
|
335
342
|
onStepFinish,
|
|
343
|
+
onWarning,
|
|
336
344
|
onFinish,
|
|
337
345
|
onEnd = onFinish,
|
|
338
346
|
...settings
|
|
@@ -599,6 +607,13 @@ async function generateTextInternal<
|
|
|
599
607
|
NoInfer<OUTPUT>
|
|
600
608
|
>;
|
|
601
609
|
|
|
610
|
+
/**
|
|
611
|
+
* Callback that receives the warnings of the call, e.g. unsupported
|
|
612
|
+
* settings or deprecated prompt parts. When it is set, the warnings are
|
|
613
|
+
* not sent to the global warning logger (`AI_SDK_LOG_WARNINGS`).
|
|
614
|
+
*/
|
|
615
|
+
onWarning?: LogWarningsFunction;
|
|
616
|
+
|
|
602
617
|
/**
|
|
603
618
|
* Callback that is called when each step (LLM call) ends, including intermediate steps.
|
|
604
619
|
*
|
|
@@ -664,7 +679,7 @@ async function generateTextInternal<
|
|
|
664
679
|
|
|
665
680
|
const resolvedToolCallConcurrency =
|
|
666
681
|
prepareToolCallConcurrency(toolCallConcurrency);
|
|
667
|
-
const model = resolveLanguageModel(modelArg);
|
|
682
|
+
const model = resolveLanguageModel(modelArg, onWarning);
|
|
668
683
|
const resolvedToolCallers = resolveToolCallerConfiguration({
|
|
669
684
|
tools,
|
|
670
685
|
toolCallers: experimental_toolCallers,
|
|
@@ -686,6 +701,7 @@ async function generateTextInternal<
|
|
|
686
701
|
onToolExecutionEnd ?? experimental_onToolCallFinish;
|
|
687
702
|
const resolvedOnStepEnd = onStepEnd ?? onStepFinish;
|
|
688
703
|
|
|
704
|
+
const logCallWarnings = createLogWarnings(onWarning);
|
|
689
705
|
const unsupportedTimeoutWarnings: Warning[] = [];
|
|
690
706
|
|
|
691
707
|
if (getFirstChunkTimeoutMs(timeout) != null) {
|
|
@@ -706,7 +722,7 @@ async function generateTextInternal<
|
|
|
706
722
|
}
|
|
707
723
|
|
|
708
724
|
if (unsupportedTimeoutWarnings.length > 0) {
|
|
709
|
-
|
|
725
|
+
logCallWarnings({
|
|
710
726
|
warnings: unsupportedTimeoutWarnings,
|
|
711
727
|
provider: model.provider,
|
|
712
728
|
model: model.modelId,
|
|
@@ -969,6 +985,7 @@ async function generateTextInternal<
|
|
|
969
985
|
|
|
970
986
|
// why waiting steering messages are not delivered when the loop ends:
|
|
971
987
|
let steeringEndReason: SteeringNotDeliveredReason = 'finished';
|
|
988
|
+
let stoppedBy: StoppedBy = 'model';
|
|
972
989
|
|
|
973
990
|
const shouldStartNextStep = async () => {
|
|
974
991
|
if (steps[steps.length - 1].finishReason === 'interrupted') {
|
|
@@ -981,6 +998,7 @@ async function generateTextInternal<
|
|
|
981
998
|
clientToolCalls.length
|
|
982
999
|
) {
|
|
983
1000
|
steeringEndReason = 'waiting-for-tool-results';
|
|
1001
|
+
stoppedBy = getPendingToolCallStoppedBy(steps[steps.length - 1]);
|
|
984
1002
|
return false;
|
|
985
1003
|
}
|
|
986
1004
|
|
|
@@ -999,6 +1017,7 @@ async function generateTextInternal<
|
|
|
999
1017
|
!steeringRun?.hasPendingMessages() &&
|
|
1000
1018
|
(isTruncated || !steeringRun?.promoteQueuedMessage())
|
|
1001
1019
|
) {
|
|
1020
|
+
stoppedBy = getModelStoppedBy(steps[steps.length - 1]);
|
|
1002
1021
|
return false;
|
|
1003
1022
|
}
|
|
1004
1023
|
|
|
@@ -1012,6 +1031,7 @@ async function generateTextInternal<
|
|
|
1012
1031
|
// continue until a stop condition is met:
|
|
1013
1032
|
if (await isStopConditionMet({ stopConditions, steps })) {
|
|
1014
1033
|
steeringEndReason = 'stop-condition';
|
|
1034
|
+
stoppedBy = 'stop-condition';
|
|
1015
1035
|
return false;
|
|
1016
1036
|
}
|
|
1017
1037
|
|
|
@@ -1096,6 +1116,7 @@ async function generateTextInternal<
|
|
|
1096
1116
|
|
|
1097
1117
|
const stepModel = resolveLanguageModel(
|
|
1098
1118
|
prepareStepResult?.model ?? model,
|
|
1119
|
+
onWarning,
|
|
1099
1120
|
);
|
|
1100
1121
|
|
|
1101
1122
|
const stepInstructions =
|
|
@@ -1174,6 +1195,7 @@ async function generateTextInternal<
|
|
|
1174
1195
|
).map(({ message }) => message),
|
|
1175
1196
|
];
|
|
1176
1197
|
|
|
1198
|
+
const promptWarnings: Warning[] = [];
|
|
1177
1199
|
const promptMessages = await convertToLanguageModelPrompt({
|
|
1178
1200
|
prompt: {
|
|
1179
1201
|
instructions: stepInstructions,
|
|
@@ -1183,8 +1205,13 @@ async function generateTextInternal<
|
|
|
1183
1205
|
download,
|
|
1184
1206
|
abortSignal: mergedAbortSignal,
|
|
1185
1207
|
provider: stepModel.provider.split('.')[0],
|
|
1208
|
+
warnings: promptWarnings,
|
|
1186
1209
|
});
|
|
1187
1210
|
|
|
1211
|
+
if (promptWarnings.length > 0) {
|
|
1212
|
+
logCallWarnings({ warnings: promptWarnings });
|
|
1213
|
+
}
|
|
1214
|
+
|
|
1188
1215
|
const stepProviderOptions = mergeObjects(
|
|
1189
1216
|
providerOptions,
|
|
1190
1217
|
prepareStepResult?.providerOptions,
|
|
@@ -1781,7 +1808,10 @@ async function generateTextInternal<
|
|
|
1781
1808
|
rawFinishReason: currentModelResponse.finishReason.raw,
|
|
1782
1809
|
usage: stepUsage,
|
|
1783
1810
|
performance: stepPerformance,
|
|
1784
|
-
warnings:
|
|
1811
|
+
warnings: [
|
|
1812
|
+
...promptWarnings,
|
|
1813
|
+
...(currentModelResponse.warnings ?? []),
|
|
1814
|
+
],
|
|
1785
1815
|
providerMetadata: currentModelResponse.providerMetadata,
|
|
1786
1816
|
request: stepRequest,
|
|
1787
1817
|
response: stepResponse,
|
|
@@ -1789,7 +1819,7 @@ async function generateTextInternal<
|
|
|
1789
1819
|
steeringMessages: stepSteeringMessages,
|
|
1790
1820
|
});
|
|
1791
1821
|
|
|
1792
|
-
|
|
1822
|
+
logCallWarnings({
|
|
1793
1823
|
warnings: currentModelResponse.warnings ?? [],
|
|
1794
1824
|
provider: stepModel.provider,
|
|
1795
1825
|
model: stepModel.modelId,
|
|
@@ -1864,6 +1894,7 @@ async function generateTextInternal<
|
|
|
1864
1894
|
runtimeContext: lastStep.runtimeContext,
|
|
1865
1895
|
finishReason: lastStep.finishReason,
|
|
1866
1896
|
rawFinishReason: lastStep.rawFinishReason,
|
|
1897
|
+
stoppedBy,
|
|
1867
1898
|
usage: totalUsage,
|
|
1868
1899
|
totalUsage,
|
|
1869
1900
|
content: steps.flatMap(step => step.content),
|
|
@@ -1925,6 +1956,7 @@ async function generateTextInternal<
|
|
|
1925
1956
|
steps,
|
|
1926
1957
|
totalUsage,
|
|
1927
1958
|
output: resolvedOutput,
|
|
1959
|
+
stoppedBy,
|
|
1928
1960
|
});
|
|
1929
1961
|
} catch (error) {
|
|
1930
1962
|
const wrappedError = wrapGatewayError(error);
|
|
@@ -154,6 +154,7 @@ type PendingSteer = {
|
|
|
154
154
|
|
|
155
155
|
class DefaultSteering implements Steering {
|
|
156
156
|
private state: 'idle' | 'running' | 'ended' = 'idle';
|
|
157
|
+
private endReason: SteeringNotDeliveredReason = 'finished';
|
|
157
158
|
private pending: Array<PendingSteer> = [];
|
|
158
159
|
private queued: Array<PendingSteer> = [];
|
|
159
160
|
private taken: Array<PendingSteer> = [];
|
|
@@ -227,7 +228,7 @@ class DefaultSteering implements Steering {
|
|
|
227
228
|
type: 'not-delivered',
|
|
228
229
|
id,
|
|
229
230
|
message: userMessage,
|
|
230
|
-
reason:
|
|
231
|
+
reason: this.endReason,
|
|
231
232
|
})
|
|
232
233
|
: new Promise<SteeringOutcome>(resolve => {
|
|
233
234
|
target.push({ id, message: userMessage, resolve });
|
|
@@ -329,6 +330,7 @@ class DefaultSteering implements Steering {
|
|
|
329
330
|
this.pending = [];
|
|
330
331
|
this.queued = [];
|
|
331
332
|
this.state = 'ended';
|
|
333
|
+
this.endReason = reason;
|
|
332
334
|
this.interruptRequested = false;
|
|
333
335
|
this.onInterrupt = undefined;
|
|
334
336
|
|
|
@@ -340,8 +342,9 @@ class DefaultSteering implements Steering {
|
|
|
340
342
|
}
|
|
341
343
|
}
|
|
342
344
|
|
|
343
|
-
// prepareStep can map messages to new objects, e.g. to add provider options
|
|
344
|
-
// so a message also counts as sent when a user
|
|
345
|
+
// prepareStep can map messages to new objects, e.g. to add provider options or
|
|
346
|
+
// to turn text into a text part, so a message also counts as sent when a user
|
|
347
|
+
// message has the same content parts, ignoring the provider options of parts:
|
|
345
348
|
function isSent({
|
|
346
349
|
message,
|
|
347
350
|
sentMessages,
|
|
@@ -349,13 +352,24 @@ function isSent({
|
|
|
349
352
|
message: UserModelMessage;
|
|
350
353
|
sentMessages: ReadonlyArray<ModelMessage>;
|
|
351
354
|
}): boolean {
|
|
352
|
-
const content =
|
|
355
|
+
const content = normalizeContent(message.content);
|
|
353
356
|
|
|
354
357
|
return sentMessages.some(
|
|
355
358
|
sentMessage =>
|
|
356
359
|
sentMessage === message ||
|
|
357
360
|
(sentMessage.role === 'user' &&
|
|
358
|
-
|
|
361
|
+
normalizeContent(sentMessage.content) === content),
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
function normalizeContent(content: UserModelMessage['content']): string {
|
|
366
|
+
const parts =
|
|
367
|
+
typeof content === 'string'
|
|
368
|
+
? [{ type: 'text' as const, text: content }]
|
|
369
|
+
: content;
|
|
370
|
+
|
|
371
|
+
return JSON.stringify(
|
|
372
|
+
parts.map(({ providerOptions: _providerOptions, ...part }) => part),
|
|
359
373
|
);
|
|
360
374
|
}
|
|
361
375
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { Context, ToolSet } from '@ai-sdk/provider-utils';
|
|
2
|
+
import { isToolExecutionAllowedFinishReason } from './is-tool-execution-allowed-finish-reason';
|
|
3
|
+
import type { StepResult } from './step-result';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Why a generation stopped its tool loop:
|
|
7
|
+
*
|
|
8
|
+
* - `model`: the model finished without tool calls that continue the loop,
|
|
9
|
+
* e.g. it gave a final answer, hit the output limit, or its call was
|
|
10
|
+
* interrupted. `finishReason` tells how the last model call finished.
|
|
11
|
+
* - `stop-condition`: a `stopWhen` condition was met.
|
|
12
|
+
* - `tool-without-execute`: a tool call needs a result from a tool without an
|
|
13
|
+
* `execute` function.
|
|
14
|
+
* - `approval`: a tool call needs an approval.
|
|
15
|
+
*/
|
|
16
|
+
export type StoppedBy =
|
|
17
|
+
| 'model'
|
|
18
|
+
| 'stop-condition'
|
|
19
|
+
| 'tool-without-execute'
|
|
20
|
+
| 'approval';
|
|
21
|
+
|
|
22
|
+
type FinishedStep = Pick<
|
|
23
|
+
StepResult<ToolSet, Context>,
|
|
24
|
+
'finishReason' | 'content'
|
|
25
|
+
>;
|
|
26
|
+
|
|
27
|
+
export function getPendingToolCallStoppedBy(step: FinishedStep): StoppedBy {
|
|
28
|
+
if (!isToolExecutionAllowedFinishReason(step.finishReason)) {
|
|
29
|
+
return 'model';
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return hasPendingApproval(step) ? 'approval' : 'tool-without-execute';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export function getModelStoppedBy(step: FinishedStep): StoppedBy {
|
|
36
|
+
return hasPendingApproval(step) ? 'approval' : 'model';
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function hasPendingApproval({ content }: FinishedStep): boolean {
|
|
40
|
+
return content.some(
|
|
41
|
+
part => part.type === 'tool-approval-request' && part.isAutomatic !== true,
|
|
42
|
+
);
|
|
43
|
+
}
|
|
@@ -280,6 +280,7 @@ export async function streamLanguageModelCall<
|
|
|
280
280
|
// a Prompt or a standardized Prompt.
|
|
281
281
|
onStart?: (args: {
|
|
282
282
|
promptMessages: LanguageModelV4Prompt;
|
|
283
|
+
promptWarnings: Array<CallWarning>;
|
|
283
284
|
}) => Promise<void> | void;
|
|
284
285
|
} & Prompt &
|
|
285
286
|
LanguageModelCallOptions): Promise<{
|
|
@@ -310,6 +311,7 @@ export async function streamLanguageModelCall<
|
|
|
310
311
|
allowSystemInMessages,
|
|
311
312
|
} as Prompt);
|
|
312
313
|
|
|
314
|
+
const promptWarnings: Array<CallWarning> = [];
|
|
313
315
|
const promptMessages = await convertToLanguageModelPrompt({
|
|
314
316
|
prompt: {
|
|
315
317
|
instructions: standardizedPrompt.instructions,
|
|
@@ -319,6 +321,7 @@ export async function streamLanguageModelCall<
|
|
|
319
321
|
download,
|
|
320
322
|
abortSignal,
|
|
321
323
|
provider: resolvedModel.provider.split('.')[0],
|
|
324
|
+
warnings: promptWarnings,
|
|
322
325
|
});
|
|
323
326
|
|
|
324
327
|
const stepTools = await prepareTools({
|
|
@@ -333,7 +336,7 @@ export async function streamLanguageModelCall<
|
|
|
333
336
|
});
|
|
334
337
|
|
|
335
338
|
await notify({
|
|
336
|
-
event: { promptMessages },
|
|
339
|
+
event: { promptMessages, promptWarnings },
|
|
337
340
|
callbacks: onStart,
|
|
338
341
|
});
|
|
339
342
|
|
|
@@ -403,6 +406,7 @@ export async function streamLanguageModelCall<
|
|
|
403
406
|
request,
|
|
404
407
|
onLanguageModelCallEnd,
|
|
405
408
|
collectContent,
|
|
409
|
+
promptWarnings,
|
|
406
410
|
}),
|
|
407
411
|
);
|
|
408
412
|
|
|
@@ -434,6 +438,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
434
438
|
request,
|
|
435
439
|
onLanguageModelCallEnd,
|
|
436
440
|
collectContent,
|
|
441
|
+
promptWarnings,
|
|
437
442
|
}: {
|
|
438
443
|
tools: TOOLS | undefined;
|
|
439
444
|
instructions: Instructions | undefined;
|
|
@@ -452,6 +457,7 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
452
457
|
request?: { url?: string };
|
|
453
458
|
onLanguageModelCallEnd?: Arrayable<OnLanguageModelCallEndCallback<TOOLS>>;
|
|
454
459
|
collectContent: boolean;
|
|
460
|
+
promptWarnings: Array<CallWarning>;
|
|
455
461
|
}) {
|
|
456
462
|
// keep track of parsed tool calls so provider-emitted approval requests can reference them
|
|
457
463
|
// keep track of tool inputs for provider-side tool results
|
|
@@ -474,11 +480,23 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
474
480
|
// repair or refine them.
|
|
475
481
|
const withholdToolParts = toolChoice.type === 'none';
|
|
476
482
|
|
|
483
|
+
let hasStarted = false;
|
|
484
|
+
|
|
477
485
|
return new TransformStream<
|
|
478
486
|
LanguageModelV4StreamPart,
|
|
479
487
|
LanguageModelStreamPart<TOOLS>
|
|
480
488
|
>({
|
|
481
489
|
async transform(chunk, controller) {
|
|
490
|
+
if (!hasStarted && chunk.type !== 'stream-start') {
|
|
491
|
+
hasStarted = true;
|
|
492
|
+
if (promptWarnings.length > 0) {
|
|
493
|
+
controller.enqueue({
|
|
494
|
+
type: 'model-call-start',
|
|
495
|
+
warnings: promptWarnings,
|
|
496
|
+
});
|
|
497
|
+
}
|
|
498
|
+
}
|
|
499
|
+
|
|
482
500
|
if (isOutputChunk(chunk)) {
|
|
483
501
|
const outputChunkTimestampMs = now();
|
|
484
502
|
|
|
@@ -881,8 +899,9 @@ function createLanguageModelV4StreamPartToLanguageModelStreamPartTransform<
|
|
|
881
899
|
case 'stream-start': {
|
|
882
900
|
controller.enqueue({
|
|
883
901
|
type: 'model-call-start',
|
|
884
|
-
warnings: chunk.warnings,
|
|
902
|
+
warnings: [...promptWarnings, ...chunk.warnings],
|
|
885
903
|
});
|
|
904
|
+
hasStarted = true;
|
|
886
905
|
break;
|
|
887
906
|
}
|
|
888
907
|
|
|
@@ -49,6 +49,7 @@ import type {
|
|
|
49
49
|
StaticToolResult,
|
|
50
50
|
TypedToolResult,
|
|
51
51
|
} from './tool-result';
|
|
52
|
+
import type { StoppedBy } from './stopped-by';
|
|
52
53
|
|
|
53
54
|
export type UIMessageStreamOptions<UI_MESSAGE extends UIMessage> = {
|
|
54
55
|
/**
|
|
@@ -266,6 +267,17 @@ export interface StreamTextResult<
|
|
|
266
267
|
*/
|
|
267
268
|
readonly rawFinishReason: PromiseLike<string | undefined>;
|
|
268
269
|
|
|
270
|
+
/**
|
|
271
|
+
* Why the generation stopped its tool loop. Unlike `finishReason`, which
|
|
272
|
+
* describes the last model call, it tells whether the model, a stop
|
|
273
|
+
* condition, a tool without an `execute` function, or a tool approval ended
|
|
274
|
+
* the loop. `streamText` and `ToolLoopAgent` always set it. Like
|
|
275
|
+
* `finishReason`, it rejects when the stream is aborted.
|
|
276
|
+
*
|
|
277
|
+
* Automatically consumes the stream.
|
|
278
|
+
*/
|
|
279
|
+
readonly stoppedBy?: PromiseLike<StoppedBy | undefined>;
|
|
280
|
+
|
|
269
281
|
/**
|
|
270
282
|
* The total token usage of the generated response.
|
|
271
283
|
* When there are multiple steps, the usage is the sum of all step usages.
|
|
@@ -618,6 +630,12 @@ export type TextStreamFinishPart = {
|
|
|
618
630
|
type: 'finish';
|
|
619
631
|
finishReason: FinishReason;
|
|
620
632
|
rawFinishReason: string | undefined;
|
|
633
|
+
|
|
634
|
+
/**
|
|
635
|
+
* Why the generation stopped its tool loop. `streamText` and
|
|
636
|
+
* `ToolLoopAgent` always set it.
|
|
637
|
+
*/
|
|
638
|
+
stoppedBy?: StoppedBy;
|
|
621
639
|
totalUsage: LanguageModelUsage;
|
|
622
640
|
};
|
|
623
641
|
|