@rheonic/sdk 0.1.0-beta.1
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 +21 -0
- package/README.md +150 -0
- package/dist/client.d.ts +59 -0
- package/dist/client.js +305 -0
- package/dist/config.d.ts +15 -0
- package/dist/config.js +15 -0
- package/dist/costCalculator.d.ts +3 -0
- package/dist/costCalculator.js +6 -0
- package/dist/eventBuilder.d.ts +35 -0
- package/dist/eventBuilder.js +15 -0
- package/dist/httpTransport.d.ts +12 -0
- package/dist/httpTransport.js +100 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.js +60 -0
- package/dist/logger.d.ts +16 -0
- package/dist/logger.js +56 -0
- package/dist/protectEngine.d.ts +48 -0
- package/dist/protectEngine.js +255 -0
- package/dist/providerModelValidation.d.ts +7 -0
- package/dist/providerModelValidation.js +26 -0
- package/dist/providers/anthropicAdapter.d.ts +9 -0
- package/dist/providers/anthropicAdapter.js +189 -0
- package/dist/providers/googleAdapter.d.ts +9 -0
- package/dist/providers/googleAdapter.js +212 -0
- package/dist/providers/openaiAdapter.d.ts +9 -0
- package/dist/providers/openaiAdapter.js +203 -0
- package/dist/rateLimiter.d.ts +3 -0
- package/dist/rateLimiter.js +6 -0
- package/dist/tokenEstimator.d.ts +2 -0
- package/dist/tokenEstimator.js +64 -0
- package/package.json +74 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { encoding_for_model, get_encoding } from "@dqbd/tiktoken";
|
|
2
|
+
import { sdkNodeConfig } from "./config.js";
|
|
3
|
+
const encoderCache = new Map();
|
|
4
|
+
export function prewarmTokenEstimator(model = null) {
|
|
5
|
+
try {
|
|
6
|
+
getEncoder(model);
|
|
7
|
+
}
|
|
8
|
+
catch {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
export function estimateInputTokensFromRequest(payload) {
|
|
13
|
+
if (!payload || typeof payload !== "object") {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
const request = payload;
|
|
17
|
+
const text = extractTextForEstimation(request);
|
|
18
|
+
if (text === null) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
try {
|
|
22
|
+
const encoder = getEncoder(typeof request.model === "string" ? request.model : null);
|
|
23
|
+
const encodedLength = encoder.encode(text).length;
|
|
24
|
+
return Math.min(sdkNodeConfig.maxInputTokenEstimate, encodedLength);
|
|
25
|
+
}
|
|
26
|
+
catch {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
function extractTextForEstimation(request) {
|
|
31
|
+
if (Array.isArray(request.messages)) {
|
|
32
|
+
try {
|
|
33
|
+
return JSON.stringify(request.messages);
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (typeof request.prompt === "string") {
|
|
40
|
+
return request.prompt;
|
|
41
|
+
}
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
function getEncoder(model) {
|
|
45
|
+
const cacheKey = model ?? sdkNodeConfig.defaultTokenizerEncoding;
|
|
46
|
+
const cached = encoderCache.get(cacheKey);
|
|
47
|
+
if (cached) {
|
|
48
|
+
return cached;
|
|
49
|
+
}
|
|
50
|
+
let encoder;
|
|
51
|
+
if (model) {
|
|
52
|
+
try {
|
|
53
|
+
encoder = encoding_for_model(model);
|
|
54
|
+
}
|
|
55
|
+
catch {
|
|
56
|
+
encoder = get_encoding(sdkNodeConfig.defaultTokenizerEncoding);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
encoder = get_encoding(sdkNodeConfig.defaultTokenizerEncoding);
|
|
61
|
+
}
|
|
62
|
+
encoderCache.set(cacheKey, encoder);
|
|
63
|
+
return encoder;
|
|
64
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rheonic/sdk",
|
|
3
|
+
"version": "0.1.0-beta.1",
|
|
4
|
+
"description": "Node.js SDK for Rheonic observability and protect preflight enforcement.",
|
|
5
|
+
"author": "Rheonic <founder@rheonic.dev>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"sideEffects": false,
|
|
9
|
+
"main": "dist/index.js",
|
|
10
|
+
"module": "dist/index.js",
|
|
11
|
+
"types": "dist/index.d.ts",
|
|
12
|
+
"files": [
|
|
13
|
+
"dist",
|
|
14
|
+
"!dist/*.test.d.ts",
|
|
15
|
+
"!dist/*.test.js",
|
|
16
|
+
"!dist/**/*.test.d.ts",
|
|
17
|
+
"!dist/**/*.test.js",
|
|
18
|
+
"README.md",
|
|
19
|
+
"CHANGELOG.md"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": {
|
|
23
|
+
"types": "./dist/index.d.ts",
|
|
24
|
+
"import": "./dist/index.js"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"scripts": {
|
|
28
|
+
"build": "tsc -p tsconfig.json",
|
|
29
|
+
"test": "npm run build && c8 --check-coverage --lines 80 --functions 80 --branches 80 --statements 80 --reporter=text --reporter=lcov node --test dist/*.test.js"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"llm",
|
|
33
|
+
"ai",
|
|
34
|
+
"observability",
|
|
35
|
+
"guardrails",
|
|
36
|
+
"openai",
|
|
37
|
+
"anthropic",
|
|
38
|
+
"google"
|
|
39
|
+
],
|
|
40
|
+
"homepage": "https://beta.rheonic.dev",
|
|
41
|
+
"bugs": {
|
|
42
|
+
"url": "mailto:support@rheonic.dev"
|
|
43
|
+
},
|
|
44
|
+
"publishConfig": {
|
|
45
|
+
"access": "public"
|
|
46
|
+
},
|
|
47
|
+
"engines": {
|
|
48
|
+
"node": ">=18"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"@dqbd/tiktoken": "^1.0.22"
|
|
52
|
+
},
|
|
53
|
+
"peerDependencies": {
|
|
54
|
+
"@anthropic-ai/sdk": ">=0.39.0",
|
|
55
|
+
"@google/generative-ai": ">=0.21.0",
|
|
56
|
+
"openai": ">=4.0.0"
|
|
57
|
+
},
|
|
58
|
+
"peerDependenciesMeta": {
|
|
59
|
+
"@anthropic-ai/sdk": {
|
|
60
|
+
"optional": true
|
|
61
|
+
},
|
|
62
|
+
"@google/generative-ai": {
|
|
63
|
+
"optional": true
|
|
64
|
+
},
|
|
65
|
+
"openai": {
|
|
66
|
+
"optional": true
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"@types/node": "^22.8.7",
|
|
71
|
+
"c8": "^10.1.3",
|
|
72
|
+
"typescript": "^5.6.3"
|
|
73
|
+
}
|
|
74
|
+
}
|