@ai-sdk/typesafe-ai 3.0.10 → 3.0.12
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 +18 -0
- package/dist/index.d.ts +20 -17
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +178 -224
- package/dist/index.js.map +1 -1
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @ai-sdk/typesafe-ai
|
|
2
2
|
|
|
3
|
+
## 3.0.12
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [8c65988]
|
|
8
|
+
- Updated dependencies [527a163]
|
|
9
|
+
- @ai-sdk/provider@4.0.21
|
|
10
|
+
- @ai-sdk/provider-utils@5.0.53
|
|
11
|
+
|
|
12
|
+
## 3.0.11
|
|
13
|
+
|
|
14
|
+
### Patch Changes
|
|
15
|
+
|
|
16
|
+
- ede5b89: chore: migrate package builds from tsup to tsdown
|
|
17
|
+
- Updated dependencies [ede5b89]
|
|
18
|
+
- @ai-sdk/provider@4.0.20
|
|
19
|
+
- @ai-sdk/provider-utils@5.0.52
|
|
20
|
+
|
|
3
21
|
## 3.0.10
|
|
4
22
|
|
|
5
23
|
### Patch Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { FetchFunction } from
|
|
3
|
-
|
|
1
|
+
import { Experimental_EvaluationModelV4, ProviderV4 } from "@ai-sdk/provider";
|
|
2
|
+
import { FetchFunction, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE } from "@ai-sdk/provider-utils";
|
|
3
|
+
//#region src/typesafe-ai-evaluation-model.d.ts
|
|
4
4
|
type TypeSafeAiEvaluationModelId = 'jev-latest' | (string & {});
|
|
5
|
-
|
|
5
|
+
//#endregion
|
|
6
|
+
//#region src/typesafe-ai-provider.d.ts
|
|
6
7
|
/** TypeSafe's experimental evaluation capability, isolated from ProviderV4. */
|
|
7
8
|
interface TypeSafeAiProvider extends ProviderV4 {
|
|
8
|
-
|
|
9
|
+
evaluationModel(modelId: TypeSafeAiEvaluationModelId): Experimental_EvaluationModelV4;
|
|
9
10
|
}
|
|
10
11
|
interface TypeSafeAiProviderSettings {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
/** API key. Defaults to the TYPESAFE_AI_API_KEY environment variable. */
|
|
13
|
+
apiKey?: string;
|
|
14
|
+
/** API base URL. Defaults to https://api.typesafe.ai/v1. */
|
|
15
|
+
baseURL?: string;
|
|
16
|
+
headers?: Record<string, string>;
|
|
17
|
+
fetch?: FetchFunction;
|
|
17
18
|
}
|
|
18
|
-
declare function createTypeSafeAi(options?: TypeSafeAiProviderSettings): TypeSafeAiProvider;
|
|
19
|
-
declare const typeSafeAi: TypeSafeAiProvider;
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
19
|
+
export declare function createTypeSafeAi(options?: TypeSafeAiProviderSettings): TypeSafeAiProvider;
|
|
20
|
+
export declare const typeSafeAi: TypeSafeAiProvider;
|
|
21
|
+
//#endregion
|
|
22
|
+
//#region src/version.d.ts
|
|
23
|
+
export declare const VERSION: string;
|
|
24
|
+
//#endregion
|
|
25
|
+
export type { TypeSafeAiEvaluationModelId as Experimental_TypeSafeAiEvaluationModelId, TypeSafeAiProvider, TypeSafeAiProviderSettings };
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":["EvaluationModelV4"],"sources":["../src/typesafe-ai-evaluation-model.ts","../src/typesafe-ai-provider.ts","../src/version.ts"],"mappings":";;;KAyBY;;;;UCPK,2BAA2B;EAC1C,gBAAgB,SAAS,8BAA8BA;;UAGxC;;EAEf;;EAEA;EACA,UAAU;EACV,QAAQ;;wBAGM,iBACd,UAAS,6BACR;qBAiCU,YAAU;;;qBChEV"}
|
package/dist/index.js
CHANGED
|
@@ -1,231 +1,185 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
NoSuchModelError
|
|
4
|
-
} from "@ai-sdk/provider";
|
|
5
|
-
import {
|
|
6
|
-
loadApiKey as loadApiKey2,
|
|
7
|
-
withoutTrailingSlash,
|
|
8
|
-
withUserAgentSuffix as withUserAgentSuffix2
|
|
9
|
-
} from "@ai-sdk/provider-utils";
|
|
10
|
-
|
|
11
|
-
// src/typesafe-ai-evaluation-model.ts
|
|
12
|
-
import {
|
|
13
|
-
InvalidArgumentError
|
|
14
|
-
} from "@ai-sdk/provider";
|
|
15
|
-
import {
|
|
16
|
-
combineHeaders,
|
|
17
|
-
createJsonResponseHandler,
|
|
18
|
-
loadApiKey,
|
|
19
|
-
postJsonToApi,
|
|
20
|
-
resolve,
|
|
21
|
-
serializeModelOptions,
|
|
22
|
-
withUserAgentSuffix,
|
|
23
|
-
WORKFLOW_SERIALIZE,
|
|
24
|
-
WORKFLOW_DESERIALIZE
|
|
25
|
-
} from "@ai-sdk/provider-utils";
|
|
26
|
-
|
|
27
|
-
// src/typesafe-ai-evaluation-api.ts
|
|
28
|
-
import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
|
|
1
|
+
import { InvalidArgumentError, NoSuchModelError } from "@ai-sdk/provider";
|
|
2
|
+
import { WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, createJsonErrorResponseHandler, createJsonResponseHandler, loadApiKey, postJsonToApi, resolve, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash } from "@ai-sdk/provider-utils";
|
|
29
3
|
import * as z from "zod/v4";
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
4
|
+
//#region src/typesafe-ai-evaluation-api.ts
|
|
5
|
+
const typesafeEvaluationResponseSchema = z.object({
|
|
6
|
+
model: z.string().nullish(),
|
|
7
|
+
answers: z.record(z.string(), z.discriminatedUnion("type", [
|
|
8
|
+
z.object({
|
|
9
|
+
type: z.literal("choice"),
|
|
10
|
+
choice: z.string(),
|
|
11
|
+
probabilities: z.record(z.string(), z.number()),
|
|
12
|
+
confidence: z.number().nullish()
|
|
13
|
+
}),
|
|
14
|
+
z.object({
|
|
15
|
+
type: z.literal("score"),
|
|
16
|
+
score: z.number(),
|
|
17
|
+
probabilities: z.record(z.string(), z.number()),
|
|
18
|
+
confidence: z.number().nullish()
|
|
19
|
+
}),
|
|
20
|
+
z.object({
|
|
21
|
+
type: z.literal("noul"),
|
|
22
|
+
noul: z.number()
|
|
23
|
+
})
|
|
24
|
+
])),
|
|
25
|
+
usage: z.object({
|
|
26
|
+
input_tokens: z.number().nullish(),
|
|
27
|
+
output_tokens: z.number().nullish()
|
|
28
|
+
}).nullish()
|
|
54
29
|
});
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
30
|
+
const typesafeFailedResponseHandler = createJsonErrorResponseHandler({
|
|
31
|
+
errorSchema: z.object({
|
|
32
|
+
message: z.string().nullish(),
|
|
33
|
+
detail: z.unknown().nullish(),
|
|
34
|
+
error: z.union([z.string(), z.object({ message: z.string().nullish() })]).nullish(),
|
|
35
|
+
error_type: z.string().nullish()
|
|
36
|
+
}),
|
|
37
|
+
errorToMessage: (error) => error.message ?? (typeof error.error === "string" ? error.error : error.error?.message) ?? (typeof error.detail === "string" ? error.detail : JSON.stringify(error.detail)) ?? error.error_type ?? "TypeSafe request failed"
|
|
63
38
|
});
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
var EvaluationTypeSafeAiModel = class
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
];
|
|
166
|
-
case "score":
|
|
167
|
-
return [
|
|
168
|
-
id,
|
|
169
|
-
{
|
|
170
|
-
type: "score",
|
|
171
|
-
score: answer.score,
|
|
172
|
-
probabilities: answer.probabilities
|
|
173
|
-
}
|
|
174
|
-
];
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
)
|
|
178
|
-
),
|
|
179
|
-
usage: {
|
|
180
|
-
inputTokens: response.usage?.input_tokens ?? void 0,
|
|
181
|
-
outputTokens: response.usage?.output_tokens ?? void 0
|
|
182
|
-
},
|
|
183
|
-
// The API rounds displayed probabilities and scores to two decimal places.
|
|
184
|
-
rounding: { probabilityDecimals: 2, scoreDecimals: 2 },
|
|
185
|
-
warnings,
|
|
186
|
-
providerMetadata: { typesafe: { confidence } },
|
|
187
|
-
response: {
|
|
188
|
-
modelId: response.model ?? this.modelId,
|
|
189
|
-
headers: responseHeaders,
|
|
190
|
-
body: rawValue
|
|
191
|
-
}
|
|
192
|
-
};
|
|
193
|
-
}
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/version.ts
|
|
41
|
+
const VERSION = "3.0.12";
|
|
42
|
+
//#endregion
|
|
43
|
+
//#region src/typesafe-ai-evaluation-model.ts
|
|
44
|
+
var EvaluationTypeSafeAiModel = class EvaluationTypeSafeAiModel {
|
|
45
|
+
constructor(modelId, config) {
|
|
46
|
+
this.modelId = modelId;
|
|
47
|
+
this.config = config;
|
|
48
|
+
this.specificationVersion = "v4";
|
|
49
|
+
this.supportedQuestionTypes = [
|
|
50
|
+
"choice",
|
|
51
|
+
"score",
|
|
52
|
+
"boolean"
|
|
53
|
+
];
|
|
54
|
+
}
|
|
55
|
+
get provider() {
|
|
56
|
+
return this.config.provider;
|
|
57
|
+
}
|
|
58
|
+
static [WORKFLOW_SERIALIZE](model) {
|
|
59
|
+
return serializeModelOptions({
|
|
60
|
+
modelId: model.modelId,
|
|
61
|
+
config: model.config
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
static [WORKFLOW_DESERIALIZE](options) {
|
|
65
|
+
return new EvaluationTypeSafeAiModel(options.modelId, options.config);
|
|
66
|
+
}
|
|
67
|
+
async doEvaluate({ state, questions, headers, abortSignal, providerOptions }) {
|
|
68
|
+
for (const [id, question] of Object.entries(questions)) {
|
|
69
|
+
if (question.type === "choice" && Object.keys(question.criteria).length > 255) throw new InvalidArgumentError({
|
|
70
|
+
argument: `questions.${id}.criteria`,
|
|
71
|
+
message: "TypeSafe Choice questions support at most 255 options."
|
|
72
|
+
});
|
|
73
|
+
if (question.type === "score" && question.criteria.length > 10) throw new InvalidArgumentError({
|
|
74
|
+
argument: `questions.${id}.criteria`,
|
|
75
|
+
message: "TypeSafe Score questions support at most 10 levels."
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const warnings = Object.keys(providerOptions?.typesafe ?? {}).map((option) => ({
|
|
79
|
+
type: "unsupported",
|
|
80
|
+
feature: `providerOptions.typesafe.${option}`
|
|
81
|
+
}));
|
|
82
|
+
const modelHeaders = this.config.headers === void 0 ? withUserAgentSuffix({ Authorization: `Bearer ${loadApiKey({
|
|
83
|
+
apiKey: void 0,
|
|
84
|
+
environmentVariableName: "TYPESAFE_AI_API_KEY",
|
|
85
|
+
description: "TypeSafe"
|
|
86
|
+
})}` }, `ai-sdk-typesafe-ai/${VERSION}`) : await resolve(this.config.headers);
|
|
87
|
+
const { value: response, rawValue, responseHeaders } = await postJsonToApi({
|
|
88
|
+
url: `${this.config.baseURL}/systemone`,
|
|
89
|
+
headers: combineHeaders(modelHeaders, headers),
|
|
90
|
+
body: {
|
|
91
|
+
model: this.modelId,
|
|
92
|
+
state,
|
|
93
|
+
questions: Object.fromEntries(Object.entries(questions).map(([id, question]) => [id, question.type === "boolean" ? {
|
|
94
|
+
...question,
|
|
95
|
+
type: "noul"
|
|
96
|
+
} : question]))
|
|
97
|
+
},
|
|
98
|
+
abortSignal,
|
|
99
|
+
fetch: this.config.fetch,
|
|
100
|
+
failedResponseHandler: typesafeFailedResponseHandler,
|
|
101
|
+
successfulResponseHandler: createJsonResponseHandler(typesafeEvaluationResponseSchema)
|
|
102
|
+
});
|
|
103
|
+
const confidence = Object.fromEntries(Object.entries(response.answers).flatMap(([id, answer]) => answer.type !== "noul" && answer.confidence != null ? [[id, answer.confidence]] : []));
|
|
104
|
+
return {
|
|
105
|
+
answers: Object.fromEntries(Object.entries(response.answers).map(([id, answer]) => {
|
|
106
|
+
switch (answer.type) {
|
|
107
|
+
case "noul": return [id, {
|
|
108
|
+
type: "boolean",
|
|
109
|
+
probability: answer.noul
|
|
110
|
+
}];
|
|
111
|
+
case "choice": return [id, {
|
|
112
|
+
type: "choice",
|
|
113
|
+
choice: answer.choice,
|
|
114
|
+
probabilities: answer.probabilities
|
|
115
|
+
}];
|
|
116
|
+
case "score": return [id, {
|
|
117
|
+
type: "score",
|
|
118
|
+
score: answer.score,
|
|
119
|
+
probabilities: answer.probabilities
|
|
120
|
+
}];
|
|
121
|
+
}
|
|
122
|
+
})),
|
|
123
|
+
usage: {
|
|
124
|
+
inputTokens: response.usage?.input_tokens ?? void 0,
|
|
125
|
+
outputTokens: response.usage?.output_tokens ?? void 0
|
|
126
|
+
},
|
|
127
|
+
rounding: {
|
|
128
|
+
probabilityDecimals: 2,
|
|
129
|
+
scoreDecimals: 2
|
|
130
|
+
},
|
|
131
|
+
warnings,
|
|
132
|
+
providerMetadata: { typesafe: { confidence } },
|
|
133
|
+
response: {
|
|
134
|
+
modelId: response.model ?? this.modelId,
|
|
135
|
+
headers: responseHeaders,
|
|
136
|
+
body: rawValue
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
194
140
|
};
|
|
195
|
-
|
|
196
|
-
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/typesafe-ai-provider.ts
|
|
197
143
|
function createTypeSafeAi(options = {}) {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
144
|
+
const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://api.typesafe.ai/v1";
|
|
145
|
+
const headers = () => withUserAgentSuffix({
|
|
146
|
+
Authorization: `Bearer ${loadApiKey({
|
|
147
|
+
apiKey: options.apiKey,
|
|
148
|
+
environmentVariableName: "TYPESAFE_AI_API_KEY",
|
|
149
|
+
description: "TypeSafe"
|
|
150
|
+
})}`,
|
|
151
|
+
...options.headers
|
|
152
|
+
}, `ai-sdk-typesafe-ai/${VERSION}`);
|
|
153
|
+
return {
|
|
154
|
+
specificationVersion: "v4",
|
|
155
|
+
evaluationModel: (modelId) => new EvaluationTypeSafeAiModel(modelId, {
|
|
156
|
+
provider: "typesafe.evaluation",
|
|
157
|
+
baseURL,
|
|
158
|
+
headers,
|
|
159
|
+
fetch: options.fetch
|
|
160
|
+
}),
|
|
161
|
+
languageModel: (modelId) => {
|
|
162
|
+
throw new NoSuchModelError({
|
|
163
|
+
modelId,
|
|
164
|
+
modelType: "languageModel"
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
embeddingModel: (modelId) => {
|
|
168
|
+
throw new NoSuchModelError({
|
|
169
|
+
modelId,
|
|
170
|
+
modelType: "embeddingModel"
|
|
171
|
+
});
|
|
172
|
+
},
|
|
173
|
+
imageModel: (modelId) => {
|
|
174
|
+
throw new NoSuchModelError({
|
|
175
|
+
modelId,
|
|
176
|
+
modelType: "imageModel"
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
};
|
|
224
180
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
typeSafeAi
|
|
230
|
-
};
|
|
181
|
+
const typeSafeAi = createTypeSafeAi();
|
|
182
|
+
//#endregion
|
|
183
|
+
export { VERSION, createTypeSafeAi, typeSafeAi };
|
|
184
|
+
|
|
231
185
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/typesafe-ai-provider.ts","../src/typesafe-ai-evaluation-model.ts","../src/typesafe-ai-evaluation-api.ts","../src/version.ts"],"sourcesContent":["import {\n NoSuchModelError,\n type Experimental_EvaluationModelV4 as EvaluationModelV4,\n type ProviderV4,\n} from '@ai-sdk/provider';\nimport {\n loadApiKey,\n withoutTrailingSlash,\n withUserAgentSuffix,\n type FetchFunction,\n} from '@ai-sdk/provider-utils';\nimport {\n EvaluationTypeSafeAiModel,\n type TypeSafeAiEvaluationModelId,\n} from './typesafe-ai-evaluation-model';\nimport { VERSION } from './version';\n\n/** TypeSafe's experimental evaluation capability, isolated from ProviderV4. */\nexport interface TypeSafeAiProvider extends ProviderV4 {\n evaluationModel(modelId: TypeSafeAiEvaluationModelId): EvaluationModelV4;\n}\n\nexport interface TypeSafeAiProviderSettings {\n /** API key. Defaults to the TYPESAFE_AI_API_KEY environment variable. */\n apiKey?: string;\n /** API base URL. Defaults to https://api.typesafe.ai/v1. */\n baseURL?: string;\n headers?: Record<string, string>;\n fetch?: FetchFunction;\n}\n\nexport function createTypeSafeAi(\n options: TypeSafeAiProviderSettings = {},\n): TypeSafeAiProvider {\n const baseURL =\n withoutTrailingSlash(options.baseURL) ?? 'https://api.typesafe.ai/v1';\n const headers = () =>\n withUserAgentSuffix(\n {\n Authorization: `Bearer ${loadApiKey({ apiKey: options.apiKey, environmentVariableName: 'TYPESAFE_AI_API_KEY', description: 'TypeSafe' })}`,\n ...options.headers,\n },\n `ai-sdk-typesafe-ai/${VERSION}`,\n );\n\n return {\n specificationVersion: 'v4',\n evaluationModel: modelId =>\n new EvaluationTypeSafeAiModel(modelId, {\n provider: 'typesafe.evaluation',\n baseURL,\n headers,\n fetch: options.fetch,\n }),\n languageModel: modelId => {\n throw new NoSuchModelError({ modelId, modelType: 'languageModel' });\n },\n embeddingModel: modelId => {\n throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n },\n imageModel: modelId => {\n throw new NoSuchModelError({ modelId, modelType: 'imageModel' });\n },\n };\n}\n\nexport const typeSafeAi = createTypeSafeAi();\n","import {\n InvalidArgumentError,\n type Experimental_EvaluationModelV4 as EvaluationModelV4,\n type Experimental_EvaluationModelV4Answer as EvaluationModelV4Answer,\n type SharedV4Warning,\n} from '@ai-sdk/provider';\nimport {\n combineHeaders,\n createJsonResponseHandler,\n loadApiKey,\n postJsonToApi,\n resolve,\n serializeModelOptions,\n withUserAgentSuffix,\n WORKFLOW_SERIALIZE,\n WORKFLOW_DESERIALIZE,\n type FetchFunction,\n type Resolvable,\n} from '@ai-sdk/provider-utils';\nimport {\n typesafeEvaluationResponseSchema,\n typesafeFailedResponseHandler,\n} from './typesafe-ai-evaluation-api';\nimport { VERSION } from './version';\n\nexport type TypeSafeAiEvaluationModelId = 'jev-latest' | (string & {});\n\ntype TypeSafeAiEvaluationModelConfig = {\n provider: string;\n baseURL: string;\n headers?: Resolvable<Record<string, string | undefined>>;\n fetch?: FetchFunction;\n};\n\nexport class EvaluationTypeSafeAiModel implements EvaluationModelV4 {\n readonly specificationVersion = 'v4';\n readonly supportedQuestionTypes = ['choice', 'score', 'boolean'] as const;\n\n constructor(\n readonly modelId: TypeSafeAiEvaluationModelId,\n private readonly config: TypeSafeAiEvaluationModelConfig,\n ) {}\n\n get provider() {\n return this.config.provider;\n }\n\n static [WORKFLOW_SERIALIZE](model: EvaluationTypeSafeAiModel) {\n return serializeModelOptions({\n modelId: model.modelId,\n config: model.config,\n });\n }\n\n static [WORKFLOW_DESERIALIZE](options: {\n modelId: TypeSafeAiEvaluationModelId;\n config: TypeSafeAiEvaluationModelConfig;\n }) {\n return new EvaluationTypeSafeAiModel(options.modelId, options.config);\n }\n\n async doEvaluate({\n state,\n questions,\n headers,\n abortSignal,\n providerOptions,\n }: Parameters<EvaluationModelV4['doEvaluate']>[0]): Promise<\n Awaited<ReturnType<EvaluationModelV4['doEvaluate']>>\n > {\n for (const [id, question] of Object.entries(questions)) {\n if (\n question.type === 'choice' &&\n Object.keys(question.criteria).length > 255\n ) {\n throw new InvalidArgumentError({\n argument: `questions.${id}.criteria`,\n message: 'TypeSafe Choice questions support at most 255 options.',\n });\n }\n if (question.type === 'score' && question.criteria.length > 10) {\n throw new InvalidArgumentError({\n argument: `questions.${id}.criteria`,\n message: 'TypeSafe Score questions support at most 10 levels.',\n });\n }\n }\n\n const warnings: SharedV4Warning[] = Object.keys(\n providerOptions?.typesafe ?? {},\n ).map(option => ({\n type: 'unsupported',\n feature: `providerOptions.typesafe.${option}`,\n }));\n const modelHeaders =\n this.config.headers === undefined\n ? withUserAgentSuffix(\n {\n Authorization: `Bearer ${loadApiKey({ apiKey: undefined, environmentVariableName: 'TYPESAFE_AI_API_KEY', description: 'TypeSafe' })}`,\n },\n `ai-sdk-typesafe-ai/${VERSION}`,\n )\n : await resolve(this.config.headers);\n const {\n value: response,\n rawValue,\n responseHeaders,\n } = await postJsonToApi({\n url: `${this.config.baseURL}/systemone`,\n headers: combineHeaders(modelHeaders, headers),\n body: {\n model: this.modelId,\n state,\n questions: Object.fromEntries(\n Object.entries(questions).map(([id, question]) => [\n id,\n question.type === 'boolean'\n ? { ...question, type: 'noul' }\n : question,\n ]),\n ),\n },\n abortSignal,\n fetch: this.config.fetch,\n failedResponseHandler: typesafeFailedResponseHandler,\n successfulResponseHandler: createJsonResponseHandler(\n typesafeEvaluationResponseSchema,\n ),\n });\n\n const confidence = Object.fromEntries(\n Object.entries(response.answers).flatMap(([id, answer]) =>\n answer.type !== 'noul' && answer.confidence != null\n ? [[id, answer.confidence]]\n : [],\n ),\n );\n\n return {\n answers: Object.fromEntries(\n Object.entries(response.answers).map(\n ([id, answer]): [string, EvaluationModelV4Answer] => {\n switch (answer.type) {\n case 'noul':\n return [id, { type: 'boolean', probability: answer.noul }];\n case 'choice':\n return [\n id,\n {\n type: 'choice',\n choice: answer.choice,\n probabilities: answer.probabilities,\n },\n ];\n case 'score':\n return [\n id,\n {\n type: 'score',\n score: answer.score,\n probabilities: answer.probabilities,\n },\n ];\n }\n },\n ),\n ),\n usage: {\n inputTokens: response.usage?.input_tokens ?? undefined,\n outputTokens: response.usage?.output_tokens ?? undefined,\n },\n // The API rounds displayed probabilities and scores to two decimal places.\n rounding: { probabilityDecimals: 2, scoreDecimals: 2 },\n warnings,\n providerMetadata: { typesafe: { confidence } },\n response: {\n modelId: response.model ?? this.modelId,\n headers: responseHeaders,\n body: rawValue,\n },\n };\n }\n}\n","import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';\nimport * as z from 'zod/v4';\n\n// Verified against the official SDK at 66880ccded6cb642dc1809620c2b108c33730214.\n// https://github.com/typesafe-ai/typesafe-sdk-js/blob/66880ccded6cb642dc1809620c2b108c33730214/src/types.ts\nexport const typesafeEvaluationResponseSchema = z.object({\n model: z.string().nullish(),\n answers: z.record(\n z.string(),\n z.discriminatedUnion('type', [\n z.object({\n type: z.literal('choice'),\n choice: z.string(),\n probabilities: z.record(z.string(), z.number()),\n confidence: z.number().nullish(),\n }),\n z.object({\n type: z.literal('score'),\n score: z.number(),\n probabilities: z.record(z.string(), z.number()),\n confidence: z.number().nullish(),\n }),\n z.object({ type: z.literal('noul'), noul: z.number() }),\n ]),\n ),\n usage: z\n .object({\n input_tokens: z.number().nullish(),\n output_tokens: z.number().nullish(),\n })\n .nullish(),\n});\n\nexport const typesafeFailedResponseHandler = createJsonErrorResponseHandler({\n errorSchema: z.object({\n message: z.string().nullish(),\n detail: z.unknown().nullish(),\n error: z\n .union([z.string(), z.object({ message: z.string().nullish() })])\n .nullish(),\n error_type: z.string().nullish(),\n }),\n errorToMessage: error =>\n error.message ??\n (typeof error.error === 'string' ? error.error : error.error?.message) ??\n (typeof error.detail === 'string'\n ? error.detail\n : JSON.stringify(error.detail)) ??\n error.error_type ??\n 'TypeSafe request failed',\n});\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n"],"mappings":";AAAA;AAAA,EACE;AAAA,OAGK;AACP;AAAA,EACE,cAAAA;AAAA,EACA;AAAA,EACA,uBAAAC;AAAA,OAEK;;;ACVP;AAAA,EACE;AAAA,OAIK;AACP;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAGK;;;AClBP,SAAS,sCAAsC;AAC/C,YAAY,OAAO;AAIZ,IAAM,mCAAqC,SAAO;AAAA,EACvD,OAAS,SAAO,EAAE,QAAQ;AAAA,EAC1B,SAAW;AAAA,IACP,SAAO;AAAA,IACP,qBAAmB,QAAQ;AAAA,MACzB,SAAO;AAAA,QACP,MAAQ,UAAQ,QAAQ;AAAA,QACxB,QAAU,SAAO;AAAA,QACjB,eAAiB,SAAS,SAAO,GAAK,SAAO,CAAC;AAAA,QAC9C,YAAc,SAAO,EAAE,QAAQ;AAAA,MACjC,CAAC;AAAA,MACC,SAAO;AAAA,QACP,MAAQ,UAAQ,OAAO;AAAA,QACvB,OAAS,SAAO;AAAA,QAChB,eAAiB,SAAS,SAAO,GAAK,SAAO,CAAC;AAAA,QAC9C,YAAc,SAAO,EAAE,QAAQ;AAAA,MACjC,CAAC;AAAA,MACC,SAAO,EAAE,MAAQ,UAAQ,MAAM,GAAG,MAAQ,SAAO,EAAE,CAAC;AAAA,IACxD,CAAC;AAAA,EACH;AAAA,EACA,OACG,SAAO;AAAA,IACN,cAAgB,SAAO,EAAE,QAAQ;AAAA,IACjC,eAAiB,SAAO,EAAE,QAAQ;AAAA,EACpC,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A,MACE,eAAe,UAAUC,YAAW,EAAE,QAAQ,QAAQ,QAAQ,yBAAyB,uBAAuB,aAAa,WAAW,CAAC,CAAC;AAAA,MACxI,GAAG,QAAQ;AAAA,IACb;AAAA,IACA,sBAAsB,OAAO;AAAA,EAC/B;AAEF,SAAO;AAAA,IACL,sBAAsB;AAAA,IACtB,iBAAiB,aACf,IAAI,0BAA0B,SAAS;AAAA,MACrC,UAAU;AAAA,MACV;AAAA,MACA;AAAA,MACA,OAAO,QAAQ;AAAA,IACjB,CAAC;AAAA,IACH,eAAe,aAAW;AACxB,YAAM,IAAI,iBAAiB,EAAE,SAAS,WAAW,gBAAgB,CAAC;AAAA,IACpE;AAAA,IACA,gBAAgB,aAAW;AACzB,YAAM,IAAI,iBAAiB,EAAE,SAAS,WAAW,iBAAiB,CAAC;AAAA,IACrE;AAAA,IACA,YAAY,aAAW;AACrB,YAAM,IAAI,iBAAiB,EAAE,SAAS,WAAW,aAAa,CAAC;AAAA,IACjE;AAAA,EACF;AACF;AAEO,IAAM,aAAa,iBAAiB;","names":["loadApiKey","withUserAgentSuffix","withUserAgentSuffix","loadApiKey"]}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/typesafe-ai-evaluation-api.ts","../src/version.ts","../src/typesafe-ai-evaluation-model.ts","../src/typesafe-ai-provider.ts"],"sourcesContent":["import { createJsonErrorResponseHandler } from '@ai-sdk/provider-utils';\nimport * as z from 'zod/v4';\n\n// Verified against the official SDK at 66880ccded6cb642dc1809620c2b108c33730214.\n// https://github.com/typesafe-ai/typesafe-sdk-js/blob/66880ccded6cb642dc1809620c2b108c33730214/src/types.ts\nexport const typesafeEvaluationResponseSchema = z.object({\n model: z.string().nullish(),\n answers: z.record(\n z.string(),\n z.discriminatedUnion('type', [\n z.object({\n type: z.literal('choice'),\n choice: z.string(),\n probabilities: z.record(z.string(), z.number()),\n confidence: z.number().nullish(),\n }),\n z.object({\n type: z.literal('score'),\n score: z.number(),\n probabilities: z.record(z.string(), z.number()),\n confidence: z.number().nullish(),\n }),\n z.object({ type: z.literal('noul'), noul: z.number() }),\n ]),\n ),\n usage: z\n .object({\n input_tokens: z.number().nullish(),\n output_tokens: z.number().nullish(),\n })\n .nullish(),\n});\n\nexport const typesafeFailedResponseHandler = createJsonErrorResponseHandler({\n errorSchema: z.object({\n message: z.string().nullish(),\n detail: z.unknown().nullish(),\n error: z\n .union([z.string(), z.object({ message: z.string().nullish() })])\n .nullish(),\n error_type: z.string().nullish(),\n }),\n errorToMessage: error =>\n error.message ??\n (typeof error.error === 'string' ? error.error : error.error?.message) ??\n (typeof error.detail === 'string'\n ? error.detail\n : JSON.stringify(error.detail)) ??\n error.error_type ??\n 'TypeSafe request failed',\n});\n","// Version string of this package injected at build time.\ndeclare const __PACKAGE_VERSION__: string | undefined;\nexport const VERSION: string =\n typeof __PACKAGE_VERSION__ !== 'undefined'\n ? __PACKAGE_VERSION__\n : '0.0.0-test';\n","import {\n InvalidArgumentError,\n type Experimental_EvaluationModelV4 as EvaluationModelV4,\n type Experimental_EvaluationModelV4Answer as EvaluationModelV4Answer,\n type SharedV4Warning,\n} from '@ai-sdk/provider';\nimport {\n combineHeaders,\n createJsonResponseHandler,\n loadApiKey,\n postJsonToApi,\n resolve,\n serializeModelOptions,\n withUserAgentSuffix,\n WORKFLOW_SERIALIZE,\n WORKFLOW_DESERIALIZE,\n type FetchFunction,\n type Resolvable,\n} from '@ai-sdk/provider-utils';\nimport {\n typesafeEvaluationResponseSchema,\n typesafeFailedResponseHandler,\n} from './typesafe-ai-evaluation-api';\nimport { VERSION } from './version';\n\nexport type TypeSafeAiEvaluationModelId = 'jev-latest' | (string & {});\n\ntype TypeSafeAiEvaluationModelConfig = {\n provider: string;\n baseURL: string;\n headers?: Resolvable<Record<string, string | undefined>>;\n fetch?: FetchFunction;\n};\n\nexport class EvaluationTypeSafeAiModel implements EvaluationModelV4 {\n readonly specificationVersion = 'v4';\n readonly supportedQuestionTypes = ['choice', 'score', 'boolean'] as const;\n\n constructor(\n readonly modelId: TypeSafeAiEvaluationModelId,\n private readonly config: TypeSafeAiEvaluationModelConfig,\n ) {}\n\n get provider() {\n return this.config.provider;\n }\n\n static [WORKFLOW_SERIALIZE](model: EvaluationTypeSafeAiModel) {\n return serializeModelOptions({\n modelId: model.modelId,\n config: model.config,\n });\n }\n\n static [WORKFLOW_DESERIALIZE](options: {\n modelId: TypeSafeAiEvaluationModelId;\n config: TypeSafeAiEvaluationModelConfig;\n }) {\n return new EvaluationTypeSafeAiModel(options.modelId, options.config);\n }\n\n async doEvaluate({\n state,\n questions,\n headers,\n abortSignal,\n providerOptions,\n }: Parameters<EvaluationModelV4['doEvaluate']>[0]): Promise<\n Awaited<ReturnType<EvaluationModelV4['doEvaluate']>>\n > {\n for (const [id, question] of Object.entries(questions)) {\n if (\n question.type === 'choice' &&\n Object.keys(question.criteria).length > 255\n ) {\n throw new InvalidArgumentError({\n argument: `questions.${id}.criteria`,\n message: 'TypeSafe Choice questions support at most 255 options.',\n });\n }\n if (question.type === 'score' && question.criteria.length > 10) {\n throw new InvalidArgumentError({\n argument: `questions.${id}.criteria`,\n message: 'TypeSafe Score questions support at most 10 levels.',\n });\n }\n }\n\n const warnings: SharedV4Warning[] = Object.keys(\n providerOptions?.typesafe ?? {},\n ).map(option => ({\n type: 'unsupported',\n feature: `providerOptions.typesafe.${option}`,\n }));\n const modelHeaders =\n this.config.headers === undefined\n ? withUserAgentSuffix(\n {\n Authorization: `Bearer ${loadApiKey({ apiKey: undefined, environmentVariableName: 'TYPESAFE_AI_API_KEY', description: 'TypeSafe' })}`,\n },\n `ai-sdk-typesafe-ai/${VERSION}`,\n )\n : await resolve(this.config.headers);\n const {\n value: response,\n rawValue,\n responseHeaders,\n } = await postJsonToApi({\n url: `${this.config.baseURL}/systemone`,\n headers: combineHeaders(modelHeaders, headers),\n body: {\n model: this.modelId,\n state,\n questions: Object.fromEntries(\n Object.entries(questions).map(([id, question]) => [\n id,\n question.type === 'boolean'\n ? { ...question, type: 'noul' }\n : question,\n ]),\n ),\n },\n abortSignal,\n fetch: this.config.fetch,\n failedResponseHandler: typesafeFailedResponseHandler,\n successfulResponseHandler: createJsonResponseHandler(\n typesafeEvaluationResponseSchema,\n ),\n });\n\n const confidence = Object.fromEntries(\n Object.entries(response.answers).flatMap(([id, answer]) =>\n answer.type !== 'noul' && answer.confidence != null\n ? [[id, answer.confidence]]\n : [],\n ),\n );\n\n return {\n answers: Object.fromEntries(\n Object.entries(response.answers).map(\n ([id, answer]): [string, EvaluationModelV4Answer] => {\n switch (answer.type) {\n case 'noul':\n return [id, { type: 'boolean', probability: answer.noul }];\n case 'choice':\n return [\n id,\n {\n type: 'choice',\n choice: answer.choice,\n probabilities: answer.probabilities,\n },\n ];\n case 'score':\n return [\n id,\n {\n type: 'score',\n score: answer.score,\n probabilities: answer.probabilities,\n },\n ];\n }\n },\n ),\n ),\n usage: {\n inputTokens: response.usage?.input_tokens ?? undefined,\n outputTokens: response.usage?.output_tokens ?? undefined,\n },\n // The API rounds displayed probabilities and scores to two decimal places.\n rounding: { probabilityDecimals: 2, scoreDecimals: 2 },\n warnings,\n providerMetadata: { typesafe: { confidence } },\n response: {\n modelId: response.model ?? this.modelId,\n headers: responseHeaders,\n body: rawValue,\n },\n };\n }\n}\n","import {\n NoSuchModelError,\n type Experimental_EvaluationModelV4 as EvaluationModelV4,\n type ProviderV4,\n} from '@ai-sdk/provider';\nimport {\n loadApiKey,\n withoutTrailingSlash,\n withUserAgentSuffix,\n type FetchFunction,\n} from '@ai-sdk/provider-utils';\nimport {\n EvaluationTypeSafeAiModel,\n type TypeSafeAiEvaluationModelId,\n} from './typesafe-ai-evaluation-model';\nimport { VERSION } from './version';\n\n/** TypeSafe's experimental evaluation capability, isolated from ProviderV4. */\nexport interface TypeSafeAiProvider extends ProviderV4 {\n evaluationModel(modelId: TypeSafeAiEvaluationModelId): EvaluationModelV4;\n}\n\nexport interface TypeSafeAiProviderSettings {\n /** API key. Defaults to the TYPESAFE_AI_API_KEY environment variable. */\n apiKey?: string;\n /** API base URL. Defaults to https://api.typesafe.ai/v1. */\n baseURL?: string;\n headers?: Record<string, string>;\n fetch?: FetchFunction;\n}\n\nexport function createTypeSafeAi(\n options: TypeSafeAiProviderSettings = {},\n): TypeSafeAiProvider {\n const baseURL =\n withoutTrailingSlash(options.baseURL) ?? 'https://api.typesafe.ai/v1';\n const headers = () =>\n withUserAgentSuffix(\n {\n Authorization: `Bearer ${loadApiKey({ apiKey: options.apiKey, environmentVariableName: 'TYPESAFE_AI_API_KEY', description: 'TypeSafe' })}`,\n ...options.headers,\n },\n `ai-sdk-typesafe-ai/${VERSION}`,\n );\n\n return {\n specificationVersion: 'v4',\n evaluationModel: modelId =>\n new EvaluationTypeSafeAiModel(modelId, {\n provider: 'typesafe.evaluation',\n baseURL,\n headers,\n fetch: options.fetch,\n }),\n languageModel: modelId => {\n throw new NoSuchModelError({ modelId, modelType: 'languageModel' });\n },\n embeddingModel: modelId => {\n throw new NoSuchModelError({ modelId, modelType: 'embeddingModel' });\n },\n imageModel: modelId => {\n throw new NoSuchModelError({ modelId, modelType: 'imageModel' });\n },\n };\n}\n\nexport const typeSafeAi = createTypeSafeAi();\n"],"mappings":";;;;AAKA,MAAa,mCAAmC,EAAE,OAAO;CACvD,OAAO,EAAE,OAAO,CAAC,CAAC,QAAQ;CAC1B,SAAS,EAAE,OACT,EAAE,OAAO,GACT,EAAE,mBAAmB,QAAQ;EAC3B,EAAE,OAAO;GACP,MAAM,EAAE,QAAQ,QAAQ;GACxB,QAAQ,EAAE,OAAO;GACjB,eAAe,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC;GAC9C,YAAY,EAAE,OAAO,CAAC,CAAC,QAAQ;EACjC,CAAC;EACD,EAAE,OAAO;GACP,MAAM,EAAE,QAAQ,OAAO;GACvB,OAAO,EAAE,OAAO;GAChB,eAAe,EAAE,OAAO,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC;GAC9C,YAAY,EAAE,OAAO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iB,CAAC;EACrE;EACA,aAAY,YAAW;GACrB,MAAM,IAAI,iBAAiB;IAAE;IAAS,WAAW;GAAa,CAAC;EACjE;CACF;AACF;AAEA,MAAa,aAAa,iBAAiB"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ai-sdk/typesafe-ai",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -29,14 +29,14 @@
|
|
|
29
29
|
}
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@ai-sdk/provider": "4.0.
|
|
33
|
-
"@ai-sdk/provider-utils": "5.0.
|
|
32
|
+
"@ai-sdk/provider": "4.0.21",
|
|
33
|
+
"@ai-sdk/provider-utils": "5.0.53"
|
|
34
34
|
},
|
|
35
35
|
"devDependencies": {
|
|
36
|
-
"@ai-sdk/test-server": "2.0.
|
|
36
|
+
"@ai-sdk/test-server": "2.0.3",
|
|
37
37
|
"@types/node": "22.19.19",
|
|
38
38
|
"@vercel/ai-tsconfig": "0.0.0",
|
|
39
|
-
"
|
|
39
|
+
"tsdown": "^0.23.0",
|
|
40
40
|
"typescript": "5.8.3",
|
|
41
41
|
"zod": "3.25.76",
|
|
42
42
|
"@edge-runtime/vm": "^5.0.0"
|
|
@@ -65,9 +65,9 @@
|
|
|
65
65
|
],
|
|
66
66
|
"source": "./src/index.ts",
|
|
67
67
|
"scripts": {
|
|
68
|
-
"build": "pnpm clean &&
|
|
69
|
-
"build:watch": "pnpm clean &&
|
|
70
|
-
"clean": "del-cli
|
|
68
|
+
"build": "pnpm clean && tsdown",
|
|
69
|
+
"build:watch": "pnpm clean && tsdown --watch",
|
|
70
|
+
"clean": "del-cli docs *.tsbuildinfo",
|
|
71
71
|
"type-check": "tsc --build",
|
|
72
72
|
"test": "pnpm test:node && pnpm test:edge",
|
|
73
73
|
"test:update": "pnpm test:node -u",
|