@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.
- package/.editorconfig +9 -0
- package/.github/CODEOWNERS +1 -0
- package/.github/workflows/ci.yml +128 -0
- package/.pnpmfile.cjs +17 -0
- package/.releaserc.json +21 -0
- package/LICENSE.txt +21 -0
- package/README.md +3 -0
- package/biome.json +67 -0
- package/dist/chunk.cjs +34 -0
- package/dist/index.cjs +356 -0
- package/dist/index.d.cts +2347 -0
- package/dist/index.d.mts +2347 -0
- package/dist/index.mjs +354 -0
- package/dist/schemas/ai-guard.cjs +1000 -0
- package/dist/schemas/ai-guard.d.cts +1232 -0
- package/dist/schemas/ai-guard.d.mts +1232 -0
- package/dist/schemas/ai-guard.mjs +907 -0
- package/dist/schemas/index.cjs +7 -0
- package/dist/schemas/index.d.cts +64 -0
- package/dist/schemas/index.d.mts +64 -0
- package/dist/schemas/index.mjs +3 -0
- package/dist/schemas.cjs +139 -0
- package/dist/schemas.mjs +108 -0
- package/flake.lock +59 -0
- package/flake.nix +26 -0
- package/openapi-ts.config.ts +15 -0
- package/package.json +55 -0
- package/pnpm-workspace.yaml +3 -0
- package/scripts/generate-models +15 -0
- package/scripts/test +10 -0
- package/specs/ai-guard.openapi.json +3721 -0
- package/src/client.ts +441 -0
- package/src/core/error.ts +78 -0
- package/src/index.ts +2 -0
- package/src/internal/builtin-types.ts +18 -0
- package/src/internal/errors.ts +34 -0
- package/src/internal/headers.ts +100 -0
- package/src/internal/parse.ts +30 -0
- package/src/internal/request-options.ts +57 -0
- package/src/internal/types.ts +3 -0
- package/src/internal/utils/sleep.ts +3 -0
- package/src/internal/utils/values.ts +38 -0
- package/src/schemas/ai-guard.ts +1215 -0
- package/src/schemas/index.ts +114 -0
- package/src/services/ai-guard.ts +27 -0
- package/src/types/ai-guard.ts +2276 -0
- package/src/types/index.ts +161 -0
- package/tests/ai-guard.test.ts +29 -0
- package/tsconfig.json +26 -0
- package/tsdown.config.mts +14 -0
- package/vitest.config.mts +4 -0
package/.editorconfig
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
* @kenany
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
pull_request:
|
|
9
|
+
types:
|
|
10
|
+
- opened
|
|
11
|
+
- synchronize
|
|
12
|
+
- reopened
|
|
13
|
+
- ready_for_review
|
|
14
|
+
|
|
15
|
+
merge_group:
|
|
16
|
+
|
|
17
|
+
workflow_dispatch:
|
|
18
|
+
|
|
19
|
+
permissions:
|
|
20
|
+
contents: read
|
|
21
|
+
|
|
22
|
+
concurrency:
|
|
23
|
+
group: ${{ github.workflow }}-${{ github.event.number || github.ref }}
|
|
24
|
+
cancel-in-progress: true
|
|
25
|
+
|
|
26
|
+
jobs:
|
|
27
|
+
build:
|
|
28
|
+
runs-on: ubuntu-24.04
|
|
29
|
+
steps:
|
|
30
|
+
- name: Checkout code
|
|
31
|
+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
|
32
|
+
|
|
33
|
+
- run: corepack enable
|
|
34
|
+
|
|
35
|
+
- name: Setup Node.js
|
|
36
|
+
uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
|
|
37
|
+
with:
|
|
38
|
+
node-version: 24.11.1
|
|
39
|
+
cache: pnpm
|
|
40
|
+
|
|
41
|
+
- name: Install dependencies
|
|
42
|
+
run: pnpm install
|
|
43
|
+
|
|
44
|
+
- name: Build
|
|
45
|
+
run: pnpm build
|
|
46
|
+
|
|
47
|
+
lint:
|
|
48
|
+
runs-on: ubuntu-24.04
|
|
49
|
+
steps:
|
|
50
|
+
- name: Checkout code
|
|
51
|
+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
|
52
|
+
|
|
53
|
+
- run: corepack enable
|
|
54
|
+
|
|
55
|
+
- name: Setup Node.js
|
|
56
|
+
uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
|
|
57
|
+
with:
|
|
58
|
+
node-version: 24.11.1
|
|
59
|
+
cache: pnpm
|
|
60
|
+
|
|
61
|
+
- name: Install dependencies
|
|
62
|
+
run: pnpm install
|
|
63
|
+
|
|
64
|
+
- name: Lint
|
|
65
|
+
run: pnpm lint
|
|
66
|
+
|
|
67
|
+
test:
|
|
68
|
+
runs-on: ubuntu-24.04
|
|
69
|
+
steps:
|
|
70
|
+
- name: Checkout code
|
|
71
|
+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
|
72
|
+
|
|
73
|
+
- run: corepack enable
|
|
74
|
+
|
|
75
|
+
- name: Setup Node.js
|
|
76
|
+
uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
|
|
77
|
+
with:
|
|
78
|
+
node-version: 24.11.1
|
|
79
|
+
cache: pnpm
|
|
80
|
+
|
|
81
|
+
- name: Install dependencies
|
|
82
|
+
run: pnpm install
|
|
83
|
+
|
|
84
|
+
- name: Test
|
|
85
|
+
run: ./scripts/test
|
|
86
|
+
|
|
87
|
+
release:
|
|
88
|
+
needs:
|
|
89
|
+
- build
|
|
90
|
+
- lint
|
|
91
|
+
- test
|
|
92
|
+
if: github.repository == 'crowdstrike/aidr-typescript' && github.event_name != 'pull_request'
|
|
93
|
+
runs-on: ubuntu-24.04
|
|
94
|
+
environment: release
|
|
95
|
+
permissions:
|
|
96
|
+
contents: write
|
|
97
|
+
id-token: write
|
|
98
|
+
issues: write
|
|
99
|
+
packages: write
|
|
100
|
+
pull-requests: write
|
|
101
|
+
steps:
|
|
102
|
+
- name: Checkout code
|
|
103
|
+
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
|
|
104
|
+
with:
|
|
105
|
+
fetch-depth: 0
|
|
106
|
+
filter: blob:none
|
|
107
|
+
show-progress: false
|
|
108
|
+
|
|
109
|
+
- run: corepack enable
|
|
110
|
+
|
|
111
|
+
- name: Setup Node.js
|
|
112
|
+
uses: actions/setup-node@395ad3262231945c25e8478fd5baf05154b1d79f # v6.1.0
|
|
113
|
+
with:
|
|
114
|
+
node-version: 24.11.1
|
|
115
|
+
cache: pnpm
|
|
116
|
+
|
|
117
|
+
- name: Install dependencies
|
|
118
|
+
run: pnpm install
|
|
119
|
+
|
|
120
|
+
- name: Build
|
|
121
|
+
run: pnpm build
|
|
122
|
+
|
|
123
|
+
- name: semantic-release
|
|
124
|
+
run: pnpx semantic-release
|
|
125
|
+
env:
|
|
126
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
|
127
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
128
|
+
LOG_LEVEL: debug
|
package/.pnpmfile.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// <https://github.com/pnpm/pnpm/issues/6667#issuecomment-2971121163>
|
|
2
|
+
|
|
3
|
+
function afterAllResolved(lockfile) {
|
|
4
|
+
// Remove any tarball URLs from the lockfile.
|
|
5
|
+
for (const key in lockfile.packages) {
|
|
6
|
+
if (lockfile.packages[key].resolution?.tarball) {
|
|
7
|
+
delete lockfile.packages[key].resolution.tarball;
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
return lockfile;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
module.exports = {
|
|
14
|
+
hooks: {
|
|
15
|
+
afterAllResolved,
|
|
16
|
+
},
|
|
17
|
+
};
|
package/.releaserc.json
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"plugins": [
|
|
3
|
+
"@semantic-release/commit-analyzer",
|
|
4
|
+
"@semantic-release/release-notes-generator",
|
|
5
|
+
[
|
|
6
|
+
"@semantic-release/github",
|
|
7
|
+
{
|
|
8
|
+
"releasedLabels": false,
|
|
9
|
+
"successCommentCondition": "<% return issue.user.type !== 'Bot'; %>"
|
|
10
|
+
}
|
|
11
|
+
],
|
|
12
|
+
"@semantic-release/npm"
|
|
13
|
+
],
|
|
14
|
+
"branches": [
|
|
15
|
+
{
|
|
16
|
+
"name": "main"
|
|
17
|
+
}
|
|
18
|
+
],
|
|
19
|
+
"preset": "conventionalcommits",
|
|
20
|
+
"tagFormat": "v${version}"
|
|
21
|
+
}
|
package/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 CrowdStrike
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
package/biome.json
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
|
|
3
|
+
"assist": {
|
|
4
|
+
"actions": {
|
|
5
|
+
"source": {
|
|
6
|
+
"organizeImports": "on",
|
|
7
|
+
"useSortedAttributes": "on",
|
|
8
|
+
"useSortedKeys": "off"
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
},
|
|
12
|
+
"formatter": {
|
|
13
|
+
"enabled": true,
|
|
14
|
+
"formatWithErrors": true,
|
|
15
|
+
"indentStyle": "space",
|
|
16
|
+
"indentWidth": 2,
|
|
17
|
+
"lineEnding": "lf",
|
|
18
|
+
"lineWidth": 80,
|
|
19
|
+
"attributePosition": "auto",
|
|
20
|
+
"bracketSpacing": true
|
|
21
|
+
},
|
|
22
|
+
"javascript": {
|
|
23
|
+
"formatter": {
|
|
24
|
+
"arrowParentheses": "always",
|
|
25
|
+
"bracketSameLine": false,
|
|
26
|
+
"jsxQuoteStyle": "double",
|
|
27
|
+
"quoteProperties": "asNeeded",
|
|
28
|
+
"quoteStyle": "single",
|
|
29
|
+
"semicolons": "always",
|
|
30
|
+
"trailingCommas": "es5"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"json": {
|
|
34
|
+
"assist": {
|
|
35
|
+
"enabled": false
|
|
36
|
+
},
|
|
37
|
+
"formatter": {
|
|
38
|
+
"enabled": true,
|
|
39
|
+
"indentStyle": "space",
|
|
40
|
+
"indentWidth": 2,
|
|
41
|
+
"lineEnding": "lf",
|
|
42
|
+
"lineWidth": 80,
|
|
43
|
+
"trailingCommas": "none"
|
|
44
|
+
},
|
|
45
|
+
"parser": {
|
|
46
|
+
"allowComments": false,
|
|
47
|
+
"allowTrailingCommas": false
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"linter": {
|
|
51
|
+
"rules": {
|
|
52
|
+
"performance": {
|
|
53
|
+
"noBarrelFile": "off",
|
|
54
|
+
"noNamespaceImport": "off"
|
|
55
|
+
},
|
|
56
|
+
"style": {
|
|
57
|
+
"noNestedTernary": "off"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"vcs": {
|
|
62
|
+
"enabled": true,
|
|
63
|
+
"clientKind": "git",
|
|
64
|
+
"useIgnoreFile": true,
|
|
65
|
+
"defaultBranch": "main"
|
|
66
|
+
}
|
|
67
|
+
}
|
package/dist/chunk.cjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
//#region rolldown:runtime
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __copyProps = (to, from, except, desc) => {
|
|
9
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
10
|
+
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) {
|
|
13
|
+
__defProp(to, key, {
|
|
14
|
+
get: ((k) => from[k]).bind(null, key),
|
|
15
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return to;
|
|
21
|
+
};
|
|
22
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
23
|
+
value: mod,
|
|
24
|
+
enumerable: true
|
|
25
|
+
}) : target, mod));
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
|
|
29
|
+
Object.defineProperty(exports, '__toESM', {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
get: function () {
|
|
32
|
+
return __toESM;
|
|
33
|
+
}
|
|
34
|
+
});
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,356 @@
|
|
|
1
|
+
const require_chunk = require('./chunk.cjs');
|
|
2
|
+
const require_schemas = require('./schemas.cjs');
|
|
3
|
+
let valibot = require("valibot");
|
|
4
|
+
valibot = require_chunk.__toESM(valibot);
|
|
5
|
+
|
|
6
|
+
//#region src/core/error.ts
|
|
7
|
+
var AIDRError = class extends Error {};
|
|
8
|
+
var APIError = class APIError extends AIDRError {
|
|
9
|
+
/** HTTP status for the response that caused the error */
|
|
10
|
+
status;
|
|
11
|
+
/** HTTP headers for the response that caused the error */
|
|
12
|
+
headers;
|
|
13
|
+
/** JSON body of the response that caused the error */
|
|
14
|
+
error;
|
|
15
|
+
constructor(status, error, message, headers) {
|
|
16
|
+
super(`${APIError.makeMessage(status, error, message)}`);
|
|
17
|
+
this.status = status;
|
|
18
|
+
this.headers = headers;
|
|
19
|
+
this.error = error;
|
|
20
|
+
}
|
|
21
|
+
static makeMessage(status, error, message) {
|
|
22
|
+
const msg = error?.message ? typeof error.message === "string" ? error.message : JSON.stringify(error.message) : error ? JSON.stringify(error) : message;
|
|
23
|
+
if (status && msg) return `${status} ${msg}`;
|
|
24
|
+
if (status) return `${status} status code (no body)`;
|
|
25
|
+
if (msg) return msg;
|
|
26
|
+
return "(no status code or body)";
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var APIUserAbortError = class extends APIError {
|
|
30
|
+
constructor({ message } = {}) {
|
|
31
|
+
super(void 0, void 0, message || "Request was aborted.", void 0);
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
var APIConnectionError = class extends APIError {
|
|
35
|
+
constructor({ message, cause }) {
|
|
36
|
+
super(void 0, void 0, message || "Connection error.", void 0);
|
|
37
|
+
if (cause) this.cause = cause;
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
//#endregion
|
|
42
|
+
//#region src/internal/errors.ts
|
|
43
|
+
function castToError(err) {
|
|
44
|
+
if (err instanceof Error) return err;
|
|
45
|
+
if (typeof err === "object" && err !== null) {
|
|
46
|
+
try {
|
|
47
|
+
if (Object.prototype.toString.call(err) === "[object Error]") {
|
|
48
|
+
const error = new Error(err.message, err.cause ? { cause: err.cause } : {});
|
|
49
|
+
if (err.stack) error.stack = err.stack;
|
|
50
|
+
if (err.cause && !error.cause) error.cause = err.cause;
|
|
51
|
+
if (err.name) error.name = err.name;
|
|
52
|
+
return error;
|
|
53
|
+
}
|
|
54
|
+
} catch {}
|
|
55
|
+
try {
|
|
56
|
+
return new Error(JSON.stringify(err));
|
|
57
|
+
} catch {}
|
|
58
|
+
}
|
|
59
|
+
return new Error(err);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/internal/utils/values.ts
|
|
64
|
+
const startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
|
|
65
|
+
function isAbsoluteURL(url) {
|
|
66
|
+
return startsWithSchemeRegexp.test(url);
|
|
67
|
+
}
|
|
68
|
+
function stringifyQuery(query) {
|
|
69
|
+
return Object.entries(query).filter(([_, value]) => typeof value !== "undefined").map(([key, value]) => {
|
|
70
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") return `${encodeURIComponent(key)}=${encodeURIComponent(value)}`;
|
|
71
|
+
if (value === null) return `${encodeURIComponent(key)}=`;
|
|
72
|
+
throw new AIDRError(`Cannot stringify type ${typeof value}; Expected string, number, boolean, or null.`);
|
|
73
|
+
}).join("&");
|
|
74
|
+
}
|
|
75
|
+
let isArray = (val) => (isArray = Array.isArray, isArray(val));
|
|
76
|
+
const isReadonlyArray = isArray;
|
|
77
|
+
|
|
78
|
+
//#endregion
|
|
79
|
+
//#region src/internal/headers.ts
|
|
80
|
+
const brand_privateNullableHeaders = /* @__PURE__ */ Symbol("brand.privateNullableHeaders");
|
|
81
|
+
function* iterateHeaders(headers) {
|
|
82
|
+
if (!headers) return;
|
|
83
|
+
if (brand_privateNullableHeaders in headers) {
|
|
84
|
+
const { values, nulls } = headers;
|
|
85
|
+
yield* values.entries();
|
|
86
|
+
for (const name of nulls) yield [name, null];
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
let shouldClear = false;
|
|
90
|
+
let iter;
|
|
91
|
+
if (headers instanceof Headers) iter = headers.entries();
|
|
92
|
+
else if (isReadonlyArray(headers)) iter = headers;
|
|
93
|
+
else {
|
|
94
|
+
shouldClear = true;
|
|
95
|
+
iter = Object.entries(headers ?? {});
|
|
96
|
+
}
|
|
97
|
+
for (const row of iter) {
|
|
98
|
+
const name = row[0];
|
|
99
|
+
if (typeof name !== "string") throw new TypeError("expected header name to be a string");
|
|
100
|
+
const values = isReadonlyArray(row[1]) ? row[1] : [row[1]];
|
|
101
|
+
let didClear = false;
|
|
102
|
+
for (const value of values) {
|
|
103
|
+
if (value === void 0) continue;
|
|
104
|
+
if (shouldClear && !didClear) {
|
|
105
|
+
didClear = true;
|
|
106
|
+
yield [name, null];
|
|
107
|
+
}
|
|
108
|
+
yield [name, value];
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
function buildHeaders(newHeaders) {
|
|
113
|
+
const targetHeaders = new Headers();
|
|
114
|
+
const nullHeaders = /* @__PURE__ */ new Set();
|
|
115
|
+
for (const headers of newHeaders) {
|
|
116
|
+
const seenHeaders = /* @__PURE__ */ new Set();
|
|
117
|
+
for (const [name, value] of iterateHeaders(headers)) {
|
|
118
|
+
const lowerName = name.toLowerCase();
|
|
119
|
+
if (!seenHeaders.has(lowerName)) {
|
|
120
|
+
targetHeaders.delete(name);
|
|
121
|
+
seenHeaders.add(lowerName);
|
|
122
|
+
}
|
|
123
|
+
if (value === null) {
|
|
124
|
+
targetHeaders.delete(name);
|
|
125
|
+
nullHeaders.add(lowerName);
|
|
126
|
+
} else {
|
|
127
|
+
targetHeaders.append(name, value);
|
|
128
|
+
nullHeaders.delete(lowerName);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return {
|
|
133
|
+
[brand_privateNullableHeaders]: true,
|
|
134
|
+
values: targetHeaders,
|
|
135
|
+
nulls: nullHeaders
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
//#endregion
|
|
140
|
+
//#region src/internal/parse.ts
|
|
141
|
+
async function defaultParseResponse(props) {
|
|
142
|
+
const { response } = props;
|
|
143
|
+
return await (async () => {
|
|
144
|
+
if (response.status === 204) return null;
|
|
145
|
+
const mediaType = response.headers.get("content-type")?.split(";")[0]?.trim();
|
|
146
|
+
if (mediaType?.includes("application/json") || mediaType?.endsWith("+json")) return await response.json();
|
|
147
|
+
return await response.text();
|
|
148
|
+
})();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
//#endregion
|
|
152
|
+
//#region src/internal/utils/sleep.ts
|
|
153
|
+
function sleep(ms) {
|
|
154
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
//#endregion
|
|
158
|
+
//#region src/client.ts
|
|
159
|
+
function isAcceptedResponse(response) {
|
|
160
|
+
return valibot.safeParse(require_schemas.AcceptedResponseSchema, response).success;
|
|
161
|
+
}
|
|
162
|
+
var Client = class {
|
|
163
|
+
/** CS AIDR API token.*/
|
|
164
|
+
token;
|
|
165
|
+
/**
|
|
166
|
+
* Template for constructing the base URL for API requests. The placeholder
|
|
167
|
+
* `{SERVICE_NAME}` will be replaced with the service name slug.
|
|
168
|
+
*/
|
|
169
|
+
baseURLTemplate;
|
|
170
|
+
timeout;
|
|
171
|
+
maxRetries;
|
|
172
|
+
maxPollingAttempts;
|
|
173
|
+
fetch;
|
|
174
|
+
_options;
|
|
175
|
+
constructor(options) {
|
|
176
|
+
if (options.token === void 0) throw new AIDRError("Client was instantiated without an API token.");
|
|
177
|
+
if (options.baseURLTemplate === void 0) throw new AIDRError("Client was instantiated without a base URL template.");
|
|
178
|
+
this.baseURLTemplate = options.baseURLTemplate;
|
|
179
|
+
this.fetch = options.fetch ?? fetch;
|
|
180
|
+
this.maxRetries = options.maxRetries ?? 2;
|
|
181
|
+
this.maxPollingAttempts = options.maxPollingAttempts ?? 5;
|
|
182
|
+
this.timeout = options.timeout ?? 6e4;
|
|
183
|
+
this.token = options.token;
|
|
184
|
+
this._options = options;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Will retrieve the result, or will return HTTP/202 if the original request
|
|
188
|
+
* is still in progress.
|
|
189
|
+
*/
|
|
190
|
+
getAsyncRequest(requestId) {
|
|
191
|
+
return this.get(`/v1/request/${requestId}`);
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Polls for an async request result with exponential backoff.
|
|
195
|
+
* Continues polling until a success response is received or max attempts are reached.
|
|
196
|
+
*/
|
|
197
|
+
async pollAsyncRequest(requestId, maxAttempts) {
|
|
198
|
+
let lastResponse = null;
|
|
199
|
+
for (let attempt = 0; attempt < maxAttempts; attempt++) {
|
|
200
|
+
const response = await this.getAsyncRequest(requestId);
|
|
201
|
+
if (response.status === "Success") return response;
|
|
202
|
+
lastResponse = response;
|
|
203
|
+
if (attempt < maxAttempts - 1) await sleep(this.calculateDefaultRetryTimeoutMillis(maxAttempts - attempt - 1, maxAttempts));
|
|
204
|
+
}
|
|
205
|
+
if (lastResponse === null) throw new AIDRError("Polling failed: no response received");
|
|
206
|
+
return lastResponse;
|
|
207
|
+
}
|
|
208
|
+
async get(path, opts) {
|
|
209
|
+
return await this.methodRequest("get", path, opts);
|
|
210
|
+
}
|
|
211
|
+
async post(path, opts) {
|
|
212
|
+
return await this.methodRequest("post", path, opts);
|
|
213
|
+
}
|
|
214
|
+
async methodRequest(method, path, opts) {
|
|
215
|
+
return await this.request(Promise.resolve(opts).then((opts$1) => {
|
|
216
|
+
return {
|
|
217
|
+
method,
|
|
218
|
+
path,
|
|
219
|
+
...opts$1
|
|
220
|
+
};
|
|
221
|
+
}));
|
|
222
|
+
}
|
|
223
|
+
async request(options, remainingRetries = null) {
|
|
224
|
+
const parsed = await defaultParseResponse(await this.makeRequest(options, remainingRetries));
|
|
225
|
+
if (isAcceptedResponse(parsed)) {
|
|
226
|
+
const maxPollingAttempts = (await Promise.resolve(options)).maxPollingAttempts ?? this.maxPollingAttempts;
|
|
227
|
+
if (maxPollingAttempts <= 0) return parsed;
|
|
228
|
+
return await this.pollAsyncRequest(parsed.request_id, maxPollingAttempts);
|
|
229
|
+
}
|
|
230
|
+
return parsed;
|
|
231
|
+
}
|
|
232
|
+
async makeRequest(optionsInput, retriesRemaining) {
|
|
233
|
+
const options = await optionsInput;
|
|
234
|
+
const maxRetries = options.maxRetries ?? this.maxRetries;
|
|
235
|
+
if (retriesRemaining == null) retriesRemaining = maxRetries;
|
|
236
|
+
const { req, url, timeout } = this.buildRequest(options, { retryCount: maxRetries - retriesRemaining });
|
|
237
|
+
if (options.signal?.aborted) throw new APIUserAbortError();
|
|
238
|
+
const controller = new AbortController();
|
|
239
|
+
const response = await this.fetchWithTimeout(url, req, timeout, controller).catch(castToError);
|
|
240
|
+
if (response instanceof globalThis.Error) {
|
|
241
|
+
if (options.signal?.aborted) throw new APIUserAbortError();
|
|
242
|
+
if (retriesRemaining) return await this.retryRequest(options, retriesRemaining);
|
|
243
|
+
throw new APIConnectionError({ cause: response });
|
|
244
|
+
}
|
|
245
|
+
if (!response.ok) {
|
|
246
|
+
const shouldRetry = this.shouldRetry(response);
|
|
247
|
+
if (retriesRemaining && shouldRetry) return await this.retryRequest(options, retriesRemaining);
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
response,
|
|
251
|
+
options,
|
|
252
|
+
controller
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
shouldRetry(response) {
|
|
256
|
+
return response.status === 408 || response.status === 409 || response.status === 429 || response.status >= 500;
|
|
257
|
+
}
|
|
258
|
+
async retryRequest(options, retriesRemaining) {
|
|
259
|
+
const maxRetries = options.maxRetries ?? this.maxRetries;
|
|
260
|
+
await sleep(this.calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries));
|
|
261
|
+
return this.makeRequest(options, retriesRemaining - 1);
|
|
262
|
+
}
|
|
263
|
+
calculateDefaultRetryTimeoutMillis(retriesRemaining, maxRetries) {
|
|
264
|
+
const initialRetryDelay = .5;
|
|
265
|
+
const maxRetryDelay = 8;
|
|
266
|
+
const numRetries = maxRetries - retriesRemaining;
|
|
267
|
+
return Math.min(initialRetryDelay * 2 ** numRetries, maxRetryDelay) * (1 - Math.random() * .25) * 1e3;
|
|
268
|
+
}
|
|
269
|
+
async fetchWithTimeout(url, init, ms, controller) {
|
|
270
|
+
const { signal, method, ...options } = init || {};
|
|
271
|
+
if (signal) signal.addEventListener("abort", () => controller.abort());
|
|
272
|
+
const timeout = setTimeout(() => controller.abort(), ms);
|
|
273
|
+
const fetchOptions = {
|
|
274
|
+
signal: controller.signal,
|
|
275
|
+
method: "GET",
|
|
276
|
+
...options
|
|
277
|
+
};
|
|
278
|
+
if (method) fetchOptions.method = method.toUpperCase();
|
|
279
|
+
try {
|
|
280
|
+
return await this.fetch.call(void 0, url, fetchOptions);
|
|
281
|
+
} finally {
|
|
282
|
+
clearTimeout(timeout);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
buildRequest(inputOptions, { retryCount = 0 } = {}) {
|
|
286
|
+
const options = { ...inputOptions };
|
|
287
|
+
const { method, path, query, baseURLTemplate } = options;
|
|
288
|
+
const url = this.buildURL(path, query, baseURLTemplate);
|
|
289
|
+
options.timeout = options.timeout ?? this.timeout;
|
|
290
|
+
const { bodyHeaders, body } = this.buildBody({ options });
|
|
291
|
+
return {
|
|
292
|
+
req: {
|
|
293
|
+
method,
|
|
294
|
+
headers: this.buildHeaders({
|
|
295
|
+
options: inputOptions,
|
|
296
|
+
method,
|
|
297
|
+
bodyHeaders,
|
|
298
|
+
retryCount
|
|
299
|
+
}),
|
|
300
|
+
...options.signal && { signal: options.signal },
|
|
301
|
+
...body && { body }
|
|
302
|
+
},
|
|
303
|
+
url,
|
|
304
|
+
timeout: options.timeout
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
buildURL(path, query, baseURLTemplate = this.baseURLTemplate) {
|
|
308
|
+
const url = new URL((isAbsoluteURL(path) ? path : baseURLTemplate + (baseURLTemplate.endsWith("/") && path.startsWith("/") ? path.slice(1) : path)).replaceAll("{SERVICE_NAME}", this.serviceName));
|
|
309
|
+
if (typeof query === "object" && query && !Array.isArray(query)) url.search = stringifyQuery(query);
|
|
310
|
+
return url.toString();
|
|
311
|
+
}
|
|
312
|
+
buildBody({ options: { body, headers: _rawHeaders } }) {
|
|
313
|
+
if (!body) return {
|
|
314
|
+
bodyHeaders: void 0,
|
|
315
|
+
body: void 0
|
|
316
|
+
};
|
|
317
|
+
return {
|
|
318
|
+
bodyHeaders: { "content-type": "application/json" },
|
|
319
|
+
body: JSON.stringify(body)
|
|
320
|
+
};
|
|
321
|
+
}
|
|
322
|
+
buildHeaders({ options, bodyHeaders }) {
|
|
323
|
+
return buildHeaders([
|
|
324
|
+
{
|
|
325
|
+
Accept: "application/json",
|
|
326
|
+
"User-Agent": "aidr-typescript"
|
|
327
|
+
},
|
|
328
|
+
this.authHeaders(),
|
|
329
|
+
this._options.defaultHeaders,
|
|
330
|
+
bodyHeaders,
|
|
331
|
+
options.headers
|
|
332
|
+
]).values;
|
|
333
|
+
}
|
|
334
|
+
authHeaders() {
|
|
335
|
+
return buildHeaders([{ Authorization: `Bearer ${this.token}` }]);
|
|
336
|
+
}
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
//#endregion
|
|
340
|
+
//#region src/services/ai-guard.ts
|
|
341
|
+
var AIGuard = class extends Client {
|
|
342
|
+
serviceName = "aiguard";
|
|
343
|
+
/**
|
|
344
|
+
* Analyze and redact content to avoid manipulation of the model, addition of
|
|
345
|
+
* malicious content, and other undesirable data transfers.
|
|
346
|
+
*/
|
|
347
|
+
guardChatCompletions(body, options) {
|
|
348
|
+
return this.post("/v1/guard_chat_completions", {
|
|
349
|
+
body,
|
|
350
|
+
...options
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
//#endregion
|
|
356
|
+
exports.AIGuard = AIGuard;
|