@ai-sdk/perplexity 4.0.0-beta.5 → 4.0.0-beta.53

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/index.js CHANGED
@@ -1,38 +1,27 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- VERSION: () => VERSION,
24
- createPerplexity: () => createPerplexity,
25
- perplexity: () => perplexity
26
- });
27
- module.exports = __toCommonJS(index_exports);
28
-
29
1
  // src/perplexity-provider.ts
30
- var import_provider2 = require("@ai-sdk/provider");
31
- var import_provider_utils3 = require("@ai-sdk/provider-utils");
2
+ import {
3
+ NoSuchModelError
4
+ } from "@ai-sdk/provider";
5
+ import {
6
+ generateId,
7
+ loadApiKey,
8
+ withoutTrailingSlash,
9
+ withUserAgentSuffix
10
+ } from "@ai-sdk/provider-utils";
32
11
 
33
12
  // src/perplexity-language-model.ts
34
- var import_provider_utils2 = require("@ai-sdk/provider-utils");
35
- var import_v4 = require("zod/v4");
13
+ import {
14
+ combineHeaders,
15
+ createEventSourceResponseHandler,
16
+ createJsonErrorResponseHandler,
17
+ createJsonResponseHandler,
18
+ isCustomReasoning,
19
+ postJsonToApi,
20
+ serializeModelOptions,
21
+ WORKFLOW_SERIALIZE,
22
+ WORKFLOW_DESERIALIZE
23
+ } from "@ai-sdk/provider-utils";
24
+ import { z } from "zod/v4";
36
25
 
37
26
  // src/convert-perplexity-usage.ts
38
27
  function convertPerplexityUsage(usage) {
@@ -73,8 +62,14 @@ function convertPerplexityUsage(usage) {
73
62
  }
74
63
 
75
64
  // src/convert-to-perplexity-messages.ts
76
- var import_provider = require("@ai-sdk/provider");
77
- var import_provider_utils = require("@ai-sdk/provider-utils");
65
+ import {
66
+ UnsupportedFunctionalityError
67
+ } from "@ai-sdk/provider";
68
+ import {
69
+ convertUint8ArrayToBase64,
70
+ getTopLevelMediaType,
71
+ resolveFullMediaType
72
+ } from "@ai-sdk/provider-utils";
78
73
  function convertToPerplexityMessages(prompt) {
79
74
  const messages = [];
80
75
  for (const { role, content } of prompt) {
@@ -86,10 +81,9 @@ function convertToPerplexityMessages(prompt) {
86
81
  case "user":
87
82
  case "assistant": {
88
83
  const hasMultipartContent = content.some(
89
- (part) => part.type === "file" && part.mediaType.startsWith("image/") || part.type === "file" && part.mediaType === "application/pdf"
84
+ (part) => part.type === "file" && getTopLevelMediaType(part.mediaType) === "image" || part.type === "file" && getTopLevelMediaType(part.mediaType) === "application"
90
85
  );
91
86
  const messageContent = content.map((part, index) => {
92
- var _a;
93
87
  switch (part.type) {
94
88
  case "text": {
95
89
  return {
@@ -98,32 +92,48 @@ function convertToPerplexityMessages(prompt) {
98
92
  };
99
93
  }
100
94
  case "file": {
101
- if (part.mediaType === "application/pdf") {
102
- return part.data instanceof URL ? {
103
- type: "file_url",
104
- file_url: {
105
- url: part.data.toString()
106
- },
107
- file_name: part.filename
108
- } : {
109
- type: "file_url",
110
- file_url: {
111
- url: typeof part.data === "string" ? part.data : (0, import_provider_utils.convertUint8ArrayToBase64)(part.data)
112
- },
113
- file_name: part.filename || `document-${index}.pdf`
114
- };
115
- } else if (part.mediaType.startsWith("image/")) {
116
- return part.data instanceof URL ? {
117
- type: "image_url",
118
- image_url: {
119
- url: part.data.toString()
95
+ switch (part.data.type) {
96
+ case "reference": {
97
+ throw new UnsupportedFunctionalityError({
98
+ functionality: "file parts with provider references"
99
+ });
100
+ }
101
+ case "text": {
102
+ throw new UnsupportedFunctionalityError({
103
+ functionality: "text file parts"
104
+ });
105
+ }
106
+ case "url":
107
+ case "data": {
108
+ if (part.mediaType === "application/pdf") {
109
+ return part.data.type === "url" ? {
110
+ type: "file_url",
111
+ file_url: {
112
+ url: part.data.url.toString()
113
+ },
114
+ file_name: part.filename
115
+ } : {
116
+ type: "file_url",
117
+ file_url: {
118
+ url: typeof part.data.data === "string" ? part.data.data : convertUint8ArrayToBase64(part.data.data)
119
+ },
120
+ file_name: part.filename || `document-${index}.pdf`
121
+ };
122
+ } else if (getTopLevelMediaType(part.mediaType) === "image") {
123
+ return part.data.type === "url" ? {
124
+ type: "image_url",
125
+ image_url: {
126
+ url: part.data.url.toString()
127
+ }
128
+ } : {
129
+ type: "image_url",
130
+ image_url: {
131
+ url: `data:${resolveFullMediaType({ part })};base64,${typeof part.data.data === "string" ? part.data.data : convertUint8ArrayToBase64(part.data.data)}`
132
+ }
133
+ };
120
134
  }
121
- } : {
122
- type: "image_url",
123
- image_url: {
124
- url: `data:${(_a = part.mediaType) != null ? _a : "image/jpeg"};base64,${typeof part.data === "string" ? part.data : (0, import_provider_utils.convertUint8ArrayToBase64)(part.data)}`
125
- }
126
- };
135
+ return void 0;
136
+ }
127
137
  }
128
138
  }
129
139
  }
@@ -135,7 +145,7 @@ function convertToPerplexityMessages(prompt) {
135
145
  break;
136
146
  }
137
147
  case "tool": {
138
- throw new import_provider.UnsupportedFunctionalityError({
148
+ throw new UnsupportedFunctionalityError({
139
149
  functionality: "Tool messages"
140
150
  });
141
151
  }
@@ -160,7 +170,7 @@ function mapPerplexityFinishReason(finishReason) {
160
170
  }
161
171
 
162
172
  // src/perplexity-language-model.ts
163
- var PerplexityLanguageModel = class {
173
+ var PerplexityLanguageModel = class _PerplexityLanguageModel {
164
174
  constructor(modelId, config) {
165
175
  this.specificationVersion = "v4";
166
176
  this.provider = "perplexity";
@@ -170,6 +180,15 @@ var PerplexityLanguageModel = class {
170
180
  this.modelId = modelId;
171
181
  this.config = config;
172
182
  }
183
+ static [WORKFLOW_SERIALIZE](model) {
184
+ return serializeModelOptions({
185
+ modelId: model.modelId,
186
+ config: model.config
187
+ });
188
+ }
189
+ static [WORKFLOW_DESERIALIZE](options) {
190
+ return new _PerplexityLanguageModel(options.modelId, options.config);
191
+ }
173
192
  getArgs({
174
193
  prompt,
175
194
  maxOutputTokens,
@@ -179,6 +198,7 @@ var PerplexityLanguageModel = class {
179
198
  frequencyPenalty,
180
199
  presencePenalty,
181
200
  stopSequences,
201
+ reasoning,
182
202
  responseFormat,
183
203
  seed,
184
204
  providerOptions
@@ -194,6 +214,13 @@ var PerplexityLanguageModel = class {
194
214
  if (seed != null) {
195
215
  warnings.push({ type: "unsupported", feature: "seed" });
196
216
  }
217
+ if (isCustomReasoning(reasoning)) {
218
+ warnings.push({
219
+ type: "unsupported",
220
+ feature: "reasoning",
221
+ details: "This provider does not support reasoning configuration."
222
+ });
223
+ }
197
224
  return {
198
225
  args: {
199
226
  // model id:
@@ -219,21 +246,21 @@ var PerplexityLanguageModel = class {
219
246
  };
220
247
  }
221
248
  async doGenerate(options) {
222
- var _a, _b, _c, _d, _e, _f, _g;
249
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
223
250
  const { args: body, warnings } = this.getArgs(options);
224
251
  const {
225
252
  responseHeaders,
226
253
  value: response,
227
254
  rawValue: rawResponse
228
- } = await (0, import_provider_utils2.postJsonToApi)({
255
+ } = await postJsonToApi({
229
256
  url: `${this.config.baseURL}/chat/completions`,
230
- headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
257
+ headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
231
258
  body,
232
- failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
259
+ failedResponseHandler: createJsonErrorResponseHandler({
233
260
  errorSchema: perplexityErrorSchema,
234
261
  errorToMessage
235
262
  }),
236
- successfulResponseHandler: (0, import_provider_utils2.createJsonResponseHandler)(
263
+ successfulResponseHandler: createJsonResponseHandler(
237
264
  perplexityResponseSchema
238
265
  ),
239
266
  abortSignal: options.abortSignal,
@@ -259,7 +286,7 @@ var PerplexityLanguageModel = class {
259
286
  content,
260
287
  finishReason: {
261
288
  unified: mapPerplexityFinishReason(choice.finish_reason),
262
- raw: (_a = choice.finish_reason) != null ? _a : void 0
289
+ raw: (_c = choice.finish_reason) != null ? _c : void 0
263
290
  },
264
291
  usage: convertPerplexityUsage(response.usage),
265
292
  request: { body },
@@ -271,32 +298,39 @@ var PerplexityLanguageModel = class {
271
298
  warnings,
272
299
  providerMetadata: {
273
300
  perplexity: {
274
- images: (_c = (_b = response.images) == null ? void 0 : _b.map((image) => ({
301
+ images: (_e = (_d = response.images) == null ? void 0 : _d.map((image) => ({
275
302
  imageUrl: image.image_url,
276
303
  originUrl: image.origin_url,
277
304
  height: image.height,
278
305
  width: image.width
279
- }))) != null ? _c : null,
306
+ }))) != null ? _e : null,
280
307
  usage: {
281
- citationTokens: (_e = (_d = response.usage) == null ? void 0 : _d.citation_tokens) != null ? _e : null,
282
- numSearchQueries: (_g = (_f = response.usage) == null ? void 0 : _f.num_search_queries) != null ? _g : null
283
- }
308
+ citationTokens: (_g = (_f = response.usage) == null ? void 0 : _f.citation_tokens) != null ? _g : null,
309
+ numSearchQueries: (_i = (_h = response.usage) == null ? void 0 : _h.num_search_queries) != null ? _i : null
310
+ },
311
+ cost: ((_j = response.usage) == null ? void 0 : _j.cost) ? {
312
+ inputTokensCost: (_k = response.usage.cost.input_tokens_cost) != null ? _k : null,
313
+ outputTokensCost: (_l = response.usage.cost.output_tokens_cost) != null ? _l : null,
314
+ requestCost: (_m = response.usage.cost.request_cost) != null ? _m : null,
315
+ totalCost: (_n = response.usage.cost.total_cost) != null ? _n : null
316
+ } : null
284
317
  }
285
318
  }
286
319
  };
287
320
  }
288
321
  async doStream(options) {
322
+ var _a, _b;
289
323
  const { args, warnings } = this.getArgs(options);
290
324
  const body = { ...args, stream: true };
291
- const { responseHeaders, value: response } = await (0, import_provider_utils2.postJsonToApi)({
325
+ const { responseHeaders, value: response } = await postJsonToApi({
292
326
  url: `${this.config.baseURL}/chat/completions`,
293
- headers: (0, import_provider_utils2.combineHeaders)(this.config.headers(), options.headers),
327
+ headers: combineHeaders((_b = (_a = this.config).headers) == null ? void 0 : _b.call(_a), options.headers),
294
328
  body,
295
- failedResponseHandler: (0, import_provider_utils2.createJsonErrorResponseHandler)({
329
+ failedResponseHandler: createJsonErrorResponseHandler({
296
330
  errorSchema: perplexityErrorSchema,
297
331
  errorToMessage
298
332
  }),
299
- successfulResponseHandler: (0, import_provider_utils2.createEventSourceResponseHandler)(
333
+ successfulResponseHandler: createEventSourceResponseHandler(
300
334
  perplexityChunkSchema
301
335
  ),
302
336
  abortSignal: options.abortSignal,
@@ -313,6 +347,7 @@ var PerplexityLanguageModel = class {
313
347
  citationTokens: null,
314
348
  numSearchQueries: null
315
349
  },
350
+ cost: null,
316
351
  images: null
317
352
  }
318
353
  };
@@ -326,7 +361,7 @@ var PerplexityLanguageModel = class {
326
361
  controller.enqueue({ type: "stream-start", warnings });
327
362
  },
328
363
  transform(chunk, controller) {
329
- var _a, _b, _c;
364
+ var _a2, _b2, _c, _d, _e, _f, _g;
330
365
  if (options.includeRawChunks) {
331
366
  controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
332
367
  }
@@ -340,7 +375,7 @@ var PerplexityLanguageModel = class {
340
375
  type: "response-metadata",
341
376
  ...getResponseMetadata(value)
342
377
  });
343
- (_a = value.citations) == null ? void 0 : _a.forEach((url) => {
378
+ (_a2 = value.citations) == null ? void 0 : _a2.forEach((url) => {
344
379
  controller.enqueue({
345
380
  type: "source",
346
381
  sourceType: "url",
@@ -353,9 +388,15 @@ var PerplexityLanguageModel = class {
353
388
  if (value.usage != null) {
354
389
  usage = value.usage;
355
390
  providerMetadata.perplexity.usage = {
356
- citationTokens: (_b = value.usage.citation_tokens) != null ? _b : null,
391
+ citationTokens: (_b2 = value.usage.citation_tokens) != null ? _b2 : null,
357
392
  numSearchQueries: (_c = value.usage.num_search_queries) != null ? _c : null
358
393
  };
394
+ providerMetadata.perplexity.cost = value.usage.cost ? {
395
+ inputTokensCost: (_d = value.usage.cost.input_tokens_cost) != null ? _d : null,
396
+ outputTokensCost: (_e = value.usage.cost.output_tokens_cost) != null ? _e : null,
397
+ requestCost: (_f = value.usage.cost.request_cost) != null ? _f : null,
398
+ totalCost: (_g = value.usage.cost.total_cost) != null ? _g : null
399
+ } : null;
359
400
  }
360
401
  if (value.images != null) {
361
402
  providerMetadata.perplexity.images = value.images.map((image) => ({
@@ -418,59 +459,66 @@ function getResponseMetadata({
418
459
  timestamp: new Date(created * 1e3)
419
460
  };
420
461
  }
421
- var perplexityUsageSchema = import_v4.z.object({
422
- prompt_tokens: import_v4.z.number(),
423
- completion_tokens: import_v4.z.number(),
424
- total_tokens: import_v4.z.number().nullish(),
425
- citation_tokens: import_v4.z.number().nullish(),
426
- num_search_queries: import_v4.z.number().nullish(),
427
- reasoning_tokens: import_v4.z.number().nullish()
462
+ var perplexityCostSchema = z.object({
463
+ input_tokens_cost: z.number().nullish(),
464
+ output_tokens_cost: z.number().nullish(),
465
+ request_cost: z.number().nullish(),
466
+ total_cost: z.number().nullish()
467
+ });
468
+ var perplexityUsageSchema = z.object({
469
+ prompt_tokens: z.number(),
470
+ completion_tokens: z.number(),
471
+ total_tokens: z.number().nullish(),
472
+ citation_tokens: z.number().nullish(),
473
+ num_search_queries: z.number().nullish(),
474
+ reasoning_tokens: z.number().nullish(),
475
+ cost: perplexityCostSchema.nullish()
428
476
  });
429
- var perplexityImageSchema = import_v4.z.object({
430
- image_url: import_v4.z.string(),
431
- origin_url: import_v4.z.string(),
432
- height: import_v4.z.number(),
433
- width: import_v4.z.number()
477
+ var perplexityImageSchema = z.object({
478
+ image_url: z.string(),
479
+ origin_url: z.string(),
480
+ height: z.number(),
481
+ width: z.number()
434
482
  });
435
- var perplexityResponseSchema = import_v4.z.object({
436
- id: import_v4.z.string(),
437
- created: import_v4.z.number(),
438
- model: import_v4.z.string(),
439
- choices: import_v4.z.array(
440
- import_v4.z.object({
441
- message: import_v4.z.object({
442
- role: import_v4.z.literal("assistant"),
443
- content: import_v4.z.string()
483
+ var perplexityResponseSchema = z.object({
484
+ id: z.string(),
485
+ created: z.number(),
486
+ model: z.string(),
487
+ choices: z.array(
488
+ z.object({
489
+ message: z.object({
490
+ role: z.literal("assistant"),
491
+ content: z.string()
444
492
  }),
445
- finish_reason: import_v4.z.string().nullish()
493
+ finish_reason: z.string().nullish()
446
494
  })
447
495
  ),
448
- citations: import_v4.z.array(import_v4.z.string()).nullish(),
449
- images: import_v4.z.array(perplexityImageSchema).nullish(),
496
+ citations: z.array(z.string()).nullish(),
497
+ images: z.array(perplexityImageSchema).nullish(),
450
498
  usage: perplexityUsageSchema.nullish()
451
499
  });
452
- var perplexityChunkSchema = import_v4.z.object({
453
- id: import_v4.z.string(),
454
- created: import_v4.z.number(),
455
- model: import_v4.z.string(),
456
- choices: import_v4.z.array(
457
- import_v4.z.object({
458
- delta: import_v4.z.object({
459
- role: import_v4.z.literal("assistant"),
460
- content: import_v4.z.string()
500
+ var perplexityChunkSchema = z.object({
501
+ id: z.string(),
502
+ created: z.number(),
503
+ model: z.string(),
504
+ choices: z.array(
505
+ z.object({
506
+ delta: z.object({
507
+ role: z.literal("assistant"),
508
+ content: z.string()
461
509
  }),
462
- finish_reason: import_v4.z.string().nullish()
510
+ finish_reason: z.string().nullish()
463
511
  })
464
512
  ),
465
- citations: import_v4.z.array(import_v4.z.string()).nullish(),
466
- images: import_v4.z.array(perplexityImageSchema).nullish(),
513
+ citations: z.array(z.string()).nullish(),
514
+ images: z.array(perplexityImageSchema).nullish(),
467
515
  usage: perplexityUsageSchema.nullish()
468
516
  });
469
- var perplexityErrorSchema = import_v4.z.object({
470
- error: import_v4.z.object({
471
- code: import_v4.z.number(),
472
- message: import_v4.z.string().nullish(),
473
- type: import_v4.z.string().nullish()
517
+ var perplexityErrorSchema = z.object({
518
+ error: z.object({
519
+ code: z.number(),
520
+ message: z.string().nullish(),
521
+ type: z.string().nullish()
474
522
  })
475
523
  });
476
524
  var errorToMessage = (data) => {
@@ -479,13 +527,13 @@ var errorToMessage = (data) => {
479
527
  };
480
528
 
481
529
  // src/version.ts
482
- var VERSION = true ? "4.0.0-beta.5" : "0.0.0-test";
530
+ var VERSION = true ? "4.0.0-beta.53" : "0.0.0-test";
483
531
 
484
532
  // src/perplexity-provider.ts
485
533
  function createPerplexity(options = {}) {
486
- const getHeaders = () => (0, import_provider_utils3.withUserAgentSuffix)(
534
+ const getHeaders = () => withUserAgentSuffix(
487
535
  {
488
- Authorization: `Bearer ${(0, import_provider_utils3.loadApiKey)({
536
+ Authorization: `Bearer ${loadApiKey({
489
537
  apiKey: options.apiKey,
490
538
  environmentVariableName: "PERPLEXITY_API_KEY",
491
539
  description: "Perplexity"
@@ -497,11 +545,11 @@ function createPerplexity(options = {}) {
497
545
  const createLanguageModel = (modelId) => {
498
546
  var _a;
499
547
  return new PerplexityLanguageModel(modelId, {
500
- baseURL: (0, import_provider_utils3.withoutTrailingSlash)(
548
+ baseURL: withoutTrailingSlash(
501
549
  (_a = options.baseURL) != null ? _a : "https://api.perplexity.ai"
502
550
  ),
503
551
  headers: getHeaders,
504
- generateId: import_provider_utils3.generateId,
552
+ generateId,
505
553
  fetch: options.fetch
506
554
  });
507
555
  };
@@ -509,19 +557,18 @@ function createPerplexity(options = {}) {
509
557
  provider.specificationVersion = "v4";
510
558
  provider.languageModel = createLanguageModel;
511
559
  provider.embeddingModel = (modelId) => {
512
- throw new import_provider2.NoSuchModelError({ modelId, modelType: "embeddingModel" });
560
+ throw new NoSuchModelError({ modelId, modelType: "embeddingModel" });
513
561
  };
514
562
  provider.textEmbeddingModel = provider.embeddingModel;
515
563
  provider.imageModel = (modelId) => {
516
- throw new import_provider2.NoSuchModelError({ modelId, modelType: "imageModel" });
564
+ throw new NoSuchModelError({ modelId, modelType: "imageModel" });
517
565
  };
518
566
  return provider;
519
567
  }
520
568
  var perplexity = createPerplexity();
521
- // Annotate the CommonJS export names for ESM import in node:
522
- 0 && (module.exports = {
569
+ export {
523
570
  VERSION,
524
571
  createPerplexity,
525
572
  perplexity
526
- });
573
+ };
527
574
  //# sourceMappingURL=index.js.map