@ai-sdk/perplexity 3.1.6 → 3.1.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.
@@ -1,124 +1,139 @@
1
+ import {
2
+ lazySchema,
3
+ zodSchema,
4
+ type InferSchema,
5
+ } from '@ai-sdk/provider-utils';
1
6
  import { z } from 'zod/v4';
2
7
 
3
- const userLocationSchema = z.looseObject({
4
- latitude: z.number().optional(),
5
- longitude: z.number().optional(),
6
- country: z.string().optional(),
7
- city: z.string().optional(),
8
- region: z.string().optional(),
9
- });
10
-
11
- const webSearchFiltersSchema = z.looseObject({
12
- search_domain_filter: z.array(z.string()).optional(),
13
- search_recency_filter: z
14
- .enum(['hour', 'day', 'week', 'month', 'year'])
15
- .optional(),
16
- search_after_date_filter: z.string().optional(),
17
- search_before_date_filter: z.string().optional(),
18
- last_updated_after_filter: z.string().optional(),
19
- last_updated_before_filter: z.string().optional(),
20
- });
21
-
22
- const webSearchToolSchema = z.looseObject({
23
- type: z.literal('web_search'),
24
- filters: webSearchFiltersSchema.optional(),
25
- max_results: z.number().int().positive().optional(),
26
- max_tokens: z.number().optional(),
27
- max_tokens_per_page: z.number().optional(),
28
- search_context_size: z.enum(['low', 'medium', 'high']).optional(),
29
- user_location: userLocationSchema.optional(),
30
- });
31
-
32
- const nativeToolSchema = z.union([
33
- webSearchToolSchema,
8
+ const userLocationSchema = () =>
34
9
  z.looseObject({
35
- type: z.literal('fetch_url'),
36
- max_urls: z.number().optional(),
37
- }),
38
- z.looseObject({ type: z.literal('people_search') }),
39
- z.looseObject({ type: z.literal('finance_search') }),
40
- z.looseObject({ type: z.literal('sandbox') }),
10
+ latitude: z.number().optional(),
11
+ longitude: z.number().optional(),
12
+ country: z.string().optional(),
13
+ city: z.string().optional(),
14
+ region: z.string().optional(),
15
+ });
16
+
17
+ const webSearchFiltersSchema = () =>
41
18
  z.looseObject({
42
- type: z.literal('mcp'),
43
- server_label: z.string(),
44
- server_url: z.string(),
45
- allowed_tools: z.array(z.string()).optional(),
46
- authorization: z.string().optional(),
47
- defer_loading: z.boolean().optional(),
48
- headers: z.record(z.string(), z.string()).optional(),
49
- }),
19
+ search_domain_filter: z.array(z.string()).optional(),
20
+ search_recency_filter: z
21
+ .enum(['hour', 'day', 'week', 'month', 'year'])
22
+ .optional(),
23
+ search_after_date_filter: z.string().optional(),
24
+ search_before_date_filter: z.string().optional(),
25
+ last_updated_after_filter: z.string().optional(),
26
+ last_updated_before_filter: z.string().optional(),
27
+ });
28
+
29
+ const webSearchToolSchema = () =>
50
30
  z.looseObject({
51
- type: z.literal('connector'),
52
- id: z.string(),
53
- server_label: z.string(),
54
- server_description: z.string().optional(),
55
- allowed_tools: z.array(z.string()).optional(),
56
- }),
57
- ]);
31
+ type: z.literal('web_search'),
32
+ filters: webSearchFiltersSchema().optional(),
33
+ max_results: z.number().int().positive().optional(),
34
+ max_tokens: z.number().optional(),
35
+ max_tokens_per_page: z.number().optional(),
36
+ search_context_size: z.enum(['low', 'medium', 'high']).optional(),
37
+ user_location: userLocationSchema().optional(),
38
+ });
39
+
40
+ const nativeToolSchema = () =>
41
+ z.union([
42
+ webSearchToolSchema(),
43
+ z.looseObject({
44
+ type: z.literal('fetch_url'),
45
+ max_urls: z.number().optional(),
46
+ }),
47
+ z.looseObject({ type: z.literal('people_search') }),
48
+ z.looseObject({ type: z.literal('finance_search') }),
49
+ z.looseObject({ type: z.literal('sandbox') }),
50
+ z.looseObject({
51
+ type: z.literal('mcp'),
52
+ server_label: z.string(),
53
+ server_url: z.string(),
54
+ allowed_tools: z.array(z.string()).optional(),
55
+ authorization: z.string().optional(),
56
+ defer_loading: z.boolean().optional(),
57
+ headers: z.record(z.string(), z.string()).optional(),
58
+ }),
59
+ z.looseObject({
60
+ type: z.literal('connector'),
61
+ id: z.string(),
62
+ server_label: z.string(),
63
+ server_description: z.string().optional(),
64
+ allowed_tools: z.array(z.string()).optional(),
65
+ }),
66
+ ]);
58
67
 
59
- export const perplexityLanguageModelOptions = z.looseObject({
60
- /** Top-level Agent API instructions. */
61
- instructions: z.string().optional(),
68
+ export const perplexityLanguageModelOptions = lazySchema(() =>
69
+ zodSchema(
70
+ z.looseObject({
71
+ /** Top-level Agent API instructions. */
72
+ instructions: z.string().optional(),
62
73
 
63
- /**
64
- * Native Agent API tools. AI SDK function tools can also be supplied through
65
- * the top-level `tools` option on `generateText` and `streamText`.
66
- */
67
- tools: z.array(nativeToolSchema).optional(),
74
+ /**
75
+ * Native Agent API tools. AI SDK function tools can also be supplied through
76
+ * the top-level `tools` option on `generateText` and `streamText`.
77
+ */
78
+ tools: z.array(nativeToolSchema()).optional(),
68
79
 
69
- /** A fallback model list for Agent API routing. */
70
- models: z.array(z.string()).optional(),
80
+ /** A fallback model list for Agent API routing. */
81
+ models: z.array(z.string()).optional(),
71
82
 
72
- /** Maximum number of agentic steps. */
73
- max_steps: z.number().int().positive().optional(),
83
+ /** Maximum number of agentic steps. */
84
+ max_steps: z.number().int().positive().optional(),
74
85
 
75
- /** Maximum number of native tool calls. */
76
- max_tool_calls: z.number().int().nonnegative().optional(),
86
+ /** Maximum number of native tool calls. */
87
+ max_tool_calls: z.number().int().nonnegative().optional(),
77
88
 
78
- /** Continue a conversation from an earlier Agent API response. */
79
- previous_response_id: z.string().optional(),
89
+ /** Continue a conversation from an earlier Agent API response. */
90
+ previous_response_id: z.string().optional(),
80
91
 
81
- /**
82
- * Whether the response can be retrieved later. Setting this to false does
83
- * not disable persistence or prevent previous_response_id continuations.
84
- */
85
- store: z.boolean().optional(),
92
+ /**
93
+ * Whether the response can be retrieved later. Setting this to false does
94
+ * not disable persistence or prevent previous_response_id continuations.
95
+ */
96
+ store: z.boolean().optional(),
86
97
 
87
- /** Preferred response language as an ISO 639-1 language code. */
88
- language_preference: z.string().optional(),
98
+ /** Preferred response language as an ISO 639-1 language code. */
99
+ language_preference: z.string().optional(),
89
100
 
90
- /** Agent API reasoning configuration. */
91
- reasoning: z
92
- .looseObject({
93
- effort: z.enum(['minimal', 'low', 'medium', 'high', 'xhigh']).optional(),
94
- })
95
- .optional(),
101
+ /** Agent API reasoning configuration. */
102
+ reasoning: z
103
+ .looseObject({
104
+ effort: z
105
+ .enum(['minimal', 'low', 'medium', 'high', 'xhigh'])
106
+ .optional(),
107
+ })
108
+ .optional(),
96
109
 
97
- /** Agent API skill configuration. */
98
- skills: z
99
- .array(
100
- z.union([
101
- z.looseObject({
102
- type: z.literal('builtin'),
103
- name: z.enum([
104
- 'office',
105
- 'office/docx',
106
- 'office/pdf',
107
- 'office/pptx',
108
- 'office/xlsx',
110
+ /** Agent API skill configuration. */
111
+ skills: z
112
+ .array(
113
+ z.union([
114
+ z.looseObject({
115
+ type: z.literal('builtin'),
116
+ name: z.enum([
117
+ 'office',
118
+ 'office/docx',
119
+ 'office/pdf',
120
+ 'office/pptx',
121
+ 'office/xlsx',
122
+ ]),
123
+ }),
124
+ z.looseObject({
125
+ type: z.literal('inline'),
126
+ name: z.string(),
127
+ description: z.string(),
128
+ instructions: z.string(),
129
+ }),
109
130
  ]),
110
- }),
111
- z.looseObject({
112
- type: z.literal('inline'),
113
- name: z.string(),
114
- description: z.string(),
115
- instructions: z.string(),
116
- }),
117
- ]),
118
- )
119
- .optional(),
120
- });
131
+ )
132
+ .optional(),
133
+ }),
134
+ ),
135
+ );
121
136
 
122
- export type PerplexityLanguageModelOptions = z.infer<
137
+ export type PerplexityLanguageModelOptions = InferSchema<
123
138
  typeof perplexityLanguageModelOptions
124
139
  >;
@@ -11,6 +11,7 @@ import {
11
11
  type SharedV3Warning,
12
12
  } from '@ai-sdk/provider';
13
13
  import {
14
+ type InferSchema,
14
15
  combineHeaders,
15
16
  createEventSourceResponseHandler,
16
17
  createJsonErrorResponseHandler,
@@ -26,6 +27,7 @@ import { convertToPerplexityInput } from './convert-to-perplexity-input';
26
27
  import { mapPerplexityFinishReason } from './map-perplexity-finish-reason';
27
28
  import {
28
29
  perplexityAgentChunkSchema,
30
+ perplexityAgentResponseSchemaValidator,
29
31
  perplexityAgentResponseSchema,
30
32
  perplexityErrorSchema,
31
33
  perplexityErrorToMessage,
@@ -48,9 +50,13 @@ type PerplexityAgentConfig = {
48
50
  fetch?: FetchFunction;
49
51
  };
50
52
 
51
- type PerplexityOutputItem = z.infer<typeof perplexityOutputItemSchema>;
52
- type PerplexitySearchResult = z.infer<typeof perplexitySearchResultSchema>;
53
- type PerplexityUsage = z.infer<typeof perplexityUsageSchema>;
53
+ type PerplexityOutputItem = z.infer<
54
+ ReturnType<typeof perplexityOutputItemSchema>
55
+ >;
56
+ type PerplexitySearchResult = z.infer<
57
+ ReturnType<typeof perplexitySearchResultSchema>
58
+ >;
59
+ type PerplexityUsage = z.infer<ReturnType<typeof perplexityUsageSchema>>;
54
60
  type PerplexityUrlSource = Extract<
55
61
  LanguageModelV3Content,
56
62
  { type: 'source'; sourceType: 'url' }
@@ -296,7 +302,7 @@ export class PerplexityLanguageModel implements LanguageModelV3 {
296
302
  errorToMessage: perplexityErrorToMessage,
297
303
  }),
298
304
  successfulResponseHandler: createJsonResponseHandler(
299
- perplexityAgentResponseSchema,
305
+ perplexityAgentResponseSchemaValidator,
300
306
  ),
301
307
  abortSignal: options.abortSignal,
302
308
  fetch: this.config.fetch,
@@ -452,7 +458,7 @@ export class PerplexityLanguageModel implements LanguageModelV3 {
452
458
  return {
453
459
  stream: response.pipeThrough(
454
460
  new TransformStream<
455
- ParseResult<z.infer<typeof perplexityAgentChunkSchema>>,
461
+ ParseResult<InferSchema<typeof perplexityAgentChunkSchema>>,
456
462
  LanguageModelV3StreamPart
457
463
  >({
458
464
  start(controller) {