ai 6.0.169 → 6.0.170

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 CHANGED
@@ -1,5 +1,11 @@
1
1
  # ai
2
2
 
3
+ ## 6.0.170
4
+
5
+ ### Patch Changes
6
+
7
+ - 19d587a: fix(ai): add allowSystemInMessages option and warn by default when system messages are found in prompt or messages
8
+
3
9
  ## 6.0.169
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -679,6 +679,15 @@ type Prompt = {
679
679
  * System message to include in the prompt. Can be used with `prompt` or `messages`.
680
680
  */
681
681
  system?: string | SystemModelMessage | Array<SystemModelMessage>;
682
+ /**
683
+ * Whether system messages are allowed in the `prompt` or `messages` fields.
684
+ *
685
+ * When disabled, system messages must be provided through the `system`
686
+ * option. When unset, system messages are allowed with a warning.
687
+ *
688
+ * @default undefined
689
+ */
690
+ allowSystemInMessages?: boolean;
682
691
  } & ({
683
692
  /**
684
693
  * A prompt. It can be either a text prompt or a list of messages.
@@ -1382,6 +1391,7 @@ type GenerateTextOnFinishCallback<TOOLS extends ToolSet> = (event: OnFinishEvent
1382
1391
  * @param system - A system message that will be part of the prompt.
1383
1392
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
1384
1393
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
1394
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
1385
1395
  *
1386
1396
  * @param maxOutputTokens - Maximum number of tokens to generate.
1387
1397
  * @param temperature - Temperature setting.
@@ -1423,7 +1433,7 @@ type GenerateTextOnFinishCallback<TOOLS extends ToolSet> = (event: OnFinishEvent
1423
1433
  * @returns
1424
1434
  * A result object that contains the generated text, the results of the tool calls, and additional information.
1425
1435
  */
1426
- declare function generateText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string>>({ model: modelArg, tools, toolChoice, system, prompt, messages, maxRetries: maxRetriesArg, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, experimental_include: include, _internal: { generateId }, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, onStepFinish, onFinish, ...settings }: CallSettings & Prompt & {
1436
+ declare function generateText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string>>({ model: modelArg, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries: maxRetriesArg, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, experimental_include: include, _internal: { generateId }, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, onStepFinish, onFinish, ...settings }: CallSettings & Prompt & {
1427
1437
  /**
1428
1438
  * The language model to use.
1429
1439
  */
@@ -2774,6 +2784,7 @@ type StreamTextOnToolCallFinishCallback<TOOLS extends ToolSet = ToolSet> = (even
2774
2784
  * @param system - A system message that will be part of the prompt.
2775
2785
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
2776
2786
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
2787
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
2777
2788
  *
2778
2789
  * @param maxOutputTokens - Maximum number of tokens to generate.
2779
2790
  * @param temperature - Temperature setting.
@@ -2809,7 +2820,7 @@ type StreamTextOnToolCallFinishCallback<TOOLS extends ToolSet = ToolSet> = (even
2809
2820
  * @returns
2810
2821
  * A result object for accessing different stream types and additional information.
2811
2822
  */
2812
- declare function streamText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string, never>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, prepareStep, providerOptions, experimental_activeTools, activeTools, experimental_repairToolCall: repairToolCall, experimental_transform: transform, experimental_download: download, includeRawChunks, onChunk, onError, onFinish, onAbort, onStepFinish, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, experimental_context, experimental_include: include, _internal: { now, generateId }, ...settings }: CallSettings & Prompt & {
2823
+ declare function streamText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string, never>>({ model, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, prepareStep, providerOptions, experimental_activeTools, activeTools, experimental_repairToolCall: repairToolCall, experimental_transform: transform, experimental_download: download, includeRawChunks, onChunk, onError, onFinish, onAbort, onStepFinish, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, experimental_context, experimental_include: include, _internal: { now, generateId }, ...settings }: CallSettings & Prompt & {
2813
2824
  /**
2814
2825
  * The language model to use.
2815
2826
  */
@@ -5098,6 +5109,7 @@ type RepairTextFunction = (options: {
5098
5109
  * @param system - A system message that will be part of the prompt.
5099
5110
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
5100
5111
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
5112
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
5101
5113
  *
5102
5114
  * @param maxOutputTokens - Maximum number of tokens to generate.
5103
5115
  * @param temperature - Temperature setting.
@@ -5449,6 +5461,7 @@ type StreamObjectOnFinishCallback<RESULT> = (event: {
5449
5461
  * @param system - A system message that will be part of the prompt.
5450
5462
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
5451
5463
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
5464
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
5452
5465
  *
5453
5466
  * @param maxOutputTokens - Maximum number of tokens to generate.
5454
5467
  * @param temperature - Temperature setting.
package/dist/index.d.ts CHANGED
@@ -679,6 +679,15 @@ type Prompt = {
679
679
  * System message to include in the prompt. Can be used with `prompt` or `messages`.
680
680
  */
681
681
  system?: string | SystemModelMessage | Array<SystemModelMessage>;
682
+ /**
683
+ * Whether system messages are allowed in the `prompt` or `messages` fields.
684
+ *
685
+ * When disabled, system messages must be provided through the `system`
686
+ * option. When unset, system messages are allowed with a warning.
687
+ *
688
+ * @default undefined
689
+ */
690
+ allowSystemInMessages?: boolean;
682
691
  } & ({
683
692
  /**
684
693
  * A prompt. It can be either a text prompt or a list of messages.
@@ -1382,6 +1391,7 @@ type GenerateTextOnFinishCallback<TOOLS extends ToolSet> = (event: OnFinishEvent
1382
1391
  * @param system - A system message that will be part of the prompt.
1383
1392
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
1384
1393
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
1394
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
1385
1395
  *
1386
1396
  * @param maxOutputTokens - Maximum number of tokens to generate.
1387
1397
  * @param temperature - Temperature setting.
@@ -1423,7 +1433,7 @@ type GenerateTextOnFinishCallback<TOOLS extends ToolSet> = (event: OnFinishEvent
1423
1433
  * @returns
1424
1434
  * A result object that contains the generated text, the results of the tool calls, and additional information.
1425
1435
  */
1426
- declare function generateText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string>>({ model: modelArg, tools, toolChoice, system, prompt, messages, maxRetries: maxRetriesArg, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, experimental_include: include, _internal: { generateId }, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, onStepFinish, onFinish, ...settings }: CallSettings & Prompt & {
1436
+ declare function generateText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string>>({ model: modelArg, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries: maxRetriesArg, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, experimental_include: include, _internal: { generateId }, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, onStepFinish, onFinish, ...settings }: CallSettings & Prompt & {
1427
1437
  /**
1428
1438
  * The language model to use.
1429
1439
  */
@@ -2774,6 +2784,7 @@ type StreamTextOnToolCallFinishCallback<TOOLS extends ToolSet = ToolSet> = (even
2774
2784
  * @param system - A system message that will be part of the prompt.
2775
2785
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
2776
2786
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
2787
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
2777
2788
  *
2778
2789
  * @param maxOutputTokens - Maximum number of tokens to generate.
2779
2790
  * @param temperature - Temperature setting.
@@ -2809,7 +2820,7 @@ type StreamTextOnToolCallFinishCallback<TOOLS extends ToolSet = ToolSet> = (even
2809
2820
  * @returns
2810
2821
  * A result object for accessing different stream types and additional information.
2811
2822
  */
2812
- declare function streamText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string, never>>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, prepareStep, providerOptions, experimental_activeTools, activeTools, experimental_repairToolCall: repairToolCall, experimental_transform: transform, experimental_download: download, includeRawChunks, onChunk, onError, onFinish, onAbort, onStepFinish, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, experimental_context, experimental_include: include, _internal: { now, generateId }, ...settings }: CallSettings & Prompt & {
2823
+ declare function streamText<TOOLS extends ToolSet, OUTPUT extends Output = Output<string, string, never>>({ model, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries, abortSignal, timeout, headers, stopWhen, experimental_output, output, experimental_telemetry: telemetry, prepareStep, providerOptions, experimental_activeTools, activeTools, experimental_repairToolCall: repairToolCall, experimental_transform: transform, experimental_download: download, includeRawChunks, onChunk, onError, onFinish, onAbort, onStepFinish, experimental_onStart: onStart, experimental_onStepStart: onStepStart, experimental_onToolCallStart: onToolCallStart, experimental_onToolCallFinish: onToolCallFinish, experimental_context, experimental_include: include, _internal: { now, generateId }, ...settings }: CallSettings & Prompt & {
2813
2824
  /**
2814
2825
  * The language model to use.
2815
2826
  */
@@ -5098,6 +5109,7 @@ type RepairTextFunction = (options: {
5098
5109
  * @param system - A system message that will be part of the prompt.
5099
5110
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
5100
5111
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
5112
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
5101
5113
  *
5102
5114
  * @param maxOutputTokens - Maximum number of tokens to generate.
5103
5115
  * @param temperature - Temperature setting.
@@ -5449,6 +5461,7 @@ type StreamObjectOnFinishCallback<RESULT> = (event: {
5449
5461
  * @param system - A system message that will be part of the prompt.
5450
5462
  * @param prompt - A simple text prompt. You can either use `prompt` or `messages` but not both.
5451
5463
  * @param messages - A list of messages. You can either use `prompt` or `messages` but not both.
5464
+ * @param allowSystemInMessages - Whether system messages are allowed in the `prompt` or `messages` fields. When unset, system messages are allowed with a warning.
5452
5465
  *
5453
5466
  * @param maxOutputTokens - Maximum number of tokens to generate.
5454
5467
  * @param temperature - Temperature setting.
package/dist/index.js CHANGED
@@ -1252,7 +1252,7 @@ var import_provider_utils3 = require("@ai-sdk/provider-utils");
1252
1252
  var import_provider_utils4 = require("@ai-sdk/provider-utils");
1253
1253
 
1254
1254
  // src/version.ts
1255
- var VERSION = true ? "6.0.169" : "0.0.0-test";
1255
+ var VERSION = true ? "6.0.170" : "0.0.0-test";
1256
1256
 
1257
1257
  // src/util/download/download.ts
1258
1258
  var download = async ({
@@ -2144,35 +2144,35 @@ var modelMessageSchema = import_v45.z.union([
2144
2144
  ]);
2145
2145
 
2146
2146
  // src/prompt/standardize-prompt.ts
2147
- async function standardizePrompt(prompt) {
2148
- if (prompt.prompt == null && prompt.messages == null) {
2147
+ async function standardizePrompt({
2148
+ allowSystemInMessages,
2149
+ system,
2150
+ prompt,
2151
+ messages
2152
+ }) {
2153
+ if (prompt == null && messages == null) {
2149
2154
  throw new import_provider24.InvalidPromptError({
2150
2155
  prompt,
2151
2156
  message: "prompt or messages must be defined"
2152
2157
  });
2153
2158
  }
2154
- if (prompt.prompt != null && prompt.messages != null) {
2159
+ if (prompt != null && messages != null) {
2155
2160
  throw new import_provider24.InvalidPromptError({
2156
2161
  prompt,
2157
2162
  message: "prompt and messages cannot be defined at the same time"
2158
2163
  });
2159
2164
  }
2160
- if (prompt.system != null && typeof prompt.system !== "string" && !asArray(prompt.system).every(
2161
- (message) => typeof message === "object" && message !== null && "role" in message && message.role === "system"
2162
- )) {
2165
+ if (typeof system !== "string" && !asArray(system).every((message) => message.role === "system")) {
2163
2166
  throw new import_provider24.InvalidPromptError({
2164
2167
  prompt,
2165
2168
  message: "system must be a string, SystemModelMessage, or array of SystemModelMessage"
2166
2169
  });
2167
2170
  }
2168
- let messages;
2169
- if (prompt.prompt != null && typeof prompt.prompt === "string") {
2170
- messages = [{ role: "user", content: prompt.prompt }];
2171
- } else if (prompt.prompt != null && Array.isArray(prompt.prompt)) {
2172
- messages = prompt.prompt;
2173
- } else if (prompt.messages != null) {
2174
- messages = prompt.messages;
2175
- } else {
2171
+ if (prompt != null && typeof prompt === "string") {
2172
+ messages = [{ role: "user", content: prompt }];
2173
+ } else if (prompt != null && Array.isArray(prompt)) {
2174
+ messages = prompt;
2175
+ } else if (messages == null) {
2176
2176
  throw new import_provider24.InvalidPromptError({
2177
2177
  prompt,
2178
2178
  message: "prompt or messages must be defined"
@@ -2184,6 +2184,19 @@ async function standardizePrompt(prompt) {
2184
2184
  message: "messages must not be empty"
2185
2185
  });
2186
2186
  }
2187
+ if (messages.some((message) => message.role === "system")) {
2188
+ if (allowSystemInMessages === false) {
2189
+ throw new import_provider24.InvalidPromptError({
2190
+ prompt,
2191
+ message: "System messages are not allowed in the prompt or messages fields. Use the system option instead."
2192
+ });
2193
+ }
2194
+ if (allowSystemInMessages === void 0) {
2195
+ console.warn(
2196
+ "AI SDK Warning: System messages in the prompt or messages fields can be a security risk because they may enable prompt injection attacks. Use the system option instead when possible. Set allowSystemInMessages to true to suppress this warning, or false to throw an error."
2197
+ );
2198
+ }
2199
+ }
2187
2200
  const validationResult = await (0, import_provider_utils8.safeValidateTypes)({
2188
2201
  value: messages,
2189
2202
  schema: import_v46.z.array(modelMessageSchema)
@@ -2195,10 +2208,7 @@ async function standardizePrompt(prompt) {
2195
2208
  cause: validationResult.error
2196
2209
  });
2197
2210
  }
2198
- return {
2199
- messages,
2200
- system: prompt.system
2201
- };
2211
+ return { messages, system };
2202
2212
  }
2203
2213
 
2204
2214
  // src/prompt/wrap-gateway-error.ts
@@ -4115,6 +4125,7 @@ async function generateText({
4115
4125
  system,
4116
4126
  prompt,
4117
4127
  messages,
4128
+ allowSystemInMessages,
4118
4129
  maxRetries: maxRetriesArg,
4119
4130
  abortSignal,
4120
4131
  timeout,
@@ -4171,7 +4182,8 @@ async function generateText({
4171
4182
  const initialPrompt = await standardizePrompt({
4172
4183
  system,
4173
4184
  prompt,
4174
- messages
4185
+ messages,
4186
+ allowSystemInMessages
4175
4187
  });
4176
4188
  const globalTelemetry = createGlobalTelemetry(telemetry == null ? void 0 : telemetry.integrations);
4177
4189
  await notify({
@@ -6452,6 +6464,7 @@ function streamText({
6452
6464
  system,
6453
6465
  prompt,
6454
6466
  messages,
6467
+ allowSystemInMessages,
6455
6468
  maxRetries,
6456
6469
  abortSignal,
6457
6470
  timeout,
@@ -6508,6 +6521,7 @@ function streamText({
6508
6521
  system,
6509
6522
  prompt,
6510
6523
  messages,
6524
+ allowSystemInMessages,
6511
6525
  tools,
6512
6526
  toolChoice,
6513
6527
  transforms: asArray(transform),
@@ -6614,6 +6628,7 @@ var DefaultStreamTextResult = class {
6614
6628
  system,
6615
6629
  prompt,
6616
6630
  messages,
6631
+ allowSystemInMessages,
6617
6632
  tools,
6618
6633
  toolChoice,
6619
6634
  transforms,
@@ -7005,7 +7020,8 @@ var DefaultStreamTextResult = class {
7005
7020
  const initialPrompt = await standardizePrompt({
7006
7021
  system,
7007
7022
  prompt,
7008
- messages
7023
+ messages,
7024
+ allowSystemInMessages
7009
7025
  });
7010
7026
  await notify({
7011
7027
  event: {
@@ -10112,6 +10128,7 @@ async function generateObject(options) {
10112
10128
  system,
10113
10129
  prompt,
10114
10130
  messages,
10131
+ allowSystemInMessages,
10115
10132
  maxRetries: maxRetriesArg,
10116
10133
  abortSignal,
10117
10134
  headers,
@@ -10196,7 +10213,8 @@ async function generateObject(options) {
10196
10213
  const standardizedPrompt = await standardizePrompt({
10197
10214
  system,
10198
10215
  prompt,
10199
- messages
10216
+ messages,
10217
+ allowSystemInMessages
10200
10218
  });
10201
10219
  const promptMessages = await convertToLanguageModelPrompt({
10202
10220
  prompt: standardizedPrompt,
@@ -10521,6 +10539,7 @@ function streamObject(options) {
10521
10539
  system,
10522
10540
  prompt,
10523
10541
  messages,
10542
+ allowSystemInMessages,
10524
10543
  maxRetries,
10525
10544
  abortSignal,
10526
10545
  headers,
@@ -10568,6 +10587,7 @@ function streamObject(options) {
10568
10587
  system,
10569
10588
  prompt,
10570
10589
  messages,
10590
+ allowSystemInMessages,
10571
10591
  schemaName,
10572
10592
  schemaDescription,
10573
10593
  providerOptions,
@@ -10592,6 +10612,7 @@ var DefaultStreamObjectResult = class {
10592
10612
  system,
10593
10613
  prompt,
10594
10614
  messages,
10615
+ allowSystemInMessages,
10595
10616
  schemaName,
10596
10617
  schemaDescription,
10597
10618
  providerOptions,
@@ -10662,7 +10683,8 @@ var DefaultStreamObjectResult = class {
10662
10683
  const standardizedPrompt = await standardizePrompt({
10663
10684
  system,
10664
10685
  prompt,
10665
- messages
10686
+ messages,
10687
+ allowSystemInMessages
10666
10688
  });
10667
10689
  const callOptions = {
10668
10690
  responseFormat: {