@ai-sdk/provider-utils 0.0.0
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/README.md +1 -0
- package/dist/index.d.mts +147 -0
- package/dist/index.d.ts +147 -0
- package/dist/index.js +435 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +385 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +74 -0
- package/test/dist/index.d.mts +25 -0
- package/test/dist/index.d.ts +25 -0
- package/test/dist/index.js +8313 -0
- package/test/dist/index.js.map +1 -0
- package/test/dist/index.mjs +8282 -0
- package/test/dist/index.mjs.map +1 -0
package/README.md
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Vercel AI SDK - Provider Implementation Utilities
|
package/dist/index.d.mts
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
import { JSONParseError, TypeValidationError, APICallError } from '@ai-sdk/provider';
|
2
|
+
import * as zod from 'zod';
|
3
|
+
import { ZodSchema } from 'zod';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Generates a 7-character random string to use for IDs. Not secure.
|
7
|
+
*/
|
8
|
+
declare const generateId: (size?: number | undefined) => string;
|
9
|
+
|
10
|
+
declare function getErrorMessage(error: unknown | undefined): string;
|
11
|
+
|
12
|
+
declare function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName, description, }: {
|
13
|
+
apiKey: string | undefined;
|
14
|
+
environmentVariableName: string;
|
15
|
+
apiKeyParameterName?: string;
|
16
|
+
description: string;
|
17
|
+
}): string;
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Parses a JSON string into an unknown object.
|
21
|
+
*
|
22
|
+
* @param text - The JSON string to parse.
|
23
|
+
* @returns {unknown} - The parsed JSON object.
|
24
|
+
*/
|
25
|
+
declare function parseJSON({ text }: {
|
26
|
+
text: string;
|
27
|
+
}): unknown;
|
28
|
+
/**
|
29
|
+
* Parses a JSON string into a strongly-typed object using the provided schema.
|
30
|
+
*
|
31
|
+
* @template T - The type of the object to parse the JSON into.
|
32
|
+
* @param {string} text - The JSON string to parse.
|
33
|
+
* @param {Schema<T>} schema - The schema to use for parsing the JSON.
|
34
|
+
* @returns {T} - The parsed object.
|
35
|
+
*/
|
36
|
+
declare function parseJSON<T>({ text, schema, }: {
|
37
|
+
text: string;
|
38
|
+
schema: ZodSchema<T>;
|
39
|
+
}): T;
|
40
|
+
type ParseResult<T> = {
|
41
|
+
success: true;
|
42
|
+
value: T;
|
43
|
+
} | {
|
44
|
+
success: false;
|
45
|
+
error: JSONParseError | TypeValidationError;
|
46
|
+
};
|
47
|
+
/**
|
48
|
+
* Safely parses a JSON string and returns the result as an object of type `unknown`.
|
49
|
+
*
|
50
|
+
* @param text - The JSON string to parse.
|
51
|
+
* @returns {object} Either an object with `success: true` and the parsed data, or an object with `success: false` and the error that occurred.
|
52
|
+
*/
|
53
|
+
declare function safeParseJSON({ text }: {
|
54
|
+
text: string;
|
55
|
+
}): ParseResult<unknown>;
|
56
|
+
/**
|
57
|
+
* Safely parses a JSON string into a strongly-typed object, using a provided schema to validate the object.
|
58
|
+
*
|
59
|
+
* @template T - The type of the object to parse the JSON into.
|
60
|
+
* @param {string} text - The JSON string to parse.
|
61
|
+
* @param {Schema<T>} schema - The schema to use for parsing the JSON.
|
62
|
+
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
63
|
+
*/
|
64
|
+
declare function safeParseJSON<T>({ text, schema, }: {
|
65
|
+
text: string;
|
66
|
+
schema: ZodSchema<T>;
|
67
|
+
}): ParseResult<T>;
|
68
|
+
declare function isParseableJson(input: string): boolean;
|
69
|
+
|
70
|
+
type ResponseHandler<RETURN_TYPE> = (options: {
|
71
|
+
url: string;
|
72
|
+
requestBodyValues: unknown;
|
73
|
+
response: Response;
|
74
|
+
}) => PromiseLike<RETURN_TYPE>;
|
75
|
+
declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
|
76
|
+
errorSchema: ZodSchema<T, zod.ZodTypeDef, T>;
|
77
|
+
errorToMessage: (error: T) => string;
|
78
|
+
isRetryable?: ((response: Response, error?: T | undefined) => boolean) | undefined;
|
79
|
+
}) => ResponseHandler<APICallError>;
|
80
|
+
declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodSchema<T, zod.ZodTypeDef, T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
|
81
|
+
declare const createJsonResponseHandler: <T>(responseSchema: ZodSchema<T, zod.ZodTypeDef, T>) => ResponseHandler<T>;
|
82
|
+
|
83
|
+
declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, }: {
|
84
|
+
url: string;
|
85
|
+
headers?: Record<string, string | undefined> | undefined;
|
86
|
+
body: unknown;
|
87
|
+
failedResponseHandler: ResponseHandler<APICallError>;
|
88
|
+
successfulResponseHandler: ResponseHandler<T>;
|
89
|
+
abortSignal?: AbortSignal | undefined;
|
90
|
+
}) => Promise<T>;
|
91
|
+
declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, failedResponseHandler, abortSignal, }: {
|
92
|
+
url: string;
|
93
|
+
headers?: Record<string, string | undefined> | undefined;
|
94
|
+
body: {
|
95
|
+
content: string | FormData | Uint8Array;
|
96
|
+
values: unknown;
|
97
|
+
};
|
98
|
+
failedResponseHandler: ResponseHandler<Error>;
|
99
|
+
successfulResponseHandler: ResponseHandler<T>;
|
100
|
+
abortSignal?: AbortSignal | undefined;
|
101
|
+
}) => Promise<T>;
|
102
|
+
|
103
|
+
declare function scale({ inputMin, inputMax, outputMin, outputMax, value, }: {
|
104
|
+
inputMin?: number;
|
105
|
+
inputMax?: number;
|
106
|
+
outputMin: number;
|
107
|
+
outputMax: number;
|
108
|
+
value: number | undefined;
|
109
|
+
}): number | undefined;
|
110
|
+
|
111
|
+
declare function convertBase64ToUint8Array(base64String: string): Uint8Array;
|
112
|
+
declare function convertUint8ArrayToBase64(array: Uint8Array): string;
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Validates the types of an unknown object using a schema and
|
116
|
+
* return a strongly-typed object.
|
117
|
+
*
|
118
|
+
* @template T - The type of the object to validate.
|
119
|
+
* @param {string} options.value - The object to validate.
|
120
|
+
* @param {Schema<T>} options.schema - The schema to use for validating the JSON.
|
121
|
+
* @returns {T} - The typed object.
|
122
|
+
*/
|
123
|
+
declare function validateTypes<T>({ value, schema, }: {
|
124
|
+
value: unknown;
|
125
|
+
schema: ZodSchema<T>;
|
126
|
+
}): T;
|
127
|
+
/**
|
128
|
+
* Safely validates the types of an unknown object using a schema and
|
129
|
+
* return a strongly-typed object.
|
130
|
+
*
|
131
|
+
* @template T - The type of the object to validate.
|
132
|
+
* @param {string} options.value - The JSON object to validate.
|
133
|
+
* @param {Schema<T>} options.schema - The schema to use for validating the JSON.
|
134
|
+
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
135
|
+
*/
|
136
|
+
declare function safeValidateTypes<T>({ value, schema, }: {
|
137
|
+
value: unknown;
|
138
|
+
schema: ZodSchema<T>;
|
139
|
+
}): {
|
140
|
+
success: true;
|
141
|
+
value: T;
|
142
|
+
} | {
|
143
|
+
success: false;
|
144
|
+
error: TypeValidationError;
|
145
|
+
};
|
146
|
+
|
147
|
+
export { type ParseResult, type ResponseHandler, convertBase64ToUint8Array, convertUint8ArrayToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, generateId, getErrorMessage, isParseableJson, loadApiKey, parseJSON, postJsonToApi, postToApi, safeParseJSON, safeValidateTypes, scale, validateTypes };
|
package/dist/index.d.ts
ADDED
@@ -0,0 +1,147 @@
|
|
1
|
+
import { JSONParseError, TypeValidationError, APICallError } from '@ai-sdk/provider';
|
2
|
+
import * as zod from 'zod';
|
3
|
+
import { ZodSchema } from 'zod';
|
4
|
+
|
5
|
+
/**
|
6
|
+
* Generates a 7-character random string to use for IDs. Not secure.
|
7
|
+
*/
|
8
|
+
declare const generateId: (size?: number | undefined) => string;
|
9
|
+
|
10
|
+
declare function getErrorMessage(error: unknown | undefined): string;
|
11
|
+
|
12
|
+
declare function loadApiKey({ apiKey, environmentVariableName, apiKeyParameterName, description, }: {
|
13
|
+
apiKey: string | undefined;
|
14
|
+
environmentVariableName: string;
|
15
|
+
apiKeyParameterName?: string;
|
16
|
+
description: string;
|
17
|
+
}): string;
|
18
|
+
|
19
|
+
/**
|
20
|
+
* Parses a JSON string into an unknown object.
|
21
|
+
*
|
22
|
+
* @param text - The JSON string to parse.
|
23
|
+
* @returns {unknown} - The parsed JSON object.
|
24
|
+
*/
|
25
|
+
declare function parseJSON({ text }: {
|
26
|
+
text: string;
|
27
|
+
}): unknown;
|
28
|
+
/**
|
29
|
+
* Parses a JSON string into a strongly-typed object using the provided schema.
|
30
|
+
*
|
31
|
+
* @template T - The type of the object to parse the JSON into.
|
32
|
+
* @param {string} text - The JSON string to parse.
|
33
|
+
* @param {Schema<T>} schema - The schema to use for parsing the JSON.
|
34
|
+
* @returns {T} - The parsed object.
|
35
|
+
*/
|
36
|
+
declare function parseJSON<T>({ text, schema, }: {
|
37
|
+
text: string;
|
38
|
+
schema: ZodSchema<T>;
|
39
|
+
}): T;
|
40
|
+
type ParseResult<T> = {
|
41
|
+
success: true;
|
42
|
+
value: T;
|
43
|
+
} | {
|
44
|
+
success: false;
|
45
|
+
error: JSONParseError | TypeValidationError;
|
46
|
+
};
|
47
|
+
/**
|
48
|
+
* Safely parses a JSON string and returns the result as an object of type `unknown`.
|
49
|
+
*
|
50
|
+
* @param text - The JSON string to parse.
|
51
|
+
* @returns {object} Either an object with `success: true` and the parsed data, or an object with `success: false` and the error that occurred.
|
52
|
+
*/
|
53
|
+
declare function safeParseJSON({ text }: {
|
54
|
+
text: string;
|
55
|
+
}): ParseResult<unknown>;
|
56
|
+
/**
|
57
|
+
* Safely parses a JSON string into a strongly-typed object, using a provided schema to validate the object.
|
58
|
+
*
|
59
|
+
* @template T - The type of the object to parse the JSON into.
|
60
|
+
* @param {string} text - The JSON string to parse.
|
61
|
+
* @param {Schema<T>} schema - The schema to use for parsing the JSON.
|
62
|
+
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
63
|
+
*/
|
64
|
+
declare function safeParseJSON<T>({ text, schema, }: {
|
65
|
+
text: string;
|
66
|
+
schema: ZodSchema<T>;
|
67
|
+
}): ParseResult<T>;
|
68
|
+
declare function isParseableJson(input: string): boolean;
|
69
|
+
|
70
|
+
type ResponseHandler<RETURN_TYPE> = (options: {
|
71
|
+
url: string;
|
72
|
+
requestBodyValues: unknown;
|
73
|
+
response: Response;
|
74
|
+
}) => PromiseLike<RETURN_TYPE>;
|
75
|
+
declare const createJsonErrorResponseHandler: <T>({ errorSchema, errorToMessage, isRetryable, }: {
|
76
|
+
errorSchema: ZodSchema<T, zod.ZodTypeDef, T>;
|
77
|
+
errorToMessage: (error: T) => string;
|
78
|
+
isRetryable?: ((response: Response, error?: T | undefined) => boolean) | undefined;
|
79
|
+
}) => ResponseHandler<APICallError>;
|
80
|
+
declare const createEventSourceResponseHandler: <T>(chunkSchema: ZodSchema<T, zod.ZodTypeDef, T>) => ResponseHandler<ReadableStream<ParseResult<T>>>;
|
81
|
+
declare const createJsonResponseHandler: <T>(responseSchema: ZodSchema<T, zod.ZodTypeDef, T>) => ResponseHandler<T>;
|
82
|
+
|
83
|
+
declare const postJsonToApi: <T>({ url, headers, body, failedResponseHandler, successfulResponseHandler, abortSignal, }: {
|
84
|
+
url: string;
|
85
|
+
headers?: Record<string, string | undefined> | undefined;
|
86
|
+
body: unknown;
|
87
|
+
failedResponseHandler: ResponseHandler<APICallError>;
|
88
|
+
successfulResponseHandler: ResponseHandler<T>;
|
89
|
+
abortSignal?: AbortSignal | undefined;
|
90
|
+
}) => Promise<T>;
|
91
|
+
declare const postToApi: <T>({ url, headers, body, successfulResponseHandler, failedResponseHandler, abortSignal, }: {
|
92
|
+
url: string;
|
93
|
+
headers?: Record<string, string | undefined> | undefined;
|
94
|
+
body: {
|
95
|
+
content: string | FormData | Uint8Array;
|
96
|
+
values: unknown;
|
97
|
+
};
|
98
|
+
failedResponseHandler: ResponseHandler<Error>;
|
99
|
+
successfulResponseHandler: ResponseHandler<T>;
|
100
|
+
abortSignal?: AbortSignal | undefined;
|
101
|
+
}) => Promise<T>;
|
102
|
+
|
103
|
+
declare function scale({ inputMin, inputMax, outputMin, outputMax, value, }: {
|
104
|
+
inputMin?: number;
|
105
|
+
inputMax?: number;
|
106
|
+
outputMin: number;
|
107
|
+
outputMax: number;
|
108
|
+
value: number | undefined;
|
109
|
+
}): number | undefined;
|
110
|
+
|
111
|
+
declare function convertBase64ToUint8Array(base64String: string): Uint8Array;
|
112
|
+
declare function convertUint8ArrayToBase64(array: Uint8Array): string;
|
113
|
+
|
114
|
+
/**
|
115
|
+
* Validates the types of an unknown object using a schema and
|
116
|
+
* return a strongly-typed object.
|
117
|
+
*
|
118
|
+
* @template T - The type of the object to validate.
|
119
|
+
* @param {string} options.value - The object to validate.
|
120
|
+
* @param {Schema<T>} options.schema - The schema to use for validating the JSON.
|
121
|
+
* @returns {T} - The typed object.
|
122
|
+
*/
|
123
|
+
declare function validateTypes<T>({ value, schema, }: {
|
124
|
+
value: unknown;
|
125
|
+
schema: ZodSchema<T>;
|
126
|
+
}): T;
|
127
|
+
/**
|
128
|
+
* Safely validates the types of an unknown object using a schema and
|
129
|
+
* return a strongly-typed object.
|
130
|
+
*
|
131
|
+
* @template T - The type of the object to validate.
|
132
|
+
* @param {string} options.value - The JSON object to validate.
|
133
|
+
* @param {Schema<T>} options.schema - The schema to use for validating the JSON.
|
134
|
+
* @returns An object with either a `success` flag and the parsed and typed data, or a `success` flag and an error object.
|
135
|
+
*/
|
136
|
+
declare function safeValidateTypes<T>({ value, schema, }: {
|
137
|
+
value: unknown;
|
138
|
+
schema: ZodSchema<T>;
|
139
|
+
}): {
|
140
|
+
success: true;
|
141
|
+
value: T;
|
142
|
+
} | {
|
143
|
+
success: false;
|
144
|
+
error: TypeValidationError;
|
145
|
+
};
|
146
|
+
|
147
|
+
export { type ParseResult, type ResponseHandler, convertBase64ToUint8Array, convertUint8ArrayToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, generateId, getErrorMessage, isParseableJson, loadApiKey, parseJSON, postJsonToApi, postToApi, safeParseJSON, safeValidateTypes, scale, validateTypes };
|