ai 5.0.183 → 5.0.184

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
+ ## 5.0.184
4
+
5
+ ### Patch Changes
6
+
7
+ - cb911d2: fix(ai): add allowSystemInMessages option and warn by default when system messages are found in prompt or messages
8
+
3
9
  ## 5.0.183
4
10
 
5
11
  ### Patch Changes
package/dist/index.d.mts CHANGED
@@ -116,6 +116,15 @@ type Prompt = {
116
116
  System message to include in the prompt. Can be used with `prompt` or `messages`.
117
117
  */
118
118
  system?: string;
119
+ /**
120
+ * Whether system messages are allowed in the `prompt` or `messages` fields.
121
+ *
122
+ * When disabled, system messages must be provided through the `system`
123
+ * option. When unset, system messages are allowed with a warning.
124
+ *
125
+ * @default undefined
126
+ */
127
+ allowSystemInMessages?: boolean;
119
128
  } & ({
120
129
  /**
121
130
  A prompt. It can be either a text prompt or a list of messages.
@@ -1090,7 +1099,7 @@ If set and supported by the model, calls will generate deterministic results.
1090
1099
  @returns
1091
1100
  A result object that contains the generated text, the results of the tool calls, and additional information.
1092
1101
  */
1093
- declare function generateText<TOOLS extends ToolSet, OUTPUT = never, OUTPUT_PARTIAL = never>({ model: modelArg, tools, toolChoice, system, prompt, messages, maxRetries: maxRetriesArg, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
1102
+ declare function generateText<TOOLS extends ToolSet, OUTPUT = never, OUTPUT_PARTIAL = never>({ model: modelArg, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries: maxRetriesArg, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
1094
1103
  /**
1095
1104
  The language model to use.
1096
1105
  */
@@ -1329,7 +1338,7 @@ If set and supported by the model, calls will generate deterministic results.
1329
1338
  @return
1330
1339
  A result object for accessing different stream types and additional information.
1331
1340
  */
1332
- declare function streamText<TOOLS extends ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, 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_context, _internal: { now, generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
1341
+ declare function streamText<TOOLS extends ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never>({ model, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries, abortSignal, 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_context, _internal: { now, generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
1333
1342
  /**
1334
1343
  The language model to use.
1335
1344
  */
package/dist/index.d.ts CHANGED
@@ -116,6 +116,15 @@ type Prompt = {
116
116
  System message to include in the prompt. Can be used with `prompt` or `messages`.
117
117
  */
118
118
  system?: string;
119
+ /**
120
+ * Whether system messages are allowed in the `prompt` or `messages` fields.
121
+ *
122
+ * When disabled, system messages must be provided through the `system`
123
+ * option. When unset, system messages are allowed with a warning.
124
+ *
125
+ * @default undefined
126
+ */
127
+ allowSystemInMessages?: boolean;
119
128
  } & ({
120
129
  /**
121
130
  A prompt. It can be either a text prompt or a list of messages.
@@ -1090,7 +1099,7 @@ If set and supported by the model, calls will generate deterministic results.
1090
1099
  @returns
1091
1100
  A result object that contains the generated text, the results of the tool calls, and additional information.
1092
1101
  */
1093
- declare function generateText<TOOLS extends ToolSet, OUTPUT = never, OUTPUT_PARTIAL = never>({ model: modelArg, tools, toolChoice, system, prompt, messages, maxRetries: maxRetriesArg, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
1102
+ declare function generateText<TOOLS extends ToolSet, OUTPUT = never, OUTPUT_PARTIAL = never>({ model: modelArg, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries: maxRetriesArg, abortSignal, headers, stopWhen, experimental_output: output, experimental_telemetry: telemetry, providerOptions, experimental_activeTools, activeTools, experimental_prepareStep, prepareStep, experimental_repairToolCall: repairToolCall, experimental_download: download, experimental_context, _internal: { generateId, currentDate, }, onStepFinish, ...settings }: CallSettings & Prompt & {
1094
1103
  /**
1095
1104
  The language model to use.
1096
1105
  */
@@ -1329,7 +1338,7 @@ If set and supported by the model, calls will generate deterministic results.
1329
1338
  @return
1330
1339
  A result object for accessing different stream types and additional information.
1331
1340
  */
1332
- declare function streamText<TOOLS extends ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never>({ model, tools, toolChoice, system, prompt, messages, maxRetries, abortSignal, 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_context, _internal: { now, generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
1341
+ declare function streamText<TOOLS extends ToolSet, OUTPUT = never, PARTIAL_OUTPUT = never>({ model, tools, toolChoice, system, prompt, messages, allowSystemInMessages, maxRetries, abortSignal, 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_context, _internal: { now, generateId, currentDate, }, ...settings }: CallSettings & Prompt & {
1333
1342
  /**
1334
1343
  The language model to use.
1335
1344
  */
package/dist/index.js CHANGED
@@ -778,7 +778,7 @@ function detectMediaType({
778
778
  var import_provider_utils2 = require("@ai-sdk/provider-utils");
779
779
 
780
780
  // src/version.ts
781
- var VERSION = true ? "5.0.183" : "0.0.0-test";
781
+ var VERSION = true ? "5.0.184" : "0.0.0-test";
782
782
 
783
783
  // src/util/download/download.ts
784
784
  var download = async ({
@@ -1452,33 +1452,35 @@ var modelMessageSchema = import_v45.z.union([
1452
1452
  var coreMessageSchema = modelMessageSchema;
1453
1453
 
1454
1454
  // src/prompt/standardize-prompt.ts
1455
- async function standardizePrompt(prompt) {
1456
- if (prompt.prompt == null && prompt.messages == null) {
1455
+ async function standardizePrompt({
1456
+ allowSystemInMessages,
1457
+ system,
1458
+ prompt,
1459
+ messages
1460
+ }) {
1461
+ if (prompt == null && messages == null) {
1457
1462
  throw new import_provider19.InvalidPromptError({
1458
1463
  prompt,
1459
1464
  message: "prompt or messages must be defined"
1460
1465
  });
1461
1466
  }
1462
- if (prompt.prompt != null && prompt.messages != null) {
1467
+ if (prompt != null && messages != null) {
1463
1468
  throw new import_provider19.InvalidPromptError({
1464
1469
  prompt,
1465
1470
  message: "prompt and messages cannot be defined at the same time"
1466
1471
  });
1467
1472
  }
1468
- if (prompt.system != null && typeof prompt.system !== "string") {
1473
+ if (system != null && typeof system !== "string") {
1469
1474
  throw new import_provider19.InvalidPromptError({
1470
1475
  prompt,
1471
1476
  message: "system must be a string"
1472
1477
  });
1473
1478
  }
1474
- let messages;
1475
- if (prompt.prompt != null && typeof prompt.prompt === "string") {
1476
- messages = [{ role: "user", content: prompt.prompt }];
1477
- } else if (prompt.prompt != null && Array.isArray(prompt.prompt)) {
1478
- messages = prompt.prompt;
1479
- } else if (prompt.messages != null) {
1480
- messages = prompt.messages;
1481
- } else {
1479
+ if (prompt != null && typeof prompt === "string") {
1480
+ messages = [{ role: "user", content: prompt }];
1481
+ } else if (prompt != null && Array.isArray(prompt)) {
1482
+ messages = prompt;
1483
+ } else if (messages == null) {
1482
1484
  throw new import_provider19.InvalidPromptError({
1483
1485
  prompt,
1484
1486
  message: "prompt or messages must be defined"
@@ -1490,6 +1492,19 @@ async function standardizePrompt(prompt) {
1490
1492
  message: "messages must not be empty"
1491
1493
  });
1492
1494
  }
1495
+ if (messages.some((message) => message.role === "system")) {
1496
+ if (allowSystemInMessages === false) {
1497
+ throw new import_provider19.InvalidPromptError({
1498
+ prompt,
1499
+ message: "System messages are not allowed in the prompt or messages fields. Use the system option instead."
1500
+ });
1501
+ }
1502
+ if (allowSystemInMessages === void 0) {
1503
+ console.warn(
1504
+ "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."
1505
+ );
1506
+ }
1507
+ }
1493
1508
  const validationResult = await (0, import_provider_utils6.safeValidateTypes)({
1494
1509
  value: messages,
1495
1510
  schema: import_v46.z.array(modelMessageSchema)
@@ -1501,10 +1516,7 @@ async function standardizePrompt(prompt) {
1501
1516
  cause: validationResult.error
1502
1517
  });
1503
1518
  }
1504
- return {
1505
- messages,
1506
- system: prompt.system
1507
- };
1519
+ return { messages, system };
1508
1520
  }
1509
1521
 
1510
1522
  // src/prompt/wrap-gateway-error.ts
@@ -2247,6 +2259,7 @@ async function generateText({
2247
2259
  system,
2248
2260
  prompt,
2249
2261
  messages,
2262
+ allowSystemInMessages,
2250
2263
  maxRetries: maxRetriesArg,
2251
2264
  abortSignal,
2252
2265
  headers,
@@ -2288,7 +2301,8 @@ async function generateText({
2288
2301
  const initialPrompt = await standardizePrompt({
2289
2302
  system,
2290
2303
  prompt,
2291
- messages
2304
+ messages,
2305
+ allowSystemInMessages
2292
2306
  });
2293
2307
  const tracer = getTracer(telemetry);
2294
2308
  try {
@@ -4535,6 +4549,7 @@ function streamText({
4535
4549
  system,
4536
4550
  prompt,
4537
4551
  messages,
4552
+ allowSystemInMessages,
4538
4553
  maxRetries,
4539
4554
  abortSignal,
4540
4555
  headers,
@@ -4574,6 +4589,7 @@ function streamText({
4574
4589
  system,
4575
4590
  prompt,
4576
4591
  messages,
4592
+ allowSystemInMessages,
4577
4593
  tools,
4578
4594
  toolChoice,
4579
4595
  transforms: asArray(transform),
@@ -4672,6 +4688,7 @@ var DefaultStreamTextResult = class {
4672
4688
  system,
4673
4689
  prompt,
4674
4690
  messages,
4691
+ allowSystemInMessages,
4675
4692
  tools,
4676
4693
  toolChoice,
4677
4694
  transforms,
@@ -5009,7 +5026,8 @@ var DefaultStreamTextResult = class {
5009
5026
  const initialPrompt = await standardizePrompt({
5010
5027
  system,
5011
5028
  prompt,
5012
- messages
5029
+ messages,
5030
+ allowSystemInMessages
5013
5031
  });
5014
5032
  const stepInputMessages = [
5015
5033
  ...initialPrompt.messages,
@@ -7030,6 +7048,7 @@ async function generateObject(options) {
7030
7048
  system,
7031
7049
  prompt,
7032
7050
  messages,
7051
+ allowSystemInMessages,
7033
7052
  maxRetries: maxRetriesArg,
7034
7053
  abortSignal,
7035
7054
  headers,
@@ -7113,7 +7132,8 @@ async function generateObject(options) {
7113
7132
  const standardizedPrompt = await standardizePrompt({
7114
7133
  system,
7115
7134
  prompt,
7116
- messages
7135
+ messages,
7136
+ allowSystemInMessages
7117
7137
  });
7118
7138
  const promptMessages = await convertToLanguageModelPrompt({
7119
7139
  prompt: standardizedPrompt,
@@ -7432,6 +7452,7 @@ function streamObject(options) {
7432
7452
  system,
7433
7453
  prompt,
7434
7454
  messages,
7455
+ allowSystemInMessages,
7435
7456
  maxRetries,
7436
7457
  abortSignal,
7437
7458
  headers,
@@ -7479,6 +7500,7 @@ function streamObject(options) {
7479
7500
  system,
7480
7501
  prompt,
7481
7502
  messages,
7503
+ allowSystemInMessages,
7482
7504
  schemaName,
7483
7505
  schemaDescription,
7484
7506
  providerOptions,
@@ -7503,6 +7525,7 @@ var DefaultStreamObjectResult = class {
7503
7525
  system,
7504
7526
  prompt,
7505
7527
  messages,
7528
+ allowSystemInMessages,
7506
7529
  schemaName,
7507
7530
  schemaDescription,
7508
7531
  providerOptions,
@@ -7571,7 +7594,8 @@ var DefaultStreamObjectResult = class {
7571
7594
  const standardizedPrompt = await standardizePrompt({
7572
7595
  system,
7573
7596
  prompt,
7574
- messages
7597
+ messages,
7598
+ allowSystemInMessages
7575
7599
  });
7576
7600
  const callOptions = {
7577
7601
  responseFormat: {