@crowdstrike/aidr 1.0.2

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.
Files changed (51) hide show
  1. package/.editorconfig +9 -0
  2. package/.github/CODEOWNERS +1 -0
  3. package/.github/workflows/ci.yml +128 -0
  4. package/.pnpmfile.cjs +17 -0
  5. package/.releaserc.json +21 -0
  6. package/LICENSE.txt +21 -0
  7. package/README.md +3 -0
  8. package/biome.json +67 -0
  9. package/dist/chunk.cjs +34 -0
  10. package/dist/index.cjs +356 -0
  11. package/dist/index.d.cts +2347 -0
  12. package/dist/index.d.mts +2347 -0
  13. package/dist/index.mjs +354 -0
  14. package/dist/schemas/ai-guard.cjs +1000 -0
  15. package/dist/schemas/ai-guard.d.cts +1232 -0
  16. package/dist/schemas/ai-guard.d.mts +1232 -0
  17. package/dist/schemas/ai-guard.mjs +907 -0
  18. package/dist/schemas/index.cjs +7 -0
  19. package/dist/schemas/index.d.cts +64 -0
  20. package/dist/schemas/index.d.mts +64 -0
  21. package/dist/schemas/index.mjs +3 -0
  22. package/dist/schemas.cjs +139 -0
  23. package/dist/schemas.mjs +108 -0
  24. package/flake.lock +59 -0
  25. package/flake.nix +26 -0
  26. package/openapi-ts.config.ts +15 -0
  27. package/package.json +55 -0
  28. package/pnpm-workspace.yaml +3 -0
  29. package/scripts/generate-models +15 -0
  30. package/scripts/test +10 -0
  31. package/specs/ai-guard.openapi.json +3721 -0
  32. package/src/client.ts +441 -0
  33. package/src/core/error.ts +78 -0
  34. package/src/index.ts +2 -0
  35. package/src/internal/builtin-types.ts +18 -0
  36. package/src/internal/errors.ts +34 -0
  37. package/src/internal/headers.ts +100 -0
  38. package/src/internal/parse.ts +30 -0
  39. package/src/internal/request-options.ts +57 -0
  40. package/src/internal/types.ts +3 -0
  41. package/src/internal/utils/sleep.ts +3 -0
  42. package/src/internal/utils/values.ts +38 -0
  43. package/src/schemas/ai-guard.ts +1215 -0
  44. package/src/schemas/index.ts +114 -0
  45. package/src/services/ai-guard.ts +27 -0
  46. package/src/types/ai-guard.ts +2276 -0
  47. package/src/types/index.ts +161 -0
  48. package/tests/ai-guard.test.ts +29 -0
  49. package/tsconfig.json +26 -0
  50. package/tsdown.config.mts +14 -0
  51. package/vitest.config.mts +4 -0
package/dist/index.mjs ADDED
@@ -0,0 +1,354 @@
1
+ import { n as AcceptedResponseSchema } from "./schemas.mjs";
2
+ import * as v from "valibot";
3
+
4
+ //#region src/core/error.ts
5
+ var AIDRError = class extends Error {};
6
+ var APIError = class APIError extends AIDRError {
7
+ /** HTTP status for the response that caused the error */
8
+ status;
9
+ /** HTTP headers for the response that caused the error */
10
+ headers;
11
+ /** JSON body of the response that caused the error */
12
+ error;
13
+ constructor(status, error, message, headers) {
14
+ super(`${APIError.makeMessage(status, error, message)}`);
15
+ this.status = status;
16
+ this.headers = headers;
17
+ this.error = error;
18
+ }
19
+ static makeMessage(status, error, message) {
20
+ const msg = error?.message ? typeof error.message === "string" ? error.message : JSON.stringify(error.message) : error ? JSON.stringify(error) : message;
21
+ if (status && msg) return `${status} ${msg}`;
22
+ if (status) return `${status} status code (no body)`;
23
+ if (msg) return msg;
24
+ return "(no status code or body)";
25
+ }
26
+ };
27
+ var APIUserAbortError = class extends APIError {
28
+ constructor({ message } = {}) {
29
+ super(void 0, void 0, message || "Request was aborted.", void 0);
30
+ }
31
+ };
32
+ var APIConnectionError = class extends APIError {
33
+ constructor({ message, cause }) {
34
+ super(void 0, void 0, message || "Connection error.", void 0);
35
+ if (cause) this.cause = cause;
36
+ }
37
+ };
38
+
39
+ //#endregion
40
+ //#region src/internal/errors.ts
41
+ function castToError(err) {
42
+ if (err instanceof Error) return err;
43
+ if (typeof err === "object" && err !== null) {
44
+ try {
45
+ if (Object.prototype.toString.call(err) === "[object Error]") {
46
+ const error = new Error(err.message, err.cause ? { cause: err.cause } : {});
47
+ if (err.stack) error.stack = err.stack;
48
+ if (err.cause && !error.cause) error.cause = err.cause;
49
+ if (err.name) error.name = err.name;
50
+ return error;
51
+ }
52
+ } catch {}
53
+ try {
54
+ return new Error(JSON.stringify(err));
55
+ } catch {}
56
+ }
57
+ return new Error(err);
58
+ }
59
+
60
+ //#endregion
61
+ //#region src/internal/utils/values.ts
62
+ const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
63
+ function isAbsoluteURL(url) {
64
+ return startsWithSchemeRegexp.test(url);
65
+ }
66
+ function stringifyQuery(query) {
67
+ return Object.entries(query).filter(([_, value]) => typeof value !== "undefined").map(([key, value]) => {
68
+ if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
69
+ if (value === null) return `${encodeURIComponent(key)}=`;
70
+ throw new AIDRError(`Cannot stringify type ${typeof value}; Expected string, number, boolean, or null.`);
71
+ }).join("&");
72
+ }
73
+ let isArray = (val) => (isArray = Array.isArray, isArray(val));
74
+ const isReadonlyArray = isArray;
75
+
76
+ //#endregion
77
+ //#region src/internal/headers.ts
78
+ const brand_privateNullableHeaders = /* @__PURE__ */ Symbol("brand.privateNullableHeaders");
79
+ function* iterateHeaders(headers) {
80
+ if (!headers) return;
81
+ if (brand_privateNullableHeaders in headers) {
82
+ const { values, nulls } = headers;
83
+ yield* values.entries();
84
+ for (const name of nulls) yield [name, null];
85
+ return;
86
+ }
87
+ let shouldClear = false;
88
+ let iter;
89
+ if (headers instanceof Headers) iter = headers.entries();
90
+ else if (isReadonlyArray(headers)) iter = headers;
91
+ else {
92
+ shouldClear = true;
93
+ iter = Object.entries(headers ?? {});
94
+ }
95
+ for (const row of iter) {
96
+ const name = row[0];
97
+ if (typeof name !== "string") throw new TypeError("expected header name to be a string");
98
+ const values = isReadonlyArray(row[1]) ? row[1] : [row[1]];
99
+ let didClear = false;
100
+ for (const value of values) {
101
+ if (value === void 0) continue;
102
+ if (shouldClear && !didClear) {
103
+ didClear = true;
104
+ yield [name, null];
105
+ }
106
+ yield [name, value];
107
+ }
108
+ }
109
+ }
110
+ function buildHeaders(newHeaders) {
111
+ const targetHeaders = new Headers();
112
+ const nullHeaders = /* @__PURE__ */ new Set();
113
+ for (const headers of newHeaders) {
114
+ const seenHeaders = /* @__PURE__ */ new Set();
115
+ for (const [name, value] of iterateHeaders(headers)) {
116
+ const lowerName = name.toLowerCase();
117
+ if (!seenHeaders.has(lowerName)) {
118
+ targetHeaders.delete(name);
119
+ seenHeaders.add(lowerName);
120
+ }
121
+ if (value === null) {
122
+ targetHeaders.delete(name);
123
+ nullHeaders.add(lowerName);
124
+ } else {
125
+ targetHeaders.append(name, value);
126
+ nullHeaders.delete(lowerName);
127
+ }
128
+ }
129
+ }
130
+ return {
131
+ [brand_privateNullableHeaders]: true,
132
+ values: targetHeaders,
133
+ nulls: nullHeaders
134
+ };
135
+ }
136
+
137
+ //#endregion
138
+ //#region src/internal/parse.ts
139
+ async function defaultParseResponse(props) {
140
+ const { response } = props;
141
+ return await (async () => {
142
+ if (response.status === 204) return null;
143
+ const mediaType = response.headers.get("content-type")?.split(";")[0]?.trim();
144
+ if (mediaType?.includes("application/json") || mediaType?.endsWith("+json")) return await response.json();
145
+ return await response.text();
146
+ })();
147
+ }
148
+
149
+ //#endregion
150
+ //#region src/internal/utils/sleep.ts
151
+ function sleep(ms) {
152
+ return new Promise((resolve) => setTimeout(resolve, ms));
153
+ }
154
+
155
+ //#endregion
156
+ //#region src/client.ts
157
+ function isAcceptedResponse(response) {
158
+ return v.safeParse(AcceptedResponseSchema, response).success;
159
+ }
160
+ var Client = class {
161
+ /** CS AIDR API token.*/
162
+ token;
163
+ /**
164
+ * Template for constructing the base URL for API requests. The placeholder
165
+ * `{SERVICE_NAME}` will be replaced with the service name slug.
166
+ */
167
+ baseURLTemplate;
168
+ timeout;
169
+ maxRetries;
170
+ maxPollingAttempts;
171
+ fetch;
172
+ _options;
173
+ constructor(options) {
174
+ if (options.token === void 0) throw new AIDRError("Client was instantiated without an API token.");
175
+ if (options.baseURLTemplate === void 0) throw new AIDRError("Client was instantiated without a base URL template.");
176
+ this.baseURLTemplate = options.baseURLTemplate;
177
+ this.fetch = options.fetch ?? fetch;
178
+ this.maxRetries = options.maxRetries ?? 2;
179
+ this.maxPollingAttempts = options.maxPollingAttempts ?? 5;
180
+ this.timeout = options.timeout ?? 6e4;
181
+ this.token = options.token;
182
+ this._options = options;
183
+ }
184
+ /**
185
+ * Will retrieve the result, or will return HTTP/202 if the original request
186
+ * is still in progress.
187
+ */
188
+ getAsyncRequest(requestId) {
189
+ return this.get(`/v1/request/${requestId}`);
190
+ }
191
+ /**
192
+ * Polls for an async request result with exponential backoff.
193
+ * Continues polling until a success response is received or max attempts are reached.
194
+ */
195
+ async pollAsyncRequest(requestId, maxAttempts) {
196
+ let lastResponse = null;
197
+ for (let attempt = 0; attempt < maxAttempts; attempt++) {
198
+ const response = await this.getAsyncRequest(requestId);
199
+ if (response.status === "Success") return response;
200
+ lastResponse = response;
201
+ if (attempt < maxAttempts - 1) await sleep(this.calculateDefaultRetryTimeoutMillis(maxAttempts - attempt - 1, maxAttempts));
202
+ }
203
+ if (lastResponse === null) throw new AIDRError("Polling failed: no response received");
204
+ return lastResponse;
205
+ }
206
+ async get(path, opts) {
207
+ return await this.methodRequest("get", path, opts);
208
+ }
209
+ async post(path, opts) {
210
+ return await this.methodRequest("post", path, opts);
211
+ }
212
+ async methodRequest(method, path, opts) {
213
+ return await this.request(Promise.resolve(opts).then((opts$1) => {
214
+ return {
215
+ method,
216
+ path,
217
+ ...opts$1
218
+ };
219
+ }));
220
+ }
221
+ async request(options, remainingRetries = null) {
222
+ const parsed = await defaultParseResponse(await this.makeRequest(options, remainingRetries));
223
+ if (isAcceptedResponse(parsed)) {
224
+ const maxPollingAttempts = (await Promise.resolve(options)).maxPollingAttempts ?? this.maxPollingAttempts;
225
+ if (maxPollingAttempts <= 0) return parsed;
226
+ return await this.pollAsyncRequest(parsed.request_id, maxPollingAttempts);
227
+ }
228
+ return parsed;
229
+ }
230
+ async makeRequest(optionsInput, retriesRemaining) {
231
+ const options = await optionsInput;
232
+ const maxRetries = options.maxRetries ?? this.maxRetries;
233
+ if (retriesRemaining == null) retriesRemaining = maxRetries;
234
+ const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });
235
+ if (options.signal?.aborted) throw new APIUserAbortError();
236
+ const controller = new AbortController();
237
+ const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
238
+ if (response instanceof globalThis.Error) {
239
+ if (options.signal?.aborted) throw new APIUserAbortError();
240
+ if (retriesRemaining) return await this.retryRequest(options, retriesRemaining);
241
+ throw new APIConnectionError({ cause: response });
242
+ }
243
+ if (!response.ok) {
244
+ const shouldRetry = this.shouldRetry(response);
245
+ if (retriesRemaining && shouldRetry) return await this.retryRequest(options, retriesRemaining);
246
+ }
247
+ return {
248
+ response,
249
+ options,
250
+ controller
251
+ };
252
+ }
253
+ shouldRetry(response) {
254
+ return response.status === 408 || response.status === 409 || response.status === 429 || response.status >= 500;
255
+ }
256
+ async retryRequest(options, retriesRemaining) {
257
+ const maxRetries = options.maxRetries ?? this.maxRetries;
258
+ await sleep(this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries));
259
+ return this.makeRequest(options, retriesRemaining - 1);
260
+ }
261
+ calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries) {
262
+ const initialRetryDelay = .5;
263
+ const maxRetryDelay = 8;
264
+ const numRetries = maxRetries - retriesRemaining;
265
+ return Math.min(initialRetryDelay * 2 ** numRetries, maxRetryDelay) * (1 - Math.random() * .25) * 1e3;
266
+ }
267
+ async fetchWithTimeout(url, init, ms, controller) {
268
+ const { signal, method, ...options } = init || {};
269
+ if (signal) signal.addEventListener("abort", () => controller.abort());
270
+ const timeout = setTimeout(() => controller.abort(), ms);
271
+ const fetchOptions = {
272
+ signal: controller.signal,
273
+ method: "GET",
274
+ ...options
275
+ };
276
+ if (method) fetchOptions.method = method.toUpperCase();
277
+ try {
278
+ return await this.fetch.call(void 0, url, fetchOptions);
279
+ } finally {
280
+ clearTimeout(timeout);
281
+ }
282
+ }
283
+ buildRequest(inputOptions, { retryCount = 0 } = {}) {
284
+ const options = { ...inputOptions };
285
+ const { method, path, query, baseURLTemplate } = options;
286
+ const url = this.buildURL(path, query, baseURLTemplate);
287
+ options.timeout = options.timeout ?? this.timeout;
288
+ const { bodyHeaders, body } = this.buildBody({ options });
289
+ return {
290
+ req: {
291
+ method,
292
+ headers: this.buildHeaders({
293
+ options: inputOptions,
294
+ method,
295
+ bodyHeaders,
296
+ retryCount
297
+ }),
298
+ ...options.signal && { signal: options.signal },
299
+ ...body && { body }
300
+ },
301
+ url,
302
+ timeout: options.timeout
303
+ };
304
+ }
305
+ buildURL(path, query, baseURLTemplate = this.baseURLTemplate) {
306
+ const url = new URL((isAbsoluteURL(path) ? path : baseURLTemplate + (baseURLTemplate.endsWith("/") && path.startsWith("/") ? path.slice(1) : path)).replaceAll("{SERVICE_NAME}", this.serviceName));
307
+ if (typeof query === "object" && query && !Array.isArray(query)) url.search = stringifyQuery(query);
308
+ return url.toString();
309
+ }
310
+ buildBody({ options: { body, headers: _rawHeaders } }) {
311
+ if (!body) return {
312
+ bodyHeaders: void 0,
313
+ body: void 0
314
+ };
315
+ return {
316
+ bodyHeaders: { "content-type": "application/json" },
317
+ body: JSON.stringify(body)
318
+ };
319
+ }
320
+ buildHeaders({ options, bodyHeaders }) {
321
+ return buildHeaders([
322
+ {
323
+ Accept: "application/json",
324
+ "User-Agent": "aidr-typescript"
325
+ },
326
+ this.authHeaders(),
327
+ this._options.defaultHeaders,
328
+ bodyHeaders,
329
+ options.headers
330
+ ]).values;
331
+ }
332
+ authHeaders() {
333
+ return buildHeaders([{ Authorization: `Bearer ${this.token}` }]);
334
+ }
335
+ };
336
+
337
+ //#endregion
338
+ //#region src/services/ai-guard.ts
339
+ var AIGuard = class extends Client {
340
+ serviceName = "aiguard";
341
+ /**
342
+ * Analyze and redact content to avoid manipulation of the model, addition of
343
+ * malicious content, and other undesirable data transfers.
344
+ */
345
+ guardChatCompletions(body, options) {
346
+ return this.post("/v1/guard_chat_completions", {
347
+ body,
348
+ ...options
349
+ });
350
+ }
351
+ };
352
+
353
+ //#endregion
354
+ export { AIGuard };